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
;
105 IN PFN_NUMBER PageFrameIndex
110 MmReleasePageMemoryConsumer(ULONG Consumer
, PFN_NUMBER Page
)
112 PMM_ALLOCATION_REQUEST Request
;
118 DPRINT1("Tried to release page zero.\n");
119 KeBugCheck(MEMORY_MANAGEMENT
);
122 KeAcquireSpinLock(&AllocationListLock
, &OldIrql
);
123 if (MmGetReferenceCountPage(Page
) == 1)
125 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
126 if (IsListEmpty(&AllocationListHead
) || MmAvailablePages
< MiMinimumAvailablePages
)
128 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
129 if(Consumer
== MC_USER
) MmRemoveLRUUserPage(Page
);
130 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
131 MmDereferencePage(Page
);
132 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
136 Entry
= RemoveHeadList(&AllocationListHead
);
137 Request
= CONTAINING_RECORD(Entry
, MM_ALLOCATION_REQUEST
, ListEntry
);
138 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
139 if(Consumer
== MC_USER
) MmRemoveLRUUserPage(Page
);
140 MiZeroPhysicalPage(Page
);
141 Request
->Page
= Page
;
142 KeSetEvent(&Request
->Event
, IO_NO_INCREMENT
, FALSE
);
147 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
148 if(Consumer
== MC_USER
) MmRemoveLRUUserPage(Page
);
149 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
150 MmDereferencePage(Page
);
151 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
154 return(STATUS_SUCCESS
);
159 MiTrimMemoryConsumer(ULONG Consumer
)
164 Target
= MiMemoryConsumers
[Consumer
].PagesUsed
-
165 MiMemoryConsumers
[Consumer
].PagesTarget
;
171 if (MiMemoryConsumers
[Consumer
].Trim
!= NULL
)
173 MiMemoryConsumers
[Consumer
].Trim(Target
, 0, &NrFreedPages
);
178 MmTrimUserMemory(ULONG Target
, ULONG Priority
, PULONG NrFreedPages
)
180 PFN_NUMBER CurrentPage
;
186 CurrentPage
= MmGetLRUFirstUserPage();
187 while (CurrentPage
!= 0 && Target
> 0)
189 NextPage
= MmGetLRUNextUserPage(CurrentPage
);
191 Status
= MmPageOutPhysicalAddress(CurrentPage
);
192 if (NT_SUCCESS(Status
))
194 DPRINT("Succeeded\n");
199 CurrentPage
= NextPage
;
201 return(STATUS_SUCCESS
);
206 MmRebalanceMemoryConsumers(VOID
)
213 Target
= (MiMinimumAvailablePages
- MmAvailablePages
) + MiPagesRequired
;
214 Target
= max(Target
, (LONG
) MiMinimumPagesPerRun
);
216 for (i
= 0; i
< MC_MAXIMUM
&& Target
> 0; i
++)
218 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
220 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
221 if (!NT_SUCCESS(Status
))
223 KeBugCheck(MEMORY_MANAGEMENT
);
225 Target
= Target
- NrFreedPages
;
231 MiIsBalancerThread(VOID
)
233 return MiBalancerThreadHandle
!= NULL
&&
234 PsGetCurrentThread() == MiBalancerThreadId
.UniqueThread
;
239 MmRequestPageMemoryConsumer(ULONG Consumer
, BOOLEAN CanWait
,
240 PPFN_NUMBER AllocatedPage
)
247 * Make sure we don't exceed our individual target.
249 OldUsed
= InterlockedIncrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
250 if (OldUsed
>= (MiMemoryConsumers
[Consumer
].PagesTarget
- 1) &&
251 !MiIsBalancerThread())
255 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
256 return(STATUS_NO_MEMORY
);
258 MiTrimMemoryConsumer(Consumer
);
262 * Allocate always memory for the non paged pool and for the pager thread.
264 if ((Consumer
== MC_NPPOOL
) || (Consumer
== MC_SYSTEM
) || MiIsBalancerThread())
266 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
267 Page
= MmAllocPage(Consumer
);
268 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
271 KeBugCheck(NO_PAGES_AVAILABLE
);
273 *AllocatedPage
= Page
;
274 if (MmAvailablePages
<= MiMinimumAvailablePages
&&
275 MiBalancerThreadHandle
!= NULL
)
277 KeSetEvent(&MiBalancerEvent
, IO_NO_INCREMENT
, FALSE
);
279 return(STATUS_SUCCESS
);
283 * Make sure we don't exceed global targets.
285 if (MmAvailablePages
<= MiMinimumAvailablePages
)
287 MM_ALLOCATION_REQUEST Request
;
291 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
292 return(STATUS_NO_MEMORY
);
295 /* Insert an allocation request. */
298 KeInitializeEvent(&Request
.Event
, NotificationEvent
, FALSE
);
299 (void)InterlockedIncrementUL(&MiPagesRequired
);
301 KeAcquireSpinLock(&AllocationListLock
, &OldIrql
);
303 if (MiBalancerThreadHandle
!= NULL
)
305 KeSetEvent(&MiBalancerEvent
, IO_NO_INCREMENT
, FALSE
);
307 InsertTailList(&AllocationListHead
, &Request
.ListEntry
);
308 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
310 KeWaitForSingleObject(&Request
.Event
,
319 KeBugCheck(NO_PAGES_AVAILABLE
);
321 /* Update the Consumer and make the page active */
322 if(Consumer
== MC_USER
) MmInsertLRULastUserPage(Page
);
323 *AllocatedPage
= Page
;
324 (void)InterlockedDecrementUL(&MiPagesRequired
);
325 return(STATUS_SUCCESS
);
329 * Actually allocate the page.
331 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
332 Page
= MmAllocPage(Consumer
);
333 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
336 KeBugCheck(NO_PAGES_AVAILABLE
);
338 if(Consumer
== MC_USER
) MmInsertLRULastUserPage(Page
);
339 *AllocatedPage
= Page
;
341 return(STATUS_SUCCESS
);
345 MiBalancerThread(PVOID Unused
)
347 PVOID WaitObjects
[2];
356 WaitObjects
[0] = &MiBalancerEvent
;
357 WaitObjects
[1] = &MiBalancerTimer
;
361 Status
= KeWaitForMultipleObjects(2,
370 if (Status
== STATUS_SUCCESS
)
372 /* MiBalancerEvent */
373 while (MmAvailablePages
< MiMinimumAvailablePages
+ 5)
375 for (i
= 0; i
< MC_MAXIMUM
; i
++)
377 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
380 Status
= MiMemoryConsumers
[i
].Trim(MiMinimumPagesPerRun
, 0, &NrFreedPages
);
381 if (!NT_SUCCESS(Status
))
383 KeBugCheck(MEMORY_MANAGEMENT
);
388 InterlockedExchange(&MiBalancerWork
, 0);
390 else if (Status
== STATUS_SUCCESS
+ 1)
392 /* MiBalancerTimer */
393 ShouldRun
= MmAvailablePages
< MiMinimumAvailablePages
+ 5 ? TRUE
: FALSE
;
394 for (i
= 0; i
< MC_MAXIMUM
; i
++)
396 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
398 NrPagesUsed
= MiMemoryConsumers
[i
].PagesUsed
;
399 if (NrPagesUsed
> MiMemoryConsumers
[i
].PagesTarget
|| ShouldRun
)
401 if (NrPagesUsed
> MiMemoryConsumers
[i
].PagesTarget
)
403 Target
= max (NrPagesUsed
- MiMemoryConsumers
[i
].PagesTarget
,
404 MiMinimumPagesPerRun
);
408 Target
= MiMinimumPagesPerRun
;
411 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
412 if (!NT_SUCCESS(Status
))
414 KeBugCheck(MEMORY_MANAGEMENT
);
422 DPRINT1("KeWaitForMultipleObjects failed, status = %x\n", Status
);
423 KeBugCheck(MEMORY_MANAGEMENT
);
431 MiInitBalancerThread(VOID
)
435 #if !defined(__GNUC__)
437 LARGE_INTEGER dummyJunkNeeded
;
438 dummyJunkNeeded
.QuadPart
= -20000000; /* 2 sec */
443 KeInitializeEvent(&MiBalancerEvent
, SynchronizationEvent
, FALSE
);
444 KeInitializeTimerEx(&MiBalancerTimer
, SynchronizationTimer
);
445 KeSetTimerEx(&MiBalancerTimer
,
446 #if defined(__GNUC__)
447 (LARGE_INTEGER
)(LONGLONG
)-20000000LL, /* 2 sec */
454 Status
= PsCreateSystemThread(&MiBalancerThreadHandle
,
459 (PKSTART_ROUTINE
) MiBalancerThread
,
461 if (!NT_SUCCESS(Status
))
463 KeBugCheck(MEMORY_MANAGEMENT
);
466 Priority
= LOW_REALTIME_PRIORITY
+ 1;
467 NtSetInformationThread(MiBalancerThreadHandle
,