iki10230 pengantar organisasi komputer bab 5.2: cache

29
1 IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache 23 April 2003 Bobby Nazief ([email protected]) Qonita Shahab ([email protected]) bahan kuliah: http://www.cs.ui.ac.id/kuliah/iki10230/ Sumber : 1. Hamacher. Computer Organization, ed-5. 2. Materi kuliah CS152/1997, UCB.

Upload: slade-dale

Post on 14-Mar-2016

51 views

Category:

Documents


0 download

DESCRIPTION

IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache. Sumber : 1. Hamacher. Computer Organization , ed-5. 2. Materi kuliah CS152/1997, UCB. 23 April 2003 Bobby Nazief ([email protected]) Qonita Shahab ([email protected]) bahan kuliah: http://www.cs.ui.ac.id/kuliah/iki10230/. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

1

IKI10230Pengantar Organisasi Komputer

Bab 5.2: Cache

23 April 2003Bobby Nazief ([email protected])Qonita Shahab ([email protected])

bahan kuliah: http://www.cs.ui.ac.id/kuliah/iki10230/

Sumber:1. Hamacher. Computer Organization, ed-5.2. Materi kuliah CS152/1997, UCB.

Page 2: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

2

MEMORY HIERARCHY

Page 3: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

3

Memory Hierarchy (1/4)

° Prosesor• menjalankan program• sangat cepat waktu eksekusi dalam orde

nanoseconds sampai dengan picoseconds• perlu mengakses kode dan data program!

Dimana program berada?° Disk

• HUGE capacity (virtually limitless)• VERY slow: runs on order of milliseconds• so how do we account for this gap?

Menggunakan teknologi memori!

Page 4: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

4

Memory Hierarchy (2/4)

° Memory (DRAM)• Kapasitas jauh lebih besar dari registers, lebih kecil

dari disk (tetap terbatas)• Access time ~50-100 nano-detik, jauh lebih cepat

dari disk (mili-detik)• Mengandung subset data pada disk (basically

portions of programs that are currently being run)

° Fakta: memori dengan kapasitas besar (murah!) lambat, sedangkan memori dengan kapasitas kecil (mahal) cepat.

° Solution: bagaimana menyediakan (ilusi) kapasitas besar dan akses cepat!

Page 5: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

5

Memory Hierarchy (3/4)Processor

Size of memory at each level

Increasing Distance

from Proc.,Decreasing

cost / MBLevel 1Level 2

Level n

Level 3. . .

Higher

Lower

Levels in memory

hierarchy

Page 6: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

6

Memory Hierarchy (4/4)

° Pada tingkat yang lebih dekat dengan Prosesor, mempunyai karakteristik:

• Lebih kecil,• Lebih cepat,• Subset semua data pada level lebih atas (mis.

menyimpan data yang sering digunakan)• Efisien dalam pemilihan mana data yang akan

disimpan, karena tempat terbatas° Lowest Level (usually disk) contains all

available data

Page 7: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

7

Memory Hierarchy Analogy: Library (1/2)

° You’re writing a term paper (Processor) at a table in Doe

° Doe Library is equivalent to disk• essentially limitless capacity• very slow to retrieve a book

° Table is memory• smaller capacity: means you must return book when

table fills up• easier and faster to find a book there once you’ve

already retrieved it

Page 8: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

8

Memory Hierarchy Analogy: Library (2/2)

° Open books on table are cache• smaller capacity: can have very few open books

fit on table; again, when table fills up, you must close a book

• much, much faster to retrieve data° Illusion created: whole library open on the

tabletop • Keep as many recently used books open on

table as possible since likely to use again• Also keep as many books on table as possible,

since faster than going to library

Page 9: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

9

Why hierarchy works° The Principle of Locality:

• Program access a relatively small portion of the address space at any instant of time.

Address Space0 2^n - 1

Probabilityof reference

Page 10: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

10

Memory Hierarchy: How Does it Work?° Temporal Locality (Locality in Time):

Keep most recently accessed data items closer to the processor

° Spatial Locality (Locality in Space): Move blocks consists of contiguous words

to the upper levels Lower Level

MemoryUpper LevelMemory

To Processor

From ProcessorBlk X

Blk Y

Page 11: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

11

Modern Computer System° By taking advantage of the principle of locality:

• Present the user with as much memory as is available in the cheapest technology.

• Provide access at the speed offered by the fastest technology.

Control

Datapath

SecondaryStorage(Disk)

Processor

Registers

MainMemory(DRAM)

SecondLevelCache

(SRAM)

On-C

hipC

ache

1s 10,000,000s (10s ms)

Speed (ns): 10s 100s100s

GsSize (bytes):

Ks Ms

TertiaryStorage(Disk)

10,000,000,000s (10s sec)

Ts

Page 12: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

12

How is the hierarchy managed?° Registers <-> Memory

• by compiler (programmer?)° Cache <-> Memory

• by the hardware° Memory <-> Disks

• by the hardware and operating system (virtual memory)

• by the programmer (files)

Page 13: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

13

Basis of Memory Hierarchy° Disk contains everything.° When Processor needs something, bring it

into to all lower levels of memory.° Cache contains copies of data in memory

that are being used.° Memory contains copies of data on disk that

are being used.° Entire idea is based on Temporal Locality: if

we use it now, we’ll want to use it again soon

Page 14: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

14

Cache

Page 15: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

15

Organisasi Hierarki Memori: Cache

diskDiskdiskDisk

