261fcf363eb85f492d633fb6714e79482df739b6
[reactos.git] / ntoskrnl / cc / lazywrite.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/cc/lazywrite.c
5 * PURPOSE: Cache manager
6 *
7 * PROGRAMMERS: Pierre Schweitzer (pierre@reactos.org)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 /* Counters:
17 * - Amount of pages flushed by lazy writer
18 * - Number of times lazy writer ran
19 */
20 ULONG CcLazyWritePages = 0;
21 ULONG CcLazyWriteIos = 0;
22
23 /* Internal vars (MS):
24 * - Lazy writer status structure
25 * - Lookaside list where to allocate work items
26 * - Queue for high priority work items (read ahead)
27 * - Queue for regular work items
28 * - Available worker threads
29 * - Queue for stuff to be queued after lazy writer is done
30 * - Marker for throttling queues
31 * - Number of ongoing workers
32 * - Three seconds delay for lazy writer
33 * - One second delay for lazy writer
34 * - Zero delay for lazy writer
35 * - Number of worker threads
36 */
37 LAZY_WRITER LazyWriter;
38 NPAGED_LOOKASIDE_LIST CcTwilightLookasideList;
39 LIST_ENTRY CcExpressWorkQueue;
40 LIST_ENTRY CcRegularWorkQueue;
41 LIST_ENTRY CcIdleWorkerThreadList;
42 LIST_ENTRY CcPostTickWorkQueue;
43 BOOLEAN CcQueueThrottle = FALSE;
44 ULONG CcNumberActiveWorkerThreads = 0;
45 LARGE_INTEGER CcFirstDelay = RTL_CONSTANT_LARGE_INTEGER((LONGLONG)-1*3000*1000*10);
46 LARGE_INTEGER CcIdleDelay = RTL_CONSTANT_LARGE_INTEGER((LONGLONG)-1*1000*1000*10);
47 LARGE_INTEGER CcNoDelay = RTL_CONSTANT_LARGE_INTEGER((LONGLONG)0);
48 ULONG CcNumberWorkerThreads;
49
50 /* FUNCTIONS *****************************************************************/
51
52 VOID
53 CcPostWorkQueue(
54 IN PWORK_QUEUE_ENTRY WorkItem,
55 IN PLIST_ENTRY WorkQueue)
56 {
57 KIRQL OldIrql;
58 PWORK_QUEUE_ITEM ThreadToSpawn;
59
60 /* First of all, insert the item in the queue */
61 OldIrql = KeAcquireQueuedSpinLock(LockQueueWorkQueueLock);
62 InsertTailList(WorkQueue, &WorkItem->WorkQueueLinks);
63
64 /* Now, define whether we have to spawn a new work thread
65 * We will spawn a new one if:
66 * - There's no throttle in action
67 * - There's still at least one idle thread
68 */
69 ThreadToSpawn = NULL;
70 if (!CcQueueThrottle && !IsListEmpty(&CcIdleWorkerThreadList))
71 {
72 PLIST_ENTRY ListEntry;
73
74 /* Get the idle thread */
75 ListEntry = RemoveHeadList(&CcIdleWorkerThreadList);
76 ThreadToSpawn = CONTAINING_RECORD(ListEntry, WORK_QUEUE_ITEM, List);
77
78 /* We're going to have one more! */
79 CcNumberActiveWorkerThreads += 1;
80 }
81
82 KeReleaseQueuedSpinLock(LockQueueWorkQueueLock, OldIrql);
83
84 /* If we have a thread to spawn, do it! */
85 if (ThreadToSpawn != NULL)
86 {
87 /* We NULLify it to be consistent with initialization */
88 ThreadToSpawn->List.Flink = NULL;
89 ExQueueWorkItem(ThreadToSpawn, CriticalWorkQueue);
90 }
91 }
92
93 VOID
94 NTAPI
95 CcScanDpc(
96 IN PKDPC Dpc,
97 IN PVOID DeferredContext,
98 IN PVOID SystemArgument1,
99 IN PVOID SystemArgument2)
100 {
101 PWORK_QUEUE_ENTRY WorkItem;
102
103 /* Allocate a work item */
104 WorkItem = ExAllocateFromNPagedLookasideList(&CcTwilightLookasideList);
105 if (WorkItem == NULL)
106 {
107 LazyWriter.ScanActive = FALSE;
108 return;
109 }
110
111 /* And post it, it will be for lazy write */
112 WorkItem->Function = LazyWrite;
113 CcPostWorkQueue(WorkItem, &CcRegularWorkQueue);
114 }
115
116 VOID
117 CcLazyWriteScan(VOID)
118 {
119 ULONG Target;
120 ULONG Count;
121 KIRQL OldIrql;
122 PLIST_ENTRY ListEntry;
123 LIST_ENTRY ToPost;
124 PWORK_QUEUE_ENTRY WorkItem;
125
126 /* Do we have entries to queue after we're done? */
127 InitializeListHead(&ToPost);
128 OldIrql = KeAcquireQueuedSpinLock(LockQueueMasterLock);
129 if (LazyWriter.OtherWork)
130 {
131 while (!IsListEmpty(&CcPostTickWorkQueue))
132 {
133 ListEntry = RemoveHeadList(&CcPostTickWorkQueue);
134 WorkItem = CONTAINING_RECORD(ListEntry, WORK_QUEUE_ENTRY, WorkQueueLinks);
135 InsertTailList(&ToPost, &WorkItem->WorkQueueLinks);
136 }
137 LazyWriter.OtherWork = FALSE;
138 }
139 KeReleaseQueuedSpinLock(LockQueueMasterLock, OldIrql);
140
141 /* Our target is one-eighth of the dirty pages */
142 Target = CcTotalDirtyPages / 8;
143 if (Target != 0)
144 {
145 /* Flush! */
146 DPRINT1("Lazy writer starting (%d)\n", Target);
147 CcRosFlushDirtyPages(Target, &Count, FALSE, TRUE);
148
149 /* And update stats */
150 CcLazyWritePages += Count;
151 ++CcLazyWriteIos;
152 DPRINT1("Lazy writer done (%d)\n", Count);
153 }
154
155 /* If we have deferred writes, try them now! */
156 if (!IsListEmpty(&CcDeferredWrites))
157 {
158 CcPostDeferredWrites();
159 }
160
161 /* Post items that were due for end of run */
162 while (!IsListEmpty(&ToPost))
163 {
164 ListEntry = RemoveHeadList(&ToPost);
165 WorkItem = CONTAINING_RECORD(ListEntry, WORK_QUEUE_ENTRY, WorkQueueLinks);
166 CcPostWorkQueue(WorkItem, &CcRegularWorkQueue);
167 }
168
169 /* We're no longer active */
170 OldIrql = KeAcquireQueuedSpinLock(LockQueueMasterLock);
171 LazyWriter.ScanActive = FALSE;
172 KeReleaseQueuedSpinLock(LockQueueMasterLock, OldIrql);
173 }
174
175 VOID CcScheduleLazyWriteScan(
176 IN BOOLEAN NoDelay)
177 {
178 /* If no delay, immediately start lazy writer,
179 * no matter it was already started
180 */
181 if (NoDelay)
182 {
183 LazyWriter.ScanActive = TRUE;
184 KeSetTimer(&LazyWriter.ScanTimer, CcNoDelay, &LazyWriter.ScanDpc);
185 }
186 /* Otherwise, if it's not running, just wait three seconds to start it */
187 else if (!LazyWriter.ScanActive)
188 {
189 LazyWriter.ScanActive = TRUE;
190 KeSetTimer(&LazyWriter.ScanTimer, CcFirstDelay, &LazyWriter.ScanDpc);
191 }
192 /* Finally, already running, so queue for the next second */
193 else
194 {
195 KeSetTimer(&LazyWriter.ScanTimer, CcIdleDelay, &LazyWriter.ScanDpc);
196 }
197 }
198
199 VOID
200 NTAPI
201 CcWorkerThread(
202 IN PVOID Parameter)
203 {
204 KIRQL OldIrql;
205 BOOLEAN DropThrottle;
206 PWORK_QUEUE_ITEM Item;
207
208 /* Get back our thread item */
209 Item = Parameter;
210 /* And by default, don't touch throttle */
211 DropThrottle = FALSE;
212
213 /* Loop till we have jobs */
214 while (TRUE)
215 {
216 PWORK_QUEUE_ENTRY WorkItem;
217
218 /* Lock queues */
219 OldIrql = KeAcquireQueuedSpinLock(LockQueueWorkQueueLock);
220
221 /* If we have to touch throttle, reset it now! */
222 if (DropThrottle)
223 {
224 CcQueueThrottle = FALSE;
225 DropThrottle = FALSE;
226 }
227
228 /* Check first if we have read ahead to do */
229 if (IsListEmpty(&CcExpressWorkQueue))
230 {
231 /* If not, check regular queue */
232 if (IsListEmpty(&CcRegularWorkQueue))
233 {
234 break;
235 }
236 else
237 {
238 WorkItem = CONTAINING_RECORD(CcRegularWorkQueue.Flink, WORK_QUEUE_ENTRY, WorkQueueLinks);
239 }
240 }
241 else
242 {
243 WorkItem = CONTAINING_RECORD(CcExpressWorkQueue.Flink, WORK_QUEUE_ENTRY, WorkQueueLinks);
244 }
245
246 /* Get our work item, if someone is waiting for us to finish
247 * and we're not the only thread in queue
248 * then, quit running to let the others do
249 * and throttle so that noone starts till current activity is over
250 */
251 if (WorkItem->Function == SetDone && CcNumberActiveWorkerThreads > 1)
252 {
253 CcQueueThrottle = TRUE;
254 break;
255 }
256
257 /* Otherwise, remove current entry */
258 RemoveEntryList(&WorkItem->WorkQueueLinks);
259 KeReleaseQueuedSpinLock(LockQueueWorkQueueLock, OldIrql);
260
261 /* And handle it */
262 switch (WorkItem->Function)
263 {
264 case ReadAhead:
265 CcPerformReadAhead(WorkItem->Parameters.Read.FileObject);
266 break;
267
268 case LazyWrite:
269 CcLazyWriteScan();
270 break;
271
272 case SetDone:
273 KeSetEvent(WorkItem->Parameters.Event.Event, IO_NO_INCREMENT, FALSE);
274 DropThrottle = TRUE;
275 break;
276
277 default:
278 DPRINT1("Ignored item: %p (%d)\n", WorkItem, WorkItem->Function);
279 break;
280 }
281
282 /* And release the item */
283 ExFreeToNPagedLookasideList(&CcTwilightLookasideList, WorkItem);
284 }
285
286 /* Our thread is available again */
287 InsertTailList(&CcIdleWorkerThreadList, &Item->List);
288 /* One less worker */
289 --CcNumberActiveWorkerThreads;
290 KeReleaseQueuedSpinLock(LockQueueWorkQueueLock, OldIrql);
291 }
292
293 /*
294 * @implemented
295 */
296 NTSTATUS
297 NTAPI
298 CcWaitForCurrentLazyWriterActivity (
299 VOID)
300 {
301 KIRQL OldIrql;
302 KEVENT WaitEvent;
303 PWORK_QUEUE_ENTRY WorkItem;
304
305 /* Allocate a work item */
306 WorkItem = ExAllocateFromNPagedLookasideList(&CcTwilightLookasideList);
307 if (WorkItem == NULL)
308 {
309 return STATUS_INSUFFICIENT_RESOURCES;
310 }
311
312 /* We want lazy writer to set our event */
313 WorkItem->Function = SetDone;
314 KeInitializeEvent(&WaitEvent, NotificationEvent, FALSE);
315 WorkItem->Parameters.Event.Event = &WaitEvent;
316
317 /* Use the post tick queue */
318 OldIrql = KeAcquireQueuedSpinLock(LockQueueMasterLock);
319 InsertTailList(&CcPostTickWorkQueue, &WorkItem->WorkQueueLinks);
320
321 /* Inform the lazy writer it will have to handle the post tick queue */
322 LazyWriter.OtherWork = TRUE;
323 /* And if it's not running, queue a lazy writer run
324 * And start it NOW, we want the response now
325 */
326 if (!LazyWriter.ScanActive)
327 {
328 CcScheduleLazyWriteScan(TRUE);
329 }
330
331 KeReleaseQueuedSpinLock(LockQueueMasterLock, OldIrql);
332
333 /* And now, wait until lazy writer replies */
334 return KeWaitForSingleObject(&WaitEvent, Executive, KernelMode, FALSE, NULL);
335 }