2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/mm/balance.c
5 * PURPOSE: kernel memory managment functions
7 * PROGRAMMERS: David Welch (welch@cwcom.net)
10 /* INCLUDES *****************************************************************/
16 #if defined (ALLOC_PRAGMA)
17 #pragma alloc_text(INIT, MmInitializeBalancer)
18 #pragma alloc_text(INIT, MmInitializeMemoryConsumer)
19 #pragma alloc_text(INIT, MiInitBalancerThread)
23 /* TYPES ********************************************************************/
24 typedef struct _MM_ALLOCATION_REQUEST
30 MM_ALLOCATION_REQUEST
, *PMM_ALLOCATION_REQUEST
;
32 /* GLOBALS ******************************************************************/
34 MM_MEMORY_CONSUMER MiMemoryConsumers
[MC_MAXIMUM
];
35 static ULONG MiMinimumAvailablePages
;
36 static ULONG MiNrTotalPages
;
37 static LIST_ENTRY AllocationListHead
;
38 static KSPIN_LOCK AllocationListLock
;
39 static ULONG MiPagesRequired
= 0;
40 static ULONG MiMinimumPagesPerRun
= 10;
42 static CLIENT_ID MiBalancerThreadId
;
43 static HANDLE MiBalancerThreadHandle
= NULL
;
44 static KEVENT MiBalancerEvent
;
45 static KTIMER MiBalancerTimer
;
46 static LONG MiBalancerWork
= 0;
48 /* FUNCTIONS ****************************************************************/
50 VOID
MmPrintMemoryStatistic(VOID
)
52 DbgPrint("MC_CACHE %d, MC_USER %d, MC_PPOOL %d, MC_NPPOOL %d, MmAvailablePages %d\n",
53 MiMemoryConsumers
[MC_CACHE
].PagesUsed
, MiMemoryConsumers
[MC_USER
].PagesUsed
,
54 MiMemoryConsumers
[MC_PPOOL
].PagesUsed
, MiMemoryConsumers
[MC_NPPOOL
].PagesUsed
,
61 MmInitializeBalancer(ULONG NrAvailablePages
, ULONG NrSystemPages
)
63 memset(MiMemoryConsumers
, 0, sizeof(MiMemoryConsumers
));
64 InitializeListHead(&AllocationListHead
);
65 KeInitializeSpinLock(&AllocationListLock
);
67 MiNrTotalPages
= NrAvailablePages
;
70 MiMinimumAvailablePages
= 64;
71 if ((NrAvailablePages
+ NrSystemPages
) >= 8192)
73 MiMemoryConsumers
[MC_CACHE
].PagesTarget
= NrAvailablePages
/ 4 * 3;
75 else if ((NrAvailablePages
+ NrSystemPages
) >= 4096)
77 MiMemoryConsumers
[MC_CACHE
].PagesTarget
= NrAvailablePages
/ 3 * 2;
81 MiMemoryConsumers
[MC_CACHE
].PagesTarget
= NrAvailablePages
/ 8;
83 MiMemoryConsumers
[MC_USER
].PagesTarget
=
84 NrAvailablePages
- MiMinimumAvailablePages
;
85 MiMemoryConsumers
[MC_PPOOL
].PagesTarget
= NrAvailablePages
/ 2;
86 MiMemoryConsumers
[MC_NPPOOL
].PagesTarget
= 0xFFFFFFFF;
87 MiMemoryConsumers
[MC_NPPOOL
].PagesUsed
= NrSystemPages
;
88 MiMemoryConsumers
[MC_SYSTEM
].PagesTarget
= 0xFFFFFFFF;
89 MiMemoryConsumers
[MC_SYSTEM
].PagesUsed
= 0;
95 MmInitializeMemoryConsumer(ULONG Consumer
,
96 NTSTATUS (*Trim
)(ULONG Target
, ULONG Priority
,
99 MiMemoryConsumers
[Consumer
].Trim
= Trim
;
104 MmReleasePageMemoryConsumer(ULONG Consumer
, PFN_NUMBER Page
)
106 PMM_ALLOCATION_REQUEST Request
;
112 DPRINT1("Tried to release page zero.\n");
113 KeBugCheck(MEMORY_MANAGEMENT
);
116 KeAcquireSpinLock(&AllocationListLock
, &OldIrql
);
117 if (MmGetReferenceCountPage(Page
) == 1)
119 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
120 if (IsListEmpty(&AllocationListHead
) || MmAvailablePages
< MiMinimumAvailablePages
)
122 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
123 if(Consumer
== MC_USER
) MmRemoveLRUUserPage(Page
);
124 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
125 MmDereferencePage(Page
);
126 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
130 Entry
= RemoveHeadList(&AllocationListHead
);
131 Request
= CONTAINING_RECORD(Entry
, MM_ALLOCATION_REQUEST
, ListEntry
);
132 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
133 if(Consumer
== MC_USER
) MmRemoveLRUUserPage(Page
);
135 Request
->Page
= Page
;
136 KeSetEvent(&Request
->Event
, IO_NO_INCREMENT
, FALSE
);
141 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
142 if(Consumer
== MC_USER
) MmRemoveLRUUserPage(Page
);
143 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
144 MmDereferencePage(Page
);
145 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
148 return(STATUS_SUCCESS
);
153 MiTrimMemoryConsumer(ULONG Consumer
)
158 Target
= MiMemoryConsumers
[Consumer
].PagesUsed
-
159 MiMemoryConsumers
[Consumer
].PagesTarget
;
165 if (MiMemoryConsumers
[Consumer
].Trim
!= NULL
)
167 MiMemoryConsumers
[Consumer
].Trim(Target
, 0, &NrFreedPages
);
172 MmTrimUserMemory(ULONG Target
, ULONG Priority
, PULONG NrFreedPages
)
174 PFN_NUMBER CurrentPage
;
180 CurrentPage
= MmGetLRUFirstUserPage();
181 while (CurrentPage
!= 0 && Target
> 0)
183 NextPage
= MmGetLRUNextUserPage(CurrentPage
);
185 Status
= MmPageOutPhysicalAddress(CurrentPage
);
186 if (NT_SUCCESS(Status
))
188 DPRINT("Succeeded\n");
193 CurrentPage
= NextPage
;
195 return(STATUS_SUCCESS
);
200 MmRebalanceMemoryConsumers(VOID
)
207 Target
= (MiMinimumAvailablePages
- MmAvailablePages
) + MiPagesRequired
;
208 Target
= max(Target
, (LONG
) MiMinimumPagesPerRun
);
210 for (i
= 0; i
< MC_MAXIMUM
&& Target
> 0; i
++)
212 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
214 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
215 if (!NT_SUCCESS(Status
))
217 KeBugCheck(MEMORY_MANAGEMENT
);
219 Target
= Target
- NrFreedPages
;
225 MiIsBalancerThread(VOID
)
227 return MiBalancerThreadHandle
!= NULL
&&
228 PsGetCurrentThread() == MiBalancerThreadId
.UniqueThread
;
231 VOID NTAPI
MiSetConsumer(IN PFN_NUMBER Pfn
, IN ULONG Consumer
);
235 MmRequestPageMemoryConsumer(ULONG Consumer
, BOOLEAN CanWait
,
236 PPFN_NUMBER AllocatedPage
)
243 * Make sure we don't exceed our individual target.
245 OldUsed
= InterlockedIncrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
246 if (OldUsed
>= (MiMemoryConsumers
[Consumer
].PagesTarget
- 1) &&
247 !MiIsBalancerThread())
251 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
252 return(STATUS_NO_MEMORY
);
254 MiTrimMemoryConsumer(Consumer
);
258 * Allocate always memory for the non paged pool and for the pager thread.
260 if ((Consumer
== MC_NPPOOL
) || (Consumer
== MC_SYSTEM
) || MiIsBalancerThread())
262 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
263 Page
= MmAllocPage(Consumer
);
264 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
267 KeBugCheck(NO_PAGES_AVAILABLE
);
269 *AllocatedPage
= Page
;
270 if (MmAvailablePages
<= MiMinimumAvailablePages
&&
271 MiBalancerThreadHandle
!= NULL
)
273 KeSetEvent(&MiBalancerEvent
, IO_NO_INCREMENT
, FALSE
);
275 return(STATUS_SUCCESS
);
279 * Make sure we don't exceed global targets.
281 if (MmAvailablePages
<= MiMinimumAvailablePages
)
283 MM_ALLOCATION_REQUEST Request
;
287 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
288 return(STATUS_NO_MEMORY
);
291 /* Insert an allocation request. */
294 KeInitializeEvent(&Request
.Event
, NotificationEvent
, FALSE
);
295 (void)InterlockedIncrementUL(&MiPagesRequired
);
297 KeAcquireSpinLock(&AllocationListLock
, &OldIrql
);
299 if (MiBalancerThreadHandle
!= NULL
)
301 KeSetEvent(&MiBalancerEvent
, IO_NO_INCREMENT
, FALSE
);
303 InsertTailList(&AllocationListHead
, &Request
.ListEntry
);
304 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
306 KeWaitForSingleObject(&Request
.Event
,
315 KeBugCheck(NO_PAGES_AVAILABLE
);
317 /* Update the Consumer and make the page active */
318 MiSetConsumer(Page
, Consumer
);
319 if(Consumer
== MC_USER
) MmInsertLRULastUserPage(Page
);
320 *AllocatedPage
= Page
;
321 (void)InterlockedDecrementUL(&MiPagesRequired
);
322 return(STATUS_SUCCESS
);
326 * Actually allocate the page.
328 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
329 Page
= MmAllocPage(Consumer
);
330 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
333 KeBugCheck(NO_PAGES_AVAILABLE
);
335 if(Consumer
== MC_USER
) MmInsertLRULastUserPage(Page
);
336 *AllocatedPage
= Page
;
338 return(STATUS_SUCCESS
);
342 MiBalancerThread(PVOID Unused
)
344 PVOID WaitObjects
[2];
353 WaitObjects
[0] = &MiBalancerEvent
;
354 WaitObjects
[1] = &MiBalancerTimer
;
358 Status
= KeWaitForMultipleObjects(2,
367 if (Status
== STATUS_SUCCESS
)
369 /* MiBalancerEvent */
370 while (MmAvailablePages
< MiMinimumAvailablePages
+ 5)
372 for (i
= 0; i
< MC_MAXIMUM
; i
++)
374 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
377 Status
= MiMemoryConsumers
[i
].Trim(MiMinimumPagesPerRun
, 0, &NrFreedPages
);
378 if (!NT_SUCCESS(Status
))
380 KeBugCheck(MEMORY_MANAGEMENT
);
385 InterlockedExchange(&MiBalancerWork
, 0);
387 else if (Status
== STATUS_SUCCESS
+ 1)
389 /* MiBalancerTimer */
390 ShouldRun
= MmAvailablePages
< MiMinimumAvailablePages
+ 5 ? TRUE
: FALSE
;
391 for (i
= 0; i
< MC_MAXIMUM
; i
++)
393 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
395 NrPagesUsed
= MiMemoryConsumers
[i
].PagesUsed
;
396 if (NrPagesUsed
> MiMemoryConsumers
[i
].PagesTarget
|| ShouldRun
)
398 if (NrPagesUsed
> MiMemoryConsumers
[i
].PagesTarget
)
400 Target
= max (NrPagesUsed
- MiMemoryConsumers
[i
].PagesTarget
,
401 MiMinimumPagesPerRun
);
405 Target
= MiMinimumPagesPerRun
;
408 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
409 if (!NT_SUCCESS(Status
))
411 KeBugCheck(MEMORY_MANAGEMENT
);
419 DPRINT1("KeWaitForMultipleObjects failed, status = %x\n", Status
);
420 KeBugCheck(MEMORY_MANAGEMENT
);
428 MiInitBalancerThread(VOID
)
432 #if !defined(__GNUC__)
434 LARGE_INTEGER dummyJunkNeeded
;
435 dummyJunkNeeded
.QuadPart
= -20000000; /* 2 sec */
440 KeInitializeEvent(&MiBalancerEvent
, SynchronizationEvent
, FALSE
);
441 KeInitializeTimerEx(&MiBalancerTimer
, SynchronizationTimer
);
442 KeSetTimerEx(&MiBalancerTimer
,
443 #if defined(__GNUC__)
444 (LARGE_INTEGER
)(LONGLONG
)-20000000LL, /* 2 sec */
451 Status
= PsCreateSystemThread(&MiBalancerThreadHandle
,
456 (PKSTART_ROUTINE
) MiBalancerThread
,
458 if (!NT_SUCCESS(Status
))
460 KeBugCheck(MEMORY_MANAGEMENT
);
463 Priority
= LOW_REALTIME_PRIORITY
+ 1;
464 NtSetInformationThread(MiBalancerThreadHandle
,