Joe Van Dyk
1/10/2006 11:47:00 PM
On 1/10/06, Joe Van Dyk <joevandyk@gmail.com> wrote:
> On 1/10/06, Joe Van Dyk <joevandyk@gmail.com> wrote:
> > Hi,
> >
> > I'm writing a small Ruby module that can find how much cpu percentage
> > a given task is using. I'm doing it by getting the process id of the
> > task that I care about, going into proc, checking the stat file for
> > the process (which contains how many jiffies the task has consumed)
> > and averaging the change of that over time to get a running
> > percentage.
> >
> > 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.
> >
> > I'm struggling to come up with an elegant way to solve this problem. Any ideas?
>
> I can't make heads or tails out of this behavior. 5463 is a thread,
> btw. Linux 2.4.21, Ruby 1.8.4. When I do a directory glob of /proc/,
> 5463 isn't coming up.
>
>
> irb(main):002:0> File.directory? "/proc/5463"
> => true
>
> irb(main):003:0> Dir["/proc/5*"].sort
> => ["/proc/5", "/proc/5111", "/proc/5113", "/proc/5459", "/proc/5470",
> "/proc/5477", "/proc/5486", "/proc/5487", "/proc/5653", "/proc/5654",
> "/proc/5800", "/proc/5810", "/proc/5811"]
>
> irb(main):004:0> Dir["/proc/5*"].include?("/proc/5463")
> => false
>
> irb(main):005:0> # WTF
> irb(main):006:0* exit
>
> % ls -ld /proc/5463
> dr-xr-xr-x 3 root games 0 Jan 10 15:26 /proc/5463/
>
> % ls -l /proc/5463
> ls: cannot read symbolic link /proc/5463/cwd: Permission denied
> ls: cannot read symbolic link /proc/5463/root: Permission denied
> ls: cannot read symbolic link /proc/5463/exe: Permission denied
> total 0
> -r--r--r-- 1 root games 0 Jan 10 15:26 cmdline
> -r--r--r-- 1 root games 0 Jan 10 15:26 cpu
> lrwxrwxrwx 1 root games 0 Jan 10 15:26 cwd
> -r-------- 1 root games 0 Jan 10 15:26 environ
> lrwxrwxrwx 1 root games 0 Jan 10 15:26 exe
> dr-x------ 2 root games 0 Jan 10 15:26 fd/
> -r-------- 1 root games 0 Jan 10 15:26 maps
> -rw------- 1 root games 0 Jan 10 15:26 mem
> -r--r--r-- 1 root games 0 Jan 10 15:26 mounts
> lrwxrwxrwx 1 root games 0 Jan 10 15:26 root
> -r--r--r-- 1 root games 0 Jan 10 15:26 stat
> -r--r--r-- 1 root games 0 Jan 10 15:26 statm
> -r--r--r-- 1 root games 0 Jan 10 15:26 status
>
> % cat /proc/5463/status
> Name: sccng.x
> State: S (sleeping)
> Tgid: 5459
> Pid: 5463
> PPid: 5459
> TracerPid: 0
> Uid: 0 0 0 0
> Gid: 20 20 20 20
> FDSize: 256
> Groups: 0 1 2 3 4 6 10 1 2 3 4
> VmSize: 147612 kB
> VmLck: 147612 kB
> VmRSS: 146648 kB
> VmData: 58268 kB
> VmStk: 776 kB
> VmExe: 468 kB
> VmLib: 11512 kB
> SigPnd: 0000000000000000
> ShdPnd: 0000000000000000
> SigBlk: 0000000000000000
> SigIgn: 0000000000001000
> SigCgt: 0000000080002e47
> CapInh: 00000000ffffffff
> CapPrm: 00000000ffffffff
> CapEff: 00000000ffffffff
Solved! I thought Ruby was being weird, shame on me.
Threads are represented in /proc with a '.' in front of the id. i.e.
/proc/.5463. For some reason though, I can access /proc/5463, but
/proc/5463 doesn't show up in directory globs.
Joe