#!/usr/bin/perl # Copyright Erik Jacobson - erik@underhanded.org use warnings; use strict; use Statistics::Descriptive; my $webuser = 'www-data'; # The user your apache children run as. I i +gnore the root process. my $command = 'apache2'; # The name of your processes as they appear t +o the "top" command. my @top = `top -bn1`; my %header; my $mem = Statistics::Descriptive::Full->new(); my $othermem = Statistics::Descriptive::Full->new(); my $totalmem; foreach (@top) { s/^\s+|\s+$//sg; my @line = split(/\s+/, $_); if(/mem:\s+(\S+)\s+total/i) { $totalmem = convtok($1); } if(defined $line[3] && exists $header{res} && $line[0] =~ /^\d ++$/) { next unless (defined $line[$header{user}] && defined $ +line[$header{res}] && defined $line[$header{shr}] && defined $lin +e[$header{command}]); if(($line[$header{user}] eq $webuser) && ($line[$heade +r{command}] eq $command)) { $mem->add_data(convtok($line[$header{res}]) - +convtok($line[$header{shr}])); } else { $othermem->add_data(convtok($line[$header{res} +]) - convtok($line[$header{shr}])); } } elsif (!exists $header{res}) { my %tempheader; for (0 .. $#line) { $tempheader{lc($line[$_])} = $_; } if(defined $tempheader{user} && defined $tempheader{re +s} && defined $tempheader{shr} && defined $temphe +ader{command}) { %header = %tempheader; } } } printf "Total memory available: %.2fG\n", ($totalmem / 1024 / 1024); printf "Total used by $command (%d instances): %.2fG\n", $mem->count() +, ($mem->sum() / 1024 / 1024); printf "Total used by other processes: %.2fG\n\n", ($othermem->sum() / + 1024 / 1024); $totalmem -= $othermem->sum(); printf "Average memory used per $command process: %.2fM\n", ($mem->mea +n() / 1024); printf "Recommended number of processes based on Average: %d\n", ($tot +almem / $mem->mean()); printf "Needed memory for 500 processes based on Average: %.2fG\n\n", +($mem->mean() / 1024 / 1024 * 500); printf "Max memory used for $command process: %.2fM\n", ($mem->max() / + 1024); printf "Recommended number of processes based on Max: %d\n", ($totalme +m / $mem->max()); printf "Needed memory for 500 processes based on Max: %.2fG\n\n", ($me +m->max() / 1024 / 1024 * 500); printf "Mean plus two Standard Deviations (bulk of usage under max): % +.2fM\n", (($mem->mean() + $mem->standard_deviation() * 2) / 1024); printf "Recommended number of processes based on Mean + 2*Stdev: %d\n" +, ($totalmem / ($mem->mean() + $mem->standard_deviation() * 2)); printf "Needed memory for 500 processes based on Mean + 2*Stdev: %.2fG +\n", (($mem->mean() + $mem->standard_deviation() * 2) / 1024 / 1024 * + 500); sub convtok { my $num = shift; if($num =~ /^\s*([0-9.]+)k\s*$/i) { $num = $1; } elsif ($num =~ /^\s*([0-9.]+)m\s*$/i) { $num = $1 * 1024; } elsif ($num =~ /^\s*([0-9.]+)g\s*$/i) { $num = $1 * 1024 * 1024; } return $num; }

In reply to Analyzing Apache memory usage by Azhrarn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.