#!/usr/bin/perl
use Time::HiRes qw(gettimeofday);
use Getopt::Std;
my @fields;
my @uidcache;
my %uidmap;

my $sortfield = 'cpu';
my %opts;
getopt('sldh', \%opts);
if ($opts{'h'}) {
    help();
    exit();
}
if ($opts{'s'}) {
    if ($opts{'s'} =~ /^cpu/i) {
        $sortfield = 'cpu';
    } elsif ($opts{'s'} =~ /^mem|^rss/i) {
        $sortfield = 'rss';
    } elsif ($opts{'s'} =~ /swap/i) {
        $sortfield = 'swap';
    } elsif ($opts{'s'} =~ /tasks?/i) {
        $sortfield = 'tasks';
    } elsif ($opts{'s'} =~ /^cac/i) {
        $sortfield = 'cache';
    } elsif ($opts{'s'} =~ /i\/?o/i) {
        $sortfield = 'io';
    } elsif ($opts{'s'} =~ /fault|majf/i) {
        $sortfield = 'majf';
    } else {
        print "Invalid value for -s (sort field).\n";
        help();
        exit();
    }
}
my $displimit = 500;
if ($opts{'l'}) {
    if ($opts{'l'} >= 1 && $opts{'l'} =~ /^[0-9]+$/) {
        $displimit = $opts{'l'};
    } else {
        print "Invalid value for -l (result limit).\n";
        help();
        exit();
    }
}
my $sleepytime = $opts{'d'} || shift;
if (!$sleepytime || $sleepytime !~ /^[0-9]+$/) {
    print "Invalid or missing sleep time.\n";
    help();
    exit();
}


if ($sleepytime !~ /^[0-9]+$/ || $displimit !~ /^[0-9]+$/) {
    help();
    exit();
}

open(FP,"<","/etc/passwd");
while (<FP>) {
    @fields = split(/:/,$_);
    if ($fields[2] >= 500 && (-e "/sys/fs/cgroup/USERS/$fields[2]")) {
        $uidmap{$fields[2]}{"user"} = $fields[0];
        $uidmap{$fields[2]}{"cp"} = 0; # curr periods
        $uidmap{$fields[2]}{"lp"} = 0; # last periods
        $uidmap{$fields[2]}{"ct"} = 0; # curr throttle
        $uidmap{$fields[2]}{"lt"} = 0; # last throttle
        $uidmap{$fields[2]}{"cache"} = 0; # mem cached
        $uidmap{$fields[2]}{"majf"} = 0; # major page faults
        $uidmap{$fields[2]}{"rss"} = 0; # rss usage
    }
}
my @uidcache = sort { $uidmap{$a}{"user"} cmp $uidmap{$b}{"user"} } keys %uidmap;

close(FP);

