computer science 37 lecture 25

23
1 Lecture25 VirtualMemory

Upload: alexander-taylor

Post on 07-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 1/23

1

Lecture25

VirtualMemory

Page 2: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 2/23

2

Case1: Myprogramis

largerthanphysical

memory.Nowwhat?

Physicalmemory

WhygoVirtual ?Becausesometimesreal isnotgoodenough.

Page 3: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 3/23

Page 4: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 4/23

4

Case1: Myprogramis

largerthanphysical

memory.Nowwhat?

Physicalmemory

OVERLAY#1

Fact:

Theuserprogramwill

not needtheentirephysical

memoryallatonce.

Solution: overlays.

Page 5: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 5/23

5

Case1: Myprogramis

largerthanphysical

memory.Nowwhat?

Physicalmemory

OVERLAY#2

Fact:

Theuserprogramwill

not needtheentirephysical

memoryallatonce.

Problem: theburdenisallontheprogrammer(loading,unloading,

partitioning,etc).

Page 6: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 6/23

6

1) Whenamemoryitemis

referencedonce,it’slikelyto

bereferencedagaininthenear

future.

2)WhenIaccessamemory

location,itislikelythatIwill

accessalsootherlocationsin

thevicinityofthefirst.

TemporalLocality

SpatialLocality

ThePrincipleofLocality

Page 7: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 7/23

7

Case2: Severalprogramsare

runningconcurrently.

Physicalmemory

XWindow

Acommandshell

(csh,bash,tcsh,etc)

xemacs

xspim

XWindowAwindowmanager

(twm,fvwm,sawfish,etc)Windowmanager

shell

emacs

xspim

OS(Unixkernel)

Page 8: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 8/23

8

Case2: Severalprogramsare

runningconcurrently.

Physicalmemory

XWindow

Acommandshell

(csh,bash,tcsh,etc)

xemacs

xspim

XWindowAwindowmanager

(twm,fvwm,sawfish,etc)Windowmanager

shell

emacs

xspim

OS(Unixkernel)

Problem: IhavelessphysicalmemorythanIneed.

Page 9: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 9/23

9

Case2: Severalprogramsare

runningconcurrently.

Physicalmemory

XWindow

Acommandshell

(csh,bash,tcsh,etc)

xemacs

xspim

XWindowAwindowmanager

(twm,fvwm,sawfish,etc)Windowmanager

shell

xemacs

xspim

OS(Unixkernel)

Fact: Idon’tneedallofeverythingatthesametime.

Page 10: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 10/23

10

1) Whenamemoryitemis

referencedonce,it’slikelyto

bereferencedagaininthenear

future.

2)WhenIaccessamemory

location,itislikelythatIwill

accessalsootherlocationsin

thevicinityofthefirst.

TemporalLocality

SpatialLocality

ThePrincipleofLocality

Page 11: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 11/23

11

Themainideaistodesignaschemethat:

1) Supportsalargeraddressspacethanthephysical,

2) Achievesthisbyswappingdatainandoutofdisk,

3) Giveseachprogramtheillusionofowningallmemory,

4) Istransparenttotheprogrammer,

5) Ontopofitall,isefficient !

TheBasicsofVirtualMemory

Page 12: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 12/23

12

TheBasicsofVirtualMemory

$0.10-$0.2010-20msMag.Disk 

$5-$1060-120nsDRAM

$100-$2505-25nsSRAM

CostperMbyteAccessTimeTechnology

Rememberthecostsofdiskusageintermsof accesstimeandmonetaryunits:

Diskspaceischeap,but diskaccessisnot!

Sidecomment: Diskaccesscosthasthreecomponents:thetimetomovethediskheadtotherightcylinder,the

timetoreachthesectorinthecylinderandtransfertime.

Page 13: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 13/23

13

TheBasicsofVirtualMemory

Physicalmemory

Dividephysicalmemory

intoequallysizedportions

calledpages.

Pagesmayresideinphysicalmemorywhentheircontents

arebeingactivelyusedorin

diskwhentheircontentshave

notbeenusedforawhile.

pagei

DISK

Page 14: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 14/23

14

Physicalmemory

Aprogrammayusealargenumber N ofpages,evenif(N*page

size)isgreaterthanthephysicalmemory.

Thepagesthataprogramusesneednotbecontiguousnorinorderinphysical

memory.

xemacs 3

xemacs 0

