Вы находитесь на странице: 1из 23

Structures of the buffer pool

Buffer pool according to LRU


The kernel maintains a free list of
buffer
doubly linked list
take a buffer from the head of the free
list.
When returning a buffer, attaches the
Forward ptrs
buffer to the
tail. buf 2
free list
buf 1
buf n
head
Back ptrs
Figure 3.2 Free list of Buffers
1

Structure of The Buffer Pool

The kernel caches data in the buffer pool according to a least


recently used algorithm : after it allocates a buffer to a disk
block , it cannot use the buffer for another block until all other
buffers have been used more recently.
The kernel maintains a free list of buffers that preserves the
least recently used order. The free list is a doubly linked circular
list of buffers with a dummy buffer header that marks its
beginning and end.
Every buffer is put on the free list when the system is booted.
The kernel takes a buffer from the free list when it wants any
free buffer , but it can take a buffer from the middle of the free
list if it identifies a particular block in the buffer from the
middle of the free list if it identifies a particular block in the
buffer pool.

N.I.T. Raipur,Computer Science

Hash Queue

When the kernel accesses a disk


block
separate queue (doublely linked circular
list)
hashed as a function of the device and
block
num
Hash queue headers
Every disk block exists
on
28
4 one and only
blkno0 mod 4
64
on hash queue and
only
once
on the
17
5
97
blkno1 mod 4
queue
blkno2
mod 4
blkno3
mod 4

98

50

10

35

99

Figure 3.3 Buffers on the Hash Queues

When the kernel accesses a disk block , it


searches for a buffer with the appropriate
device-block number combination. Rather than
search the entire buffer pool , it organizes the
buffer into separate queues hashed as a
function of the device and block number. The
kernel links the buffers on a hash queue into a
circular , doubly linked list , similar to the
structure of the free list.

Scenarios for Retrieval of a Buffer

To allocate a buffer for a disk block


Use getblk()
Has Five scenarios
algorithm getblk
Input: file system number
block number
Output: locked buffer
that can now be used for block
{
while(buffer not found)
{
if(block in hash queue){ /*scenario 5*/
if(buffer busy){
sleep(event buffer becomes free)
continue;
}
mark buffer busy; /*scenario 1*/
remove buffer from free list;
return buffer;
}
2001.3.9

else{
if ( there are no buffers on free list)
{
/*scenario 4*/
sleep(event any buffer becomes free)
continue;
}
remove buffer from free list;
if(buffer marked for delayed write)
{
/*scenario 3*/
asynchronous write buffer to disk;
continue;
}
/*scenario 2*/
remove buffer from old hash queue;
put buffer onto new hash queue;
return buffer;
}
}}

There are five scenarios , the kernel may follow in getblk to allocate
a buffer for a disk block

The kernel finds the block on its hash queue and its buffer is
free.
The kernel cannot find the block on the hash queue , so it
allocates a buffer from the free list.
The kernel cannot find the block on the hash queue and in
attempting to allocate a buffer from the free list , finds a buffer
on the free list that has marked delayed write. The kernel
must write the delayed write buffer to disk and allocate
another buffer.
The kernel cannot find the block on the hash queue and the free
list of buffers is empty.
The kernel finds the block on the hash queue , but its buffer is
currently busy.
6
N.I.T. Raipur,Computer Science

Scenario 1 : The kernel finds the block on its hash queue and its buffer is free.

Block is in hash queue, not busy


Choose that buffer
Blkno 0 mod 4

28

64

Blkno 1 mod 4

17

97

Blkno 2 mod 4

98

50

10

Blkno 3 mod 4

35

99

Freelist header

2001.3.9

(a) Search for block 4 on first hash queue

1 Scenario
st

After allocating
Blkno 0 mod 4

28

64

Blkno 1 mod 4

17

97

Blkno 2 mod 4

98

50

10

Blkno 3 mod 4

35

99

Freelist header

2001.3.9

(b) Remove block 4 from free list

Algorithm for Releasing a Buffer

algorithm brelse
input: locked buffer
output: none
{
wakeup all pros: event , waiting for any buffer to become free;
wakeup all pros: event , waiting for this buffer to become free;
raise processor execution level to block interrupts;
if (buffer contents valid and buffer not old)
enquence buffer at end of free list
else
enquence buffer at beginning of free list
lower processor execution level to allow interrupts;
unlock(buffer);
}
9
N.I.T. Raipur,Computer Science

Scenario 2 : The kernel cannot find the block on the hash queue , so it allocates a buffer from the free list.

Not in the hash queue and exist free


buff.
Choose one buffer in front of free list
Blkno 0 mod 4
28
4
64
Blkno 1 mod 4

17

97

Blkno 2 mod 4

98

50

10

Blkno 3 mod 4

35

99

Freelist header

2001.3.9

(a) Search for block 18 Not in the cache

nd

Scenario

After allocating
Blkno 0 mod 4

28

64

Blkno 1 mod 4

17

97

Blkno 2 mod 4

98

50

10

35

99

Blkno 3 mod 4

Freelist header
(b) Remove first block from free list, assign to 18
2001.3.9

