[NTOSKRNL] Drop the always running thread for lazy writer.
[reactos.git] / ntoskrnl / cc / cacheman.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/cc/cacheman.c
5 * PURPOSE: Cache manager
6 *
7 * PROGRAMMERS: David Welch (welch@cwcom.net)
8 * Pierre Schweitzer (pierre@reactos.org)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <debug.h>
16
17 BOOLEAN CcPfEnablePrefetcher;
18 PFSN_PREFETCHER_GLOBALS CcPfGlobals;
19 MM_SYSTEMSIZE CcCapturedSystemSize;
20
21 static ULONG BugCheckFileId = 0x4 << 16;
22
23 /* FUNCTIONS *****************************************************************/
24
25 VOID
26 NTAPI
27 INIT_FUNCTION
28 CcPfInitializePrefetcher(VOID)
29 {
30 /* Notify debugger */
31 DbgPrintEx(DPFLTR_PREFETCHER_ID,
32 DPFLTR_TRACE_LEVEL,
33 "CCPF: InitializePrefetecher()\n");
34
35 /* Setup the Prefetcher Data */
36 InitializeListHead(&CcPfGlobals.ActiveTraces);
37 InitializeListHead(&CcPfGlobals.CompletedTraces);
38 ExInitializeFastMutex(&CcPfGlobals.CompletedTracesLock);
39
40 /* FIXME: Setup the rest of the prefetecher */
41 }
42
43 BOOLEAN
44 NTAPI
45 INIT_FUNCTION
46 CcInitializeCacheManager(VOID)
47 {
48 ULONG Thread;
49
50 CcInitView();
51
52 /* Initialize lazy-writer lists */
53 InitializeListHead(&CcIdleWorkerThreadList);
54 InitializeListHead(&CcRegularWorkQueue);
55
56 /* Define lazy writer threshold and the amount of workers,
57 * depending on the system type
58 */
59 CcCapturedSystemSize = MmQuerySystemSize();
60 switch (CcCapturedSystemSize)
61 {
62 case MmSmallSystem:
63 CcNumberWorkerThreads = ExCriticalWorkerThreads - 1;
64 CcDirtyPageThreshold = MmNumberOfPhysicalPages / 8;
65 break;
66
67 case MmMediumSystem:
68 CcNumberWorkerThreads = ExCriticalWorkerThreads - 1;
69 CcDirtyPageThreshold = MmNumberOfPhysicalPages / 4;
70 break;
71
72 case MmLargeSystem:
73 CcNumberWorkerThreads = ExCriticalWorkerThreads - 2;
74 CcDirtyPageThreshold = MmNumberOfPhysicalPages / 8 + MmNumberOfPhysicalPages / 4;
75 break;
76
77 default:
78 CcNumberWorkerThreads = 1;
79 CcDirtyPageThreshold = MmNumberOfPhysicalPages / 8;
80 break;
81 }
82
83 /* Allocate a work item for all our threads */
84 for (Thread = 0; Thread < CcNumberWorkerThreads; ++Thread)
85 {
86 PWORK_QUEUE_ITEM Item;
87
88 Item = ExAllocatePoolWithTag(NonPagedPool, sizeof(WORK_QUEUE_ITEM), 'qWcC');
89 if (Item == NULL)
90 {
91 CcBugCheck(0, 0, 0);
92 }
93
94 /* By default, it's obviously idle */
95 ExInitializeWorkItem(Item, CcWorkerThread, Item);
96 InsertTailList(&CcIdleWorkerThreadList, &Item->List);
97 }
98
99 /* Initialize our lazy writer */
100 RtlZeroMemory(&LazyWriter, sizeof(LazyWriter));
101 InitializeListHead(&LazyWriter.WorkQueue);
102 /* Delay activation of the lazy writer */
103 KeInitializeDpc(&LazyWriter.ScanDpc, CcScanDpc, NULL);
104 KeInitializeTimer(&LazyWriter.ScanTimer);
105
106 /* Lookaside list for our work items */
107 ExInitializeNPagedLookasideList(&CcTwilightLookasideList, NULL, NULL, 0, sizeof(WORK_QUEUE_ENTRY), 'KWcC', 0);
108
109 /* HACK: for lazy writer watching */
110 KeInitializeEvent(&iLazyWriterNotify, NotificationEvent, FALSE);
111
112 return TRUE;
113 }
114
115 VOID
116 NTAPI
117 CcShutdownSystem(VOID)
118 {
119 /* NOTHING TO DO */
120 }
121
122 /*
123 * @unimplemented
124 */
125 LARGE_INTEGER
126 NTAPI
127 CcGetFlushedValidData (
128 IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
129 IN BOOLEAN BcbListHeld
130 )
131 {
132 LARGE_INTEGER i;
133
134 UNIMPLEMENTED;
135
136 i.QuadPart = 0;
137 return i;
138 }
139
140 /*
141 * @unimplemented
142 */
143 PVOID
144 NTAPI
145 CcRemapBcb (
146 IN PVOID Bcb
147 )
148 {
149 UNIMPLEMENTED;
150
151 return 0;
152 }
153
154 /*
155 * @unimplemented
156 */
157 VOID
158 NTAPI
159 CcScheduleReadAhead (
160 IN PFILE_OBJECT FileObject,
161 IN PLARGE_INTEGER FileOffset,
162 IN ULONG Length
163 )
164 {
165 UNIMPLEMENTED;
166 }
167
168 /*
169 * @unimplemented
170 */
171 VOID
172 NTAPI
173 CcSetAdditionalCacheAttributes (
174 IN PFILE_OBJECT FileObject,
175 IN BOOLEAN DisableReadAhead,
176 IN BOOLEAN DisableWriteBehind
177 )
178 {
179 CCTRACE(CC_API_DEBUG, "FileObject=%p DisableReadAhead=%d DisableWriteBehind=%d\n",
180 FileObject, DisableReadAhead, DisableWriteBehind);
181
182 UNIMPLEMENTED;
183 }
184
185 /*
186 * @unimplemented
187 */
188 VOID
189 NTAPI
190 CcSetBcbOwnerPointer (
191 IN PVOID Bcb,
192 IN PVOID Owner
193 )
194 {
195 PINTERNAL_BCB iBcb = Bcb;
196
197 CCTRACE(CC_API_DEBUG, "Bcb=%p Owner=%p\n",
198 Bcb, Owner);
199
200 if (!ExIsResourceAcquiredExclusiveLite(&iBcb->Lock) && !ExIsResourceAcquiredSharedLite(&iBcb->Lock))
201 {
202 DPRINT1("Current thread doesn't own resource!\n");
203 return;
204 }
205
206 ExSetResourceOwnerPointer(&iBcb->Lock, Owner);
207 }
208
209 /*
210 * @implemented
211 */
212 VOID
213 NTAPI
214 CcSetDirtyPageThreshold (
215 IN PFILE_OBJECT FileObject,
216 IN ULONG DirtyPageThreshold
217 )
218 {
219 PFSRTL_COMMON_FCB_HEADER Fcb;
220 PROS_SHARED_CACHE_MAP SharedCacheMap;
221
222 CCTRACE(CC_API_DEBUG, "FileObject=%p DirtyPageThreshold=%lu\n",
223 FileObject, DirtyPageThreshold);
224
225 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
226 if (SharedCacheMap != NULL)
227 {
228 SharedCacheMap->DirtyPageThreshold = DirtyPageThreshold;
229 }
230
231 Fcb = FileObject->FsContext;
232 if (!BooleanFlagOn(Fcb->Flags, FSRTL_FLAG_LIMIT_MODIFIED_PAGES))
233 {
234 SetFlag(Fcb->Flags, FSRTL_FLAG_LIMIT_MODIFIED_PAGES);
235 }
236 }
237
238 /*
239 * @unimplemented
240 */
241 VOID
242 NTAPI
243 CcSetReadAheadGranularity (
244 IN PFILE_OBJECT FileObject,
245 IN ULONG Granularity
246 )
247 {
248 static ULONG Warn;
249
250 CCTRACE(CC_API_DEBUG, "FileObject=%p Granularity=%lu\n",
251 FileObject, Granularity);
252
253 if (!Warn++) UNIMPLEMENTED;
254 }