2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/cc/lazywrite.c
5 * PURPOSE: Cache manager
7 * PROGRAMMERS: Pierre Schweitzer (pierre@reactos.org)
10 /* INCLUDES *****************************************************************/
17 * - Amount of pages flushed by lazy writer
18 * - Number of times lazy writer ran
20 ULONG CcLazyWritePages
= 0;
21 ULONG CcLazyWriteIos
= 0;
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
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
;
50 /* FUNCTIONS *****************************************************************/
54 IN PWORK_QUEUE_ENTRY WorkItem
,
55 IN PLIST_ENTRY WorkQueue
)
58 PWORK_QUEUE_ITEM ThreadToSpawn
;
60 /* First of all, insert the item in the queue */
61 OldIrql
= KeAcquireQueuedSpinLock(LockQueueWorkQueueLock
);
62 InsertTailList(WorkQueue
, &WorkItem
->WorkQueueLinks
);
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
70 if (!CcQueueThrottle
&& !IsListEmpty(&CcIdleWorkerThreadList
))
72 PLIST_ENTRY ListEntry
;
74 /* Get the idle thread */
75 ListEntry
= RemoveHeadList(&CcIdleWorkerThreadList
);
76 ThreadToSpawn
= CONTAINING_RECORD(ListEntry
, WORK_QUEUE_ITEM
, List
);
78 /* We're going to have one more! */
79 CcNumberActiveWorkerThreads
+= 1;
82 KeReleaseQueuedSpinLock(LockQueueWorkQueueLock
, OldIrql
);
84 /* If we have a thread to spawn, do it! */
85 if (ThreadToSpawn
!= NULL
)
87 /* We NULLify it to be consistent with initialization */
88 ThreadToSpawn
->List
.Flink
= NULL
;
89 ExQueueWorkItem(ThreadToSpawn
, CriticalWorkQueue
);
97 IN PVOID DeferredContext
,
98 IN PVOID SystemArgument1
,
99 IN PVOID SystemArgument2
)
101 PWORK_QUEUE_ENTRY WorkItem
;
103 /* Allocate a work item */
104 WorkItem
= ExAllocateFromNPagedLookasideList(&CcTwilightLookasideList
);
105 if (WorkItem
== NULL
)
107 LazyWriter
.ScanActive
= FALSE
;
111 /* And post it, it will be for lazy write */
112 WorkItem
->Function
= LazyScan
;
113 CcPostWorkQueue(WorkItem
, &CcRegularWorkQueue
);
121 Target
= CcTotalDirtyPages
/ 8;
125 DPRINT("Lazy writer starting (%d)\n", Target
);
126 CcRosFlushDirtyPages(Target
, &Count
, FALSE
, TRUE
);
128 /* And update stats */
129 CcLazyWritePages
+= Count
;
131 DPRINT("Lazy writer done (%d)\n", Count
);
136 CcLazyWriteScan(VOID
)
140 PLIST_ENTRY ListEntry
;
142 PWORK_QUEUE_ENTRY WorkItem
;
144 /* Do we have entries to queue after we're done? */
145 InitializeListHead(&ToPost
);
146 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMasterLock
);
147 if (LazyWriter
.OtherWork
)
149 while (!IsListEmpty(&CcPostTickWorkQueue
))
151 ListEntry
= RemoveHeadList(&CcPostTickWorkQueue
);
152 WorkItem
= CONTAINING_RECORD(ListEntry
, WORK_QUEUE_ENTRY
, WorkQueueLinks
);
153 InsertTailList(&ToPost
, &WorkItem
->WorkQueueLinks
);
155 LazyWriter
.OtherWork
= FALSE
;
157 KeReleaseQueuedSpinLock(LockQueueMasterLock
, OldIrql
);
159 /* Our target is one-eighth of the dirty pages */
160 Target
= CcTotalDirtyPages
/ 8;
163 /* There is stuff to flush, schedule a write-behind operation */
165 /* Allocate a work item */
166 WorkItem
= ExAllocateFromNPagedLookasideList(&CcTwilightLookasideList
);
167 if (WorkItem
!= NULL
)
169 WorkItem
->Function
= WriteBehind
;
170 CcPostWorkQueue(WorkItem
, &CcRegularWorkQueue
);
174 /* Post items that were due for end of run */
175 while (!IsListEmpty(&ToPost
))
177 ListEntry
= RemoveHeadList(&ToPost
);
178 WorkItem
= CONTAINING_RECORD(ListEntry
, WORK_QUEUE_ENTRY
, WorkQueueLinks
);
179 CcPostWorkQueue(WorkItem
, &CcRegularWorkQueue
);
182 /* If we have deferred writes, try them now! */
183 if (!IsListEmpty(&CcDeferredWrites
))
185 CcPostDeferredWrites();
186 /* Reschedule immediately a lazy writer run
187 * Keep us active to have short idle delay
189 CcScheduleLazyWriteScan(FALSE
);
193 /* We're no longer active */
194 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMasterLock
);
195 LazyWriter
.ScanActive
= FALSE
;
196 KeReleaseQueuedSpinLock(LockQueueMasterLock
, OldIrql
);
200 VOID
CcScheduleLazyWriteScan(
203 /* If no delay, immediately start lazy writer,
204 * no matter it was already started
208 LazyWriter
.ScanActive
= TRUE
;
209 KeSetTimer(&LazyWriter
.ScanTimer
, CcNoDelay
, &LazyWriter
.ScanDpc
);
211 /* Otherwise, if it's not running, just wait three seconds to start it */
212 else if (!LazyWriter
.ScanActive
)
214 LazyWriter
.ScanActive
= TRUE
;
215 KeSetTimer(&LazyWriter
.ScanTimer
, CcFirstDelay
, &LazyWriter
.ScanDpc
);
217 /* Finally, already running, so queue for the next second */
220 KeSetTimer(&LazyWriter
.ScanTimer
, CcIdleDelay
, &LazyWriter
.ScanDpc
);
230 BOOLEAN DropThrottle
, WritePerformed
;
231 PWORK_QUEUE_ITEM Item
;
236 /* Get back our thread item */
238 /* And by default, don't touch throttle */
239 DropThrottle
= FALSE
;
240 /* No write performed */
241 WritePerformed
= FALSE
;
244 /* Top level IRP should be clean when started
245 * Save it to catch buggy drivers (or bugs!)
247 TopLevel
= IoGetTopLevelIrp();
248 if (TopLevel
!= NULL
)
250 DPRINT1("(%p) TopLevel IRP for this thread: %p\n", PsGetCurrentThread(), TopLevel
);
254 /* Loop till we have jobs */
257 PWORK_QUEUE_ENTRY WorkItem
;
260 OldIrql
= KeAcquireQueuedSpinLock(LockQueueWorkQueueLock
);
262 /* If we have to touch throttle, reset it now! */
265 CcQueueThrottle
= FALSE
;
266 DropThrottle
= FALSE
;
269 /* Check first if we have read ahead to do */
270 if (IsListEmpty(&CcExpressWorkQueue
))
272 /* If not, check regular queue */
273 if (IsListEmpty(&CcRegularWorkQueue
))
279 WorkItem
= CONTAINING_RECORD(CcRegularWorkQueue
.Flink
, WORK_QUEUE_ENTRY
, WorkQueueLinks
);
284 WorkItem
= CONTAINING_RECORD(CcExpressWorkQueue
.Flink
, WORK_QUEUE_ENTRY
, WorkQueueLinks
);
287 /* Get our work item, if someone is waiting for us to finish
288 * and we're not the only thread in queue
289 * then, quit running to let the others do
290 * and throttle so that noone starts till current activity is over
292 if (WorkItem
->Function
== SetDone
&& CcNumberActiveWorkerThreads
> 1)
294 CcQueueThrottle
= TRUE
;
298 /* Otherwise, remove current entry */
299 RemoveEntryList(&WorkItem
->WorkQueueLinks
);
300 KeReleaseQueuedSpinLock(LockQueueWorkQueueLock
, OldIrql
);
303 switch (WorkItem
->Function
)
306 CcPerformReadAhead(WorkItem
->Parameters
.Read
.FileObject
);
311 WritePerformed
= TRUE
;
319 KeSetEvent(WorkItem
->Parameters
.Event
.Event
, IO_NO_INCREMENT
, FALSE
);
324 DPRINT1("Ignored item: %p (%d)\n", WorkItem
, WorkItem
->Function
);
328 /* And release the item */
329 ExFreeToNPagedLookasideList(&CcTwilightLookasideList
, WorkItem
);
332 /* Our thread is available again */
333 InsertTailList(&CcIdleWorkerThreadList
, &Item
->List
);
334 /* One less worker */
335 --CcNumberActiveWorkerThreads
;
336 KeReleaseQueuedSpinLock(LockQueueWorkQueueLock
, OldIrql
);
338 /* If there are pending write openations and we have at least 20 dirty pages */
339 if (!IsListEmpty(&CcDeferredWrites
) && CcTotalDirtyPages
>= 20)
341 /* And if we performed a write operation previously, then
342 * stress the system a bit and reschedule a scan to find
352 /* Top level shouldn't have changed */
353 if (TopLevel
!= IoGetTopLevelIrp())
355 DPRINT1("(%p) Mismatching TopLevel: %p, %p\n", PsGetCurrentThread(), TopLevel
, IoGetTopLevelIrp());
365 CcWaitForCurrentLazyWriterActivity (
370 PWORK_QUEUE_ENTRY WorkItem
;
372 /* Allocate a work item */
373 WorkItem
= ExAllocateFromNPagedLookasideList(&CcTwilightLookasideList
);
374 if (WorkItem
== NULL
)
376 return STATUS_INSUFFICIENT_RESOURCES
;
379 /* We want lazy writer to set our event */
380 WorkItem
->Function
= SetDone
;
381 KeInitializeEvent(&WaitEvent
, NotificationEvent
, FALSE
);
382 WorkItem
->Parameters
.Event
.Event
= &WaitEvent
;
384 /* Use the post tick queue */
385 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMasterLock
);
386 InsertTailList(&CcPostTickWorkQueue
, &WorkItem
->WorkQueueLinks
);
388 /* Inform the lazy writer it will have to handle the post tick queue */
389 LazyWriter
.OtherWork
= TRUE
;
390 /* And if it's not running, queue a lazy writer run
391 * And start it NOW, we want the response now
393 if (!LazyWriter
.ScanActive
)
395 CcScheduleLazyWriteScan(TRUE
);
398 KeReleaseQueuedSpinLock(LockQueueMasterLock
, OldIrql
);
400 /* And now, wait until lazy writer replies */
401 return KeWaitForSingleObject(&WaitEvent
, Executive
, KernelMode
, FALSE
, NULL
);