Buffer Cache

Embed Size (px)

Citation preview

  • 7/24/2019 Buffer Cache

    1/28

    1

    UNIX Kernel Architecture

    libraries

    User level

    Kernel level

    User programs

    hardware

    Kernel level

    Hardware level

    trap

    Sytem call interface

    File subsytem

    Buffer cache

    Chracter block

    Device drivers

    Hardware control

    Inter-processcommunication

    scheduler

    Memory

    management

    Process

    control

    sybsystem

  • 7/24/2019 Buffer Cache

    2/28

    System Calls System call are similar to ordinary functions

    Libraries map these function call to primitivesneeded to enter OS

    Assembly programs can directly invoke system

    call System calls can be categorized into :

    File subsystem related calls

    Process control system related calls

    2

  • 7/24/2019 Buffer Cache

    3/28

    File subsystem related calls

    Allocating file space

    Free space management

    File access control

    open, close read, write, stat, chown, chmode etc

    Process control system related calls

    Process synchronization

    Interprocess communication

    Memory management

    Process scheduling

    Fork, exec, wait, exit etc. 3

  • 7/24/2019 Buffer Cache

    4/28

    File system layout

    4

    Boot block Super block Inode list Data blocks

    Boot block can contain the boot strap code

    A file system may have an empty bootblock

    describe state of file system

    Size, number file it can store, location of freespace etc

  • 7/24/2019 Buffer Cache

    5/28

    Buffering

    File subsystem accesses file data usingbuffering mechanism

    The mechanism regulates the flow between

    buffer and secondary storage The mechanism interacts with block i/o

    device driver to initiate the data transfer

    block I/O devices are random accessstorage devices

    Device drivers are kernel module that

    controls the device operation 5

  • 7/24/2019 Buffer Cache

    6/28

    6

    Buffer Cache

    When a process wants to access data from a file,

    the kernel brings the data into main memory,alters it and then request to save in the file

    system

    Buffering increases the response time ,throughput,

    Buffering minimizes the frequency of disk

    access by keeping a pool of internal data buffercalled buffer cache.

  • 7/24/2019 Buffer Cache

    7/28

    7

    Buffer Cache

    Buffer cache contains the data in recently useddisk blocks

    When reading data from disk, the kernel

    attempts to read from buffer cache. If data is already in the buffer cache, the kernel

    does not need to read from disk

    If data is not in the buffer cache, the kernelreads the data from disk and cache it

  • 7/24/2019 Buffer Cache

    8/28

    8

    Buffer Headers

    A buffer consists of two partsa memory array

    buffer header

    disk block : buffer = 1 : 1

    Figure 3.1 Buffer Header

    device num

    block num

    status

    ptr to next buf on hash queue

    ptr to previous buf on hash queue

    ptr to next buf on free list

    ptr to previous buf on free list

    ptr to data area

  • 7/24/2019 Buffer Cache

    9/28

    9

    Buffer Headers

    device num logical file system number

    block num

    block number of the data on disk

    status

    The buffer is currently locked.

    The buffer contains valid data.

    delayed-write

    The kernel is currently reading or writing the contents of buffer to disk.

    A process is currently waiting for the buffer to become free.

    kernel identifies the buffer content by examing device num andblock num.

  • 7/24/2019 Buffer Cache

    10/28

    10

    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 buffer to the

    tail.

    free list

    head

    buf 1 buf 2 buf n

    Forward ptrs

    Back ptrs

    Figure 3.2 Free list of Buffers

  • 7/24/2019 Buffer Cache

    11/28

    11

    Structures of the buffer pool

    When the kernel accesses a disk block

    separate queue (doublely linked circular list)

    hashed as a function of the device and block num

    Every disk block exists on one and only on hash

    queue and only once on the queue

    4

    517

    105098

    99353

    28 64

    97

    blkno0 mod 4

    blkno1 mod 4

    blkno2 mod 4

    blkno3 mod 4

    Hash queue headers

    Figure 3.3 Buffers on the Hash Queues

  • 7/24/2019 Buffer Cache

    12/28

    12

    Scenarios for retrieval of a buffer

    Determine the logical device num and block num The algorithms for reading and writing disk blocks use

    the algorithmgetblk

    The kernel finds the block on its hash queue

    The buffer is free.

    The buffer is currently busy.

    The kernel cannot find the block on the hash queue

    The kernel allocates a buffer from the free list.

    In attempting to allocate a buffer from the free list, finds a buffer onthe free list that has been marked delayed write.

    The free list of buffers is empty.

  • 7/24/2019 Buffer Cache

    13/28

    13

    Retrieval of a Buffer:1stScenario (a)

    The kernel finds the block on the hash queue and its buffer isfree

    4

    517

    105098

    99353

    28 64

    97

    blkno0 mod 4

    blkno1 mod 4

    blkno2 mod 4

    blkno3 mod 4

    Hash queue headers

    freelist header

    Search for block 4

  • 7/24/2019 Buffer Cache

    14/28

    14

    Retrieval of a Buffer:1stScenario (b)

    4

    517

    105098

    99353

    28 64

    97

    blkno0 mod 4

    blkno1 mod 4

    blkno2 mod 4

    blkno3 mod 4

    freelist header

    Remove block 4 from free list

  • 7/24/2019 Buffer Cache

    15/28

    15

    Retrieval of a Buffer: 2ndScenario (a)

    The kernel cannot find the block on the hash queue, so it

    allocates a buffer from free list

    4

    517

    105098

    99353

    28 64

    97

    blkno0 mod 4

    blkno1 mod 4

    blkno2 mod 4

    blkno3 mod 4

    Hash queue headers

    freelist header

    Search for block 18: Not in cache

  • 7/24/2019 Buffer Cache

    16/28

    16

    Retrieval of a Buffer: 2ndScenario (b)

    Hash queue headers

    18

    4

    517

    105098

    9935

    28 64

    97

    blkno0 mod 4

    blkno1 mod 4

    blkno2 mod 4

    blkno3 mod 4

    freelist header

    Remove 1stblock from free list: Assign to 18

  • 7/24/2019 Buffer Cache

    17/28

    17

    Retrieval of a Buffer: 3rdScenario (a)

    The kernel cannot find the block on the hash queue, and finds delayed write

    buffers on hash queue

    4

    517

    105098

    99353

    28 64

    97

    blkno0 mod 4

    blkno1 mod 4

    blkno2 mod 4

    blkno3 mod 4

    Hash queue headers

    freelist header

    Search for block 18, Delayed write blocks on free list

    delay

    delay

  • 7/24/2019 Buffer Cache

    18/28

    18

    Retrieval of a Buffer: 3rdScenario (b)

    517

    105098

    99353

    28 64

    97

    blkno0 mod 4

    blkno1 mod 4

    blkno2 mod 4

    blkno3 mod 4

    Hash queue headers

    freelist header

    18

    writing

    writing

    (b) Writing Blocks 3, 5, Reassign 4 to 18

    Figure 3.8

  • 7/24/2019 Buffer Cache

    19/28

    19

    Retrieval of a Buffer: 4th Scenario

    The kernel cannot find the buffer on the hash queue, and the free list is empty

    28blkno0 mod 4

    blkno1 mod 4

    blkno2 mod 4

    blkno3 mod 4

    Hash queue headers

    freelist header

    4 64

    5 9717

    105098

    99353

    Search for block 18, free list empty

  • 7/24/2019 Buffer Cache

    20/28

    20

    Retrieval of a Buffer: 5th Scenario

    Kernel finds the buffer on hash queue, but it is currently busy

    4

    517

    105098

    99353

    28 64

    97

    blkno0 mod 4

    blkno1 mod 4

    blkno2 mod 4

    blkno3 mod 4

    Hash queue headers

    freelist header

    Search for block 99, block busy

    busy

  • 7/24/2019 Buffer Cache

    21/28

    21

    File System Algorithms

    Lower Level File system lgorithms

    namei

    alloc free ialloc ifree

    iget iput

    buffer allocation algorithms

    getblk brelse bread bwrite

  • 7/24/2019 Buffer Cache

    22/28

    22

    Inode

    contains the information necessary for a process to

    access a file

    exits in a static form on disk and the kernel reads

    them into an in-core inode

  • 7/24/2019 Buffer Cache

    23/28

    23

    Inodes

    consists of- file owner identifier

    - file type

    - file access permissions

    - file access times

    - number of links to the file

    - table of contents for the disk address of data in afile

    - file size

  • 7/24/2019 Buffer Cache

    24/28

    24

    Inodes in-core copy of the inode contains

    - status of the in-core inode indicating

    - logical device number of file system

    - inode number

    - pointers to other in-core inodes

    - reference count

  • 7/24/2019 Buffer Cache

    25/28

    inode contains the table of content to locate a file

    data

    Since each block on a disk is addressable bynumber , the table of content consists of set of

    disk block number

    If file were stored in contiguous manner, startblock number and file size would have been

    enough .

    This strategy would not allow expansion/ contraction

    25

  • 7/24/2019 Buffer Cache

    26/28

    File data may not be necessarily be stored in

    contiguous manner

    This would enable file to be of any size and can be

    easily expanded or contracted

    This would require basic unit of allocation unit to be a

    blockThe table of content would set of block number

    The size of table of content will vary with size of file

    26

  • 7/24/2019 Buffer Cache

    27/28

    27

    Structure of a regular file

    direct0

    direct1

    direct2

    direct3

    direct4

    direct5

    direct6

    direct7

    direct8

    direct9

    single indirect

    double indirect

    triple indirect

    Inode Data Blocks

  • 7/24/2019 Buffer Cache

    28/28

    28

    Structure of a regular file

    Assume that a logical block on the file system holds 1K bytes,

    A block number is addressable by a 32 bit integer, then a block can hold up to 256block numbers

    10 direct blocks with 1K bytes each=10K bytes

    1 indirect block with 256 direct blocks= 1K*256=256K bytes

    1 double indirect block with 256 indirect blocks=256K*256=64M bytes

    1 triple indirect block with 256 double indirect blocks=64M*256=16G bytes