# Patent

## Title
Locking and memory allocation in file system cache

## Abstract

A method may involve assigning each of a plurality of freelists and a plurality of hashlists used to implement a cache to one of a plurality of lock groups and acquiring one of a plurality of locks. Objects on each freelist and hashlist that are assigned to the same lock group are allocated from the same one of a plurality of memory allocation pools. Each lock group is associated with a respective one of the plurality of locks. Acquiring the lock locks a freelist and several hashlists included in an associated lock group of the plurality of lock groups.

## Background
BACKGROUND
1. Field of the Invention
This invention relates to file systems and, more particularly, to various caches used by file systems.
2. Description of the Related Art
File systems organize and manage information stored in a computer system. Typically, information is stored in the form of files. File systems may support the organization of user data by providing and tracking organizational structures such as folders or directories. The file system may interpret and access information physically stored in a variety of storage media, abstracting complexities associated with the tasks of locating, retrieving, and writing data to the storage media. In order to avoid having to access disk each time a file or its associated metadata is accessed, a file system may temporarily cache recently accessed file system information in a variety of caches within system memory.
SUMMARY
Various embodiments of systems and methods for implementing a cache are disclosed. In one embodiment, a method may involve assigning each of a plurality of freelists and a plurality of hashlists used to implement a cache to one of a plurality of lock groups and acquiring one of a plurality of locks. Objects on each freelist and hashlist that are assigned to the same lock group are allocated from the same one of a plurality of memory allocation pools. Each lock group is associated with a respective one of the plurality of locks. Acquiring the lock locks a freelist and several hashlists included in an associated lock group of the plurality of lock groups.
In some embodiments, such a method may also involve searching one of the hashlists in the lock group associated with the lock for an object and, if the object is found in the hashlist, acquiring a lock on the object by writing to a semaphore associated with the object, where write access to the semaphore is protected by the lock. The value of the semaphore may indicate whether the object is locked in a shared mode or an exclusive mode.
A method may also involve a cache memory manager requesting a plurality of slabs of memory from an operating system for use as part of the cache, where each of the plurality of slabs is a uniformly sized chunk of memory. The cache memory manager may subdivide each of the plurality of slabs of memory into one or more buffers and track which objects are included in each slab. The cache memory manager releasing memory back to the operating system in slabs such that if any object included in one of the plurality of slabs is released back to the operating system, all of the objects included in that one of the plurality of slabs are released back to the operating system. Each of the plurality of slabs may be assigned to one of the plurality of memory allocation pools so that each memory allocation pool includes a unique subset of the plurality of slabs.



BRIEF DESCRIPTION OF THE DRAWINGS
A better understanding of the present invention can be obtained when the following detailed description is considered in conjunction with the following drawings, in which:
FIG. 1 illustrates a networked computer system, according to one embodiment.
FIG. 2 illustrates a computer system that includes a file system buffer cache, according to one embodiment.
FIG. 3 illustrates a file system buffer cache, according to one embodiment.
FIG. 4 shows one embodiment of a method of accessing a file system buffer cache.
FIG. 5 illustrates a file system buffer cache, according to another embodiment.
FIG. 6 shows one embodiment of a method of allocating buffers in a file system buffer cache.



