CSE Roadmap

Core tier · about 5 hours · 7 parts

Part 7 of 7 · 3 min read

Exercises

Easy

Easy 1: Strict limit (20 minutes)

A machine has 16 GiB of memory and 4 GiB of swap, with vm.overcommit_memory = 2 and vm.overcommit_ratio = 50. Find the simplified commit limit.

Done when: 4 + 0.5 × 16 = 12 GiB (swap plus the share of memory).

Hint

The formula is swap plus the ratio share of memory.

Easy 2: Read the status (20 minutes)

A shell reports that a program ended with exit status 137. What happened, and what is the likely cause when the program ran in a container?

Done when: it was killed by signal 9 (137 = 128 + 9), and in a container the usual cause is the OOM killer after the memory limit was passed.

Hint

Subtract 128 from the exit status.

Easy 3: Which setting (20 minutes)

Name the setting that makes (a) a 100 GiB mmap fail on an 8 GiB machine; (b) a process exempt from the OOM killer; (c) malloc fail at 512 MiB.

Done when: (a) heuristic overcommit, vm.overcommit_memory 0; (b) oom_score_adj −1000; (c) ulimit -v 524288.

Hint

ulimit -v takes kilobytes.

Medium

Medium 1: Reproduce the promise (45 minutes)

Write a program that maps 75% of your machine's memory (anonymous, private), prints VmSize and VmRSS, and then maps 12 times the memory and prints the result.

Done when: the first call succeeds with a small VmRSS, and the second fails with "Cannot allocate memory", with the sizes in your report.

Hint

Read the memory size from /proc/meminfo.

Medium 2: A failed malloc (45 minutes)

Write the 10 MiB allocator of section 5 and run it under ulimit -v with 256, 512 and 1024 MiB.

Done when: a table of the limit and the point at which malloc failed, always a little below the limit.

Hint

Print after each 10 MiB step, and flush the output.

Medium 3: Read the memory settings (45 minutes)

Collect vm.overcommit_memory, vm.overcommit_ratio, vm.swappiness, SwapTotal, CommitLimit and Committed_AS of your machine.

Done when: a table of six values and one sentence on whether the machine can swap and how strict it is.

Hint

sysctl prints the vm. values, and /proc/meminfo the others.

Hard

Hard 1: An OOM kill in a control group (90 minutes)

On a machine where you are the administrator, create a control group with a memory limit of 200 MiB (or run a container with --memory=200m) and run the program of section 3 in it.

Done when: exit status 137, the last printed line before the kill, and an oom_kill counter of 1, with the commands listed.

Hint

On cgroup version 1 the file is memory.limit_in_bytes, on version 2 it is memory.max.

Hard 2: Graceful failure (90 minutes)

Write a cache that grows by 10 MiB blocks. When malloc returns NULL, the cache must free half of its blocks and retry. Run it under ulimit -v 512000 and show that it keeps running.

Done when: a log of block counts that rise, fall by half at each failure and rise again, with no crash.

Hint

Keep the blocks in an array, and free the upper half on failure.

Hard 3: Reserved and used (90 minutes)

Create 100 threads that sleep, and print VmSize and VmRSS before and after. Then set the stack size to 256 KiB with pthread_attr_setstacksize and repeat.

Done when: VmSize grows by about 800 MB with the default stack and by about 25 MB with 256 KiB, while VmRSS grows by little in both.

Hint

Use pthread_attr_setstacksize before pthread_create.