dcacheCbio.c
上传用户:nvosite88
上传日期:2007-01-17
资源大小:4983k
文件大小:89k
- /* dcacheCbio.c - Disk Cache Driver */
- /* Copyright 1999-2002 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01z,03mar02,jkf SPR#32277, adding dcacheDevEnable and Disable(), orig by chn
- 01y,21dec01,chn SPRs 30130, 22463, 21975 (partial). Disabled defaulting
- tuneable parameters after they are explicitly set by user.
- 01x,12dec01,jkf fixing diab build warnings.
- 01w,09dec01,jkf SPR#71637, fix for SPR#68387 caused ready changed bugs.
- 01v,09nov01,jkf SPR#71633, dont set errno when DevCreate is called w/BLK_DEV
- SPR#65431, fixing typo in man page
- 01u,29aug01,jkf SPR#69031, common code for both AE & 5.x.
- 01t,01aug01,jyo Fixed SPR#68387: readyChanged bit is not correctly checked to
- verify the change in the media, SPR#69411: Change in media's
- readyChanged bit is not being propogated appropriately to the
- layers above.
- 01s,13jun01,jyo SPR#67729: Updating blkSubDev, cbioSubDev and isDriver in
- dcacheDevCreate().
- 01r,19apr00,dat doc fixup
- 01q,09mar00,jkf removed taskUndelay & taskPrioritySet from CBIO_FLUSH
- now forcing ioctl flushes inline.
- 01p,29feb00,jkf T3 changes
- 01o,31aug99,jkf changes for new CBIO API.
- 01n,31jul99,jkf T2 merge, tidiness & spelling.
- 01i,17nov98,lrn zero-fill blocks allocated with CBIO_CACHE_NEWBLK
- 01h,29oct98,lrn pass along 3rd arg on CBIO_RESET, removed old mod history
- 01g,20oct98,lrn fixed SPR#22553, SPR#22731
- 01f,14sep98,lrn refined error handling, made updTask unconditional
- 01e,08sep98,lrn add hash for speed (SPR#21972), size change (SPR#21975)
- 01d,06sep98,lrn modify to work on top of CBIO (SPR#21974), and use
- wrapper for block devices b.c.
- 01c,30jul98,wlf partial doc cleanup
- 01b,01jul98,lrn written.
- 01a,28jan98,lrn written, preliminary
- */
- /*
- DESCRIPTION
- This module implements a disk cache mechanism via the CBIO API.
- This is intended for use by the VxWorks DOS file system, to store
- frequently used disk blocks in memory. The disk cache is unaware
- of the particular file system format on the disk, and handles the
- disk as a collection of blocks of a fixed size, typically the sector
- size of 512 bytes.
- The disk cache may be used with SCSI, IDE, ATA, Floppy or any other
- type of disk controllers. The underlying device driver may be either
- comply with the CBIO API or with the older block device API.
- This library interfaces to device drivers implementing the block
- device API via the basic CBIO BLK_DEV wrapper provided by cbioLib.
- Because the disk cache complies with the CBIO programming interface on
- both its upper and lower layers, it is both an optional and a stackable
- module. It can be used or omitted depending on resources available and
- performance required.
- The disk cache module implements the CBIO API, which is used by the file
- system module to access the disk blocks, or to access bytes within a
- particular disk block. This allows the file system to use the disk cache
- to store file data as well as Directory and File Allocation Table blocks,
- on a Most Recently Used basis, thus keeping a controllable subset of these
- disk structures in memory. This results in minimized memory requirements
- for the file system, while avoiding any significant performance degradation.
- The size of the disk cache, and thus the memory consumption of the disk
- subsystem, is configured at the time of initialization (see
- dcacheDevCreate()), allowing the user to trade-off memory consumption
- versus performance. Additional performance tuning capabilities are
- available through dcacheDevTune().
- Briefly, here are the main techniques deployed by the disk cache:
- .IP
- Least Recently Used block re-use policy
- .IP
- Read-ahead
- .IP
- Write-behind with sorting and grouping
- .IP
- Hidden writes
- .IP
- Disk cache bypass for large requests
- .IP
- Background disk updating (flushing changes to disk) with an adjustable
- update period (ioctl flushes occur without delay.)
- .LP
- Some of these techniques are discussed in more detail below; others
- are described in varrious professional and academic publications.
- DISK CACHE ALGORITHM
- The disk cache is composed internally of a number cache blocks, of
- the same size as the disk physical block (sector). These cache blocks
- are maintained in a list in "Most Recently Used" order, that is, blocks
- which are used are moved to the top of this list. When a block needs to
- be relinquished, and made available to contain a new disk block, the
- Least Recently Used block will be used for this purpose.
- In addition to the regular cache blocks, some of the memory allocated
- for cache is set aside for a "big buffer", which may range from 1/4 of
- the overall cache size up to 64KB. This buffer is used for:
- .IP
- Combining cache blocks with adjacent disk block numbers, in order to
- write them to disk in groups, and save on latency and overhead
- .IP
- Reading ahead a group of blocks, and then converting them to normal
- cache blocks.
- .LP
- Because there is significant overhead involved in accessing the disk
- drive, read-ahead improves performance significantly by reading groups
- of blocks at once.
- TUNABLE PARAMETERS
- There are certain operational parameters that control the disk cache
- operation which are tunable. A number of
- .I preset
- parameter sets is provided, dependent on the size of the cache. These
- should suffice for most purposes, but under certain types of workload,
- it may be desirable to tune these parameters to better suite the
- particular workload patterns.
- See dcacheDevTune() for description of the tunable parameters. It is
- recommended to call dcacheShow() after calling dcacheTune() in order
- to verify that the parameters where set as requested, and to inspect
- the cache statistics which may change dramatically. Note that the hit
- ratio is a principal indicator of cache efficiency, and should be inspected
- during such tuning.
- BACKGROUND UPDATING
- A dedicated task will be created to take care of updating the disk with
- blocks that have been modified in cache. The time period between updates
- is controlled with the tunable parameter syncInterval. Its priority
- should be set above the priority of any CPU-bound tasks so as to assure
- it can wake up frequently enough to keep the disk synchronized with the
- cache. There is only one such task for all cache devices configured.
- The task name is tDcacheUpd
- The updating task also has the responsibility to invalidate disk cache
- blocks for removable devices which have not been used for 2 seconds or more.
- There are a few global variables which control the parameters of this
- task, namely:
- .IP <dcacheUpdTaskPriority>
- controls the default priority of the update task, and is set by default to 250.
- .IP <dcacheUpdTaskStack>
- is used to set the update task stack size.
- .IP <dcacheUpdTaskOptions>
- controls the task options for the update task.
- .LP
- All the above global parameters must be set prior to calling
- dcacheDevCreate() for the first time, with the exception of
- dcacheUpdTaskPriority, which may be modified in run-time, and takes
- effect almost immediately. It should be noted that this priority is not
- entirely fixed, at times when critical disk operations are performed,
- and FIOFLUSH ioctl is called, the caller task will temporarily
- .I loan
- its priority to the update task, to insure the completion of the flushing
- operation.
- REMOVABLE DEVICES
- For removable devices, disk cache provides these additional features:
- .IP "disk updating"
- is performed such that modified blocks will be written to disk within
- one second, so as to minimize the risk of losing data in case of a
- failure or disk removal.
- .IP "error handling"
- includes a test for disk removal, so that if a disk is removed from the
- drive while an I/O operation is in progress, the disk removal event will
- be set immediately.
- .IP "disk signature"
- which is a checksum of the disk's boot block, is maintained by the cache
- control structure, and it will be verified against the disk if it was
- idle for 2 seconds or more. Hence if during that idle time a disk was
- replaced, the change will be detected on the next disk access, and the
- condition will be flagged to the file system.
- .IP NOTE
- It is very important that removable disks should all have a unique
- volume label, or volume serial number, which are stored in the disk's
- boot sector during formatting. Changing disks which have an identical
- boot sector may result in failure to detect the change, resulting in
- unpredictable behavior, possible file system corruption.
- .LP
- CACHE IMPLEMENTATION
- Most Recently Used (MRU) disk blocks are stored in a collection of memory
- buffers called the disk cache. The purpose of the disk cache is to reduce
- the number of disk accesses and to accelerate disk read and write operations,
- by means of the following techniques:
- .IP
- Most Recently Used blocks are stored in RAM, which results in the most
- frequently accessed data being retrieved from memory rather than from disk.
- .IP
- Reading data from disk is performed in large units, relying on the read-ahead
- feature, one of the disk cache