CSE Roadmap

Core tier · about 5 hours · 7 parts

Part 7 of 7 · 3 min read

Exercises

Easy

Easy 1: Add the parts (20 minutes)

A process has RssAnon 80,000 kB, RssFile 12,000 kB and RssShmem 8,000 kB. Find its VmRSS.

Done when: 100,000 kB.

Hint

Add the three numbers.

Easy 2: Reserved and used (20 minutes)

A program calls malloc(500 << 20) and never writes to the block. What changes in VmSize and in VmRSS?

Done when: VmSize grows by about 512,000 kB, and VmRSS by almost nothing.

Hint

500 MiB is 512,000 kB.

Easy 3: Compute the PSS (20 minutes)

Two processes share a region of 100 MiB. Each also has 10 MiB of private pages. Find the RSS and the PSS of each.

Done when: RSS 110 MiB each, PSS 10 + 50 = 60 MiB each, and the sum of the PSS is 120 MiB, the real use.

Hint

Charge each sharer half of 100 MiB.

Medium

Medium 1: The mystery program (60 minutes)

Write a program that does the five steps of section 1 and prints VmSize, VmRSS, RssAnon, RssFile and RssShmem. Check that the three parts add up to VmRSS.

Done when: a table like section 1 and 2 with your numbers and the check of the sum.

Hint

Read the fields from /proc/self/status.

Medium 2: RSS and PSS after a fork (45 minutes)

Extend it to fork a child that sleeps without writing. Print Rss and Pss of the parent from smaps_rollup before and after the fork. Then let the child write to the 100 MiB block and print again.

Done when: a table in which the PSS falls after the fork and rises again after the child's writes.

Hint

Wait a moment after the fork before you read smaps_rollup.

Medium 3: Read a real process (45 minutes)

Run pmap -x on a program on your machine (a browser or an editor). List the three mappings with the largest resident size, and say what each is.

Done when: three lines with size, resident size and a guess of the kind.

Hint

pmap -x PID | sort -k3 -n -r | head.

Hard

Hard 1: A growth detector (90 minutes)

Write a script that samples RssAnon, RssFile and RssShmem of a PID every second for a minute and prints the rate of change per second of each. Test it on the leaking handler.

Done when: a rate of about 1,000 kB per second of RssAnon for a leak of 1 MiB per second, and about 0 for the others.

Hint

Use sleep 1 in a loop, and subtract consecutive values.

Hard 2: Find the line with Massif (90 minutes)

Run valgrind --tool=massif on a program with a leak that you wrote, and read the report with ms_print. Fix the leak, and run again.

Done when: the report before the fix names the file and line, and the report after it shows a flat curve.

Hint

ms_print massif.out prints the peak snapshot with the call stack.

Hard 3: Three kinds of growth (120 minutes)

Write a program with three modes: (1) leak 1 MiB per second on the heap; (2) map and never unmap 1 MiB per second of anonymous memory; (3) a cache that grows to 50 MiB and stops. For each mode, record RssAnon over 60 seconds and the pmap before and after, and say how you can tell them apart.

Done when: a table with a rising line for modes 1 and 2, a line that levels for mode 3, and pmap showing a growing heap line for mode 1 and a growing number of anonymous lines for mode 2.

Hint

A mapping that is not unmapped appears as its own line only if its neighbours differ in permissions or kind, so look at the sizes as well.