in reply to capture cpu and memory usage of JVM using Perl

You should look at 'ps -aux' (the '-' is deprecated on some *nix then use 'ps aux') and its output, else if 'ps' does not give all the info you need read up on the proc filesystem.

Here is some code:
#!/usr/bin/perl use strict; use Data::Dumper; use constant PROG_NAME => 10; my $what_im_looking_for = 'JVM'; my %ps_hash; my @ps = `ps aux`; foreach (@ps) { my @tmp = split; next if ($tmp[PROG_NAME] ne $what_im_looking_for); $ps_hash{$tmp[PROG_NAME]}{cpu} += $tmp[3]; $ps_hash{$tmp[PROG_NAME]}{mem} += $tmp[4]; $ps_hash{$tmp[PROG_NAME]}{time} += $tmp[9]; } print Dumper(\%ps_hash);
Look at the output of 'ps aux' and find what your JVM is listed as in the proc table. Then just stick the name in $what_im_looking_for
Sample ps output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 1324 76 ? S 2001 4:22 init [3 +] root 2 0.0 0.0 0 0 ? SW 2001 0:07 [kflush +d] root 3 0.0 0.0 0 0 ? SW 2001 0:12 [kupdat +e] root 4 0.0 0.0 0 0 ? SW 2001 0:00 [kpiod] root 5 0.0 0.0 0 0 ? SW 2001 0:25 [kswapd +] root 6 0.0 0.0 0 0 ? SW< 2001 0:00 [mdreco +veryd] root 45 0.0 0.0 0 0 ? SW 2001 0:00 [khubd] root 353 0.0 0.5 1384 340 ? S 2001 0:26 syslogd + -m 0 root 363 0.0 0.2 1680 184 ? S 2001 0:01 klogd ^^^^^^^ +^^^^^^^ This is + where the name is


grep
grep> cd pub grep> more beer

Replies are listed 'Best First'.
Re: Re: capture cpu and memory usage of JVM using Perl
by rob_au (Abbot) on Jan 05, 2002 at 09:14 UTC
    I have described an alternate method using the Proc::Process module at Determining memory usage of a process... - This module gives access to the process table in a consistent fashion, hiding the vagarities of different /proc implementations.

    With regard to the question at hand, the following code incorporating Proc::ProcessTable may be of use:

    use strict; print join("\n", &usage("jvm")), "\n"; # this subroutine takes only one parameter, the process name # to return memory usage information on # # Returns array of two values, raw process memory size and # percentage memory utilisation, in this order. Returns # undefined if these values cannot be determined. sub usage { use Proc::ProcessTable; my @usage; my $proc = Proc::ProcessTable->new; my %fields = map { $_ => 1 } $proc->fields; return undef unless exists $fields{'cmnd'}; foreach (@{$proc->table}) { if ($_->cmnd =~ /$_[0]/) { my @proc; push (@proc, $_->size) if exists $fields{'size'}; push (@proc, $_->pctmem) if exists $fields{'pctmem'}; push @usage, \@proc; } } return @usage; }

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'