18

Scenario 3 :The kernel cannot


find the block on the hash queue
and in attempting allocate a
buffer from the free list
(Scenario-2) , finds a buffer on
the free list that has been
marked delayed write . The
kernel must write the delayed
write buffer to disk and allocate
another buffer.

3rd Scenario
Not in the hash queue and there exists
delayed write buffer in the front of free list
Write delayed buffer . and choose next
Blkno 0 mod 4

28

64

Blkno 1 mod 4

17

97

Blkno 2 mod 4

98

50

10

Blkno 3 mod 4

35

99

delay

delay

Freelist header
(a) Search for block 18, delayed write blocks on free list

3 Scenario
rd

After allocating
Blkno 0 mod 4

28

Blkno 1 mod 4

17

Blkno 2 mod 4
Blkno 3 mod 4

64
5

97

98

50

10

35

99

writing

writing

Freelist header
(b) Writing block 3, 5, reassign 4 to 18

18

Scenario 4: The kernel cannot find the block on the hash queue, and the free list of buffers is empty.

Not in the hash queue and no free


buffer
Blkno
Wait
until
any buffer
free
0 mod
4
28
4 and re-do
64
Blkno 1 mod 4

17

97

Blkno 2 mod 4

98

50

10

Blkno 3 mod 4

35

99

Freelist header
(a) Search for block 18, empty free list

4th Scenario
Process A

Process B

Cannot find block b


on the hash queue
No buffer on free list
Sleep

Cannot find block b


on hash queue
No buffers on free list
Sleep

Somebody frees a buffer: brelse


Takes buffer from free list
Assign to block b
Figure 3.10 Race for free buffer

4 Scenario
th

What to remind
When process release a buffer wake all
process waiting any buffer cache

2001.3.9

SS 1

Scenario 5:The kernel finds the block on the hash queue , but its buffer is currently
busy.

Block is in hash queue, but busy


Wait until usable and re-do
Blkno 0 mod 4

28

64

Blkno 1 mod 4

17

97

Blkno 2 mod 4

98

50

10

Blkno 3 mod 4

35

99

Freelist header
(a) Search for block 99, block busy

busy

5th Scenario
Process A

Process B

Process C

Allocate buffer
to block b
Find block b
on hash queue
Buffer locked, sleep

Lock buffer
Initiate I/O
Sleep until I/O done
I/O done, wake up
brelse(): wake up others

Buffer does not contain


block b
start search again

time

Sleep waiting for


any free buffer
( scenario 4 )
Get buffer previously
assigned to block b
reassign buffer to buffer b

Reading and Writing disk


blocks
To read block ahead
The kernel checks if the block is in the cache or
not.
If the block in not in the cache, it invokes the
disk driver to read the block.
The the process goes to sleep awaiting the
event that the I/O is complete.
The disk controller interrupts the processor
when the I/O is complete
The disk interrupt handler awakens the sleeping
processes
The content of disk blocks are now in the buffer
When the process no longer need the buffer, it
releases it so that other processes can access it
20

Reading and Writing disk


blocks
To write a disk block
The kernel informs the disk driver that it
has a buffer whose contents should be
output.
The disk driver schedules the block for
I/O.
If the write is synchronous, the calling
process goes the sleep awaiting I/O
completion and releases the buffer when
awakens.
If the write is asynchronous, the kernel
starts the disk write. The kernel release 21

Advantages and Disadvantages of the


Cache
Buffer
The use of
buffers allows uniform disk access , because the kernel

does not need to know the reason for the I/O. Instead , it copies
data to and from buffers , regardless of whether the data is part
of a file , an inode or a super block.
The system places no data alignment restrictions on user
processes doing I/O , because the kernel aligns data internally.
Hardware implementation frequently require a particular
alignment of data for disk I/O , such as aligning the data on a
two-byte boundary or on a four-byte boundary in memory.
The kernel eliminates the need for special alignment of user
buffers , making user programs simpler and more portable.
Use of the buffer cache can reduce the amount of disk traffic ,
thereby increasing overall system throughput and decreasing
response time.
Process reading from the file system may find data blocks in the
cache and the need for disk I/O.

22

N.I.T. Raipur,Computer Science

22

The kernel frequently uses delayed write to avoid unnecessary disk


writes , leaving the block in the buffer cache and hoping for a cache
hit on the block.
If too much memory is used for buffers , the system may slow down
because of excessive process swapping or paging.
The buffer algorithm help insure file system integrity , because they
maintain a common , single image of disk blocks contained in the
cache.
If two processes simultaneously attempt to manipulate one disk block
, the buffer algorithm s serialize their access , preventing data
corruption.
Reduction of disk traffic is important for good throughput and
response time , but the cache strategy also introduces several
disadvantages.
Use of the buffer cache requires an extra data copy when reading and
writing to and from user processes. A process writing data copies the
data into the kernel and the kernel copies the data to disk ; a process
reading data has the data read from disk into the kernel and from
the kernel to the user process.
23
N.I.T. Raipur,Computer Science

23

Вам также может понравиться