Linuxã®ããã»ã¹æ¯ã¡ã¢ãªä½¿ç¨éã調ã¹ã¦ã¿ã
大å¦ã®èª²é¡ã§ããããããªæ¡ä»¶ä¸ã§åããã»ã¹ã®ã¡ã¢ãªä½¿ç¨éã調ã¹ããã¨ããã®ãåºã¾ãããã¡ã¢ãªä½¿ç¨éã¯/proc/PID/smapsã«æ¸ããã¦ããã¨ãããã¨ãç¥ã£ãã®ã§ãããã使ã£ã¦ã¿ããã¨ã«ãã¾ããã
ããã¬ãã¼ãæåºãã¦ããã ãã¶æ¥ãçµéããã®ã§å
¬éãã¦ãã¾ããã¨æãã¾ãã
ã«ã¼ãã«ã«ãã£ã¦ã¯smapsãåå¨ããªãã¨ããããããå ´åã«ã¯ä½¿ãã¾ããã
ä»åãä¾ã«ãã£ã¦Perlã§æ¸ããã¨ã«ãã©ãããCPANã«ï¼ï½ï½ã¨ãããã¨ã§Linux::Smapsã使ã£ã¦ãã¾ãã
ããã°ã©ã ã¯è¿½è¨ã«ç½®ãã¦ããã¾ãã
ãã¸ãã¯ã軽ãæ¸ãã¦ããã¾ãã
- pid_list()ã/procä¸ã«ããé©åãªPID(æ°åã®é£ç¶)ã®ãªã¹ãã¸ã®ãªãã¡ã¬ã³ã¹ãè¿ãã¾ãã
- åPIDã«ã¤ãã¦Linux::Smapsã®ãªãã¸ã§ã¯ããçæããLinux::Smaps::sizeã¡ã½ãããã¡ã¢ãªä½¿ç¨éãè¿ãã¾ããæå¾ã«ã¾ã¨ãã¦åºåãã¾ãã
- æ®å¿µãªãã¨ã«Linux::Smapsã§ã¯ããã»ã¹åãåãããªãã®ã§ãget_proc_name(PID)ã«ãã£ã¦/proc/PID/smapsãéãã¦å é è¡ããããã»ã¹åãå¼ã£å¼µãåºãã¦ãã¾ãã
ã³ã¡ã³ãã«ãæ¸ãã¦ããã¾ããroot権éã§ããåãã¾ããã®ã§ã注æãã ããã
æ¹é ãåé
å¸ã¯ãèªç±ã«ã
åºåã¯æ¨æºåºåããã©ã¼ãããã¯
PID ã¡ã¢ãªæ¶è²»é ããã»ã¹å[æ¹è¡]
ã¨ãªã£ã¦ãã¾ããåºåã®1è¡ç®ãåç
§ãã¦ãã ããã
ç§ã®ã¬ãã¼ãã§ã¯ãã®çµæãRã§å¦çãã¦Pie Chartã«ãã¾ããã
process_mem.pl
#!/usr/bin/perl # Created by Limit <[email protected]> # http://d.hatena.ne.jp/limitusus/ # *** RUN WITH ROOT PERMISSION *** use strict; use warnings; use Linux::Smaps; # Setting my $prefix = q|/proc|; my $pids = pid_list(); print qq|# PID SIZE PROCESSNAME\n|; for my $pid (@$pids) { #print $pid, $/; my $smaps = new Linux::Smaps($pid) or die "Can't open smaps"; $smaps->update; next if $smaps->size == 0; print qq|$pid |, $smaps->size ," ", get_proc_name($pid), $/; } sub pid_list { opendir PROC, $prefix or die "Can't open dir!"; my @files = readdir PROC; my @pids = (); for my $pid (@files) { next if ($pid !~ (/\d+/)); push @pids, $pid; } closedir PROC; return \@pids; } sub get_proc_name { my $pid = shift; open SMAPS, "/proc/$pid/smaps" or die "Can't open smaps"; my $line = <SMAPS>; my @array = split /\s+/, $line; close SMAPS; return $array[$#array]; }