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.16 2003/06/14 17:53:25 hbirr 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
= 10;
67 /* FUNCTIONS ****************************************************************/
69 VOID
MmPrintMemoryStatistic(VOID
)
71 DbgPrint("MC_CACHE %d, MC_USER %d, MC_PPOOL %d, MC_NPPOOL %d, MiNrAvailablePages %d\n",
72 MiMemoryConsumers
[MC_CACHE
].PagesUsed
, MiMemoryConsumers
[MC_USER
].PagesUsed
,
73 MiMemoryConsumers
[MC_PPOOL
].PagesUsed
, MiMemoryConsumers
[MC_NPPOOL
].PagesUsed
,
78 MmInitializeBalancer(ULONG NrAvailablePages
)
80 memset(MiMemoryConsumers
, 0, sizeof(MiMemoryConsumers
));
81 InitializeListHead(&AllocationListHead
);
82 KeInitializeSpinLock(&AllocationListLock
);
84 MiNrAvailablePages
= MiNrTotalPages
= NrAvailablePages
;
87 MiMinimumAvailablePages
= 64;
88 MiMemoryConsumers
[MC_CACHE
].PagesTarget
= NrAvailablePages
/ 2;
89 MiMemoryConsumers
[MC_USER
].PagesTarget
=
90 NrAvailablePages
- MiMinimumAvailablePages
;
91 MiMemoryConsumers
[MC_PPOOL
].PagesTarget
= NrAvailablePages
/ 2;
92 MiMemoryConsumers
[MC_NPPOOL
].PagesTarget
= 0xFFFFFFFF;
96 MmInitializeMemoryConsumer(ULONG Consumer
,
97 NTSTATUS (*Trim
)(ULONG Target
, ULONG Priority
,
100 MiMemoryConsumers
[Consumer
].Trim
= Trim
;
104 MmReleasePageMemoryConsumer(ULONG Consumer
, PHYSICAL_ADDRESS Page
)
106 PMM_ALLOCATION_REQUEST Request
;
110 if (Page
.QuadPart
== 0LL)
112 DPRINT1("Tried to release page zero.\n");
116 KeAcquireSpinLock(&AllocationListLock
, &oldIrql
);
117 if (MmGetReferenceCountPage(Page
) == 1)
119 InterlockedDecrement((LONG
*)&MiMemoryConsumers
[Consumer
].PagesUsed
);
120 InterlockedIncrement((LONG
*)&MiNrAvailablePages
);
121 if (IsListEmpty(&AllocationListHead
))
123 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
124 MmDereferencePage(Page
);
128 Entry
= RemoveHeadList(&AllocationListHead
);
129 Request
= CONTAINING_RECORD(Entry
, MM_ALLOCATION_REQUEST
, ListEntry
);
130 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
131 Request
->Page
= Page
;
132 KeSetEvent(&Request
->Event
, IO_NO_INCREMENT
, FALSE
);
137 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
138 MmDereferencePage(Page
);
141 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 MiRebalanceMemoryConsumers(VOID
)
171 Target
= (MiMinimumAvailablePages
- MiNrAvailablePages
) + MiPagesRequired
;
172 Target
= max(Target
, (LONG
) MiMinimumPagesPerRun
);
174 for (i
= 0; i
< MC_MAXIMUM
&& Target
> 0; i
++)
176 if (MiMemoryConsumers
[i
].Trim
!= NULL
)
178 Status
= MiMemoryConsumers
[i
].Trim(Target
, 0, &NrFreedPages
);
179 if (!NT_SUCCESS(Status
))
183 Target
= Target
- NrFreedPages
;
193 MmRequestPageMemoryConsumer(ULONG Consumer
, BOOLEAN CanWait
,
194 PHYSICAL_ADDRESS
* AllocatedPage
)
198 PHYSICAL_ADDRESS Page
;
202 * Make sure we don't exceed our individual target.
204 OldUsed
= InterlockedIncrement((LONG
*)&MiMemoryConsumers
[Consumer
].PagesUsed
);
205 if (OldUsed
>= (MiMemoryConsumers
[Consumer
].PagesTarget
- 1) &&
206 WorkerThreadId
!= PsGetCurrentThreadId())
210 InterlockedDecrement((LONG
*)&MiMemoryConsumers
[Consumer
].PagesUsed
);
211 return(STATUS_NO_MEMORY
);
213 MiTrimMemoryConsumer(Consumer
);
217 * Make sure we don't exceed global targets.
219 OldAvailable
= InterlockedDecrement((LONG
*)&MiNrAvailablePages
);
220 if (OldAvailable
< MiMinimumAvailablePages
)
222 MM_ALLOCATION_REQUEST Request
;
226 InterlockedIncrement((LONG
*)&MiNrAvailablePages
);
227 InterlockedDecrement((LONG
*)&MiMemoryConsumers
[Consumer
].PagesUsed
);
228 return(STATUS_NO_MEMORY
);
231 /* Insert an allocation request. */
232 Request
.Page
.QuadPart
= 0LL;
233 KeInitializeEvent(&Request
.Event
, NotificationEvent
, FALSE
);
234 InterlockedIncrement((LONG
*)&MiPagesRequired
);
236 KeAcquireSpinLock(&AllocationListLock
, &oldIrql
);
237 if (NrWorkingThreads
== 0)
239 InsertTailList(&AllocationListHead
, &Request
.ListEntry
);
241 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
242 WorkerThreadId
= PsGetCurrentThreadId();
243 MiRebalanceMemoryConsumers();
244 KeAcquireSpinLock(&AllocationListLock
, &oldIrql
);
247 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
251 if (WorkerThreadId
== PsGetCurrentThreadId())
253 Page
= MmAllocPage(Consumer
, 0);
254 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
255 if (Page
.QuadPart
== 0LL)
259 *AllocatedPage
= Page
;
260 InterlockedDecrement((LONG
*)&MiPagesRequired
);
261 return(STATUS_SUCCESS
);
263 InsertTailList(&AllocationListHead
, &Request
.ListEntry
);
264 KeReleaseSpinLock(&AllocationListLock
, oldIrql
);
266 KeWaitForSingleObject(&Request
.Event
,
273 if (Page
.QuadPart
== 0LL)
277 MmTransferOwnershipPage(Page
, Consumer
);
278 *AllocatedPage
= Page
;
279 InterlockedDecrement((LONG
*)&MiPagesRequired
);
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
);