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 ****************************************************************/
53 MmInitializeBalancer(ULONG NrAvailablePages
, ULONG NrSystemPages
)
55 memset(MiMemoryConsumers
, 0, sizeof(MiMemoryConsumers
));
56 InitializeListHead(&AllocationListHead
);
57 KeInitializeSpinLock(&AllocationListLock
);
59 MiNrTotalPages
= NrAvailablePages
;
62 MiMinimumAvailablePages
= 64;
63 if ((NrAvailablePages
+ NrSystemPages
) >= 8192)
65 MiMemoryConsumers
[MC_CACHE
].PagesTarget
= NrAvailablePages
/ 4 * 3;
67 else if ((NrAvailablePages
+ NrSystemPages
) >= 4096)
69 MiMemoryConsumers
[MC_CACHE
].PagesTarget
= NrAvailablePages
/ 3 * 2;
73 MiMemoryConsumers
[MC_CACHE
].PagesTarget
= NrAvailablePages
/ 8;
75 MiMemoryConsumers
[MC_USER
].PagesTarget
= NrAvailablePages
- MiMinimumAvailablePages
;
81 MmInitializeMemoryConsumer(ULONG Consumer
,
82 NTSTATUS (*Trim
)(ULONG Target
, ULONG Priority
,
85 MiMemoryConsumers
[Consumer
].Trim
= Trim
;
91 IN PFN_NUMBER PageFrameIndex
96 MmReleasePageMemoryConsumer(ULONG Consumer
, PFN_NUMBER Page
)
98 PMM_ALLOCATION_REQUEST Request
;
104 DPRINT1("Tried to release page zero.\n");
105 KeBugCheck(MEMORY_MANAGEMENT
);
108 KeAcquireSpinLock(&AllocationListLock
, &OldIrql
);
109 if (MmGetReferenceCountPage(Page
) == 1)
111 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
112 if (IsListEmpty(&AllocationListHead
) || MmAvailablePages
< MiMinimumAvailablePages
)
114 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
115 if(Consumer
== MC_USER
) MmRemoveLRUUserPage(Page
);
116 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
117 MmDereferencePage(Page
);
118 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
122 Entry
= RemoveHeadList(&AllocationListHead
);
123 Request
= CONTAINING_RECORD(Entry
, MM_ALLOCATION_REQUEST
, ListEntry
);
124 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
125 MiZeroPhysicalPage(Page
);
126 Request
->Page
= Page
;
127 KeSetEvent(&Request
->Event
, IO_NO_INCREMENT
, FALSE
);
132 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
133 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
134 MmDereferencePage(Page
);
135 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
138 return(STATUS_SUCCESS
);
143 MiTrimMemoryConsumer(ULONG Consumer
)
148 Target
= MiMemoryConsumers
[Consumer
].PagesUsed
-
149 MiMemoryConsumers
[Consumer
].PagesTarget
;
155 if (MiMemoryConsumers
[Consumer
].Trim
!= NULL
)
157 MiMemoryConsumers
[Consumer
].Trim(Target
, 0, &NrFreedPages
);
162 MmTrimUserMemory(ULONG Target
, ULONG Priority
, PULONG NrFreedPages
)
164 PFN_NUMBER CurrentPage
;
170 CurrentPage
= MmGetLRUFirstUserPage();
171 while (CurrentPage
!= 0 && Target
> 0)
173 Status
= MmPageOutPhysicalAddress(CurrentPage
);
174 if (NT_SUCCESS(Status
))
176 DPRINT("Succeeded\n");
181 NextPage
= MmGetLRUNextUserPage(CurrentPage
);
182 if (NextPage
<= CurrentPage
)
184 /* We wrapped around, so we're done */
187 CurrentPage
= NextPage
;
190 return STATUS_SUCCESS
;
195 MmRebalanceMemoryConsumers(VOID
)
202 Target
= (ULONG
)(MiMinimumAvailablePages
- MmAvailablePages
) + MiPagesRequired
;
203 Target
= max(Target
, (LONG
) MiMinimumPagesPerRun
);
205 for (i
= 0; i
< MC_MAXIMUM
&& Target
> 0; i
++)
207 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
209 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
210 if (!NT_SUCCESS(Status
))
212 KeBugCheck(MEMORY_MANAGEMENT
);
214 Target
= Target
- NrFreedPages
;
220 MiIsBalancerThread(VOID
)
222 return (MiBalancerThreadHandle
!= NULL
) &&
223 (PsGetCurrentThreadId() == MiBalancerThreadId
.UniqueThread
);
228 MmRequestPageMemoryConsumer(ULONG Consumer
, BOOLEAN CanWait
,
229 PPFN_NUMBER AllocatedPage
)
236 * Make sure we don't exceed our individual target.
238 OldUsed
= InterlockedIncrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
239 if (OldUsed
>= (MiMemoryConsumers
[Consumer
].PagesTarget
- 1) &&
240 !MiIsBalancerThread())
244 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
245 return(STATUS_NO_MEMORY
);
247 MiTrimMemoryConsumer(Consumer
);
251 * Allocate always memory for the non paged pool and for the pager thread.
253 if ((Consumer
== MC_SYSTEM
) || MiIsBalancerThread())
255 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
256 Page
= MmAllocPage(Consumer
);
257 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
260 KeBugCheck(NO_PAGES_AVAILABLE
);
262 if (Consumer
== MC_USER
) MmInsertLRULastUserPage(Page
);
263 *AllocatedPage
= Page
;
264 if (MmAvailablePages
<= MiMinimumAvailablePages
&&
265 MiBalancerThreadHandle
!= NULL
)
267 KeSetEvent(&MiBalancerEvent
, IO_NO_INCREMENT
, FALSE
);
269 return(STATUS_SUCCESS
);
273 * Make sure we don't exceed global targets.
275 if (MmAvailablePages
<= MiMinimumAvailablePages
)
277 MM_ALLOCATION_REQUEST Request
;
281 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
282 return(STATUS_NO_MEMORY
);
285 /* Insert an allocation request. */
288 KeInitializeEvent(&Request
.Event
, NotificationEvent
, FALSE
);
289 (void)InterlockedIncrementUL(&MiPagesRequired
);
291 KeAcquireSpinLock(&AllocationListLock
, &OldIrql
);
293 if (MiBalancerThreadHandle
!= NULL
)
295 KeSetEvent(&MiBalancerEvent
, IO_NO_INCREMENT
, FALSE
);
297 InsertTailList(&AllocationListHead
, &Request
.ListEntry
);
298 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
300 KeWaitForSingleObject(&Request
.Event
,
309 KeBugCheck(NO_PAGES_AVAILABLE
);
312 *AllocatedPage
= Page
;
313 (void)InterlockedDecrementUL(&MiPagesRequired
);
315 return(STATUS_SUCCESS
);
319 * Actually allocate the page.
321 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
322 Page
= MmAllocPage(Consumer
);
323 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
326 KeBugCheck(NO_PAGES_AVAILABLE
);
328 if(Consumer
== MC_USER
) MmInsertLRULastUserPage(Page
);
329 *AllocatedPage
= Page
;
331 return(STATUS_SUCCESS
);
335 MiBalancerThread(PVOID Unused
)
337 PVOID WaitObjects
[2];
346 WaitObjects
[0] = &MiBalancerEvent
;
347 WaitObjects
[1] = &MiBalancerTimer
;
351 Status
= KeWaitForMultipleObjects(2,
360 if (Status
== STATUS_SUCCESS
)
362 /* MiBalancerEvent */
363 while (MmAvailablePages
< MiMinimumAvailablePages
+ 5)
365 for (i
= 0; i
< MC_MAXIMUM
; i
++)
367 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
370 Status
= MiMemoryConsumers
[i
].Trim(MiMinimumPagesPerRun
, 0, &NrFreedPages
);
371 if (!NT_SUCCESS(Status
))
373 KeBugCheck(MEMORY_MANAGEMENT
);
378 InterlockedExchange(&MiBalancerWork
, 0);
380 else if (Status
== STATUS_SUCCESS
+ 1)
382 /* MiBalancerTimer */
383 ShouldRun
= MmAvailablePages
< MiMinimumAvailablePages
+ 5 ? TRUE
: FALSE
;
384 for (i
= 0; i
< MC_MAXIMUM
; i
++)
386 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
388 NrPagesUsed
= MiMemoryConsumers
[i
].PagesUsed
;
389 if (NrPagesUsed
> MiMemoryConsumers
[i
].PagesTarget
|| ShouldRun
)
391 if (NrPagesUsed
> MiMemoryConsumers
[i
].PagesTarget
)
393 Target
= max (NrPagesUsed
- MiMemoryConsumers
[i
].PagesTarget
,
394 MiMinimumPagesPerRun
);
398 Target
= MiMinimumPagesPerRun
;
401 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
402 if (!NT_SUCCESS(Status
))
404 KeBugCheck(MEMORY_MANAGEMENT
);
412 DPRINT1("KeWaitForMultipleObjects failed, status = %x\n", Status
);
413 KeBugCheck(MEMORY_MANAGEMENT
);
421 MiInitBalancerThread(VOID
)
425 #if !defined(__GNUC__)
427 LARGE_INTEGER dummyJunkNeeded
;
428 dummyJunkNeeded
.QuadPart
= -20000000; /* 2 sec */
433 KeInitializeEvent(&MiBalancerEvent
, SynchronizationEvent
, FALSE
);
434 KeInitializeTimerEx(&MiBalancerTimer
, SynchronizationTimer
);
435 KeSetTimerEx(&MiBalancerTimer
,
436 #if defined(__GNUC__)
437 (LARGE_INTEGER
)(LONGLONG
)-20000000LL, /* 2 sec */
444 Status
= PsCreateSystemThread(&MiBalancerThreadHandle
,
449 (PKSTART_ROUTINE
) MiBalancerThread
,
451 if (!NT_SUCCESS(Status
))
453 KeBugCheck(MEMORY_MANAGEMENT
);
456 Priority
= LOW_REALTIME_PRIORITY
+ 1;
457 NtSetInformationThread(MiBalancerThreadHandle
,