While the invention is described herein by way of example for several embodiments and illustrative drawings, those skilled in the art will recognize that the invention is not limited to the embodiments or drawings described. It should be understood that the drawings and detailed description thereto are not intended to limit the invention to the particular form disclosed, but on the contrary, the intention is to cover all modifications, equivalents and alternatives falling within the spirit and scope of the present invention as defined by the appended claims. The headings used herein are for organizational purposes only and are not meant to be used to limit the scope of the description or the claims. As used throughout this application, the word “may” is used in a permissive sense (e.g., meaning having the potential to), rather than the mandatory sense (i.e., meaning must). Similarly, the words “include”, “including”, and “includes” mean including, but not limited to.
DETAILED DESCRIPTION OF EMBODIMENTS
FIG. 1 illustrates an exemplary computer system that may implement a file system buffer cache as described herein, according to one embodiment. In the illustrated embodiment, a storage area network (SAN) environment is formed by one or more hosts 10 (e.g., hosts 10A, 10B and 10i) (which may also be referred to as servers) that are interconnected with one or more associated storage devices 50 (e.g., storage devices 50A, 50B . . . 50n) through an interconnect fabric 106. One or more client systems 108A–108D may access the SAN by accessing one or more of the hosts 10 via a network 110. Network 110 may include wired or wireless communication mechanisms such as, for example, Ethernet, LAN (Local Area Network), WAN (Wide Area Network), or modem, among others. As used herein, the term “host” refers to any computing device that includes a memory and a processor configured to execute instructions stored in the memory.
Each of the storage devices 50 may include any of one or more types of storage devices including, but not limited to, storage systems such as RAID (Redundant Array of Independent Disks) systems, disk arrays, JBODs (Just a Bunch Of Disks, used to refer to disks that are not configured according to RAID), tape devices, and optical storage devices. These devices may be products of any of a number of vendors including, but not limited to, Compaq, EMC, and Hitachi. Hosts 10 may run any of a variety of operating systems such as a UNIX operating system, Linux operating system, or a Windows operating system. Each host 10 may be connected to the fabric 106 via one or more Host Bus Adapters (HBAs).
Fabric 106 includes hardware that connects hosts 10 to storage devices 50. The fabric 106 may enable host-to-storage device connectivity through Fibre Channel switching technology. The fabric 106 hardware may include one or more switches (also referred to as fabric switches), bridges, hubs, and/or other devices such as routers, as well as the interconnecting cables (e.g., fiber optic or copper cables for Fibre Channel SANs), as desired.
In one embodiment, the computer system may include a LAN that uses the Network File System (NFS) protocol to provide access to shared files on the LAN. Using NFS, each host 10 may export a logical hierarchy of files (e.g., a directory tree) physically stored on one or more of storage devices 50 and accessible by the client systems 108 through the host 10. These hierarchies of files, or portions or sub-trees of the hierarchies of files, are referred to herein as “file systems.”
In one embodiment, the SAN components may be organized into one or more clusters to provide high availability, load balancing, and/or parallel processing. For example, in FIG. 1, a selected set of the hosts 10A, 10B . . . 10i may be operated in a cluster configuration.
It is noted that while in the embodiments described above, hosts 10 may be coupled to networked storage devices 50 through a storage area network, other embodiments are possible in which the hosts 10 are coupled to storage devices 50 via the network 110. Furthermore, other embodiments may not include any clients. For example, one embodiment may involve a single host directly connected to storage devices 50.
FIG. 2 illustrates a block diagram of a host computer system 10. As illustrated in FIG. 2, each computer system 10 may include various conventional software and hardware components, as desired. A computer system 10 is illustrated with one or more processors 12 as well as a main memory 16 for storing instructions and/or data accessible by the processors 12. Computer systems 10 may also each include one or more interfaces 14 for interfacing with other hosts, clients and/or storage devices 50 via a network 110 and/or for interfacing to storage devices 50 via a SAN, SCSI (Small Computer System Interface), IDE (Integrated Drive Electronics), and/or other interfaces. In one embodiment, main memory 16 may be implemented using dynamic random access memory (DRAM), although it is noted that in other embodiments, other specific types of memory, or combinations thereof, may be utilized.
FIG. 2 further illustrates various software components executable by processors 12 out of a computer accessible medium such as main memory 16. The depicted software components include a file system 20. It is noted that these software components may be paged in and out of main memory 16 from a secondary storage medium (e.g., storage devices 50) according to conventional techniques.
The file system 20 may buffer disk blocks in a buffer cache 30 in memory. The buffer cache 30 may be divided into a group of buffers. Each buffer may be assigned the identity of a disk block stored within that buffer. The file system 20 may manage the contents of buffer cache 30 in order to benefit from temporal locality by storing recently accessed disk blocks in one or more buffers 36 within the buffer cache 30. Accordingly, if these recently accessed disk blocks are needed again, the file system 20 may access the disk blocks in the buffer cache 30 in memory 16 instead of having to retrieve the disk blocks from one or more storage devices 50.
A memory manager 40 in the buffer cache 30 may interact with an operating system (not shown) to manage the allocation of memory 16 to buffer cache 30. For example, memory manager 40 may respond to usage conditions in the buffer cache 30 to release unused memory to the operating system or to request additional memory from the operating system.
The file system 20 may use several freelists 32 and hashlists 34 when accessing (i.e., reading and/or writing) disk blocks cached in buffer cache 30. Freelists 32 may identify which buffers 36 are currently free (i.e., available to be accessed and/or allocated). Hashlists 34 may allow the file system to search for a buffer with a particular identity. For example, if the file system 20 is looking for a particular disk block in the buffer cache 30, the file system 20 may calculate an index for that disk block based on the disk block's identity. The index may select one of several hashlists 34 from a hash table (not shown). The file system 20 may then search the indexed hashlist for a buffer storing the disk block. The freelists and hashlists may be implemented using doubly linked lists, singly linked lists, arrays, or other data structures.
Serializing Access to the Buffer Cache
In order to maintain consistency, the file system 20 may acquire a lock 38 before accessing a hashlist 34, freelist 32, or buffer 36. For example, if processes executing on each of several processors 12 may access buffers via the file system 20, the file system 20 may acquire a lock whenever a buffer is accessed so as to not inadvertently read file system information from the buffer cache 30 as that information is being modified by another process.
A single lock 38 may be associated with a “lock group” of freelists 32, hashlists 34, and buffers 36. In order to access any buffer, freelist, or hashlist within that lock group, the file system 20 may first acquire that lock 38. Once the lock 38 is acquired, the file system 20 may access other buffers, freelists, and/or hashlists in that group without having to reacquire the lock 38. The file system 20 may release the lock 38 (e.g., by clearing a value written to the lock) when the access is complete. Freelists 32, hashlists 34, and buffers 36 may be subdivided into several unique lock groups and thus there may be several associated locks 38. Each freelist 32 and hashlist 34 maps to exactly one lock.
Each freelist 32 may be associated with a different lock 38 than each other freelist 32. In some embodiments, the number of freelists 32 may be proportional to the number of processors 12 in the computer system(s) 10, so that at any given time, each process executing on a given processor 12 may tend to be accessing a different freelist 32 than the processes on other processors 12. Accordingly, having one lock 38 per freelist may allow each processor 12 to obtain a lock without unnecessarily blocking another processor's access to a different freelist 32 while at the same time serializing accesses to the same freelist. Note that the number of freelists may be more or less than the number of processors in some embodiments. For example, the file system 30 may initialize the buffer cache with a number of freelists 32 equal to the minimum of sixteen (or another integer selected based on expected performance) or the number of processors 12 in the system in one embodiment. Restricting the number of freelists 32 may decrease the complexity of searches for free buffers and prevent degradation of buffer cache performance when buffers 36 are reused (e.g., if a least-recently-used algorithm is used to select which buffer to overwrite, an excessive number of freelists may degrade the performance of the least recently used algorithm).
A group of several hashlists 34 may also be associated with each lock 38. Thus, each lock 38 may be associated with one freelist 32 and one or more hashlists 34. In one embodiment, each hashlist 34 may tend to identify a few (e.g., fewer than ten, and in some cases only one or two) buffers. The number of hashlists 34 may be selected to be proportional to the total allowed capacity of the buffer cache 30 divided by the average buffer 36 size. For example, in one embodiment, if the average buffer 36 is 2 Kilobytes in size, the number of hashlists 34 may be selected by dividing the total allowed capacity of the buffer cache, expressed in Kilobytes, by 2.
In some embodiments, the same number of hashlists may be associated with each freelist to form a lock group. To allow hashlists 34 to be symmetrically associated with freelists 32 for lock sharing purposes, the computed number of hashlists (e.g., based on maximum buffer cache 30 size) may be rounded up to the next integer multiple of the number of freelists 32.
Hashlists 34 may be associated with freelists 32 so that hashlists 34 and freelists 32 that are part of the same lock group tend to identify the same subgroup of buffers. This association may be based on how memory is allocated to the buffer cache 30 (buffers from each “pool” of memory allocation, and the hashlists and freelists that correspond to that pool, may be assigned to the same lock group), as described below.
FIG. 3 illustrates exemplary contents of buffer cache 30, according to one embodiment. Here, two locks 38A and 38B are each associated with a lock group 200 that includes a freelist 32, several hashlists 34, and several buffers 36. Lock 38A is associated with lock group 200A, which includes freelist 32A, hashlists 34A–34B, and buffers 36A–36E. Lock 38B is associated lock group 200B, which includes freelist 32B, hashlists 34C–34D, and buffers 36F–36H. In this embodiment, each freelist and hashlist includes a pointer 80 to that list's associated lock 38. In alternative embodiments, pointers 80 may not be used to identify locks. For example, the file system may identify the appropriate lock for each freelist and hashlist algorithmically or by using an additional hash function.
Whenever the file system 20 accesses a particular hashlist, such as hashlist 34A, to search for a buffer storing a particular disk block, the file system 20 may first lock the hashlist 34A by acquiring the lock 38A identified in the hashlist's lock pointer 80A. If the buffer is not found in the hashlist 34A, the file system 30 may use a replacement algorithm such as least recently used replacement to select a buffer, such as buffer 36C, identified in the corresponding freelist 32A, reallocate the selected buffer to that item of file system information, and add the buffer 36C to the hashlist 34A (e.g., by adding a pointer to the buffer 36C to the hashlist 34A). If a corresponding buffer is found in the hashlist 34A, the file system 20 may verify that the corresponding buffer is on the associated freelist 32A and, if so, remove the buffer from the freelist and return the buffer to a requesting process. Since the same lock protects the freelist 32A and the hashlist 34C, the file system 20 only needs to acquire one lock 38A to access both lists. The file system 20 may release the lock 38A upon returning the buffer to the requesting process or upon failure to locate an appropriate buffer, in which case the requesting process may retry its request at a later time.
In some embodiments, each buffer 36 may additionally be protected by a semaphore (“Smp” in FIG. 3) 201. As shown in FIG. 3, each buffer 36 may include its own semaphore 201. The file system 20 may acquire a lock on a buffer 36 prior to returning that buffer to a requesting process by both acquiring the lock 38 on the freelist 32 and/or hashlist 34 associated with that buffer 36 (which the file system 20 would already be holding in order to have found the buffer in the hashlist) and updating the value of the buffer's semaphore 201. In some embodiments, the semaphore may be implemented as a spin lock or spin lock-like primitive. The semaphore 201 may be protected by the lock 38 on the corresponding freelist and hashlist.
The semaphore 201 may provide shared (read-only) or exclusive (read or write) locking in some embodiments. For example, the semaphore 201 may be implemented using a counter. When the buffer 36 is not locked, the counter value may be zero. Thus, when a shared lock is held on the buffer 36, the counter value may be equal to a value greater than zero. Each time the file system 20 acquires a shared lock on the buffer, the file system 20 may increment the counter (assuming the counter previously indicated that the buffer was locked in shared mode or unlocked). When the buffer is locked in exclusive mode, the counter value may equal negative one. Thus, a buffer's lock may be acquired by acquiring the lock 38 on the appropriate freelist or hashlist and updating the counter as needed. Other embodiments may implement shared or exclusive locking in different ways or may alternatively implement mutual exclusion locking.
The file system 30 may include buffer locking and unlocking routines to update the buffer semaphores 201. When a buffer identified on a freelist is locked, the buffer locking routine may also remove that buffer from the freelist. When such a buffer is unlocked, the buffer unlocking routine may return the buffer to the freelist. If shared and exclusive buffer locks are available, these routines may include a parameter that indicates whether the buffer is being locked in shared or exclusive mode. When a file system process requests a buffer in shared mode, the buffer locking routine may return the buffer in either shared or exclusive mode. However, when a file system process requests a buffer in exclusive mode, the buffer locking routine may not return the buffer in shared mode.
FIG. 4 illustrates one embodiment of a method of accessing file system information from a buffer in a buffer cache. At 401, the hash index of an object (e.g., a disk block) to be accessed in the buffer cache is calculated. This hash index identifies the hashlist in the buffer cache to search for the desired buffer, as indicated at 403. The hashlist belongs to a lock group that has an associated group lock. The lock group to which the hashlist belongs may be identified by a pointer to the group lock included in the hashlist. Alternatively, the lock group to which the hashlist belongs may be identified algorithmically or by using a hashing function. In order to access the hashlist, the hashlist's group lock is acquired, as indicated at 405. Acquiring the group lock also locks a corresponding freelist and may lock one or more other hashlists.
Once the hashlist identified at 403 is locked by locking the group lock, the hashlist may be searched for a buffer corresponding to the desired object (e.g., disk block). If a matching buffer is found, as determined at 407, an attempt to lock the buffer may be attempted. If the lock attempt successfully locks the buffer, as determined at 409, the group lock may be released, as shown at 413, and the locked buffer may be returned to the requesting process, as shown at 415. Note that acquisition of the buffer lock may also remove the buffer from the freelist. Also note that in some embodiments, the lock on the buffer may be acquired in one of several modes. Successfully acquiring the lock may require acquisition of the lock in a desired mode (e.g., if a shared or exclusive lock already exists on the buffer, an exclusive lock may not be successfully acquired). If the buffer's lock cannot be acquired (e.g., because the buffer is already locked), the process may release the group lock and wait for an indication that the buffer is no longer locked, as shown at 429, before reacquiring the group lock at 405.
If no matching buffer is found in the hashlist, as determined at 407, the freelist in the hashlist's lock group may be searched for a clean buffer, as indicated at 417. If a clean buffer is found, the buffer may be locked in exclusive mode, as shown at 425. At 427, the buffer may then be assigned the identity of the object (e.g., disk block), and the buffer may be moved to the hashlist identified at 403. Processing may continue as indicated at 413–415. At 415, the buffer may be marked as empty when it is returned to the requesting process. Note that the use of lock groups may avoid having to juggle locks or to release and acquire different locks when assigning a new identity to a buffer. For example, since the group lock on the hashlist acquired at 405 also locks the associated freelist, the freelist can be searched for a clean buffer at 417 without having to release the hashlist lock and/or acquire a different lock on the freelist.
If no clean buffers are available in the freelist, as determined at 417, the group lock may be released, as shown at 419. A new buffer may be allocated to the freelist or a dirty buffer on the freelist may be flushed, as indicated at 421. Then, the group lock may be reacquired, as shown at 423, and the hashlist may again be searched for a matching buffer.
Handling Multiple Buffer Sizes & Buffer Allocation
In some embodiments, the file system 20 may support multiple different buffer sizes within the buffer cache 30. In such embodiments, each freelist 32 may be subdivided into multiple sub-freelists. Each sub-freelist may correspond to a particular buffer size. Accordingly, to find a free buffer of a desired size, file system 30 may access a freelist 32, identify which sub-freelist in that freelist has buffers that are the desired buffer size (or at least as large as the desired buffer size), and search the identified sub-freelist for a free buffer. In one embodiment, each freelist may include a sub-freelist corresponding to each of the following buffer sizes: 1K, 2K, 4K, 8K, and 64K. Other embodiments may support different and/or additional buffer sizes.
FIG. 5 illustrates how freelists in the buffer cache 30 may be subdivided into sub-freelists, according to one embodiment. Three freelists 32A–32C are each subdivided into three sub-freelists 501. Each sub-freelist corresponds to a particular buffer size. For example, sub-freelists 501AA, 501BA, and 501CA each correspond to a 2K buffer size. Similarly, sub-freelists 501AB, 501BB, and 501CB correspond to a 4K buffer size and sub-freelists 501AC, 501BC, and 501CC correspond to an 8K buffer size. Whenever the file system 20 searches one of the freelists 32A–32C for a buffer of a particular size, the file system 20 may identify which sub-freelist 501 included in that freelist 32 identifies buffers having that size and limit its search to that sub-freelist 501. Accordingly, the file system's searches for buffers within a freelist may be limited to searching the portion of the freelist that corresponds to the desired buffer size.
Whenever a file system process needs to allocate a buffer 36 to store a disk block, that process may request a buffer having a particular size. If that size does not correspond to one of the supported buffer sizes, a file system 20 allocation process may round that size up to the next largest supported buffer size, access a sub-freelist 501 corresponding to that buffer size, and select a buffer (e.g., the least recently used buffer of a group of sufficiently aged buffers on that sub-freelist) to return to the requesting process. If none of the buffers on the appropriate sub-freelist are available for reallocation (e.g., if none of the buffers on the sub-freelist are sufficiently aged to qualify for least-recently-used replacement), the allocation process may attempt to acquire more memory for use with that sub-freelist.
In an alternative embodiment, all or some sub-freelists 501 may correspond to a particular range of buffer sizes. For example, one sub-freelist may correspond to buffers that are sized between 8K and 64K. In such embodiments, the appropriate sub-freelist to access may be determined based on the buffer size range in which a requested buffer size belongs.
A buffer cache memory manager 40 may request more memory from the operating system whenever more buffer cache memory is needed, up to a total allowed buffer cache capacity. The memory manager 40 may be configured to request memory allocations from the operating system in one or more uniform sizes. For example, in one embodiment, the memory manager 40 may request memory allocations of 64K and 8K. The number of different sizes of memory allocations may be fewer than the number of different supported buffer sizes.
The memory allocated to the buffer cache 30 by the operating system in response to a single request from the memory manager 40 is referred to herein as a “slab.” The memory manager 40 may subdivide each slab of memory into one or more buffers 36 of the same size. For example, if an 8K slab is requested from the operating system in response to a process requesting a 2K buffer, the 8K slab may be assigned a 2K buffer size. Four 2K buffers may be allocated from the 8K slab. Note that in some embodiments, slabs may be assigned ranges of buffer sizes instead of discrete buffer sizes.
The memory manager 40 may track the slab to which each buffer belongs. For example, each time a buffer is allocated from a slab, the memory manager 40 may add that buffer to a list associated with the slab. The buffer may also include information identifying the slab to which that buffer belongs in some embodiments.
The memory manager 40 may associate several slabs, at least some of which are allocated to different buffer sizes, to form a “pool.” A freelist 32 (and its sub-freelists, if any) and several hashlists 34 may be assigned to each pool. Freelists 32 and hashlists 34 may be associated into lock groups 200 for lock sharing purposes by pools such that each freelist 32 and hashlist 34 that share the same lock identify buffers formed from in the same pool. For example, looking back at FIG. 3, each buffer 36 protected by the same lock group's lock 38 is included in the same pool 300. The freelist 32 and hashlists in each lock group 200 are also associated with the same pool 300. Freelists 32 and hashlists 34 in different lock groups 200 are associated with different pools 300.
When the buffer cache 30 is initialized, the memory manager 40 may establish a number of pools based on the number of processors in the system. For example, the number of pools may be the same as the number of freelists, calculated as described above with respect to calculating the number of freelists. Alternatively, the number of pools, and thus the number of freelists, may be selected as the nearest prime number larger than the number of processors 12 in the system (a maximum prime number may be specified to avoid an undesirably large number of pools in some embodiments). At buffer cache 30 initialization, none of the pools may include any slabs.
As file system 20 requests buffers, memory manager 40 may request slabs from the operating system, allocate those slabs to pools, subdivide the slabs into buffers, and return the buffers to the requesting process. Each request for a buffer from a particular calling process may include information identifying the pool from which that calling process is requesting a buffer. For example, the pool may be identified by determining which lock group includes the hashlist 34 corresponding to the disk block to be cached in the requested buffer. The memory manager 40 may use this information to identify the pool in which to allocate a buffer for the calling process to use.
Whenever a slab is allocated to a pool, the memory manager 40 may assign that slab a particular buffer size. The memory manager 40 may subdivide the slab into buffers of the assigned buffer size and place each of these buffers on the freelist associated with that pool. For example, the memory manager may place all of the buffers on the appropriate sub-freelist 501 within the pool's freelist 32 upon allocating the slab to the pool. One of the buffers from the slab may be returned to the requesting process whose request initiated the slab allocation. The remaining buffers from that slab may be identified as being available to be assigned a new identity of a requested data object not already stored in the buffer cache (e.g., by indicating that the buffers are among the least recently used buffers in the freelist and that the buffers are sufficiently aged for reassignment). In embodiments such as this, a slab may be described as being fully available when all of the component buffers in that slab are sufficiently aged for reassignment.
In an alternative embodiment, whenever a slab is allocated to a pool, the identity of a requested data object may be assigned to one buffer from that slab and that buffer may be placed on the appropriate hashlist and returned to a calling process that initiated the slab allocation. In such an embodiment, if the assigned buffer size is less than the size of the slab, the other buffers may be allocated from the slab at a later time. In order to utilize the remaining portions of each slab, the memory manager may track how much of each slab is currently in use. For example, the memory manager may maintain a slab-available-space counter for each slab that indicates how many buffers of the buffer size assigned to that slab are currently available for allocation. Slabs may be described as fully available (e.g., no buffers are currently allocated from that slab), partially available (e.g., at least one buffer is available to be allocated from that slab), and unavailable (e.g., no buffers are available to be allocated from that slab).
As buffers 36 are no longer needed, those buffers may be released back to the operating system. The memory manager 40 may control the release of memory from the buffer cache 30 such that either all of the buffers in a given slab are released or none of the buffers in a given slab are released. In embodiments that track the available buffers within each slab, a buffer may be individually released back to a slab by removing the buffer from the sub-freelist 501 and increasing the number of buffers available within the slab.
The memory manager 40 may occasionally search for fully available slabs that can be released back to the operating system. For example, the memory manager 40 may search each sub-freelist 501 to identify slabs in which all of the component buffers are sufficiently aged for reassignment. The memory manager 40 may also reassign buffer identities of buffers that are less frequently accessed to buffers within the same slab so that when that slab is freed, the slab is likely to contain only buffers that have not been recently accessed. In embodiments using slab-available-space counters, the memory manager 40 may search for slabs whose slab-available-space counters indicate that no buffers are currently allocated from those slabs and release those slabs back to the operating system. In some embodiments, the memory manager 40 may track how long each slab is fully available and only release those slabs that have been fully available for at least a minimum duration.
By handling buffer cache 30 in slabs instead of individually requesting and releasing buffers from the operating system, memory fragmentation due to operation of the buffer cache 30 may be reduced. Also, the memory manager 40 may more accurately track how much memory is currently allocated to buffer cache by identifying how many slabs of each slab size are currently allocated.
FIG. 6 illustrates one embodiment of a method of allocating buffers using slab allocation within a file system buffer cache. At 601, the pool from which a buffer should be allocated is determined. If the identified pool has a fully or partially available slab that is assigned the desired buffer size, as determined at 603, a buffer from that slab may be assigned a new identity, as shown at 605, and returned to a requesting process. If all of the buffers in slabs are not immediately placed on the freelist when slabs are allocated to pools, function 605 may also involve allocating a buffer from the slab before assigning a new identity to the buffer. Information indicating how many buffers remain to be allocated from that pool may also be updated at 605. In embodiments that track how many buffers from each slab are allocated, the number of buffers allocated in the slab may also be updated at 605.
If no slab of the desired buffer size allocated to the pool has buffers available and more memory is available for the buffer cache (i.e., the buffer cache has not reached its maximum capacity yet), as determined at 607, another slab of memory may be requested from the operating system. This slab may be allocated to the pool identified at 601 and assigned the buffer size of the desired buffer, as shown at 609. Then, one or more buffers from this slab may be allocated.
If the buffer cache is already at its maximum size, as determined at 607, the pool identified at 601 may be searched for any fully available slabs of other buffer sizes, as shown at 615. If a fully available slab is found within that pool, the slab may be reassigned the desired buffer size, as shown at 617, and one or more buffers may be allocated from this slab. This may involve dropping a lock acquired on the first pool's lock group and acquiring a lock on the other pool's lock group in order to search the other pool for slabs and/or to reallocate the slab to the first pool.
If the pool identified at 601 does not include any fully available slabs, other pools' slabs may be checked to see if any other pool has a fully available slab of the desired buffer size, as shown at 611. If so, that slab may be reallocated, as indicated at 613, to the pool identified at 601. One or more buffers may be allocated from the reallocated slab.
If no other pool has a fully available slab associated with the desired buffer size, the other pools may be searched for fully available slabs, as indicated at 619. If a fully available slab is found, the slab may be reallocated to the pool identified at 601 and reassigned the desired buffer size, as shown at 621. Then, a buffer may be allocated from this slab.
If no fully available slabs are found in any of the pools, the slab that includes the oldest (most aged with respect to a least recently used algorithm) buffer in the pool identified at 601 may be released back to the operating system, as shown at 623, in order to free memory for reallocation to the buffer cache. Freeing the slab may involve identifying each buffer in the slab and writing data stored in any dirty buffers back to disk or reallocating a buffer from another slab to store the data in any dirty buffers. If no slab is available for release within the pool identified at 601, additional pools may be searched for a slab to release (not shown).
Note that while the above description has focused on embodiments involving a file system and file system buffer cache, other embodiments may involve different types of caches. For example, alternative embodiments may involve other types of caches including a DNLC (Directory Name Lookup Cache), inode cache, or page cache. Similarly, some embodiments may involve a cache of database objects maintained and accessed by a database. Generally, these various types of caches may each contain objects, and freelists and hashlists may be used to track these objects. As used herein, “objects” are uniquely identifiable data or metadata objects. For example, the objects that make up a buffer cache are referred to above as buffers, and each buffer is assigned the unique identity of a disk block stored within that buffer. A DNLC may cache filename objects, an inode cache may cache inode objects, and a page cache may cache page objects. Within a cache, freelists identify objects that are available for reuse. An object is placed on particular hashlist based on a hash of the object's identity. Hashlists may be used to increase the efficiency of searches for objects within a cache.
Returning to FIG. 1, note that each client system 108 and/or host 10 may take various forms, including a workstation, server, mainframe computer system, network appliance, network computer, Internet appliance, personal digital assistant (PDA), embedded device, smart phone, television system, another suitable device, or combinations thereof.
Additionally, note that all or part of a file system application 30 may be stored on various computer accessible media, such as storage devices 50. Examples of computer accessible media include hard disk storage, floppy disk storage, removable disk storage, flash memory, punch cards, magneto-optical storage, MEMs, magnetic core, and random access memory (RAM). Various embodiments may further include receiving, sending or storing instructions and/or data implemented in accordance with the foregoing description upon a computer accessible medium. A computer accessible medium may include a communication medium such as network and/or a wireless link on which communication signals such as electrical, electromagnetic, or digital signals are conveyed.
The flow charts described herein represent exemplary embodiments of methods. The methods may be implemented in software, hardware, or a combination thereof. The order of method may be changed, and various elements may be added, reordered, combined, omitted, modified, etc.
Various modifications and changes may be made to the invention as would be obvious to a person skilled in the art having the benefit of this disclosure. It is intended that the following claims be interpreted to embrace all such modifications and changes and, accordingly, the specifications and drawings are to be regarded in an illustrative rather than a restrictive sense.

