CS 134

Metrics, Monitoring, and Tuning

  • Cat speaking

    There seem to be lots of knobs and dials to tweak when it comes to paging algorithms. But how do we know what to set them to?

  • Goat speaking

    And can the system know for itself so we don't have to?

Working Set

One metric that might help inform our decisions is the working set of a process. The working set is the set of pages that a process is actively using at any given time. If we can keep all of those pages in memory, the process will run without any page faults. The size of the working set tells us how many frames the process needs to run efficiently—fewer and it will page fault frequently, more and we're wasting memory.

Some algorithms, like the Mach paging-queues approach, provide a good estimate of the working set almost for free. But for others, we might need a way to figure it out. One approach is to progressively reduce the number of frames allocated to a process until it starts to page fault frequently and then back off a bit. We call that working-set estimation.

  • Cat speaking

    So we allocate frames on a per-process basis, right? Not system-wide?

  • PinkRobot speaking

    Well, actually, it depends.

Global or Local Frame Allocation?

In all our examples, we've restricted ourselves to looking at just one process. But since memory is a global resource, it may be useful to consider the global picture.

Traditionally,

  • Windows uses local frame allocation, where each process gets a fixed number of frames, although the operating system can vary this number based on the process's behavior.
  • Unix uses global frame allocation, where the operating system can take frames from one process and give them to another if the change improves the overall system performance.

However, Linux today provides a general resource limit-system known as control groups or cgroups that allows us to limit the number of frames a process or group of processes can use.

Each approach has its own trade-offs. For example,

  • How should we deal with shared memory if we have local frame allocation? Which process do we charge the memory to? Both? Neither? Half each?
  • If one process on the system wants more memory than is reasonable (i.e., would cause thrashing), is it really okay for all the other processes to suffer? With local frame allocation the impact would (mostly) be limited to that one process, rather than the whole system.

Paging Daemons

Rather than require the kernel to figure everything out in the moment, we can use a paging daemon to make decisions about paging. The daemon can run in the background and adjust the system's paging parameters based on the system's behavior.

  • Hedgehog speaking

    Wait… Daemons?!

  • Pumpy speaking

    The term “daemon” comes from the programmers working on MIT's Project MAC, who used it to describe background processes doing things to keep the system going, inspired by Maxwell's demon (which would tirelessly sort molecules based on their heat). Daemon as used in operating systems refers back to the ancient Greek daimon (δαίμων), a lesser deity or guiding spirit.

  • L-Chippy speaking

    Some systems today use different terms, such as “service”, “started task”, or “ghost job”. Daemon is just part of the Unix tradition of programmers giving things quirky names.

  • PinkRobot speaking

    Back to paging…

On macOS, we can run the command memory_pressure to get insight into the information the system uses to make paging decisions. Here's the output of memory_pressure on my Mac laptop:

The system has 68719476736 (4194304 pages with a page size of 16384).

Stats:
Pages free: 4736
Pages purgeable: 101694
Pages purged: 671369004

Swap I/O:
Swapins: 42495278
Swapouts: 46422135

Page Q counts:
Pages active: 1720577
Pages inactive: 1717904
Pages speculative: 1375
Pages throttled: 0
Pages wired down: 391849

Compressor Stats:
Pages used by compressor: 282130
Pages decompressed: 3158063518
Pages compressed: 3411520016

File I/O:
Pageins: 147342743
Pageouts: 3730974

System-wide memory free percentage: 83%

(You can also run sysctl vm.memory_pressure to get a single number that represents the system's memory pressure.)

When the memory pressure is high, the system can speed up the movement of pages between the different queues. And it has some other tricks up its sleeve, too, like compressing pages in memory to save space.

  • Pig speaking

    I want to know MORE about some of those other things, like wired-down pages and speculative pages.

  • PinkRobot speaking

    Okay, let's wrap up the lesson by thinking about those things.

(When logged in, completion status appears here.)