1 | /* $Id: PDMBlkCacheInternal.h 34221 2010-11-21 18:26:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Block Cache.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2008 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ___PDMBlkCacheInternal_h
|
---|
19 | #define ___PDMBlkCacheInternal_h
|
---|
20 |
|
---|
21 | #include <VBox/cfgm.h>
|
---|
22 | #include <VBox/stam.h>
|
---|
23 | #include <VBox/tm.h>
|
---|
24 | #include <VBox/pdmblkcache.h>
|
---|
25 | #include <iprt/types.h>
|
---|
26 | #include <iprt/file.h>
|
---|
27 | #include <iprt/thread.h>
|
---|
28 | #include <iprt/semaphore.h>
|
---|
29 | #include <iprt/critsect.h>
|
---|
30 | #include <iprt/avl.h>
|
---|
31 | #include <iprt/list.h>
|
---|
32 | #include <iprt/spinlock.h>
|
---|
33 | #include <iprt/memcache.h>
|
---|
34 |
|
---|
35 | RT_C_DECLS_BEGIN
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * A few forward declarations.
|
---|
39 | */
|
---|
40 | /** Pointer to a cache LRU list. */
|
---|
41 | typedef struct PDMBLKLRULIST *PPDMBLKLRULIST;
|
---|
42 | /** Pointer to the global cache structure. */
|
---|
43 | typedef struct PDMBLKCACHEGLOBAL *PPDMBLKCACHEGLOBAL;
|
---|
44 | /** Pointer to a cache entry waiter structure. */
|
---|
45 | typedef struct PDMBLKCACHEWAITER *PPDMBLKCACHEWAITER;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * A cache entry
|
---|
49 | */
|
---|
50 | typedef struct PDMBLKCACHEENTRY
|
---|
51 | {
|
---|
52 | /** The AVL entry data. */
|
---|
53 | AVLRU64NODECORE Core;
|
---|
54 | /** Pointer to the previous element. Used in one of the LRU lists.*/
|
---|
55 | struct PDMBLKCACHEENTRY *pPrev;
|
---|
56 | /** Pointer to the next element. Used in one of the LRU lists.*/
|
---|
57 | struct PDMBLKCACHEENTRY *pNext;
|
---|
58 | /** Pointer to the list the entry is in. */
|
---|
59 | PPDMBLKLRULIST pList;
|
---|
60 | /** Cache the entry belongs to. */
|
---|
61 | PPDMBLKCACHE pBlkCache;
|
---|
62 | /** Flags for this entry. Combinations of PDMACFILECACHE_* #defines */
|
---|
63 | volatile uint32_t fFlags;
|
---|
64 | /** Reference counter. Prevents eviction of the entry if > 0. */
|
---|
65 | volatile uint32_t cRefs;
|
---|
66 | /** Size of the entry. */
|
---|
67 | size_t cbData;
|
---|
68 | /** Pointer to the memory containing the data. */
|
---|
69 | uint8_t *pbData;
|
---|
70 | /** Head of list of tasks waiting for this one to finish. */
|
---|
71 | PPDMBLKCACHEWAITER pWaitingHead;
|
---|
72 | /** Tail of list of tasks waiting for this one to finish. */
|
---|
73 | PPDMBLKCACHEWAITER pWaitingTail;
|
---|
74 | /** Node for dirty but not yet committed entries list per endpoint. */
|
---|
75 | RTLISTNODE NodeNotCommitted;
|
---|
76 | } PDMBLKCACHEENTRY, *PPDMBLKCACHEENTRY;
|
---|
77 | /** I/O is still in progress for this entry. This entry is not evictable. */
|
---|
78 | #define PDMBLKCACHE_ENTRY_IO_IN_PROGRESS RT_BIT(0)
|
---|
79 | /** Entry is locked and thus not evictable. */
|
---|
80 | #define PDMBLKCACHE_ENTRY_LOCKED RT_BIT(1)
|
---|
81 | /** Entry is dirty */
|
---|
82 | #define PDMBLKCACHE_ENTRY_IS_DIRTY RT_BIT(2)
|
---|
83 | /** Entry is not evictable. */
|
---|
84 | #define PDMBLKCACHE_NOT_EVICTABLE (PDMBLKCACHE_ENTRY_LOCKED | PDMBLKCACHE_ENTRY_IO_IN_PROGRESS | PDMBLKCACHE_ENTRY_IS_DIRTY)
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * LRU list data
|
---|
88 | */
|
---|
89 | typedef struct PDMBLKLRULIST
|
---|
90 | {
|
---|
91 | /** Head of the list. */
|
---|
92 | PPDMBLKCACHEENTRY pHead;
|
---|
93 | /** Tail of the list. */
|
---|
94 | PPDMBLKCACHEENTRY pTail;
|
---|
95 | /** Number of bytes cached in the list. */
|
---|
96 | uint32_t cbCached;
|
---|
97 | } PDMBLKLRULIST;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Global cache data.
|
---|
101 | */
|
---|
102 | typedef struct PDMBLKCACHEGLOBAL
|
---|
103 | {
|
---|
104 | /** Pointer to the owning VM instance. */
|
---|
105 | PVM pVM;
|
---|
106 | /** Maximum size of the cache in bytes. */
|
---|
107 | uint32_t cbMax;
|
---|
108 | /** Current size of the cache in bytes. */
|
---|
109 | uint32_t cbCached;
|
---|
110 | /** Critical section protecting the cache. */
|
---|
111 | RTCRITSECT CritSect;
|
---|
112 | /** Maximum number of bytes cached. */
|
---|
113 | uint32_t cbRecentlyUsedInMax;
|
---|
114 | /** Maximum number of bytes in the paged out list .*/
|
---|
115 | uint32_t cbRecentlyUsedOutMax;
|
---|
116 | /** Recently used cache entries list */
|
---|
117 | PDMBLKLRULIST LruRecentlyUsedIn;
|
---|
118 | /** Scorecard cache entry list. */
|
---|
119 | PDMBLKLRULIST LruRecentlyUsedOut;
|
---|
120 | /** List of frequently used cache entries */
|
---|
121 | PDMBLKLRULIST LruFrequentlyUsed;
|
---|
122 | /** Commit timeout in milli seconds */
|
---|
123 | uint32_t u32CommitTimeoutMs;
|
---|
124 | /** Number of dirty bytes needed to start a commit of the data to the disk. */
|
---|
125 | uint32_t cbCommitDirtyThreshold;
|
---|
126 | /** Current number of dirty bytes in the cache. */
|
---|
127 | volatile uint32_t cbDirty;
|
---|
128 | /** Flag whether a commit is currently in progress. */
|
---|
129 | volatile bool fCommitInProgress;
|
---|
130 | /** Commit interval timer */
|
---|
131 | PTMTIMERR3 pTimerCommit;
|
---|
132 | /** Number of endpoints using the cache. */
|
---|
133 | uint32_t cRefs;
|
---|
134 | /** List of all users of this cache. */
|
---|
135 | RTLISTNODE ListUsers;
|
---|
136 | #ifdef VBOX_WITH_STATISTICS
|
---|
137 | /** Hit counter. */
|
---|
138 | STAMCOUNTER cHits;
|
---|
139 | /** Partial hit counter. */
|
---|
140 | STAMCOUNTER cPartialHits;
|
---|
141 | /** Miss counter. */
|
---|
142 | STAMCOUNTER cMisses;
|
---|
143 | /** Bytes read from cache. */
|
---|
144 | STAMCOUNTER StatRead;
|
---|
145 | /** Bytes written to the cache. */
|
---|
146 | STAMCOUNTER StatWritten;
|
---|
147 | /** Time spend to get an entry in the AVL tree. */
|
---|
148 | STAMPROFILEADV StatTreeGet;
|
---|
149 | /** Time spend to insert an entry in the AVL tree. */
|
---|
150 | STAMPROFILEADV StatTreeInsert;
|
---|
151 | /** Time spend to remove an entry in the AVL tree. */
|
---|
152 | STAMPROFILEADV StatTreeRemove;
|
---|
153 | /** Number of times a buffer could be reused. */
|
---|
154 | STAMCOUNTER StatBuffersReused;
|
---|
155 | #endif
|
---|
156 | } PDMBLKCACHEGLOBAL;
|
---|
157 | #ifdef VBOX_WITH_STATISTICS
|
---|
158 | AssertCompileMemberAlignment(PDMBLKCACHEGLOBAL, cHits, sizeof(uint64_t));
|
---|
159 | #endif
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Block cache type.
|
---|
163 | */
|
---|
164 | typedef enum PDMBLKCACHETYPE
|
---|
165 | {
|
---|
166 | /** Device . */
|
---|
167 | PDMBLKCACHETYPE_DEV = 1,
|
---|
168 | /** Driver consumer. */
|
---|
169 | PDMBLKCACHETYPE_DRV,
|
---|
170 | /** Internal consumer. */
|
---|
171 | PDMBLKCACHETYPE_INTERNAL,
|
---|
172 | /** Usb consumer. */
|
---|
173 | PDMBLKCACHETYPE_USB
|
---|
174 | } PDMBLKCACHETYPE;
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Per user cache data.
|
---|
178 | */
|
---|
179 | typedef struct PDMBLKCACHE
|
---|
180 | {
|
---|
181 | /** Pointer to the id for the cache. */
|
---|
182 | char *pszId;
|
---|
183 | /** AVL tree managing cache entries. */
|
---|
184 | PAVLRU64TREE pTree;
|
---|
185 | /** R/W semaphore protecting cached entries for this endpoint. */
|
---|
186 | RTSEMRW SemRWEntries;
|
---|
187 | /** Pointer to the gobal cache data */
|
---|
188 | PPDMBLKCACHEGLOBAL pCache;
|
---|
189 | /** Lock protecting the dirty entries list. */
|
---|
190 | RTSPINLOCK LockList;
|
---|
191 | /** List of dirty but not committed entries for this endpoint. */
|
---|
192 | RTLISTNODE ListDirtyNotCommitted;
|
---|
193 | /** Node of the cache user list. */
|
---|
194 | RTLISTNODE NodeCacheUser;
|
---|
195 | /** Block cache type. */
|
---|
196 | PDMBLKCACHETYPE enmType;
|
---|
197 | /** Type specific data. */
|
---|
198 | union
|
---|
199 | {
|
---|
200 | /** PDMASYNCCOMPLETIONTEMPLATETYPE_DEV */
|
---|
201 | struct
|
---|
202 | {
|
---|
203 | /** Pointer to the device instance owning the block cache. */
|
---|
204 | R3PTRTYPE(PPDMDEVINS) pDevIns;
|
---|
205 | /** Complete callback to the user. */
|
---|
206 | R3PTRTYPE(PFNPDMBLKCACHEXFERCOMPLETEDEV) pfnXferComplete;
|
---|
207 | /** I/O enqueue callback. */
|
---|
208 | R3PTRTYPE(PFNPDMBLKCACHEXFERENQUEUEDEV) pfnXferEnqueue;
|
---|
209 | } Dev;
|
---|
210 | /** PDMASYNCCOMPLETIONTEMPLATETYPE_DRV */
|
---|
211 | struct
|
---|
212 | {
|
---|
213 | /** Pointer to the driver instance owning the block cache. */
|
---|
214 | R3PTRTYPE(PPDMDRVINS) pDrvIns;
|
---|
215 | /** Complete callback to the user. */
|
---|
216 | R3PTRTYPE(PFNPDMBLKCACHEXFERCOMPLETEDRV) pfnXferComplete;
|
---|
217 | /** I/O enqueue callback. */
|
---|
218 | R3PTRTYPE(PFNPDMBLKCACHEXFERENQUEUEDRV) pfnXferEnqueue;
|
---|
219 | } Drv;
|
---|
220 | /** PDMASYNCCOMPLETIONTEMPLATETYPE_INTERNAL */
|
---|
221 | struct
|
---|
222 | {
|
---|
223 | /** Pointer to user data. */
|
---|
224 | R3PTRTYPE(void *) pvUser;
|
---|
225 | /** Complete callback to the user. */
|
---|
226 | R3PTRTYPE(PFNPDMBLKCACHEXFERCOMPLETEINT) pfnXferComplete;
|
---|
227 | /** I/O enqueue callback. */
|
---|
228 | R3PTRTYPE(PFNPDMBLKCACHEXFERENQUEUEINT) pfnXferEnqueue;
|
---|
229 | } Int;
|
---|
230 | /** PDMASYNCCOMPLETIONTEMPLATETYPE_USB */
|
---|
231 | struct
|
---|
232 | {
|
---|
233 | /** Pointer to the usb instance owning the template. */
|
---|
234 | R3PTRTYPE(PPDMUSBINS) pUsbIns;
|
---|
235 | /** Complete callback to the user. */
|
---|
236 | R3PTRTYPE(PFNPDMBLKCACHEXFERCOMPLETEUSB) pfnXferComplete;
|
---|
237 | /** I/O enqueue callback. */
|
---|
238 | R3PTRTYPE(PFNPDMBLKCACHEXFERENQUEUEUSB) pfnXferEnqueue;
|
---|
239 | } Usb;
|
---|
240 | } u;
|
---|
241 |
|
---|
242 | #ifdef VBOX_WITH_STATISTICS
|
---|
243 | uint32_t u32Alignment;
|
---|
244 | /** Number of times a write was deferred because the cache entry was still in progress */
|
---|
245 | STAMCOUNTER StatWriteDeferred;
|
---|
246 | #endif
|
---|
247 | } PDMBLKCACHE, *PPDMBLKCACHE;
|
---|
248 | #ifdef VBOX_WITH_STATISTICS
|
---|
249 | AssertCompileMemberAlignment(PDMBLKCACHE, StatWriteDeferred, sizeof(uint64_t));
|
---|
250 | #endif
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * I/O task.
|
---|
254 | */
|
---|
255 | typedef struct PDMBLKCACHEREQ
|
---|
256 | {
|
---|
257 | /** Opaque user data returned on completion. */
|
---|
258 | void *pvUser;
|
---|
259 | /** Number of bytes to transfer. */
|
---|
260 | volatile uint32_t cbXfer;
|
---|
261 | /** Number of pending transfers (waiting for a cache entry and passed through). */
|
---|
262 | volatile uint32_t cXfersPending;
|
---|
263 | /** Status code. */
|
---|
264 | volatile int rcReq;
|
---|
265 | } PDMBLKCACHEREQ, *PPDMBLKCACHEREQ;
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * I/O transfer from the cache to the underlying medium.
|
---|
269 | */
|
---|
270 | typedef struct PDMBLKCACHEIOXFER
|
---|
271 | {
|
---|
272 | /** Flag whether the I/O xfer updates a cache entry or updates the request directl. */
|
---|
273 | bool fIoCache;
|
---|
274 | /** Type dependent data. */
|
---|
275 | union
|
---|
276 | {
|
---|
277 | /** Pointer to the entry the transfer updates. */
|
---|
278 | PPDMBLKCACHEENTRY pEntry;
|
---|
279 | /** Pointer to the request the ztransfer updates. */
|
---|
280 | PPDMBLKCACHEREQ pReq;
|
---|
281 | };
|
---|
282 | /** Segment used if a cache entry is updated. */
|
---|
283 | RTSGSEG SgSeg;
|
---|
284 | /** S/G buffer. */
|
---|
285 | RTSGBUF SgBuf;
|
---|
286 | /** Transfer direction. */
|
---|
287 | PDMBLKCACHEXFERDIR enmXferDir;
|
---|
288 | /** Size of the transfer. */
|
---|
289 | size_t cbXfer;
|
---|
290 | } PDMBLKCACHEIOXFER;
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Cache waiter
|
---|
294 | */
|
---|
295 | typedef struct PDMBLKCACHEWAITER
|
---|
296 | {
|
---|
297 | /* Next waiter in the list. */
|
---|
298 | struct PDMBLKCACHEWAITER *pNext;
|
---|
299 | /** S/G buffer holding or receiving data. */
|
---|
300 | RTSGBUF SgBuf;
|
---|
301 | /** Offset into the cache entry to start the transfer. */
|
---|
302 | uint32_t offCacheEntry;
|
---|
303 | /** How many bytes to transfer. */
|
---|
304 | size_t cbTransfer;
|
---|
305 | /** Flag whether the task wants to read or write into the entry. */
|
---|
306 | bool fWrite;
|
---|
307 | /** Task the waiter is for. */
|
---|
308 | PPDMBLKCACHEREQ pReq;
|
---|
309 | } PDMBLKCACHEWAITER;
|
---|
310 |
|
---|
311 | RT_C_DECLS_END
|
---|
312 |
|
---|
313 | #endif
|
---|
314 |
|
---|