Recall: Address Space Map Somet imes St ack Biggest Reserved f or - - PDF document

recall address space map
SMART_READER_LITE
LIVE PREVIEW

Recall: Address Space Map Somet imes St ack Biggest Reserved f or - - PDF document

Recall: Address Space Map Somet imes St ack Biggest Reserved f or (Space f or local var iables et c. Vir t ual OS 13: Memory Management For each nest ed pr ocedur e call) Address St ack Point er Last Modif ied: Heap (Space f or


slide-1
SLIDE 1

1

  • 1

13: Memory Management

Last Modif ied: 6/ 21/ 2004 9:51:29 AM

  • 2

Recall: Address Space Map

St ack

(Space f or local var iables et c. For each nest ed pr ocedur e call)

Heap

(Space f or memor y dynamically allocat ed e.g. wit h malloc)

St at ically declar ed var iables

(Global var iables)

Code

(Text Segment )

St ack Point er PC Ox0000 Biggest Vir t ual Address Somet imes Reserved f or OS Somet imes Reserved f or Error Cat ching

  • 3

P rocesses Address Space

Logically all of t his address space should

be resident in physical memory when t he pr ocess is r unning

How many machines do you use t hat have

232= 4 GB of DRAM? Let alone 4 GB f or *each* process!!

  • 4

Let ’s be reasonable

Does each process really need all of t his

space in memory at all t imes?

Fir st has it even used it all? lot s of r oom in t he

middle bet ween t he heap gr owing up and t he st ack gr owing down

Second even it has act ively used a chunk of t he

addr ess space is it using it act ively r ight now

  • May be lot s of code t hat is rarely used (init ializat ion

code used only at beginning, error handling code, et c.)

  • Allocat e space on heap t hen deallocat e
  • St ack grows big once but t hen normally small
  • 5

Freeing up Syst em Memory

What do we do wit h por t ions of addr ess space

never used?

Don’t allocat e t hem unt il t ouched!

What do we do wit h r ar ely used por t ions of t he

addr ess space?

This isn’t so easy J ust because a variable rarely used doesn’t mean t hat we

don’t need t o st ore it s value in memory

St ill it ’s a shame t o t ake up precious syst em memory wit h

t hings we are rarely using! (The FS could sure use t hat space t o do caching remember?)

What could we do wit h it ?

  • 6

Send it t o disk

Why couldn’t we send it t o disk t o get it

  • ut of our way?

I n t his case, t he disk is not r eally being used

f or non-volat ile st or age but simply as t empor ar y st aging ar ea

What would it t ake t o r est or e r unning

pr ocesses af t er a cr ash? (Maybe r est or e t o a consist ent checkpoint in t he past ?) Would you want t hat f unct ionalit y? We’d have t o remember where we wrot e it

so t hat if we need it again we can r ead it back in

slide-2
SLIDE 2

2

  • 7

Logist ics

How will we keep t r ack of which r egions ar e paged

  • ut and where we put t hem?

What will happen when a pr ocess t r ies t o access a

r egion t hat has been paged t o disk?

How will we shar e DRAM and disk wit h t he FS? Will we have a minimum size region t hat can be

sent t o disk?

Like in FS, a f ixed size block or page is usef ul f or

reducing f ragment at ion and f or ef f icient disk access

  • 8

Virt ual Memory

Vir t ual Memor y = basic OS memor y management

abst r act ion/ t echnique

Pr ocesses use vir t ual addr esses

Every t ime a process f et ches an inst ruct ion or loads a

value int o a regist er it ref ers t o virt ual memory address OS (wit h help f r om har dwar e) t r anslat es vir t ual

addr esses t o physical addr esses

Translat ion must be f ast !

OS manages sending some por t ions of vir t ual

addr ess space t o disk when needed

Somet ime t ranslat ion will involve st alling t o f et ch page

f rom disk

  • 9

Virt ual Memory provides…

Prot ect ion/ isolat ion among processes I llusion of more available syst em memory

  • 10

Virt ual Memory: I solat ion Among P rocesses

Pr ot ect ion (Dat a I solat ion)

P

rocesses use virt ual memory addresses

These must be convert ed t o physical memory addresses

in order t o access t he physical memory in t he syst em

Gives prot ect ion because processes unable even t o

address (t alk about ) anot her processes address space Per f or mance I solat ion

OS also t ries t o share limit ed memory resources f airly

among processes

Can one process use so much of t he memory t hat ot her

processes f orced t o page heavily?

Can one process use so much of t he backing st ore t hat

  • t her processes get out of memory errors?
  • 11

Virt ual Memory: I llusion of Full Address Space

We’ve seen t hat it makes sense f or pr ocesses not

t o have t heir ent ir e addr ess space r esident in memor y but r at her t o move it in and out as needed

P

rogrammers used t o manage t his t hemselves One ser vice of vir t ual memor y is t o pr ovide an

convenient abst r act ion f or pr ogr ammer s (“Your whole wor king set is available and if necessar y I will br ing it t o and f r om disk f or you”)

Br eaks in t his illusion?

When you are “paging” heavily you know it ! Out of memory errors - what do t hey mean?

  • 12

HW Support f or Virt ual Memory

Fast t ranslat ion =>

hardware support

Or OS would have t o be involved on ever y

inst r uct ion execut ion OS init ializes hardware properly on

cont ext swit ch and t hen hardware supplies t ranslat ion and prot ect ion while

slide-3
SLIDE 3

3

  • 13

Technique 1: Fixed P art it ions

OS could divide physical memory int o f ixed

sized regions t hat are available t o hold port ions of t he address spaces of processes

Each process get s a part it ion and so t he

number of part it ions => max r unnable processes

  • 14

Translat ion/ P rot ect ion Wit h Fixed Sized P art it ions

Hardware support

Base r egist er Physical addr ess = Vir t ual Addr ess + base

Regist er

I f Physical addr ess >

par t it ion size t hen har dwar e can gener at e a “f ault ” During cont ext swit ch, OS will set base

regist er t o t he beginning of t he new processes part it ion

  • 15

P aging t o Disk wit h Fixed Sized P art it ions?

Har dwar e could have anot her r egist er t hat says

t he base vir t ual addr ess in t he par t it ion

Then t r anslat ion/ pr ot ect ion would go like t his:

I f virt ual address generat ed by t he process is bet ween

t he base virt ual address and base virt ual address + lengt h t hen access is ok and physical address is Virt ual Address – Base Virt ual Address Regist er + Base Regist er

Ot herwise OS must writ e out t he current cont ent s of

t he part it ion and read in t he sect ion of t he address space being accessed now

OS must record locat ion on disk where all non resident

regions are writ t en (or record t hat no space has been allocat ed on disk or in memory if a region has never been accessed)

  • 16

P roblems Wit h Fixed Sized P art it ions

Must access cont iguous por t ion of addr ess space

Using bot h code and st ack could mean a lot of paging!!!

What is t he best f ixed size?

I f t ry t o keep everyt hing a process needs part it ion might

need t o be very big (or we would need t o change how compiler lays out code)

P

aging in such a big t hing could t ake a long t ime (especially if only using a small port ion)

Also would “best ” size vary per process?

  • Some pr ocesses might not need all of t he “f ixed” size while
  • t her s need mor e t han t he “f ixed” size
  • I nt er nal f r agment at ion
  • One f ixed sized part it ion = heavy paging f or all processes

why?

  • 17

Technique 2: Variable Sized P art it ions

Ver y similar t o f ixed sized par t it ions Add a lengt h regist er (no longer f ixed size f or

each process) t hat hardware uses in t ranslat ion/ prot ect ion calculat ions and t hat OS saves/ rest ores on cont ext swit ch

No longer have problem wit h int ernal

f ragment at ion

  • 18

Variable P art it ions (con’t)

May have ext er nal f r agment at ion

As processes are creat ed and complet e, f ree space in

memory is likely t o be divided int o small pieces

Could relocat e processes t o coalesce t he f ree space?

How does OS know how big t o make each

pr ocesses par t it ion? Also how does OS decide what is a f air amount t o give each pr ocess?

St ill have pr oblem of only using only cont iguous

r egions

slide-4
SLIDE 4

4

  • 19

P aging

Could solve t he ext ernal f ragment at ion

problem, minimize t he int ernal f ragment at ion problem and allow non- cont iguous regions of address space t o be resident by..

Breaking bot h physical and virt ual memory

up int o f ixed sized unit s

Smaller t han a par t it ion but big enough t o make

r ead/ wr it e t o disk ef f icient of t en 4K/ 8K

Of t en mat ch FS – why?

  • 20

Finding pages?

Any page of physical memory can hold any page

  • f virt ual memory f rom any process

How ar e we going t o keep t r ack of t his? How ar e we going t o do t r anslat ion?

Need t o map virt ual memory pages t o physical

memory pages (or t o disk locat ions or t hat no space is yet allocat ed)

Such maps called Page t ables

One f or each pr ocess (vir t ual addr ess x will map

dif f er ent ly t o physcial pages f or dif f er ent processes)

  • 21

P age Table Ent ries

Each ent ry in a page t able maps virt ual

page numbers (VPNs) t o physical page f rame numbers (PFNs)

Vir t ual addr esses have 2 par t s: VPN and of f set Physical addr esses have 2 par t s: PFN and

  • f f set

Of f set st ays t he same is vir t ual and physical

pages ar e t he same size

VPN is index int o page t able; page t able ent r y

t ells PFN

Ar e VPN and PFN t he same size?

  • 22

Translat ion

Vir t ual page #

  • f f set

Virt ual Address Page f r ame # Page f r ame #

  • f f set

Physical Address Page f r ame 0 Page Fr ame 1 Page f r ame N

  • 23

Example

Assume a 32 bit addr ess space and 4K page size

32 bit address space =>

virt ual addresses have 32 bit s and f ull address space is 4 GB

4K page means of f set is 12 bit s (212 = 4K) 32-12 = 20 so VPN is 20 bit s How many bit s in PFN? Of t en 20 bit s as well but wouldn’t

have t o be (enough j ust t o cover physical memory) Suppose vir t ual addr ess

00000000000000011000000000000111 or Ox18007

Of f set is Ox7, VP

N is 0x18

Suppose page t able says VPN 0x18 t ranslat es t o PFN 0x148

  • r 101001000

So physical addr ess is

00000000000101001000 000000000111 or 0x148007

  • 24

P age Table Ent ries Revisit ed

Ent r y can and does cont ain mor e t han j ust a page

f r ame number

(M)odif y bit – whet her or not t he page is dir t y (R )ef er ence bit – whet her of not t he page page

has been r ead/ wr it t en

(V)alid bit – whet her or not t he page t able ent r y

cont ains valid t r anslat ion

(pr ot )ect ion bit s say which oper at ions ar e valid on

t his page (Read/ Wr it e/ Execut e)

Page f r ame number

M R V pr ot Page f r ame number

slide-5
SLIDE 5

5

  • 25

P rocesses’ View of P aging

Pr ocesses view memor y as a cont iguous addr ess

space f rom byt es 0 t hrough N

OS may reserve some of t his address space f or it s own

use (map OS int o all processes address space is a cert ain range or declare some addresses invalid) I n r ealit y, vir t ual pages ar e scat t er ed acr oss

physical memor y f r ames (and possibly paged out t o disk)

Mapping is invisible t o t he program and beyond it s cont rol

Pr ogr ams cannot r ef er ence memor y out side it s

vir t ual addr ess space because vir t ual addr ess X will map t o dif f er ent physical addr esses f or dif f er ent pr ocesses!

  • 26

Advant ages of P aging

Avoid ext er nal f r agment at ion

Any physical page can be used f or any virt ual page OS maint ains list of f ree physical f rames

Minimize int er nal f r agment at ion (pages ar e much

smaller t han par t it ions)

Easy t o send pages t o disk

Don’t need t o send a huge region at once Use valid bit t o det ect ref erence t o paged out regions

Can have non-cont iguous r egions of t he addr ess

space r esident in memor y

  • 27

Disadvant age of P aging

Memor y t o hold page t ables can be lar ge

One P

TE per virt ual page

32 bit address space wit h 4KB pages and 4 byt es/ PTE =

4 MB per page t able per process!!!

25 processes = 100 MB of page t ables!!!!! Can we reduce t his size?

Memor y r ef er ence over head

Memory ref erence means 1 memory access f or t he page

t able ent ry, doing t he t ranslat ion t hen 1 memory access f or t he act ual memory ref erence

Caching t ranslat ions?

St ill some int er nal f r agment at ion

P

rocess may not be using memory in exact mult iples of page size

P

ages big enough t o amort ize disk lat ency

  • 28

Reduce t he Size of t he P age Tables?

Play same t rick we did wit h address space –

why have a PTE f or vir t ual pages never t ouched?

Add a level of indir ect ion ☺ Two level page t ables

  • 29

Two level P age Table

Add a level of indir ect ion Vir t ual addr esses now have 3 par t s: mast er page

# , secondar y page # and of f set

Make Mast er page t able f it in one page

4K page = 1024 4 byt e P

TEs

So 1024 secondary page t ables = 10 bit s f or mast er, st ill

12 f or of f set so 10 lef t f or secondary I nvalid MPTE means whole chunk of addr ess space

not t here

Virt ual Address

  • f f set

Secondar y page # Mast er page # Vir t ual page #

  • f f set
  • 30

Translat ion

Secondar y page t able Page f r ame #

  • f f set

Physical Address Virt ual Address

  • f f set

Secondar y page # Mast er page # Page f r ame #

slide-6
SLIDE 6

6

  • 31

P age t he page t ables

I n addit ion t o allowing MP

TE’s t o say invalid could also say t his secondary page t able is on disk

Mast er PTE f or each process must st ay in

memory

Or maybe add anot her level of indir ect ion? Table mapping Mast er PTEs f or each pr ocess t o

DRAM locat ion of disk LBA

  • 32

Too much of a good t hing?

Each level of indirect ion adds t o access

cost

Original page t able scheme doubled t he

cost of memory access (one f or page t able ent ry and one f or real memory locat ion)

Two level page t ables t riple t he cost Solve problem wit h our ot her f avorit e CS

t echnique… .caching!

  • 33

TLB

Add a hardware cache inside t he CPU t o

st ore recent virt ual page t o page t able ent ries

Fast ! One machine cycle f or a hit

OS doesn’t even have t o get involved when

hit in t he TLB

TLB = t ranslat ion lookaside buf f er

  • 34

TLB

Usually a f ully associat ive cache Cache t ags are virt ual page numbers

FAST! All ent r ies ar e sear ched/ compar ed in

par allel

SMALL! Usually only 16-48 ent r ies (64-192KB) I n har dwar e, SMALL of t en equals FAST

Cache values ar e P

TEs

TLB is managed by t he memory

management unit or MMU

Wit h PTE + of f set , MMU can dir ect ly calculat e

t he physical addr ess

  • 35

How ef f ect ive are TLBs?

Only 16-48 ent ries

Maps only 64- 192 KB of address space I f process act ive using more address space t han t hat will

get TLB misses Amazingly >

99% of addr ess t r anslat ions ar e hit s!!

What does t hat mean?

Pr ocesses have ver y high degr ee of localit y t o

t heir accesses pat t er ns

When map a 4K page likely access one memory locat ion,

t hat pref et ches t he rest and likely t o access t hem next (if so 1 in 1024 4 byt e accesses will be hit s)

  • 36

TLB miss

On a TLB miss, what happens? Har dwar e loaded TLBs

Hardware knows where page t ables are in memory

(st ored in regist er say)

Tables must be in HW def ined f ormat so t hat it can

parse t hem

X86 wor ks t his way

Sof t war e loaded TLB

On TLB miss generat e an OS f ault and OS must f ind and

load t he correct P TE and t hen rest art t he access

OS can def ine PTE f ormat as long as loads in f ormat HW

want s int o t he TLB Eit her way have t o choose one of cur r ent ent r ies

t o kick out – which one?

TLB replacement policy usually simple LRU

slide-7
SLIDE 7

7

  • 37

Ot her OS responsibilit y?

Even if have HW loaded TLB What else must t he OS do?

Hint : cont ext swit ch

  • 38

Cont ext Swit ch

Cont ent s of TLB r ef lect mapping f r om vir t ual t o

physical - t hat applies t o only one pr ocess

On cont ext swit ch must f lush t he cur r ent TLB

ent ries of t hings f rom last process

Could r est or e ent r ies f or new pr ocess (pr eload) or

j ust set t hem all t o invalid and gener at e f ault s f or f ir st f ew accesses

This is a big r eason cont ext swit ches ar e

expensive!!

Recall: kernel level t hread swit ch more expensive t he

user level swit ch ..now you know even more why!

  • 39

Segment at ion

Similar t echnique t o paging except par t it ion

addr ess space int o var iable sized segment s r at her t han int o f ixed sized pages

Recall FS blocks vs ext ent s? Variable size = more ext ernal f ragment at ion

Segment s usually cor r espond t o logical unit s

Code segment , Heap segment , St ack segment et c

Vir t ual Addr ess = <

segment # , of f set >

HW Suppor t ? Of t en mult iple base/ limit r egist er

pair s, one per segment

St ored in segment t able Segment # used as index int o t he t able

  • 40

Segment Lookups

  • 41

P aging Segment s?

Use segment s t o manage logical unit s and

t hen divide each segment int o f ixed sized pages

No ext er nal f r agment at ion Segment s ar e pageable so don’t need whole

segment in memory at a t ime x86 archit ect ure does t his

  • 42

Linux on x86

1 kernel code segment and 1 kernel dat a

segment

1 user code segment and 1 user dat a

segment

Belongs t o pr ocess cur r ent ly r unning

N t ask segment s (st ores regist ers on

cont ext swit ch)

All segment s paged wit h t hree level page

t ables

slide-8
SLIDE 8

8

  • 43

Shared Memory

Exploit level of indir ect ion bet ween vir t ual

addr ess and physical addr ess t o allow pr ocesses t o communicat e t hr ough memor y Shar ed memor y

Map t he same set of physical page f r ames int o

dif f er ent pr ocesses vir t ual addr ess space (maybe at dif f er ent vir t ual addr esses)

Each process has it s own P

TEs so can give dif f erent processes dif f erent t ypes of access (read/ writ e/ execut e)

Execut e access t o same regions good f or shared

libraries!

  • 44

Duplicat es of large it ems?

Suppose t wo processes each want a privat e

writ eable copy of t he same dat a

I f is is small give t hem t here own physical

pages

They want privat e writ eable copies so can’t

j ust use normal shared memory

I f it is big painf ul t o duplicat e especially if

t hey are each only going t o change a lit t le bit

  • 45

Copy-on-Writ e

I nst ead of copying, make a shar ed memor y r egion

but mar k ever yone’s per missions as r ead only (even t hough t hey r eally have per mission t o wr it e)

Then if t hey t r y t o wr it e, HW will gener at e an

access violat ion f ault

OS invoked on f ault s and usually end processes I n t his case, OS will make a copy of j ust t he page writ t en

and t hen set t he P TE t o point t o t he new privat e copy (wit h writ e access t his t ime!) and rest art

Much like servicing a page f ault where have t o bring dat a

in f rom disk Copy-on-wr it e of t en used on f or k t o shar e a copy

  • f t he par ent ’s addr ess space even t hough logically

par ent and child each get t heir own pr ivat e writ eable copy (esp good because of t en quickly

  • ver wr it t en)
  • 46

Memory Mapped Files

Can access f iles t hr ough t he vir t ual memor y

syst em as well as t hr ough t ypical open/ r ead/ wr it e FS int er f ace

Map a f ile int o a r egion of your addr ess space

File st art = address X Then read f ile of f set Y = look at dat a a memory locat ion

X+ Y

Writ e f ile of f set Y = set memory locat ion X+Y equal t o

new value Doesn’t r ead ent ir e f ile when mapped

I nit ially pages mapped t o f ile are invalid When access t he memory mapped region, t ranslat ed int o

FS read/ writ e operat ions by t he OS