24
1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

Embed Size (px)

Citation preview

Page 1: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

1

Il Buffer Cache Unix

(Bach: the Design of the Unix Operating System (cap: 3)

Page 2: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

2

Il Buffer Cache Unix

• Obiettivo: minimizzare il numero degli accessi al disco

• É un’area della memoria primaria che contiene una copia dei blocchi del disco usati piu’di recente

• Il kernel alloca una porzione della RAM per il buffer cache in fase di inizializzazione

Page 3: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

3

Funzionamento del Buffer Bache

• Quando si devono leggere dei dati da disco, il kernel li cerca prima nel Buffer Cache (B.C.)

• se non li trova li legge da disco e li copia nel B.C. per eventuali accessi successivi

• i blocchi scritti su disco sono tenuti anche nel B.C. a disposizione per letture successive

• il kernel minimizza le scritture su disco cercando di capire se i dati da scrivere sono definitivi o saranno presto sostituiti da altri

Page 4: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

4

Il Buffer Header

• Ogni buffer del B.C. contiene spazio sufficiente per contenere un blocco del disco e

• il buffer header, il quale contiene:• numero del blocco /file system• 2 puntatori alla free list, 2 puntatori per la coda di hash• flag per “buffer locked”• il buffer contiene dati validi?• il buffer deve essere scritto su disco prima di essere riassegnato ad un altro

blocco (delayed write)?

• N.B.: non ci possono essere due buffer che contengono lo stesso blocco.

Page 5: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

5

Organizzazione dei buffers:

• i buffer liberi stanno in una free list circolare

• il kernel preleva un buffer di cui ha bisogno dalla testa e inserisce i buffer appena liberati in coda: gestione LRU dei buffer liberi

testa della free list

BUF. 2BUF. 1 BUF. n

Page 6: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

6

Organizzazione dei buffer:

• i buffer contenenti un blocco sono anche organizzati in code di hash

• i buffer sono distribuiti secondo una funzione del numero di blocco e del file system

• il kernel cerca un buffer specifico della hash queue, e un buffer qualsiasi nella free list

• un buffer contenente un blocco é sempre in una hash queue e se é unlocked anche nella free list

Page 7: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

7

Buffers nelle hash queues

28 4 640 mod 4

17 5 971 mod 4

98 50 102 mod 4

3 35 993 mod 4

Page 8: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

8

Allocazione dei Buffer

• Tutte le richieste di lettura e scrittura di blocchi passano attraverso il buffer cache

• Se il blocco su cui si deve compiere una operazione si trova nel buffer cache si puó evitare l’accesso al disco

• Il kernel deve gestire 5 situazioni diverse per riuscire ad allocare un buffer per un blocco dei disco su cui sia stata richiesta una operazione

Page 9: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

9

Algoritmo “GETBLK”

• input: numero blocco / file system

• output: buffer contenente il blocco “locked”

• 5 casi:– blocco nella hash queue– allocazione di un buffer in free list– allocazione di un buffer in free list con scrittura su

disco dei buffer marcati come delayed write– free list vuota– blocco nella hash queue locked

Page 10: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

10

1: buffer in hash queue unlocked (a)

28 4 640 mod 4

17 5 971 mod 4

98 50 102 mod 4

3 35 993 mod 4

free list

header delle code di hash

ricerca del blocco 4 nella coda di hash corrispondente

Page 11: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

11

1: buffer in hash queue unlocked (a)

28 4 640 mod 4

17 5 971 mod 4

98 50 102 mod 4

3 35 993 mod 4

free list

lockedheader delle code di hash

rimozione del blocco 4 dalla free list

Page 12: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

12

Gestione di un buffer allocato

• Quando un buffer é stato allocato puó essere usato da un processo. I suoi dati possono essere letti o modificati. Il buffer é “locked”

• Quando il buffer non é piú in uso viene rilasciato e messo in coda alla free list

• Il kernel sveglia i processi in attesa perché “free list empty” o perché “buffer locked”

Page 13: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

13

2: il blocco non é nella hash queue

• Il kernel preleva il primo buffer nella free list

• marca il buffer “locked”

• inserisce il buffer nella hash queue corretta (eventualmente togliendolo da quella vecchia)

Page 14: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

14

2: Il blocco non é nella hash queue (a)

28 4 640 mod 4

17 5 971 mod 4

98 50 102 mod 4

3 35 993 mod 4

free list

header delle code di hash

Ricerca del blocco 18: non é nella hash queue

Page 15: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

15

2: Il blocco non é nella hash queue (b)

28 4 640 mod 4

17 5 971 mod 4

98 50 102 mod 4 18

35 993 mod 4

free list

header delle code di hash

rimuove il primo blocco dalla free list e lo assegna al blocco 18

locked

Page 16: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

16

3: il buffer selezionato nella free list é marcato delayed write

• il kernel incomincia la scrittura asincrona su disco del buffer

• prosegue la ricerca di un buffer nella free list

• a scrittura completata reinserisce il buffer appena scritto in testa alla free list (per evitare un giro a vuoto del buffer e rispettare la gestione LRU)

Page 17: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

17

3: Il buffer é delayed write (a)

28 4 640 mod 4

17 5 971 mod 4

98 50 102 mod 4

3 35 993 mod 4

free list

header delle code di hash

Ricerca del blocco 18 con blocchi a scrittura ritardata in free list

buffer a scrittura ritardata

Page 18: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

18

3: Il buffer é delayed write (a)

28

18

640 mod 4

17 5 971 mod 4

98 50 102 mod 4

3 35 993 mod 4

free list

header delle code di hash

Scrittura dei blocchi 3 e 5; riassegnazione del buffer selezionato al blocco 18

in fase di scrittura

Page 19: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

19

4: blocco non in hash queue e free list vuota

• il processo si sospende in attesa di un buffer libero

• quando il processo é risvegliato ricomincia a cercare nella hash queue

• se qualcuno ha allocato un buffer con quel blocco il processo puó sospendersi su quel buffer

• altrimenti ricomincia a cercare un blocco libero nella free list.

Page 20: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

20

4: blocco non in hash queue, e free list vuota

processo A: non trova il blocco 5 nella coda di hash e non ci sono buffer in free list. ATTENDE

processo B: Non trova il blocco 5 e ATTENDE

Un buffer viene liberato: B viene svegliato prima di A, preleva il buffer e lo assegna al blocco 5

A viene svegliato: il blocco 5 é presente ma locked. A si mette in attesa del ritorno del buffer del blocco 5 in free list

Page 21: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

21

5 piú processi concorrono per lo stesso buffer

• Nella stessa situazione del caso 4: il buffer contenente il blocco 5 viene rilasciato dal processo B e messo in free list.

• prima che il processo A riesca ad intervenire, un processo C preleva il buffer del blocco 5 e lo usa per caricare un altro blocco. Il blocco 5 non é piú in memoria!!!

• Il processo A deve ricominciare tutto da capo!

Page 22: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

22

I blocchi Delayed Write

• I blocchi modificati e “unlocked” sono marcati come delayed write

• La scrittura effettiva su disco avviene solo nel caso 3 dell’algoritmo GETBLK

• Tuttavia, si spera che il blocco delayed write sia modificato prima di essere selezionato da GETBLK, in modo da non dover effettuare la scrittura su disco

Page 23: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

23

Vantaggi del buffer cache

• il traffico su disco diminuisce

• la delayed write elimina le scritture inutili

• l’integritá dei file aumenta perché l’accesso ad un blocco da due processi é serializzato su un unica copia

Page 24: 1 Il Buffer Cache Unix (Bach: the Design of the Unix Operating System (cap: 3)

24

Svantaggi del buffer cache

• Il sistema richiede piú memoria primaria

• Il sistema é piú complesso e lavora di piú

• Il sistema é piú vulnerabile (si possono creare delle inconsistenze, ad esempio se manca corrente prima che vengano scritti effettivamente i buffer delayed write)