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
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!
For sure! D has the precise focus there.
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!
Thanks for breaking it down! I was leaning towards D as well, but wasn't sure about the record separator part.
Great explanation! I appreciate how you clarified the grouping with double newlines.

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