Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on capture cpu and memory usage of JVM using Perl

Replies are listed 'Best First'.
Re: capture cpu and memory usage of JVM using Perl
by grep (Monsignor) on Jan 05, 2002 at 03:31 UTC
    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
      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'

Re: capture cpu and memory usage of JVM using Perl
by Anonymous Monk on Jan 05, 2002 at 04:22 UTC
    You may want to look at this.
Re: capture cpu and memory usage of JVM using Perl
by Aighearach (Initiate) on Jan 05, 2002 at 03:11 UTC
    That is a very general question. What are you trying to capture? CPU and memory are at the OS level. Perhaps a basic UNIX book would help you assess the problem deep enough to ask a Perl-centric question about accessing it.

    Or if you don't want to learn UNIX and Perl, you could just hire a consultant... there's a zillion of us around...
    --
    Snazzy tagline here