## Claims

1. A method, comprising:
assigning each of a plurality of freelists and a plurality of hashlists used to implement a cache to one of a plurality of lock groups, wherein objects on each freelist and hashlist assigned to a same one of the plurality of lock groups are allocated from a same one of a plurality of memory allocation pools, and wherein each lock group is associated with a respective one of a plurality of locks;
acquiring a lock of the plurality of locks, wherein acquiring the lock locks a freelist and several hashlists included in an associated lock group of the plurality of lock groups.

2. The method of claim 1, further comprising:
searching a hashlist of the several hashlists for an object; and
if the object is found in the hashlist, acquiring a lock on the object by writing to a semaphore associated with the object, wherein write access to the semaphore is protected by the lock.

3. The method of claim 2, wherein a value of the semaphore indicates whether the object is locked in shared mode or exclusive mode.

4. The method of claim 1, further comprising:
a cache memory manager requesting a plurality of slabs of memory from an operating system for use as part of the cache, wherein each of the plurality of slabs is a uniformly sized chunk of memory; and
the cache memory manager subdividing each of the plurality of slabs of memory into one or more objects and tracking which objects are included in each slab.

5. The method of claim 4, further comprising:
the cache memory manager releasing memory back to the operating system in slabs such that if any object included in one of the plurality of slabs is released back to the operating system, all of the objects included in that one of the plurality of slabs are released back to the operating system.

