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
= LazyWrite
;
113 CcPostWorkQueue(WorkItem
, &CcRegularWorkQueue
);
117 CcLazyWriteScan(VOID
)
122 PLIST_ENTRY ListEntry
;
124 PWORK_QUEUE_ENTRY WorkItem
;
126 /* Do we have entries to queue after we're done? */
127 InitializeListHead(&ToPost
);
128 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMasterLock
);
129 if (LazyWriter
.OtherWork
)
131 while (!IsListEmpty(&CcPostTickWorkQueue
))
133 ListEntry
= RemoveHeadList(&CcPostTickWorkQueue
);
134 WorkItem
= CONTAINING_RECORD(ListEntry
, WORK_QUEUE_ENTRY
, WorkQueueLinks
);
135 InsertTailList(&ToPost
, &WorkItem
->WorkQueueLinks
);
137 LazyWriter
.OtherWork
= FALSE
;
139 KeReleaseQueuedSpinLock(LockQueueMasterLock
, OldIrql
);
141 /* Our target is one-eighth of the dirty pages */
142 Target
= CcTotalDirtyPages
/ 8;
146 DPRINT("Lazy writer starting (%d)\n", Target
);
147 CcRosFlushDirtyPages(Target
, &Count
, FALSE
, TRUE
);
149 /* And update stats */
150 CcLazyWritePages
+= Count
;
152 DPRINT("Lazy writer done (%d)\n", Count
);
155 /* If we have deferred writes, try them now! */
156 if (!IsListEmpty(&CcDeferredWrites
))
158 CcPostDeferredWrites();
161 /* Post items that were due for end of run */
162 while (!IsListEmpty(&ToPost
))
164 ListEntry
= RemoveHeadList(&ToPost
);
165 WorkItem
= CONTAINING_RECORD(ListEntry
, WORK_QUEUE_ENTRY
, WorkQueueLinks
);
166 CcPostWorkQueue(WorkItem
, &CcRegularWorkQueue
);
169 /* We're no longer active */
170 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMasterLock
);
171 LazyWriter
.ScanActive
= FALSE
;
172 KeReleaseQueuedSpinLock(LockQueueMasterLock
, OldIrql
);
175 VOID
CcScheduleLazyWriteScan(
178 /* If no delay, immediately start lazy writer,
179 * no matter it was already started
183 LazyWriter
.ScanActive
= TRUE
;
184 KeSetTimer(&LazyWriter
.ScanTimer
, CcNoDelay
, &LazyWriter
.ScanDpc
);
186 /* Otherwise, if it's not running, just wait three seconds to start it */
187 else if (!LazyWriter
.ScanActive
)
189 LazyWriter
.ScanActive
= TRUE
;
190 KeSetTimer(&LazyWriter
.ScanTimer
, CcFirstDelay
, &LazyWriter
.ScanDpc
);
192 /* Finally, already running, so queue for the next second */
195 KeSetTimer(&LazyWriter
.ScanTimer
, CcIdleDelay
, &LazyWriter
.ScanDpc
);
205 BOOLEAN DropThrottle
;
206 PWORK_QUEUE_ITEM Item
;
211 /* Get back our thread item */
213 /* And by default, don't touch throttle */
214 DropThrottle
= FALSE
;
217 /* Top level IRP should be clean when started
218 * Save it to catch buggy drivers (or bugs!)
220 TopLevel
= IoGetTopLevelIrp();
221 if (TopLevel
!= NULL
)
223 DPRINT1("(%p) TopLevel IRP for this thread: %p\n", PsGetCurrentThread(), TopLevel
);
227 /* Loop till we have jobs */
230 PWORK_QUEUE_ENTRY WorkItem
;
233 OldIrql
= KeAcquireQueuedSpinLock(LockQueueWorkQueueLock
);
235 /* If we have to touch throttle, reset it now! */
238 CcQueueThrottle
= FALSE
;
239 DropThrottle
= FALSE
;
242 /* Check first if we have read ahead to do */
243 if (IsListEmpty(&CcExpressWorkQueue
))
245 /* If not, check regular queue */
246 if (IsListEmpty(&CcRegularWorkQueue
))
252 WorkItem
= CONTAINING_RECORD(CcRegularWorkQueue
.Flink
, WORK_QUEUE_ENTRY
, WorkQueueLinks
);
257 WorkItem
= CONTAINING_RECORD(CcExpressWorkQueue
.Flink
, WORK_QUEUE_ENTRY
, WorkQueueLinks
);
260 /* Get our work item, if someone is waiting for us to finish
261 * and we're not the only thread in queue
262 * then, quit running to let the others do
263 * and throttle so that noone starts till current activity is over
265 if (WorkItem
->Function
== SetDone
&& CcNumberActiveWorkerThreads
> 1)
267 CcQueueThrottle
= TRUE
;
271 /* Otherwise, remove current entry */
272 RemoveEntryList(&WorkItem
->WorkQueueLinks
);
273 KeReleaseQueuedSpinLock(LockQueueWorkQueueLock
, OldIrql
);
276 switch (WorkItem
->Function
)
279 CcPerformReadAhead(WorkItem
->Parameters
.Read
.FileObject
);
287 KeSetEvent(WorkItem
->Parameters
.Event
.Event
, IO_NO_INCREMENT
, FALSE
);
292 DPRINT1("Ignored item: %p (%d)\n", WorkItem
, WorkItem
->Function
);
296 /* And release the item */
297 ExFreeToNPagedLookasideList(&CcTwilightLookasideList
, WorkItem
);
300 /* Our thread is available again */
301 InsertTailList(&CcIdleWorkerThreadList
, &Item
->List
);
302 /* One less worker */
303 --CcNumberActiveWorkerThreads
;
304 KeReleaseQueuedSpinLock(LockQueueWorkQueueLock
, OldIrql
);
307 /* Top level shouldn't have changed */
308 if (TopLevel
!= IoGetTopLevelIrp())
310 DPRINT1("(%p) Mismatching TopLevel: %p, %p\n", PsGetCurrentThread(), TopLevel
, IoGetTopLevelIrp());
320 CcWaitForCurrentLazyWriterActivity (
325 PWORK_QUEUE_ENTRY WorkItem
;
327 /* Allocate a work item */
328 WorkItem
= ExAllocateFromNPagedLookasideList(&CcTwilightLookasideList
);
329 if (WorkItem
== NULL
)
331 return STATUS_INSUFFICIENT_RESOURCES
;
334 /* We want lazy writer to set our event */
335 WorkItem
->Function
= SetDone
;
336 KeInitializeEvent(&WaitEvent
, NotificationEvent
, FALSE
);
337 WorkItem
->Parameters
.Event
.Event
= &WaitEvent
;
339 /* Use the post tick queue */
340 OldIrql
= KeAcquireQueuedSpinLock(LockQueueMasterLock
);
341 InsertTailList(&CcPostTickWorkQueue
, &WorkItem
->WorkQueueLinks
);
343 /* Inform the lazy writer it will have to handle the post tick queue */
344 LazyWriter
.OtherWork
= TRUE
;
345 /* And if it's not running, queue a lazy writer run
346 * And start it NOW, we want the response now
348 if (!LazyWriter
.ScanActive
)
350 CcScheduleLazyWriteScan(TRUE
);
353 KeReleaseQueuedSpinLock(LockQueueMasterLock
, OldIrql
);
355 /* And now, wait until lazy writer replies */
356 return KeWaitForSingleObject(&WaitEvent
, Executive
, KernelMode
, FALSE
, NULL
);