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 if(Consumer
== MC_USER
) MmRemoveLRUUserPage(Page
);
126 MiZeroPhysicalPage(Page
);
127 Request
->Page
= Page
;
128 KeSetEvent(&Request
->Event
, IO_NO_INCREMENT
, FALSE
);
133 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
134 if(Consumer
== MC_USER
) MmRemoveLRUUserPage(Page
);
135 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
136 MmDereferencePage(Page
);
137 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
140 return(STATUS_SUCCESS
);
145 MiTrimMemoryConsumer(ULONG Consumer
)
150 Target
= MiMemoryConsumers
[Consumer
].PagesUsed
-
151 MiMemoryConsumers
[Consumer
].PagesTarget
;
157 if (MiMemoryConsumers
[Consumer
].Trim
!= NULL
)
159 MiMemoryConsumers
[Consumer
].Trim(Target
, 0, &NrFreedPages
);
164 MmTrimUserMemory(ULONG Target
, ULONG Priority
, PULONG NrFreedPages
)
166 PFN_NUMBER CurrentPage
;
172 CurrentPage
= MmGetLRUFirstUserPage();
173 while (CurrentPage
!= 0 && Target
> 0)
175 NextPage
= MmGetLRUNextUserPage(CurrentPage
);
177 Status
= MmPageOutPhysicalAddress(CurrentPage
);
178 if (NT_SUCCESS(Status
))
180 DPRINT("Succeeded\n");
185 CurrentPage
= NextPage
;
187 return(STATUS_SUCCESS
);
192 MmRebalanceMemoryConsumers(VOID
)
199 Target
= (MiMinimumAvailablePages
- MmAvailablePages
) + MiPagesRequired
;
200 Target
= max(Target
, (LONG
) MiMinimumPagesPerRun
);
202 for (i
= 0; i
< MC_MAXIMUM
&& Target
> 0; i
++)
204 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
206 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
207 if (!NT_SUCCESS(Status
))
209 KeBugCheck(MEMORY_MANAGEMENT
);
211 Target
= Target
- NrFreedPages
;
217 MiIsBalancerThread(VOID
)
219 return MiBalancerThreadHandle
!= NULL
&&
220 PsGetCurrentThread() == MiBalancerThreadId
.UniqueThread
;
225 MmRequestPageMemoryConsumer(ULONG Consumer
, BOOLEAN CanWait
,
226 PPFN_NUMBER AllocatedPage
)
233 * Make sure we don't exceed our individual target.
235 OldUsed
= InterlockedIncrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
236 if (OldUsed
>= (MiMemoryConsumers
[Consumer
].PagesTarget
- 1) &&
237 !MiIsBalancerThread())
241 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
242 return(STATUS_NO_MEMORY
);
244 MiTrimMemoryConsumer(Consumer
);
248 * Allocate always memory for the non paged pool and for the pager thread.
250 if ((Consumer
== MC_SYSTEM
) || MiIsBalancerThread())
252 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
253 Page
= MmAllocPage(Consumer
);
254 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
257 KeBugCheck(NO_PAGES_AVAILABLE
);
259 *AllocatedPage
= Page
;
260 if (MmAvailablePages
<= MiMinimumAvailablePages
&&
261 MiBalancerThreadHandle
!= NULL
)
263 KeSetEvent(&MiBalancerEvent
, IO_NO_INCREMENT
, FALSE
);
265 return(STATUS_SUCCESS
);
269 * Make sure we don't exceed global targets.
271 if (MmAvailablePages
<= MiMinimumAvailablePages
)
273 MM_ALLOCATION_REQUEST Request
;
277 (void)InterlockedDecrementUL(&MiMemoryConsumers
[Consumer
].PagesUsed
);
278 return(STATUS_NO_MEMORY
);
281 /* Insert an allocation request. */
284 KeInitializeEvent(&Request
.Event
, NotificationEvent
, FALSE
);
285 (void)InterlockedIncrementUL(&MiPagesRequired
);
287 KeAcquireSpinLock(&AllocationListLock
, &OldIrql
);
289 if (MiBalancerThreadHandle
!= NULL
)
291 KeSetEvent(&MiBalancerEvent
, IO_NO_INCREMENT
, FALSE
);
293 InsertTailList(&AllocationListHead
, &Request
.ListEntry
);
294 KeReleaseSpinLock(&AllocationListLock
, OldIrql
);
296 KeWaitForSingleObject(&Request
.Event
,
305 KeBugCheck(NO_PAGES_AVAILABLE
);
307 /* Update the Consumer and make the page active */
308 if(Consumer
== MC_USER
) MmInsertLRULastUserPage(Page
);
309 *AllocatedPage
= Page
;
310 (void)InterlockedDecrementUL(&MiPagesRequired
);
311 return(STATUS_SUCCESS
);
315 * Actually allocate the page.
317 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
318 Page
= MmAllocPage(Consumer
);
319 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
322 KeBugCheck(NO_PAGES_AVAILABLE
);
324 if(Consumer
== MC_USER
) MmInsertLRULastUserPage(Page
);
325 *AllocatedPage
= Page
;
327 return(STATUS_SUCCESS
);
331 MiBalancerThread(PVOID Unused
)
333 PVOID WaitObjects
[2];
342 WaitObjects
[0] = &MiBalancerEvent
;
343 WaitObjects
[1] = &MiBalancerTimer
;
347 Status
= KeWaitForMultipleObjects(2,
356 if (Status
== STATUS_SUCCESS
)
358 /* MiBalancerEvent */
359 while (MmAvailablePages
< MiMinimumAvailablePages
+ 5)
361 for (i
= 0; i
< MC_MAXIMUM
; i
++)
363 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
366 Status
= MiMemoryConsumers
[i
].Trim(MiMinimumPagesPerRun
, 0, &NrFreedPages
);
367 if (!NT_SUCCESS(Status
))
369 KeBugCheck(MEMORY_MANAGEMENT
);
374 InterlockedExchange(&MiBalancerWork
, 0);
376 else if (Status
== STATUS_SUCCESS
+ 1)
378 /* MiBalancerTimer */
379 ShouldRun
= MmAvailablePages
< MiMinimumAvailablePages
+ 5 ? TRUE
: FALSE
;
380 for (i
= 0; i
< MC_MAXIMUM
; i
++)
382 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
384 NrPagesUsed
= MiMemoryConsumers
[i
].PagesUsed
;
385 if (NrPagesUsed
> MiMemoryConsumers
[i
].PagesTarget
|| ShouldRun
)
387 if (NrPagesUsed
> MiMemoryConsumers
[i
].PagesTarget
)
389 Target
= max (NrPagesUsed
- MiMemoryConsumers
[i
].PagesTarget
,
390 MiMinimumPagesPerRun
);
394 Target
= MiMinimumPagesPerRun
;
397 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
398 if (!NT_SUCCESS(Status
))
400 KeBugCheck(MEMORY_MANAGEMENT
);
408 DPRINT1("KeWaitForMultipleObjects failed, status = %x\n", Status
);
409 KeBugCheck(MEMORY_MANAGEMENT
);
417 MiInitBalancerThread(VOID
)
421 #if !defined(__GNUC__)
423 LARGE_INTEGER dummyJunkNeeded
;
424 dummyJunkNeeded
.QuadPart
= -20000000; /* 2 sec */
429 KeInitializeEvent(&MiBalancerEvent
, SynchronizationEvent
, FALSE
);
430 KeInitializeTimerEx(&MiBalancerTimer
, SynchronizationTimer
);
431 KeSetTimerEx(&MiBalancerTimer
,
432 #if defined(__GNUC__)
433 (LARGE_INTEGER
)(LONGLONG
)-20000000LL, /* 2 sec */
440 Status
= PsCreateSystemThread(&MiBalancerThreadHandle
,
445 (PKSTART_ROUTINE
) MiBalancerThread
,
447 if (!NT_SUCCESS(Status
))
449 KeBugCheck(MEMORY_MANAGEMENT
);
452 Priority
= LOW_REALTIME_PRIORITY
+ 1;
453 NtSetInformationThread(MiBalancerThreadHandle
,