6. The method of claim 4, further comprising assigning each of the plurality of slabs to one of the plurality of memory allocation pools, wherein each memory allocation pool includes a unique subset of the plurality of slabs.

7. The method of claim 4, further comprising:
assigning each of the plurality of slabs an object size or a range of object sizes; and
the memory manager allocating objects of the assigned object size or assigned range of object sizes from each of the plurality of slabs.

8. The method of claim 1, wherein each of the plurality of freelists includes a plurality of sub-freelists, wherein objects on a same one of the plurality of sub-freelists have a same object size, and wherein objects on different ones of the plurality of sub-freelists have different object sizes.

9. A computer system, comprising:
a plurality of processors; and
a memory coupled to the plurality of processors and storing data implementing a cache and program instructions executable by one or more of the plurality of processors to:
assign each of a plurality of freelists and a plurality of hashlists used to implement a cache to one of a plurality of lock groups, wherein objects on each freelist and hashlist assigned to a same one of the plurality of lock groups are allocated from a same one of a plurality of memory allocation pools, and wherein each lock group is associated with a respective one of a plurality of locks;
acquire a lock of the plurality of locks, wherein acquiring the lock locks a freelist and several hashlists included in an associated lock group of the plurality of lock groups.

10. The computer system of claim 9, wherein program instructions are executable to search a hashlist of the several hashlists for an object and, if the object is found in the hashlist, to acquire a lock on the object by writing to a semaphore associated with the object, wherein write access to the semaphore is protected by the lock.

