Skip Menu |

This queue is for tickets about the Proc-ProcessTable CPAN distribution.

Report information
The Basics
Id: 112432
Status: resolved
Priority: 0/
Queue: Proc-ProcessTable

People
Owner: Nobody in particular
Requestors: kevin.buterbaugh [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Percent CPU incorrect
Date: Thu, 25 Feb 2016 14:17:36 -0600
To: bug-Proc-ProcessTable [...] rt.cpan.org
From: Kevin Buterbaugh <kevin.buterbaugh [...] gmail.com>
Hi, “top” says a particular process is using 99 or 100% CPU. Proc::ProcessTable-0.53 says it’s using much less. top - 14:13:39 up 90 days, 5:25, 3 users, load average: 1.99, 2.04, 2.05 Tasks: 791 total, 2 running, 789 sleeping, 0 stopped, 0 zombie Cpu(s): 4.6%us, 0.5%sy, 0.0%ni, 94.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 65534936k total, 41368244k used, 24166692k free, 367964k buffers Swap: 4194300k total, 0k used, 4194300k free, 845956k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 12653 root 10 -10 433m 11m 9264 R 99.6 0.0 14:13.47 smbd uid: 0, gid: 0, pid: 12653, fname: smbd, ppid: 21434, pgrp: 21434, sess: 21434, ttynum: 0, flags: 4202816, minflt: 3711, cminflt: 0, majflt: 0, cmajflt: 0, utime: 916430000, stime: 450000, cutime: 0, cstime: 0, priority: 10, start: 1456418376, size: 454041600, rss: 12349440, wchan: 0, time: 916880000, ctime: 0, state: run, euid: 0, suid: 0, fuid: 0, egid: 0, sgid: 0, fgid: 0, pctcpu: 7.10, pctmem: 0.02, cmndline: /usr/sbin/smbd -D, exec: /usr/sbin/smbd, cwd: /, 7% seems to be on the low side … I had another smbd that top said was using 100% CPU and Proc::ProcessTable said it was using ~85% CPU. Based on the responsiveness of the box, I believe top… ;-) Thanks… Kevin [root@somehost ~]# uname -a Linux somehost 2.6.32-504.23.4.el6.x86_64 #1 SMP Fri May 29 10:16:43 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux [root@somehost ~]# perl -v This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi Copyright 1987-2009, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page. [root@somehost ~]# -- Kevin L. Buterbaugh Kevin.Buterbaugh@gmail.com <mailto:Kevin.Buterbaugh@gmail.com> “There’s a feeling I get when I look to the west and my spirit is crying for leaving.” - Led Zeppelin, “Stairway to Heaven"
Hi Kevin, this is a tricky one. Conceptually Proc::ProcessTable is more similar to "ps" than to "top". To calculate %CPU, you need to calculate how much time a process spent on the kernel/system divided by the elapsed time. See here for more info: http://unix.stackexchange.com/questions/58539/top-and-ps-not-showing-the-same-cpu-result BUT you can use Proc::ProcessTable to calculate the %CPU. I just hacked together a little script to give you an idea. Probably my calculation is also not 100% correct. I hope this helps. As this is not a bug of Proc::ProcessTable itself (it's a feature! ;) ), I'll close this issue. Best, Joachim Am Do 25. Feb 2016, 15:17:48, kevin.buterbaugh@gmail.com schrieb: Show quoted text
> Hi, > > “top” says a particular process is using 99 or 100% CPU. > Proc::ProcessTable-0.53 says it’s using much less. > > top - 14:13:39 up 90 days, 5:25, 3 users, load average: 1.99, 2.04, > 2.05 > Tasks: 791 total, 2 running, 789 sleeping, 0 stopped, 0 zombie > Cpu(s): 4.6%us, 0.5%sy, 0.0%ni, 94.9%id, 0.0%wa, 0.0%hi, 0.0%si, > 0.0%st > Mem: 65534936k total, 41368244k used, 24166692k free, 367964k > buffers > Swap: 4194300k total, 0k used, 4194300k free, 845956k > cached > > PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND > 12653 root 10 -10 433m 11m 9264 R 99.6 0.0 14:13.47 smbd > > uid: 0, gid: 0, pid: 12653, fname: smbd, ppid: 21434, pgrp: > 21434, sess: 21434, ttynum: 0, flags: 4202816, minflt: 3711, > cminflt: 0, majflt: 0, cmajflt: 0, utime: 916430000, stime: > 450000, cutime: 0, cstime: 0, priority: 10, start: 1456418376, > size: 454041600, rss: 12349440, wchan: 0, time: 916880000, ctime: > 0, state: run, euid: 0, suid: 0, fuid: 0, egid: 0, sgid: 0, > fgid: 0, pctcpu: 7.10, pctmem: 0.02, cmndline: /usr/sbin/smbd > -D, exec: /usr/sbin/smbd, cwd: /, > > 7% seems to be on the low side … I had another smbd that top said was > using 100% CPU and Proc::ProcessTable said it was using ~85% CPU. > > Based on the responsiveness of the box, I believe top… ;-) > > Thanks… > > Kevin > > [root@somehost ~]# uname -a > Linux somehost 2.6.32-504.23.4.el6.x86_64 #1 SMP Fri May 29 10:16:43 > EDT 2015 x86_64 x86_64 x86_64 GNU/Linux > [root@somehost ~]# perl -v > > This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi > > Copyright 1987-2009, Larry Wall > > Perl may be copied only under the terms of either the Artistic License > or the > GNU General Public License, which may be found in the Perl 5 source > kit. > > Complete documentation for Perl, including FAQ lists, should be found > on > this system using "man perl" or "perldoc perl". If you have access to > the > Internet, point your browser at http://www.perl.org/, the Perl Home > Page. > > [root@somehost ~]# > > -- > Kevin L. Buterbaugh > Kevin.Buterbaugh@gmail.com <mailto:Kevin.Buterbaugh@gmail.com> > > “There’s a feeling I get when I look to the west and my spirit is > crying for leaving.” - Led Zeppelin, “Stairway to Heaven"
Subject: top_cpu.pl
#!/usr/bin/env perl # created on 2016-02-25 use warnings; use strict; use 5.010; use Time::HiRes qw/gettimeofday tv_interval usleep/; use Proc::ProcessTable; my $poll_int_us = int( 1 * 1000 * 1000 ); # record CPU every second my $nsteps = 3; # average over this number of steps my $pid = shift; # the process in question die unless ($pid); my @cpu_time; my @times; # write unbuffered to STDOUT $| = 1; my $ppt = Proc::ProcessTable->new; while (1) { # remove entries if we have more values than steps shift @cpu_time if ( @cpu_time > $nsteps ); shift @times if ( @times > $nsteps ); my $pt = $ppt->table; # record time push @times, [ gettimeofday() ]; # record time spent doing someting (cpu time) # get time of given process plus all children push @cpu_time, sum_cpu_time( $pid, $pt ); # only calc if we have sufficient time steps if ( @times >= $nsteps ) { # how much cpu time in seconds my $diff_cpu = ( $cpu_time[-1] - $cpu_time[0] ) / ( 1000 * 1000 ); # how much time elapsed in seconds my $elapsed = tv_interval( $times[0], $times[-1] ); # %CPU = <time spent doing something on kernel/system> / <elapsed time> my $ratio = $elapsed > 0 ? $diff_cpu / $elapsed : 0; # print CPU in % printf "%.1f %s\n", $ratio * 100, $pid; } # sleep a little bit usleep($poll_int_us); } sub sum_cpu_time { my ( $pid, $pt ) = @_; my $time = 0; for my $p (@$pt) { # add cpu time of child process if ( $p->ppid == $pid ) { $time += sum_cpu_time( $p->pid, $pt ); } # add cpu time of current process if ( $p->pid eq $pid ) { $time += $p->time; } } return $time; }