Virtual Memory
Ch. 9
SISTIM OPERASI
(Operating System)
IKI- 20230
Johny Moningka
([email protected])
Fakultas Ilmu Komputer Universitas Indonesia Semester 2000/2001
Virtual Memory
n
Background
n
Demand Paging
n
Page- Replacement Algorithms
n
Allocation of Frames
n
Thrashing
OS/ JM-2001/v1.1/3
Review: Demand Paging
n
Asumsi pemakaian memori:
n
Load entire process into memory. Run it. Exit.
n
Problems?
n
Slow (apalagi proses besar => transfer ke memori)
nWasteful of space (process doesn’t use all of its
memory)
n
Solusi: sebagian saja program berada di memori
n
demand paging: ambil pages yang digunakan/diakses
npaging: only keep frequently used pages in memory
n
Mekanisme:
n
Gunakan virtual memory untuk melakukan: map
address, manage replacement, manage page di disk.
Review: VM = OS big lie
n Objektif: disk-sized memory yang secepat akses physical memory (DRAM).
n Program execution: 10% program mempunyai 90% porsi diakses/digunakan (memori reference).
n Jadi, usahakan 10% program berada di memori dan sisanya di disk => kecepatan akses mendekati memori (90% reference).
Physical memory
Disk
# of references
OS/ JM-2001/v1.1/5
n
Ekstensi page table entry: extra 1 bit valid/invalid
nApakah page di memori? 1 = ada, 0 = tidak ada.
nJika bit == 1, translasi dan akses langsung ke memori.
nJika bit == 0, menyebabkan page fault.
• What happens on page fault?
• OS finds a free page or replace one (which one??) • Issues a disk request to read in data into that page • Puts process on blocked queue, switches to other process • when disk completes: set present = 1, put back on ready
queue
Review: VM Mechanism
Disk Mem 32 :P=1 4183:P=0 177 :P=1 5721:P=0 OS/ JM-2001/v1.1/6Review: Page replacement
nRandom: sembarang page (any page).
nPro: good for avoiding worst case
nCon: good for avoiding best case (hasil … lumayan, cukup). n
FIFO: ganti page yang terlama di memori
nAdil: semua pages akan diganti (menjadi “tua”).
nProblem: bukan objektif, tidak melihat pemakaian pages. n
OPT (optimal): ganti page yang tidak akan digunakan
untuk waktu terlama.
nSulit diramalkan (dapat dijadikan benchmark => simulasi). n
LRU (least recently used): ganti page yang tidak
digunakan dalam waktu yang terlama.
nPast = future? LRU = OPT.OS/ JM-2001/v1.1/7
LRU: perfect
n Saat mengakses page frame: Gunakan time stamp informasi kapan terakhir page diakses.
n Saat penggantian: cari/compare yang terlama.
n Problems: Page frame besar n Memori: 256 MB (page: 4 KB)
=> 64 K entry * 1 byte stamp.
n Tidak ada dukungan h/w (lambat).
n Approximation
n Gunakan cara sederhana tapi cepat mencari page yang tidak di reference.
n Tidak sempurna, tapi mewakili LRU. 0xffdcd: add r1,r2,r3 0xffdd0: ld r1, 0(sp)
t=4
t=14
t=14
t=5
13
14
OS/ JM-2001/v1.1/8LRU: Clock algorithm
nSetiap page frame => reference bit (1 bit)
nHardware set pada saat diakses => OS secara periodik reset.
nPages dengan bit yang di -set => recently used. n
Implementasi: simpan page dalam FIFO circular list
nOS scan: page’s ref bit = 1, set to 0& skip, otherwise replace.
n Good? Bad?:
n Waktu scan lambat/cepat?
n Interval pendek => informasi hilang,
n Interval lama => semua page di akses
R=1 R=0 R=1 R=1 R=1 R=0 R=0 R=1 R=0 R=0 R=1
OS/ JM-2001/v1.1/9
LRU: Implementation
nProblem: sweep interval?
nGunakan: n bits per page
nJika page digunakan: set use bit (1 bit) = 1.
nOS sweep: gunakan sisa bit sebagai counter (used count, shift register)
nMisalkan: n = 1 (Second chance): reset bit reference = 0; Page akan diganti jika pada saat sweep yang kedua bitnya tetap 0.
nMisalkan: n = 2, gunakan 1 bit untuk reference, dan 1 bit untuk counter
• Sweep:
jika bit reference 1, maka set bit counter = 1, clear bit reference. jika bit reference 0, maka reset bit counter = 0, clear bit reference. • Page yang akan diganti adalah page: 00.
LRU: Unix SunOS
Script started on Mon Apr 16 13:03:45 2001 caplin% vmstat -s
9049361 pages paged in 7061493 pages paged out
4421662 pages examined by the clock daemon 377 revolutions of the clock hand 9298235 pages freed by the clock daemon …
57724440 cpu context switches 143532739 system calls
OS/ JM-2001/v1.1/11
Allocation of Frames
n
Setiap proses memerlukan pages jumlah
minimum.
n
Contoh:
n
Instruksi yang panjang dan melibatkan lebih dari satu
memori reference, contoh MOVE dapat mencakup 2
pages (instruksi) + 2 pages (data)
n
Two major allocation schemes.
n
fixed allocation
npriority allocation
Fixed Allocation
n
Alokasi jumlah page sama:
n
Jika jumlah memori fisik (page frame): 100 pages, dan
jumlah proses 5, maka setiap proses => 20 pages.
nAlokasi proporsionil:
n
Sesuai ukuran besarny proses.
m S s p a m s S p s i i i i i i × = = = ∑ = = for allocation frames of number total process of size 59 64 137 127 5 64 137 10 127 10 64 2 1 2 ≈ × = ≈ × = = = = a a s s m i
OS/ JM-2001/v1.1/13
Priority Allocation
n
Gunakan alokasi yang proporsionil sesuai
dengan prioritas proses.
nJika proses Pi => page fault:
n
Pilih penggantian frame dari alokasi untuk proses
tersebut (frame yang digunakan oleh Pi).
• Setiap proses mempunyai jatah pages yang dapat digunakan (pool).
n
Pilih penggantian frame dari proses dengan “low
prioritiy number”, CPU intensive etc.
OS/ JM-2001/v1.1/14
Thrashing
n
Jika page frame yang dapat disediakan untuk seluruh
proses tidak mencukupi.
n
Dalam kasus tertentu proses tidak pernah dapat
mempunyai jumlah pages yang cukup (misalkan jika
jumlah proses terlalu banyak):
nTerjadi page-faults terus menerus.
nLow CPU utilization.
• operating system thinks that it needs to increase the degree of multiprogramming.
• another process added to the system.
n
Thrashing
≡
a process is busy swapping pages in and
out.
nThroughput/kemajuan proses tersebut sangat kecil/ tidak ada sama sekali.
OS/ JM-2001/v1.1/15
Why?
Real mem
P1
P2
P3
Setiap kali sebuah page akan di copy ke memori,
terdapat page lain yang harus diganti, dan page yang akan diganti tersebut akan di referensi lagi => page fault meningkat drastis.
Implikasi: Proses block => operasi I/O ke disk, I/O sibuk (full utilization), Tapi hanya melakukan swap in/out page dari memori ke disk.
What we wanted: virtual memory the size of disk with access time of of physical memory
What we have: memory with access time = disk access
OS/ JM-2001/v1.1/16
OS coping with thrashing
nSingle process thrashing?
nJika proses terlalu besar dan tidak terstruktur atau reuse memor y (malloc dan free) => OS tidak dapat membantu banyak, tapi melindungi proses lain.
n
System thrashing?
nJika karena page tidak mencukupi untuk seluruh proses:
nEvaluasi kebutuhan pages setiap proses.
nJadwal/prioritas proses yang run, hanya memori mencukupi.
nMenolak menerima proses baru. n
Note:
nOS tidak dapat menjamin “tidak ada problem”.
nSolusi (“rule of thumb”): upgrade memory (go to M -2).
OS/ JM-2001/v1.1/17
Thrashing Diagram
n
Why does paging work?
Locality model
nProcess migrates from one locality to another.
nLocalities may overlap. n
Why does thrashing occur?
Σ
size of locality > total memory size
Methodology for solving?
nPendekatan 1: page fault frequency (PFF)
nPFF = page faults / instructions executed
nJika PFF meningkat di atas “threshold”, proses memerlukan memory yang lebih banyak (give pages).
• Tidak mencukupi memori di sistim? Proses swap out ke disk.
nJika PFF menurun jauh di bawah “threshold”, memory pages dapat diambil dari proses, dan memperbanyak proses yang aktif. n
Pendekatan 2: working set
nJika kita dapat memperkirakan berapa kebutuhan pages supaya terdapat progress (needs a working set of pages).
nHanya menjalankan proses yang kebutuhannya dapat dipenuhi pada saat tersebut.
• OS dapat melihat “real demand” untuk keseluruhan jumlah proses dan page frame yang tersedia.
OS/ JM-2001/v1.1/19
Page-Fault Frequency Scheme
n
Establish “acceptable” page-fault rate.
nIf actual rate too low, process loses frame.
nIf actual rate too high, process gains frame.
Working Set
n
Pengamatan:
n
Menambah jumlah alokasi frame untuk satu proses
tidak berpengaruh banyak terhadap performance
akses memori dari proses tsb (misalkan sulit
meniadaka page fault).
n
Mengurangi jumlah alokasi frame untuk satu proses
=> pada titik tertentu berpengaruh besar: page fault
akan meningkat drastis => thrashing.
n
Evaluasi kebutuhan pages oleh satu proses
dalam kurun waktu T tertentu:
n
Real need: jumlah pages untuk proses tersebut.
nProblem? T is “magic”, how long?
OS/ JM-2001/v1.1/21
WS Model
n
WSS
i(working set of Process P
i) =
jumlah total pages yang direferensi dalam interval waktu
tertentu.
n
total number of pages referenced in the most recent
∆
(varies in time)
nif ∆too small will not encompass entire locality.
nif ∆too large will encompass several localities.
nif ∆= ∞ ⇒will encompass entire program.
n
D =
Σ
WSS
i≡
total demand frames untuk semua proses.
nif D > m⇒Thrashingn
Policy if D > m, then suspend (block) one of the
processes.
OS/ JM-2001/v1.1/22
How to implement working set?
nKita memerlukan informasi “idle time” untuk
setiap page frame:
n
Idle time => berhubungan dengan jumlah waktu CPU
proses tsb, sejak page diakses.
n
Jika page’s idle time > T? page bukan bagian dari
working set.
n
Scan semua page di memori oleh proses tsb.
• Use bit di set (1)? Reset page’s idle time.
• Use bit masih reset (0)? Tambahkan CPU time proses tersebut ke idle time.
n
Unix:
• Scan dilakukan dalam orde detik. • T dalam orde menit.
OS/ JM-2001/v1.1/23
Working set less important
n
The concept is a good perspective on system
behavior.
n
As optimization trick, it’s less important: Early systems
thrashed lots, current systems not so much.
n
Have OS designers gotten smarter? No. It’s the
hardware guys (cf. Moore’s law):
n
Obvious: Memory much larger (more to go around)
nLess obvious: CPU faster so jobs exit quicker, return
memory to freelist faster.
n
Some app can eat as much as you give, the
percentage of them that have “enough” seems to be
increasing.
OS/ JM-2001/v1.1/24
Other Considerations
n
Prepaging
n
Pada saat proses start (suspend, baru), jumlah pages
yang diberikan ke proses tersebut memenuhi
perkiraan working set untuk proses tersebut
• Sekaligus ditransfer ke memori (sehingga page faults akan berkurang pada saat awal).
• Masalah: kemungkinan page tersbut tidak penah diakses.
n
Page size selection
n
Ukuran dari 512 bytes – 16 KB.
nPertimbangan:
• besarnya page table (setiap proses) => large • Internal fragmentation => small
• Transfer time dari disk ke memori => large • Page faults => large