11. The computer system of claim 10, wherein a value of the semaphore indicates whether the object is locked in shared mode or exclusive mode.

12. The computer system of claim 9, wherein the memory stores program instructions executable to implement an operating system and program instructions executable to implement a cache memory manager;
wherein the cache memory manager is configured to request a plurality of slabs of memory from the operating system for use as part of the cache, wherein each of the plurality of slabs is a uniformly sized chunk of memory;
wherein the cache memory manager is configured to subdivide each of the plurality of slabs of memory into one or more objects and to track which objects are included in each slab.

13. The computer system of claim 12, wherein the cache memory manager is configured to release memory back to the operating system in slabs such that if any object included in one of the plurality of slabs is released back to the operating system, all of the object included in that one of the plurality of slabs are released back to the operating system.

14. The computer system of claim 12, wherein the cache memory manager is configured to assign each of the plurality of slabs to one of the plurality of memory allocation pools, wherein each memory allocation pool includes a unique subset of the plurality of slabs.

15. The computer system of claim 14, wherein each of the plurality of slabs is assigned an object size or a range of object sizes, and wherein the memory manager is configured to only allocate objects of the assigned object size or range of object sizes from each of the plurality of slabs.

16. A tangible computer accessible medium comprising program instructions executable to:
assign each of a plurality of freelists and a plurality of hashlists used to implement a cache to one of a plurality of lock groups, wherein objects on each freelist and hashlist assigned to a same one of the plurality of lock groups are allocated from a same one of a plurality of memory allocation pools, and wherein each lock group is associated with a respective one of a plurality of locks;
acquire a lock of the plurality of locks, wherein acquiring the lock locks a freelist and several hashlists included in an associated lock group of the plurality of lock groups.

17. The tangible computer accessible medium of claim 16, wherein the program instructions are further executable to:
search a hashlist of the several hashlists for an object; and
if the object is found in the hashlist, acquire a lock on the object by writing to a semaphore associated with the object, wherein write access to the semaphore is protected by the lock.

18. The tangible computer accessible medium of claim 17, wherein a value of the semaphore indicates whether the object is locked in shared mode or exclusive mode.

19. The tangible computer accessible medium of claim 16, wherein the program instructions are further executable to
request a plurality of slabs of memory from an operating system for use as part of the cache; and
subdivide each of the plurality of slabs of memory into one or more objects and track which objects are included in each slab; and
release memory back to the operating system in slabs such that if any object included in one of the plurality of slabs is released back to the operating system, all of the objects included in that one of the plurality of slabs are released back to the operating system.

20. The tangible computer accessible medium of claim 19, wherein the program instructions are further executable to:
assign each of the plurality of slabs to one of the plurality of memory allocation pools, wherein each memory allocation pool includes a unique subset of the plurality of slabs.