my $uid;
my $iter = 0;
my ($then,$now);
$then = gettimeofday() - $sleepytime;
while (1) {
    $iter++;
    open(my $fh,'<', '/proc/loadavg');
    my $loadavg = <$fh>;
    close($fh);
    print ((scalar localtime time) . '    ' . $loadavg );
    my %dout = ();
    $now = gettimeofday();
    foreach $uid (@uidcache) {
        open(FC,"<","/sys/fs/cgroup/USERS/$uid/cpu.stat");
        if (FC) {
            while (<FC>) {
                if (/nr_periods ([0-9]+)$/) {
                    $uidmap{$uid}{'lp'} = $uidmap{$uid}{'cp'};
                    $uidmap{$uid}{'cp'} = $1;
                } elsif (/nr_throttled ([0-9]+)$/) {
                    $uidmap{$uid}{'lt'} = $uidmap{$uid}{'ct'};
                    $uidmap{$uid}{'ct'} = $1;
                }
            }
            close(FC);
        }
        next if ($uidmap{$uid}{'cp'} - $uidmap{$uid}{'lp'} == 0);
        open(FC,"<", "/sys/fs/cgroup/USERS/$uid/cpuacct.stat"); 
        if (FC) {
            $uidmap{$uid}{'sp'}=0;
            while (<FC>) {
                chomp;
                my @splat = split(/ /,$_);
                $uidmap{$uid}{'sp'}+=$splat[1];
            }
            close(FC);
        }

        open(FC,"<", "/sys/fs/cgroup/USERS/$uid/memory.stat");
        if (FC) {
            while (<FC>) {
                chomp;
                my @splat = split(/ /,$_);
                if ($splat[0] eq 'cache') {
                    $uidmap{$uid}{'cache'}=$splat[1];
                } elsif ($splat[0] eq 'total_pgmajfault') {
                    $uidmap{$uid}{'majf'} = $splat[1] - int($uidmap{$uid}{'majfl'});
                    $uidmap{$uid}{'majfl'} = $splat[1];
                } elsif ($splat[0] eq 'total_rss') {
                    $uidmap{$uid}{'rss'} = $splat[1];
                } elsif ($splat[0] eq 'total_swap') {
                    $uidmap{$uid}{'swap'} = $splat[1];
                }
            }
            close(FC);
        }

        open(FC,"<", "/sys/fs/cgroup/USERS/$uid/blkio.throttle.io_service_bytes");
        if (FC) {
            my $ior = 0;
            my $iow = 0;
            while (<FC>) {
                chomp;
                my @splat = split(/ /,$_);
                if ($splat[1] eq 'Read') {
                    $ior+=$splat[2];
                } elsif ($splat[1] eq 'Write') {
                    $iow=$splat[2];
                }

            }
            close(FC);
            $uidmap{$uid}{'ior'} = $ior - $uidmap{$uid}{'iorl'};
            $uidmap{$uid}{'iow'} = $iow - $uidmap{$uid}{'iowl'};
            $uidmap{$uid}{'iorl'} = $ior;
            $uidmap{$uid}{'iowl'} = $iow;            
        }
        open(FC,"<", "/sys/fs/cgroup/USERS/$uid/tasks");
        if (FC) {
            $uidmap{$uid}{'tasks'} = scalar grep { 1 } <FC>;
            close(FC);
        }
        if (($uidmap{$uid}{'cp'} - $uidmap{$uid}{'lp'} > 0 && ($uidmap{$uid}{'ct'} - $uidmap{$uid}{'lt'} > 0 || $uidmap{$uid}{'sp'}-$uidmap{$uid}{'sl'} > 25)) && $uidmap{$uid}{'sl'} > 0) {
            my $sval;
            if ($sortfield eq 'cpu') {
                $sval = $uidmap{$uid}{'sp'} - $uidmap{$uid}{'sl'};
            } if ($sortfield eq 'rss') {
                $sval = $uidmap{$uid}{'rss'};
            } elsif ($sortfield eq 'swap') {
                $sval = $uidmap{$uid}{'swap'};
            } elsif ($sortfield eq 'io') {
                $sval = $uidmap{$uid}{'ior'} + $uidmap{$uid}{'iow'};
            } elsif ($sortfield eq 'tasks') {
                $sval = $uidmap{$uid}{'tasks'};
            } elsif ($sortfield eq 'cache') {
                $sval = $uidmap{$uid}{'cache'};
            } elsif ($sortfield eq 'majf') {
                $sval = $uidmap{$uid}{'majf'};
            } else {
                $sval = $uidmap{$uid}{"sp"} - $uidmap{$uid}{"sl"};
            }
            push @{$dout{$sval}}, sprintf("%6s/%-16s   %10d np   %10d tp   %4d tasks %6.2f%%   | %9.2f rss %9.2f cac  %9.2f swap %10d majf | %8d rd %8d wr\n", 
                                                                        $uid, $uidmap{$uid}{'user'}, $uidmap{$uid}{'cp'}-$uidmap{$uid}{'lp'}, $uidmap{$uid}{'ct'}-$uidmap{$uid}{'lt'}, 
                                                                        $uidmap{$uid}{'tasks'}, ($uidmap{$uid}{'sp'}-$uidmap{$uid}{'sl'})/($now-$then), $uidmap{$uid}{'rss'}/1048576, 
                                                                        $uidmap{$uid}{'cache'}/1048576, $uidmap{$uid}{'swap'}/1048576, $uidmap{$uid}{'majf'}, $uidmap{$uid}{'ior'}/1024, $uidmap{$uid}{'iow'}/1024);
        }
        $uidmap{$uid}{"sl"} = $uidmap{$uid}{"sp"};

    }
    $then = $now;
    my $count = 0;
    foreach my $dk (sort { $b <=> $a } keys %dout) {
        foreach (@{$dout{$dk}}) {
            $count++;
            last if ($count > $displimit);
            print $_;
        }
        last if ($count > $displimit);
        #print $dout{$_};
    }
    print "\n";
    sleep($sleepytime);
}


sub help {
    print "Usage: cgwatch [-s sort-field] [-l record-limit] [-d] <delay>\n";
    print "Show user cgroup statistics every <delay> seconds.  If a second numeric argument is set, limit number of results per interval to the top <result size> users.\n";
    print "Each interval starts by showing the load average stats (/proc/loadavg), with the 1, 5, 15 minute load averages and time.\n";
    print "The first interval will only show this, since it takes an interval to get cgroup data.\n\n";
    print "Records for each interval are shown highest CPU usage first, with the following fields:\n";
    print "\tUID/user: The ID and user associated with the cgroup.\n";
    print "\tnp: number of periods, a period is 1 second with the cgroup configuration. This is the number of periods where the user was active.\n";
    print "\ttp: number of throttled periods.  For example, if a line says '15 np   10 tp', this means the user was active during 15 of the periods since \n";
    print "\t    the last update, and was throttled during 10 of them due to using more CPU than allowed.\n";
    print "\tTasks: the number of current running tasks for the cgroup.\n";
    print "\t%: The percent CPU usage for the user during the last interval. This combined with the throttled periods is the easiest way to see a cgroup's usage.\n";
    print "\trss: The amount of RSS memory this cgroup is using (in MB).\n";
    print "\tcac: How much filesystem cache (in MB) is associated with the cgroup (indicator of things like recent backup or scan type operations).\n";
    print "\tswap: The amount of swap this cgroup is using (in MB).\n";
    print "\tmajf: Major faults cgroup triggered since last update.\n";
    print "\trd and rw: The amount of read/write I/O (in KB) since last update.\n";
    print "\nCommand line options:\n";
    print "\tThe delay time can be set just by adding the time (eg, cgwatch 10) or using the -d switch (cgwatch -d 10).\n";
    print "\t-l <record-limit>: The number of results per report can be limited to the top record-limit results.\n";
    print "\t-s <sort-field>: A sort field can be chosen from: cpu (default), rss, swap, tasks, io, majf.\n";
    print "\tExample:  cgwatch -s rss -l 10 5 will show the top 10 cgroups by memory usage every 5 seconds.\n";
    
}