xemacs 1

xemacs 2Question: Whatdoweneedtocreate

thisillusionfortheprogrammer?

Theprogrammerdoesn’tneedto

explicitlymovepagestoandfrom

disk.

Theprogrammerseestheaddressspace

asifitwereinamemorymuchlargerthan

thephysicalmemory.

Page 15: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 15/23

15

Physicalmemory

Weneedamappingmechanism

xemacs 3

xemacs 0

xemacs 1

xemacs 2

Virtualmemory

xemacs 0

xemacs 1

xemacs 2

xemacs 3Address

Translation:

from

virtualaddresstophysicaladdress

Page 16: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 16/23

16

Virtualmemory

xemacs 0

xemacs 1

xemacs 2

xemacs 3

Pointstonotice:

1) Virtualmemorydefinesavirtual

addressspace.

2) Virtualaddressspaceismadeof

pagesofthesamesizeasthephysical

addressspace.

3) Thepagesinvirtualaddress

spacearenicelycontiguousand

ordered.

Page 17: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 17/23

17

AddressTranslationforVirtualMemory

3 2 1 011 10 9 815 14 13 1231 30 29 28 27

Page offset Virtual page number

 Virtual address

3 2 1 011 10 9 815 14 13 1229 28 27

Page offsetPhysical page number

Physical address

Translation

Virtualaddress

Example: Pagesize=4KB=2^12Bytes

Physicalmemorysize=2^18pages=2^30Bytes=1GB

Virtualmemorysize=2^32Bytes=4GB

Question: Whataboutthismappingfromalargertoasmallerspace?

Page 18: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 18/23

18

 Address (showing bit positions)

20 10

Byteoffset

 Valid Tag DataIndex

0

1

2

1021

1022

1023

Tag

Index

Hit Data

20 32

31 30 13 12 11 2 1 0

Address(showingbitpositions)

Remembercachemapping?

Page 19: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 19/23

19

ImportantConsiderations

Cachemiss

PageFault:howdothepenaltiescompare?

Pagesshouldbelargetoamortizeaccesstime,butnot

toolarge.Why?)

Whataboutwriting?Shouldwethinkwritethrough or

writeback ?

Shouldweusea fullyassociative oradirectmapping

scheme?Why?

Shouldweleavepagefaulthandlingtosoftwareorshould

wemakeitahardwaretask?

Page 20: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 20/23

20

Page offset Virtual page number

 Virtual address

Page offsetPhysical page number

Physical address

Physical page number Valid

If 0 then page is notpresent in memory

Page table register

Page table

20 12

18

31 30 29 28 27 15 14 13 12 11 10 9 8 3 2 1 0

29 28 27 15 14 13 12 11 10 9 8 3 2 1 0

MappingMechanismforVirtualMemory

Pagetable

Weusefullyassociativemapping.

(eachprogramhasitsown)

Page 21: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 21/23

21

Page offset Virtual page number

 Virtual address

Page offsetPhysical page number

Physical address

Physical page number Valid

If 0 then page is notpresent in memory

Page table register

Page table

20 12

18

31 30 29 28 27 15 14 13 12 11 10 9 8 3 2 1 0

29 28 27 15 14 13 12 11 10 9 8 3 2 1 0

MappingMechanismforVirtualMemory

Pagetable

Question: Howmanyentriesinthepagetable?

(eachprogramhasitsown)

Page 22: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 22/23

22

Interlude:Program,ProcessandContext

EachprogramusestheCPUregistersasitwants.

Eachprogramhasitsownpagetable,itsownvaluefor

thepagetableregister..

Ifthere’smorethanoneprogramexecutingunderthesame

CPU, theysharetheCPU .

Sharingmeansthatalltherepresentsoneprocessisswapped

inandoutoftheCPU,dependingonwhethertheprocessis

activeorinactive.

Theoperatingsystem handlesCPUsharingand,thus,

alsoprocesscontextmanagement.

Page 23: Computer Science 37 Lecture 25

8/4/2019 Computer Science 37 Lecture 25

http://slidepdf.com/reader/full/computer-science-37-lecture-25 23/23

TheVirtualMemorySystem

Physical memory

Disk storage

 Valid

1

1

1

1

0

1

1

0

1

1

0

1

Page table

 Virtual pagenumber

Physical page ordisk address

Virtualpagenumber

Pagefaultsareexceptionsbydefinition.