Memory Management intro:

- Looking at three problems:

1. page replacement. e.g. clock, lru, fifo, working set

2. kernel page table management

3. vm page table management.

QUESTION: Why such a big deal?

- benefit: avoiding 1 page fault saves 5 million cycles -- can do a
lot of work to avoid paging

- Opportunity: lots of different programs, no perfect prediction,
always an opportunity to improve

Major themes:

- page replacement: global vs. local, etc.

Issue: avoid thrashing, good performance

- Kernel page table structures:

Issue: portability across machines, use virtual memory for new things


- virtual machine memory management

Issue: managing memory without cooperation of an OS


Working set.




Context: automatic resource and scheduling a pretty new thing, as was
multiprogramming. Previously, applications handled it themselves
(e.g. explicitly swap memory, reserve fixed amount).

Key problem: efficiency, simple programming

Efficiency: want full utilization of CPU -- it was the critical
resource

Simple programming: not have to deal with manual resource allocation.

Prior work: automatically give pages to processes, use simple LRU or
FIFO global page replacement. Clock.

CLOCK problems: need to know how fast clock hands move to make memory
available. If too fast for some programs, the memory it needs won't be
there. If too slow, not enough free memory when needed.

Problem: thrashing. Every program takes a page fault, swaps waiting
for a page, meanwhile somebody takes another page from it, and when it
comes back, it faults again.

Other people: perhaps VM not worth it? Ask for advice? e.g. let
programmer tell system how much memory it needs, when to bring in
pages?

Problem:

- Usage may depend on environment, hard for programmer
- Modular compilation makes it hard for compilers.

Observation: not worth scheduling a process that doesn't have enough
memory to make progress. Need to make scheduler + memory manager work
together.


Approach: build a model of program behavior, use model + measurements
to make scheduling + allocation decisions.

APPROACH (KEY): instead of figuring out what page to discard, figure
out what pages should be there, and make sure they are. COMPARE TO CLOCK

MODEL:

2 level memory

Traverse time T == time to transfer a page between memory

Goal of mem mgmt:

- Decide which pages are in core -- decide which pages to REMOVE (not
load)

- optimize to minize page traffic == pages/unit time moved between memories.

- page in on demand only

Prior work: LRU, Random, LFU, FIO, ... Problem: susceptible to
overload under heavy competition. EXPLAIN WHY


Working set == minimal set of pages that must be in main memory for a
process to operate efficiently without unnecessary page faults


Presentation:

1. identify problem.

2. Show graph of working set over time - curves up and to the
right. Longer window, more is in there. Show what happens as you
slide incrementally - a few pages enter a few pages leave.

3. Model drives a notion that over some time scale tau, you can
capture the locality of a program: what is referenced? Locality is
both size of w-set and sensitivity - how much does it change as tau
is changed. This lets you capture memory, cpu demand

4. Give app what it demands, measure what it needs




W(t, tau) of a process at time t is the pages referenced in time
(t-tau, t) == set of pages referenced in last interval of length
tau.

Working set size w(t,tau) = |W(t,tau)|

Properties:

1. Size: monotonically increasing, converging: w(t,2tau) <= 2w(t,tau)
(show plot)

2. Prediction: immediate past behavior predicts the future:

W(t,tau) is agood predictor for W(T+a,tau) for small a. For large
a, it is not good.

Probability that W(t+a,a) intersect W(t,tau) = null is small

3. Reentry rate:

As tau is reduces, w(t,tau) (size) reduces; probability that useful
pages are not in W(t,tau) increases.

Lambda = process entry rate (only while process is running)
        
            mean process time between instants at which a
         given page reenters W(t,tau) = 1/lambda(tau)

phi = real-time entry rage, mean real time between reentry of a page
= 1/phi(tau)

A reentry point is a reference instant which finds the page not in
W(t,tau) - at such an instant, it reenters W.

{tn} = sequence of instances when references to page
occur. inter-reference interval xn - tn - tn-1.

Idea: lambda(tau) = 1/m(tau), m == mean time between reenties

lambda(tau) = (1 - Fx(tau)) / mean inter-arrival time, Fx(tau) =
probability that interval between request < tau

         1 - Pr[interval < tau]/ mean interval

So: in interval A, single page reentiers is Alambda(tau)
But, each reentry causes wait for time T to bring back page

so: real time to retrieve execute that much = A + Alambda(tau) T
(execution time + page fault time) assuming no unnecessary stalls

Return traffic rate phi(t) = A lambda(tau) / (A + A lambda(tau) T)
         = lambda(tau)/(1+lambda(tau)T)
         = fault rate / total elapsed time

DEFINE: memory balance = sum(w(t,tau) = betaM == some fraction beta
of meain memory size

Total return traffic rate == total reentry rate i nreal-time to
main memory when no unnecessary stalling

== betaMphi(tau) = betaM * lambda(tau)/(1+lambda(tau)T)


Key point: total traffic limited by bus/disk bandwidth. So, choice
of Tau, beta informed by physical limitations

4. Tau-senstivity

sigma(tau) = how sensitive reentry rate is to tau.

tau-sensitivity = -(d/dr)lambda(tau) = fx(tau)/average interval,
fx(tau) = probability density of interval function

Meaning: if tau is decreased by delta-tau, lambda(tau) increases by
sigma(ta).delta-tau

NOTE: Choice of tau:

- too small means pages may be removed while still useful
- too large, pages in memory too long, wasting memory (impacted
by how many simultaneous working sets you need in memory)


Recommmendation: tau comparable to traverse time T

Example: interrefeence rate x:

1. x < tau : page in memory 100% of time, always in W(t,tau)

2. tau < x < tau + T : page in memory for tau/(tau+2T) fraction
of time: in for tau, then paged out, during which it is
referenced, so begins return trip (e.g. 2T for out to disk and
back), page reappears tau+2T after previous reference

3. x > t+tau. Page in memory for tau/(x+T): in memoyr for tau,
swapped out , then referenced (at x) then brough back in
(time T)

            Residency function: interval time vs. residency fraction:
            - residency starts at 100, step drop to case 2, curve down to
            zero at T+tau

How do we detect W (the working set of pages)?

W(t,tau) = pages process referenced in last tau interval
So: have timer for tau on each page. Pages not marked are evicted.

Or: reference bits on page, shift at intervals adding up to tau.

Resource allocation with working set:

Memory demand of process i mi = min(wi/M,1) = fraction of main memory

Processor demand = amount of quanta used before blocking.
            = Q(ti)/ NA == if was just given ti seconds,
            how much more would it use before blocking / # of processors
            * standard time interval (NA = total capacity)

Balance = memory alpha % utilized, CPU beta percent utilitzed

S = sum(processes in running list) mem demand, CPU demand

Balance = total mem demand is betaM, total CPU demand is alpha .N.A

SCHEDULING POLICY TO ACHIEVE BALANCE:

- block jobs on the ready list: pick the one that brings CPU, memory
closest back to balance (e.g. amount used - beta, ...)

1. avoid thrasing by balancing memory first, then balancing processor
(avoid memory overcommit)


OVERALL BIG PICTURE:
- track pages in use by a process. Throw out anything not referenced
in last window
- make sure every process gets its CPU, memory demand
- swap out jobs to bring machine back to utliization goal


QUESTIONS:
- what happens during load, when no pages in memory? A: need to
identify load phase, do something different