Skip to content
Advertisement

Linux /proc/pid/smaps proportional swap (like Pss but for swap)

It seems (from looking at the Linux kernel source) that the Swap: metric in /proc/pid/smaps is the total swap accessible by the given pid.

In the case where there is shared memory involved, this seems to be an over-approximation of actual swap usage. For example when summing swap usage of a parent pid with its forked children, and if they have common shared memory in the swap, then it appears that this portion (swapped shared memory) is counted multiple times (once per pid).

My question is whether there is a way to figure out a fair swap usage metric based on the number of processes sharing it (similar to Pss:).

Advertisement

Answer

You just have to divide Swap value by number of process that are sharing this Virtual Memory Area.

Actually, I didn’t find how to get number of process sharing a VMA. However, it is sometime possible to calculate it by dividing RSS with PSS. Sure, it only work if PSS != 0.

Finaly, you can use this perl code (pass smap file as argument):

#!/usr/bin/perl -w
my ($rss, $pss);
my $total = 0;

while(<>) {
  $rss = $1 if /Rss: *([0-9]*) kB/;
  $pss = $1 if /Pss: *([0-9]*) kB/;
  if (/Swap: *([0-9]*) kB/) {
    my $swap = $1;
    if ($swap != 0) {
      if ($pss == 0) {
        print "Cannot get number of process using this VMAn";

      } else {
        my $swap = $swap * $rss / $pss;
        print "P-swap: $swapn";
      }
      $total += $swap;
    }
  }
}
print "Total P-Swap: $total kBn"
Advertisement