• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to find out what is using swap on FreeBSD?

pentago

Member

Thanks: 3
Messages: 66

#1
I'm trying to pinpoint what process(es) is eating swap on my FreeBSD server.

I can only see how much is being used (top) but not what process is swapping its guts out.

I tried searching Google for this but didn't find anything.

Is there a way to display list of swapped data by process natively or by using some external software to achieve this?

Thanks
 

Nicola Mingotti

Member

Thanks: 44
Messages: 89

#3
The thread is a bit old, but i had the same need today.

The fastest solution I found is, run top with the option to show swap usage
Code:
$> top -w
Then, sort processes according to their swap usage pressing "o",
then write "swap" and press RETURN.

Tested working in FreeBSD-11.1.

Bye
Nicola
 

PacketMan

Aspiring Daemon

Thanks: 104
Messages: 773

#4
Yeah it does show swap but I find the numbers don't add up. My system currently says 67M of swap used, but the two process that are using swap add up to less than 5M of swap. The rest of the processes show 0K. And I am running -S flag so I see all processes.
 

Nicola Mingotti

Member

Thanks: 44
Messages: 89

#5
I agree with you PacketMan, also my per process swap measures summed up are well below
the grand total i see in "Swap Used".

"top" man page explicitely says:
Code:
-w Display approximate swap usage for each process.
I wrote this script in Ruby for reproducibility but there is one issue,
I am not sure in batch mode "top" will print all relevant rows.
Code:
[NOPARSE]
#!/usr/local/bin/ruby

topDat = `top -S -w -o swap -b`
topLines = topDat.split /\n/;

puts "\n--- summary ----"
puts topLines[3..4]
puts ""

print "--- per pid data ----"
# line 8 is where data starts
# remove last line which is white
puts "pid, swap"
allSwap = 0
(8..(topLines.length - 1)).each do |i|
  line = topLines[i]
  # add a space at beginning for ease of splitting
  line = "x #{line}"
  fields = line.split /\s+/
  puts "#{fields[1]}, #{fields[8]}"
  fields[8].sub!(/K/, " * 1024")
  fields[8].sub!(/M/, " * 1024 * 1024")
  # puts fields[8];
  newVal = eval(fields[8])
  allSwap = allSwap + newVal
end

puts "\n------ SUMMING on pid ----"
sepThous = sprintf("%d", allSwap).gsub(/(\d)(?=\d{3}+$)/, '\1,')
puts "summed per pid Swap: #{sepThous}"
puts ""
[/NOPARSE]
To know more we should read "top" code, I did it one week ago,
for another problem, it is readable. I can't do it right now, if you
can do it and discover some deep secret let me know;)