Memory-I/O bus

Processor

Cache

Memory

I/Ocontroller

I/Ocontroller

I/Ocontroller

Display Network

Reg

Page 16: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

16

What is a cache?

° Kecil, storage cepat untuk meningkatkan “average access time” dibandingkan memori

° Memanfaatkan spatial & temporal locality° Sebenarnya “cache” terlihat pada hirarkis

penyimpanan• Registers “a cache” on variables – software

managed• First-level cache a cache on second-level cache• Second-level cache a cache on memory• Memory a cache on disk (virtual memory)

Page 17: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

17

Cache Design

° Bagaimana organisasi cache? (ingat: cache akan menerima alamat memori dari prosesor, tidak ada perubahan pada rancangan prosesor ada/tanpa cache).

° Bagaimana melakukan mapping (relasi) antara alamat memori dengan cache (ingat: prosesor hanya melihat memory byte addressable)

° Bagaimana mengetahui elemen data tsb berada di cache (hit) atau tidak ada (miss) (ingat: cache jauh lebih kecil dari main memory)?

Page 18: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

18

Cache: Blok Memory

• Transfer data antara cache dan memori dalam satuan blok (kelipatan words)

• Mapping (penerjemahan) antara blok di cache dan di main memory (ingat: cache copy dari main memory)

MainMemoryCacheTo Processor

From ProcessorBlk X

Blk Y

Hit

Page 19: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

19

Direct Mapping

° Lokasi cache 0 dapat diisidata lokasi dari:

• Lokasi memori:0, 4, 8, ... • Secara umum: lokasi memori

yang merupakan kelipatan 4 (jumlah blok cache)

MemoryMemory Address

0123456789ABCDEF

4 Byte Direct Mapped Cache

Cache Index

0123

Page 20: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

20

Associative Mapping

MemoryMemory Address

0123456789ABCDEF

4 Byte Associative Mapped Cache

Cache Index

0123

Page 21: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

21

Set-Associative Mapping

MemoryMemory Address

0123456789ABCDEF

4 Byte Set-Associative Mapped Cache

Cache Index

0123

Cache Set

0

1

Page 22: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

22

Address Mapping

Page 23: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

23

Example: Direct-Mapped

° Masalah: multiple alamat memori “map” ke indeks blok yang sama!

• Bagaimana kita mengetahui apakah alamat blok yang diinginkan berada di cache?

• Harus ada identifikasi blok memori dikaitkan dengan alamat

° Bagaimana jika ukuran blok > 1 byte?° Solusi: bagi alamat dalam bentuk fields

ttttttttttttttttt iiiiiiiiii oooo tag index byte

to check to offsetif have select withincorrect block block block

Page 24: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

24

Cache Address Terminology° Semua fields untuk penerjemahan dianggap

sebagai bilangan positif integer.° Index: indeks pada blok cache, dimana blok

(baris, entry) dari cache alamat tersebut harus berada.

° Offset: sekali kita telah menemukan blok tsb, manakah byte pada blok tersebut akan diakses.

° Tag: sisa bit dari alamat setelah field index dan offset; digunakan untuk membedakan alamat memory yang mappingnya pada blok yang sama dari cache.

Page 25: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

25

Direct-Mapped Cache Example (1/3)

° Misalkan kita mempunya 16KB data dengan skema direct-mapped cache.

• Setiap blok terdiri dari 4 word° Tentukan ukuran field: tag, index, dan offset

jika menggunakan komputer 32-bit.° Offset

• Diperlukan untuk mengambil satu byte dalam blok• blok mempunyai: 4 words

16 bytes24 bytes

• diperlukan 4 bit untuk menentukan alamat byte

Page 26: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

26

Direct-Mapped Cache Example (2/3)

° Index: (~index into an “array of blocks”)• diperlukan untuk menentukan blok yang mana

pada cache (rows yang mana)• cache mempunyai 16 KB = 214 bytes• blok mempunyai 24 bytes (4 words)

# rows/cache = # blocks/cache (sebab setiap rows terdiri dari satu blok)

= bytes/cache / bytes/row

= 214 bytes/cache / 24 bytes/row = 210 rows/cache

• diperlukan 10 bit untuk menentukan indeks pada rows dari cache

° Tag: 32 – (4 + 10) = 18 bit

Page 27: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

27

Direct-Mapped Cache Example (3/3)° Contoh direct mapped cache tsb membagi

field atas• 4 bits offset• 10 bit index• 18 bit tag

° Struktur cache:• Menyimpan tag (unik untuk setiap blok)

dikaitkan dengan blok/rows.1. Akses dilakukan dengan mencari index blok/

rows2. Bandingkan tag dari alamat dan tag yang

disimpan pada row tersebut3. Jika tag sama => HIT, ambil byte (offset)4. Jika tidak sama => MISS, ambil blok dari

memori.

Page 28: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

28

Akses Memori pada Direct-mapped Cache

Cache Index

0000000000000000000100000000100000000011

Cache Tag

ttttttttttttttttt iiiiiiiiii oooo

000000000000000000

Page 29: IKI10230 Pengantar Organisasi Komputer Bab 5.2: Cache

29

Replacement Algorithms° Berlaku bagi:

• Associative Mapping• Set-Associative Mapping

° Bagaimana menempatkan blok baru jika cache sudah penuh berpengaruh pada kinerja!

° Beberapa algoritme:• LRU: Least Recently Used• LFU: Least Frequently Used• Random