3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: balance.c,v 1.14 2002/11/10 18:17:42 chorns Exp $
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/mm/balance.c
23 * PURPOSE: kernel memory managment functions
24 * PROGRAMMER: David Welch (welch@cwcom.net)
29 /* INCLUDES *****************************************************************/
31 #include <ddk/ntddk.h>
32 #include <internal/mm.h>
33 #include <ntos/minmax.h>
36 #include <internal/debug.h>
38 /* TYPES ********************************************************************/
40 typedef struct _MM_MEMORY_CONSUMER
44 NTSTATUS (*Trim
)(ULONG Target
, ULONG Priority
, PULONG NrFreed
);
45 } MM_MEMORY_CONSUMER
, *PMM_MEMORY_CONSUMER
;
47 typedef struct _MM_ALLOCATION_REQUEST
49 PHYSICAL_ADDRESS Page
;
52 } MM_ALLOCATION_REQUEST
, *PMM_ALLOCATION_REQUEST
;
54 /* GLOBALS ******************************************************************/
56 static MM_MEMORY_CONSUMER MiMemoryConsumers
[MC_MAXIMUM
];
57 static ULONG MiMinimumAvailablePages
;
58 static ULONG MiNrAvailablePages
;
59 static ULONG MiNrTotalPages
;
60 static LIST_ENTRY AllocationListHead
;
61 static KSPIN_LOCK AllocationListLock
;
62 static ULONG NrWorkingThreads
= 0;
63 static HANDLE WorkerThreadId
;
64 static ULONG MiPagesRequired
= 0;
65 static ULONG MiMinimumPagesPerRun
= 1;
67 /* FUNCTIONS ****************************************************************/
69 VOID
MmPrintMemoryStatistic(VOID
)
71 DbgPrint("MC_CACHE %d, MC_USER %d, MC_PPOOL %d, MC_NPPOOL %d\n",
72 MiMemoryConsumers
[MC_CACHE
].PagesUsed
,
73 MiMemoryConsumers
[MC_USER
].PagesUsed
,
74 MiMemoryConsumers
[MC_PPOOL
].PagesUsed
,
75 MiMemoryConsumers
[MC_NPPOOL
].PagesUsed
);
79 MmInitializeBalancer(ULONG NrAvailablePages
)
81 memset(MiMemoryConsumers
, 0, sizeof(MiMemoryConsumers
));
82 InitializeListHead(&AllocationListHead
);
83 KeInitializeSpinLock(&AllocationListLock
);
85 MiNrAvailablePages
= MiNrTotalPages
= NrAvailablePages
;
88 MiMinimumAvailablePages
= 64;
89 MiMemoryConsumers
[MC_CACHE
].PagesTarget
= NrAvailablePages
/ 2;
90 MiMemoryConsumers
[MC_USER
].PagesTarget
=
91 NrAvailablePages
- MiMinimumAvailablePages
;
92 MiMemoryConsumers
[MC_PPOOL
].PagesTarget
= NrAvailablePages
/ 2;
93 MiMemoryConsumers
[MC_NPPOOL
].PagesTarget
= 0xFFFFFFFF;
97 MmInitializeMemoryConsumer(ULONG Consumer
,
98 NTSTATUS (*Trim
)(ULONG Target
, ULONG Priority
,
101 MiMemoryConsumers
[Consumer
].Trim
= Trim
;
105 MmReleasePageMemoryConsumer(ULONG Consumer
, PHYSICAL_ADDRESS Page
)
107 PMM_ALLOCATION_REQUEST Request
;
111 if (Page
.QuadPart
== 0LL)
113 DPRINT1("Tried to release page zero.\n");
117 KeAcquireSpinLock(&AllocationListLock
, &oldIrql
);
118 if (MmGetReferenceCountPage(Page
) == 1)
120 InterlockedDecrement(&MiMemoryConsumers
[Consumer
].PagesUsed
);
121 InterlockedIncrement(&MiNrAvailablePages
);
122 InterlockedDecrement(&MiPagesRequired
);
123 if (IsListEmpty(&AllocationListHead
))
125 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
126 MmDereferencePage(Page
);
130 Entry
= RemoveHeadList(&AllocationListHead
);
131 Request
= CONTAINING_RECORD(Entry
, MM_ALLOCATION_REQUEST
, ListEntry
);
132 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
133 Request
->Page
= Page
;
134 KeSetEvent(&Request
->Event
, IO_NO_INCREMENT
, FALSE
);
139 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
140 MmDereferencePage(Page
);
143 return(STATUS_SUCCESS
);
147 MiTrimMemoryConsumer(ULONG Consumer
)
152 Target
= MiMemoryConsumers
[Consumer
].PagesUsed
-
153 MiMemoryConsumers
[Consumer
].PagesTarget
;
159 if (MiMemoryConsumers
[Consumer
].Trim
!= NULL
)
161 MiMemoryConsumers
[Consumer
].Trim(Target
, 0, &NrFreedPages
);
166 MiRebalanceMemoryConsumers(VOID
)
173 Target
= (MiMinimumAvailablePages
- MiNrAvailablePages
) + MiPagesRequired
;
174 Target
= min(Target
, (LONG
) MiMinimumPagesPerRun
);
176 for (i
= 0; i
< MC_MAXIMUM
&& Target
> 0; i
++)
178 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
180 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
181 if (!NT_SUCCESS(Status
))
185 Target
= Target
- NrFreedPages
;
195 MmRequestPageMemoryConsumer(ULONG Consumer
, BOOLEAN CanWait
,
196 PHYSICAL_ADDRESS
* AllocatedPage
)
200 PHYSICAL_ADDRESS Page
;
204 * Make sure we don't exceed our individual target.
206 OldUsed
= InterlockedIncrement(&MiMemoryConsumers
[Consumer
].PagesUsed
);
207 if (OldUsed
>= (MiMemoryConsumers
[Consumer
].PagesTarget
- 1) &&
208 WorkerThreadId
!= PsGetCurrentThreadId())
212 InterlockedDecrement(&MiMemoryConsumers
[Consumer
].PagesUsed
);
213 return(STATUS_NO_MEMORY
);
215 MiTrimMemoryConsumer(Consumer
);
219 * Make sure we don't exceed global targets.
221 OldAvailable
= InterlockedDecrement(&MiNrAvailablePages
);
222 if (OldAvailable
< MiMinimumAvailablePages
)
224 MM_ALLOCATION_REQUEST Request
;
228 InterlockedIncrement(&MiNrAvailablePages
);
229 InterlockedDecrement(&MiMemoryConsumers
[Consumer
].PagesUsed
);
230 return(STATUS_NO_MEMORY
);
233 /* Insert an allocation request. */
234 Request
.Page
.QuadPart
= 0LL;
235 KeInitializeEvent(&Request
.Event
, NotificationEvent
, FALSE
);
236 InterlockedIncrement(&MiPagesRequired
);
238 KeAcquireSpinLock(&AllocationListLock
, &oldIrql
);
239 if (NrWorkingThreads
== 0)
241 InsertTailList(&AllocationListHead
, &Request
.ListEntry
);
243 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
244 WorkerThreadId
= PsGetCurrentThreadId();
245 MiRebalanceMemoryConsumers();
246 KeAcquireSpinLock(&AllocationListLock
, &oldIrql
);
249 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
253 if (WorkerThreadId
== PsGetCurrentThreadId())
255 Page
= MmAllocPage(Consumer
, 0);
256 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
257 if (Page
.QuadPart
== 0LL)
261 *AllocatedPage
= Page
;
262 return(STATUS_SUCCESS
);
264 InsertTailList(&AllocationListHead
, &Request
.ListEntry
);
265 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
267 KeWaitForSingleObject(&Request
.Event
,
274 if (Page
.QuadPart
== 0LL)
278 MmTransferOwnershipPage(Page
, Consumer
);
279 *AllocatedPage
= Page
;
280 return(STATUS_SUCCESS
);
284 * Actually allocate the page.
286 Page
= MmAllocPage(Consumer
, 0);
287 if (Page
.QuadPart
== 0LL)
291 *AllocatedPage
= Page
;
293 return(STATUS_SUCCESS
);