El blog de Juan Palómez

18 abril 2013

Memory usage treemap / WinDirStat for memory

Filed under: Uncategorized — Etiquetas: , , , , — thisisoneball @ 15:11

Warning: this is a quick hack, it’s not complete and it has bugs. Feel free to correct them and send me the changes.
Also, unlike WinDirStat, measuring memory usage is more difficult than measuring disk space. There are many different memory sizes for one process (Private Bytes, Working Set, Virtual Size, …), this just uses the output of the Windows tasklist command.

memoria

It’s a Perl program that tries to run that command, parses the output, and uses Google Charts Treemap to render the output.
It prints the HTML to stdout so the best way to run it is:
perl memoryusage.pl > memoryusage.html && start memoryusage.html

my %groups = ();
my $output;

my $output_pre = "
        <html>
          <head>
            <script type='text/javascript' src='https://www.google.com/jsapi'></script>
            <script type='text/javascript'>
              google.load('visualization', '1', {packages:['treemap']});
              google.setOnLoadCallback(drawChart);
              function drawChart() {
                  // Create and populate the data table.
                  var data = google.visualization.arrayToDataTable([
                    ['Process', 'Parent', 'Size'],
                    ['root',    null,                 0],
";

my $output_post = "
                  ]);

                  // Create and draw the visualization.
                  var treemap = new google.visualization.TreeMap(document.getElementById('chart_div'));
                  treemap.draw(data, {
                    minColor: 'red',
                    midColor: '#ddd',
                    maxColor: '#0d0',
                    headerHeight: 15,
                    fontColor: 'black',
                    showScale: true});
                }
        </script>
  </head>

  <body>
    <div id='chart_div' style='width: 900px; height: 500px;'></div>
  </body>
</html>
";

open TASKLIST, "tasklist /nh /fo CSV |" or die "Can't execute tasklist command $!\n";
while (<TASKLIST>) {
        if (m/"(.+)","(.+)",".+",".+","(.+) KB?"/) {
                $name = $1;
                $pid  = $2;
                $size = $3;
                $size =~ s/\.//;
                $output .= "['$pid','$name',$size],\n";
                $groups{"$name"} = 1;
        }
}

close TASKLIST;

while ( my ($key, $value) = each(%groups) ) {
        $output .= "['$key','root',0],\n";
}

chop $output;
chop $output;

print $output_pre . $output . $output_post;

Blog de WordPress.com.