From: Michal Suchanek Date: 2006-01-11T07:07:20+09:00 Subject: Re: Finding CPU% of a linux task On 1/10/06, Joe Van Dyk wrote:> On 1/10/06, James Edward Gray II wrote:> > On Jan 10, 2006, at 3:31 PM, Joe Van Dyk wrote:> >> > > However, that doesn't capture threads or forked processes. If a> > > program starts a couple threads (or forks a child process), I then> > > have to figure out the cpu percentage of each of the child threads> > > (and their childs and their child's childs, and so on) and add them> > > together to get a total for the process.> >> > Ruby's threads are in process, so they should already be in your> > percentage, shouldn't they?>> To clarify, these aren't Ruby processes I'm tracking.>> Sample usage of my class:>> pi = ProcessInfo.new > pi.cpu_percentage # returns the cpu % of the process> pi.running? # sees if the process is still running>> etc>> I added ProcessInfo#child_tasks, that searches /proc for all child> tasks of the process. But then, when I calculate the cpu% of the> process, I need to recursively go in and get all the cpu% of all the> child threads/processes of the process. And my code got ugly fast. What is wrong with class ProcessInfo def direct_children end def children direct_children.collect{|ch|ch.chilfren}.flatten!end def cpu_sum sum = cpu_percentage children.each{|ch| sum += ch.cpu_percentage} sumend end Thanks Michal -- Support the freedom of music!Maybe it's a weird genre .. but weird is *not* illegal.Maybe next time they will send a special forces commandoto your picnic .. because they think you are weird. www.music-versus-guns.org http://en.policejnistat.cz