File: pctcpu_example.pl

package info (click to toggle)
libproc-processtable-perl 0.637-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 672 kB
  • sloc: ansic: 5,765; perl: 568; makefile: 15
file content (80 lines) | stat: -rw-r--r-- 2,106 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env perl
# created on 2018-02-12

use warnings;
use strict;
use v5.11;
use Proc::ProcessTable;
use Time::HiRes;

my @time_spans;
my %cpu_times;

my $ppt        = Proc::ProcessTable->new;
my $start_time = [ Time::HiRes::gettimeofday() ];

my $poll_intervall = int( 2 * 1000 * 1000 );    # 2 sec
my $num_steps      = 2;

local $| = 1;

my $cur_step = 0;
while (1) {


  # current and old index of array.
  # because of modulo, the idcs are rotated.
  # the old index is always the oldest recorded entry
  # this means the entry *after* the current index
  #[ c o . . . ]
  #[ . c o . . ]
  #[ . . c o . ]
  #[ . . . c o ]
  #[ o . . . c ]
  my $cur_idx = $cur_step % $num_steps;
  my $old_idx = ( $cur_step + 1 ) % $num_steps;

  # calc pct cpu per interval:
  # we need seconds since
  # https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat

  my $t = Time::HiRes::tv_interval($start_time);
  my $pt           = $ppt->table;

  my %active_procs = ();
  for my $p (@$pt) {
    # init process info if non-existent
    my $pid = $p->pid;
    $active_procs{$pid} = 1;
    $cpu_times{$pid} //= [];

    $cpu_times{$pid}[$cur_idx] = $p->time;
  }
  $time_spans[$cur_idx] = $t;

  # clean the cputimes hash
  my @killed_procs = grep { !$active_procs{$_} } keys %cpu_times ;
  delete @cpu_times{@killed_procs};

  my $ratio = 0;
  # make sure the array is filled with cpu times
  if ( $cur_step >= $num_steps ) {

    # move cursor to top left of screen
    print "\033[2J";

    printf( "%-5s %s\n", "cpu", "pid");
    # show cpu usage per process
    for my $pid (keys %cpu_times) {
      my $cpu_time   = $cpu_times{$pid};
      my $diff_cpu   = ( $cpu_time->[$cur_idx] - ( $cpu_time->[$old_idx] // 0 ) ) / 1e6;
      my $diff_start = ( $time_spans[$cur_idx] - $time_spans[$old_idx] );
      $ratio = $diff_cpu / $diff_start if ( $diff_start > 0 );
      # show only processes that have a usage > 1%
      printf( "%5.1f %s\n", $ratio * 100, $pid ) if ( $ratio > 0.01 );
    }
  }

  $cur_step++;
  Time::HiRes::usleep($poll_intervall);
}