newspaint

Documenting Problems That Were Difficult To Find The Answer To

Monthly Archives: December 2017

What Processes Are Using Swap Memory in Linux?

On both RedHat 6 and Ubuntu Xenial the amount of a process’s memory stored in the swapfile can be found in the /proc/pid/status file for that process.

Such a file might contain the following (with many lines omitted for the sake of simplicity):

Name:   evolution-calen
State:  S (sleeping)
Tgid:   4207
Ngid:   0
Pid:    4207
PPid:   3688
TracerPid:      0
Uid:    1000    1000    1000    1000
Gid:    1000    1000    1000    1000
VmPeak:   861644 kB
VmSize:   861500 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:     55848 kB
VmRSS:         0 kB
VmData:   477660 kB
VmStk:       132 kB
VmExe:         8 kB
VmLib:     80684 kB
VmPTE:       776 kB
VmPMD:        16 kB
VmSwap:    40128 kB
Threads:        5
voluntary_ctxt_switches:        439
nonvoluntary_ctxt_switches:     4

A Perl script can be used to list what processes currently have memory allocated in the swapfile:

#!/usr/bin/perl -w

use strict;

sub commify {
  local $_  = shift;
  1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
  return $_;
}

my %pids;

foreach my $fname ( glob( "/proc/*/status" ) ) {
  # slurp in status file for process
  local $/ = undef;
  my $fin;
  next if ( ! open( $fin, "<", $fname ) );
  my $data = <$fin>;
  close( $fin );

  # get PID
  next if ( $data !~ m/^Pid:\s*(\d+)/m );
  my $pid = $1;

  # get name
  next if ( $data !~ m/^Name:\s*(.*\S)/m );
  my $name = $1;

  # get swap
  next if ( $data !~ m/^VmSwap:\s*(\d+)\s*([a-zA-Z])?B/m );
  my $vmswap = $1;
  if ( $1 and ( lc($2) eq "k" ) ) {
    $vmswap *= 1024;
  } elsif ( $1 and ( lc($2) eq "m" ) ) {
    $vmswap *= 1024 * 1024;
  } elsif ( $1 and ( lc($2) eq "g" ) ) {
    $vmswap *= 1024 * 1024 * 1024;
  }

  $pids{$pid} = { 'name' => $name, 'vmswap' => $vmswap };
}

# list processes in order of swap memory usage
printf( "%-8s | %-19s | %-24s\n", "PID", "Swap Bytes", "Name" );
foreach my $pid ( sort { $pids{$b}->{'vmswap'} <=> $pids{$a}->{'vmswap'} } keys %pids ) {
  last if ( $pids{$pid}->{'vmswap'} == 0 );

  printf(
    "%8s | %19s | %-24s\n",
    $pid,
    commify( $pids{$pid}->{'vmswap'} ),
    substr( $pids{$pid}->{'name'}, 0, 24 )
  );
}

Typical output might be similar to:

PID      | Swap Bytes          | Name                    
    4207 |          41,091,072 | evolution-calen         
    4277 |          39,084,032 | evolution-calen         
    4288 |          36,646,912 | evolution-calen         
    2588 |          36,634,624 | Xorg                    
   21438 |          28,733,440 | thunderbird             
    4152 |          21,938,176 | blueman-applet          
    4120 |          10,338,304 | applet.py               
    3920 |           8,720,384 | Thunar                  
   10419 |           8,544,256 | gvfsd-http              
    3844 |           6,307,840 | gvfsd-fuse      

Cron – Authentication Failure

You’ve manually added a new user, possibly directly editing the /etc/passwd file, but getting the following errors in /var/log/syslog about cron:

Dec 30 20:57:01 myhost cron[61]: Authentication failure
Dec 30 20:57:01 myhost CRON[212]: Authentication failure

In my case it was because I failed to add a corresponding entry for the user in the /etc/shadow file.