#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/kassert.h>
#include <sys/kdebug.h>
#include <sys/kmem.h>
#include <sys/spinlock.h>
#include <sys/disk.h>
#include <sys/bufcache.h>
#include <errno.h>
Go to the source code of this file.
|
static | TAILQ_HEAD (CacheHashTable, DEFINE_SLAB(BufCacheEntry) |
|
static int | BufCacheLookup (Disk *disk, uint64_t diskOffset, BufCacheEntry **entry) |
|
static int | BufCacheAlloc (Disk *disk, uint64_t diskOffset, BufCacheEntry **entry) |
|
int | BufCache_Alloc (Disk *disk, uint64_t diskOffset, BufCacheEntry **entry) |
|
void | BufCache_Release (BufCacheEntry *entry) |
|
int | BufCache_Read (Disk *disk, uint64_t diskOffset, BufCacheEntry **entry) |
|
int | BufCache_Write (BufCacheEntry *entry) |
|
static void | Debug_BufCache (int argc, const char *argv[]) |
|
| REGISTER_DBGCMD (diskcache, "Display disk cache statistics", Debug_BufCache) |
|
◆ BLOCKSIZE
#define BLOCKSIZE (16*1024) |
◆ CACHESIZE
#define CACHESIZE (16*1024*1024) |
◆ HASHTABLEENTRIES
#define HASHTABLEENTRIES 128 |
◆ BufCache_Alloc()
BufCache_Alloc –
Allocate a buffer cache entry to allow writing new data to disk.
- Parameters
-
[in] | disk | Disk object |
[in] | diskOffset | Block offset within the disk |
[out] | entry | If successful, this contains the buffer cache entry. |
- Return values
-
- Returns
- Otherwise returns an error code.
Definition at line 178 of file bufcache.c.
179{
180 int status;
181
183
185 if (*entry ==
NULL) {
187 }
188
189 cacheAlloc++;
190
192
193 return status;
194}
static int BufCacheLookup(Disk *disk, uint64_t diskOffset, BufCacheEntry **entry)
static int BufCacheAlloc(Disk *disk, uint64_t diskOffset, BufCacheEntry **entry)
void Spinlock_Unlock(Spinlock *lock) __UNLOCK_EX(*lock)
void Spinlock_Lock(Spinlock *lock) __LOCK_EX(*lock)
◆ BufCache_Read()
BufCache_Read –
Read block from disk into the buffer cache.
- Parameters
-
[in] | disk | Disk object |
[in] | diskOffset | Block offset within the disk |
[out] | entry | If successful, this contains the buffer cache entry. |
- Return values
-
- Returns
- Otherwise returns an error code.
Definition at line 229 of file bufcache.c.
230{
231 int status;
234
237 if (*entry !=
NULL) {
238 cacheHit++;
240 return status;
241 }
242 cacheMiss++;
243
245 if (status != 0) {
247 return status;
248 }
249
250 buf = (*entry)->buffer;
253
254
255
256
257
260
261 return status;
262}
int Disk_Read(Disk *disk, void *buf, SGArray *sga, DiskCB cb, void *arg)
void SGArray_Init(SGArray *sga)
int SGArray_Append(SGArray *sga, uint64_t off, uint64_t len)
◆ BufCache_Release()
BufCache_Release –
Release a buffer cache entry. If no other references are held the buffer cache entry is placed on the LRU list.
- Parameters
-
[in] | entry | Buffer cache entry. |
Definition at line 205 of file bufcache.c.
206{
208
212 }
213
215}
#define TAILQ_INSERT_TAIL(head, elm, field)
◆ BufCache_Write()
BufCache_Write –
Write a buffer cache entry to disk.
- Return values
-
- Returns
- Otherwise an error code is returned.
Definition at line 273 of file bufcache.c.
274{
277
280
282}
int Disk_Write(Disk *disk, void *buf, SGArray *sga, DiskCB cb, void *arg)
◆ BufCacheAlloc()
BufCacheAlloc –
Allocates a buffer cache entry that can be used by BufCache_Alloc or BufCache_Read to allocate the underlying buffer..
- Parameters
-
[in] | disk | Disk object |
[in] | diskOffset | Block offset within the disk |
[out] | entry | If successful, this contains the buffer cache entry. |
- Return values
-
- Returns
- ENOMEM if there's no buffer cache entries free.
Definition at line 133 of file bufcache.c.
134{
135 struct CacheHashTable *table;
137
138
141 kprintf(
"BufCache: No space left!\n");
143 }
145
146
150 }
151
152
156
157
160 *entry = e;
161
162 return 0;
163}
int kprintf(const char *fmt,...)
#define TAILQ_FIRST(head)
#define TAILQ_REMOVE(head, elm, field)
#define TAILQ_INSERT_HEAD(head, elm, field)
◆ BufCacheLookup()
BufCacheLookup –
Looks up a buffer cache entry that can be used by BufCache_Alloc or BufCache_Read to allocate the underlying buffer.
- Parameters
-
[in] | disk | Disk object |
[in] | diskOffset | Block offset within the disk |
[out] | entry | If successful, this contains the buffer cache entry. |
- Return values
-
- Returns
- ENOENT if not present.
Definition at line 97 of file bufcache.c.
98{
99 struct CacheHashTable *table;
101
102
109 }
110 *entry = e;
111 return 0;
112 }
113 }
114
117}
#define TAILQ_FOREACH(var, head, field)
◆ Debug_BufCache()
static void Debug_BufCache |
( |
int |
argc, |
|
|
const char * |
argv[] |
|
) |
| |
|
static |
Definition at line 285 of file bufcache.c.
286{
287 kprintf(
"Hits: %lld\n", cacheHit);
288 kprintf(
"Misses: %lld\n", cacheMiss);
289 kprintf(
"Allocations: %lld\n", cacheAlloc);
290}
◆ REGISTER_DBGCMD()
REGISTER_DBGCMD |
( |
diskcache |
, |
|
|
"Display disk cache statistics" |
, |
|
|
Debug_BufCache |
|
|
) |
| |
◆ TAILQ_HEAD()
static TAILQ_HEAD |
( |
CacheHashTable |
, |
|
|
DEFINE_SLAB( |
BufCacheEntry |
|
) |
| |
|
static |
BufCache_Init –
Initialize the system buffer cache.
Definition at line 21 of file bufcache.c.
41{
42 int i;
43
45
48 Panic(
"BufCache: Cannot create XMem region\n");
49
51 Panic(
"BufCache: Cannot back XMem region\n");
52
54
56 if (!hashTable)
57 Panic(
"BufCache: Cannot allocate hash table\n");
60 }
61
63
64
68 if (!e) {
69 Panic(
"BufCache: Cannot allocate cache entry\n");
70 }
71
76 }
77
78 cacheHit = 0;
79 cacheMiss = 0;
80 cacheAlloc = 0;
81}
void * PAlloc_AllocPage()
void Slab_Init(Slab *slab, const char *name, uintptr_t objsz, uintptr_t align)
#define SPINLOCK_TYPE_NORMAL
void Spinlock_Init(Spinlock *lock, const char *name, uint64_t type)
void * memset(void *dst, int c, size_t len)
void Panic(const char *str)
bool XMem_Allocate(XMem *xmem, uintptr_t length)
uintptr_t XMem_GetBase(XMem *xmem)
◆ cacheLock
◆ diskBuf