Class: Daemonizer::Stats::MemoryStats

Inherits:
Object
  • Object
show all
Defined in:
lib/daemonizer/stats.rb

Defined Under Namespace

Classes: Process

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ MemoryStats

Returns a new instance of MemoryStats.



136
137
138
# File 'lib/daemonizer/stats.rb', line 136

def initialize(pool)
  @pool = pool
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



134
135
136
# File 'lib/daemonizer/stats.rb', line 134

def pool
  @pool
end

Instance Method Details

#find_all_processesObject



140
141
142
# File 'lib/daemonizer/stats.rb', line 140

def find_all_processes
  find_monitor + find_workers
end

#find_monitorObject



148
149
150
# File 'lib/daemonizer/stats.rb', line 148

def find_monitor
 self.list_processes(:match => /#{@pool.name} monitor/)
end

#find_workersObject



144
145
146
# File 'lib/daemonizer/stats.rb', line 144

def find_workers
 self.list_processes(:match => /#{@pool.name} worker: instance \d{1,}/)
end

#list_processes(options) ⇒ Object

Returns a list of Process objects that match the given search criteria.

# Search by executable path.
list_processes(:exe => '/usr/sbin/apache2')

# Search by executable name.
list_processes(:name => 'ruby1.8')

# Search by process name.
list_processes(:match => 'Passenger FrameworkSpawner')


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/daemonizer/stats.rb', line 184

def list_processes(options)
	if options[:exe]
		name = options[:exe].sub(/.*\/(.*)/, '\1')
		if RUBY_PLATFORM =~ /linux/
			ps = "ps -C '#{name}'"
		else
			ps = "ps -A"
			options[:match] = Regexp.new(Regexp.escape(name))
		end
	elsif options[:name]
		if RUBY_PLATFORM =~ /linux/
			ps = "ps -C '#{options[:name]}'"
		else
			ps = "ps -A"
			options[:match] = Regexp.new(" #{Regexp.escape(options[:name])}")
		end
	elsif options[:match]
		ps = "ps -A"
	else
		raise ArgumentError, "Invalid options."
	end

	processes = []
	case RUBY_PLATFORM
	when /solaris/
		list = `#{ps} -o pid,ppid,nlwp,vsz,rss,comm`.split("\n")
		threads_known = true
	when /darwin/
		list = `#{ps} -w -o pid,ppid,vsz,rss,command`.split("\n")
		threads_known = false
	else
		list = `#{ps} -w -o pid,ppid,nlwp,vsz,rss,command`.split("\n")
		threads_known = true
	end
	list.shift
	list.each do |line|
		line.gsub!(/^ */, '')
		line.gsub!(/ *$/, '')

		p = Process.new
		if threads_known
			p.pid, p.ppid, p.threads, p.vm_size, p.rss, p.name = line.split(/ +/, 6)
		else
			p.pid, p.ppid, p.vm_size, p.rss, p.name = line.split(/ +/, 5)
			p.threads = "?"
		end
		p.name.sub!(/\Aruby: /, '')
		p.name.sub!(/ \(ruby\)\Z/, '')
		if p.name !~ /^ps/ && (!options[:match] || p.name.match(options[:match]))
			# Convert some values to integer.
			[:pid, :ppid, :vm_size, :rss].each do |attr|
				p.send("#{attr}=", p.send(attr).to_i)
			end
			p.threads = p.threads.to_i if threads_known

			if platform_provides_private_dirty_rss_information?
				p.private_dirty_rss = determine_private_dirty_rss(p.pid)
			end
			processes << p
		end
	end
	return processes
end


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/daemonizer/stats.rb', line 152

def print
	puts
	pool_processes = find_all_processes
	if pool_processes.size == 0
		puts "*** It seems like pool '#{@pool.name}' is not running"
		return
 end
 puts
	print_process_list("#{@pool.name} processes", pool_processes, :show_ppid => false)

	if RUBY_PLATFORM !~ /linux/
		puts
		puts "*** WARNING: The private dirty RSS can only be displayed " <<
			"on Linux. You're currently using '#{RUBY_PLATFORM}'."
	elsif ::Process.uid != 0 && pool_processes.any?{ |p| p.private_dirty_rss.nil? }
		puts
		puts "*** WARNING: Please run this tool as root. Otherwise the " <<
			"private dirty RSS of processes cannot be determined."
	end
end