How to use awk to sum Pss memory in a smaps file?

0
13
Asked By CuriousCoder92 On

I'm trying to figure out which awk command works best to parse the /proc/1234/smaps file and sum up the Pss (Proportional Set Size) memory for all private, clean memory mappings from a process. Here are the options I'm considering:

A. awk '/^Pss:/ {pss=$2} /Private_Clean/ {sum += pss} END {print sum}' /proc/1234/smaps
B. awk '/^Pss:/ {sum += $2} END {print sum}' /proc/1234/smaps
C. awk '/Private_Clean/ {getline; if ($1=="Pss:") sum+=$2} END {print sum}' /proc/1234/smaps
D. awk 'BEGIN {RS="nn"} /Private_Clean/ {for(i=1;i<=NF;i++) if($i=="Pss:") {sum+=$(i+1); break}} END {print sum}' /proc/1234/smaps

I'd love to hear which option is correct and why!

2 Answers

Answered By TechTinker30 On

Option B looks tempting because it’s simpler. However, it doesn't specifically target Pss values for Private_Clean mappings, so it may include unwanted results. If you're strictly after the Pss for Private_Clean, D is definitely the way to go!

MemoryGuru21 -

Yeah, B is too broad. D gets you exactly what you need.

DataDynamo58 -

For sure! D has the precise focus there.

Answered By KnowledgeSeeker47 On

The best choice here is option D. This command correctly sets the record separator to double newlines, which helps it effectively group the memory mappings together before checking for the Pss value. Therefore, it sums the correct values for all private, clean memory mappings. It's a bit complex but works well!

SyntaxNinja33 -

Thanks for breaking it down! I was leaning towards D as well, but wasn't sure about the record separator part.

CodeWhiz88 -

Great explanation! I appreciate how you clarified the grouping with double newlines.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.