[NTOS:MM] Implement PeakCommitment (MmPeakCommitment, MmTotalCommittedPages) (#4650)

And return the corresponding values in SystemPerformanceInformation.
Lockless updating counters suggestion by Thomas Faber.
This commit is contained in:
Kyle Katarn 2022-09-12 14:22:52 +02:00 committed by GitHub
parent 51dd0523c7
commit 3703bbd631
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 8 deletions

View file

@ -714,13 +714,8 @@ QSI_DEF(SystemPerformanceInformation)
} }
Spi->AvailablePages = (ULONG)MmAvailablePages; Spi->AvailablePages = (ULONG)MmAvailablePages;
/*
* Add up all the used "Committed" memory + pagefile. Spi->CommittedPages = MmTotalCommittedPages;
* Not sure this is right. 8^\
*/
Spi->CommittedPages = MiMemoryConsumers[MC_SYSTEM].PagesUsed +
MiMemoryConsumers[MC_USER].PagesUsed +
MiUsedSwapPages;
/* /*
* Add up the full system total + pagefile. * Add up the full system total + pagefile.
* All this make Taskmgr happy but not sure it is the right numbers. * All this make Taskmgr happy but not sure it is the right numbers.
@ -728,7 +723,7 @@ QSI_DEF(SystemPerformanceInformation)
*/ */
Spi->CommitLimit = MmNumberOfPhysicalPages + MiFreeSwapPages + MiUsedSwapPages; Spi->CommitLimit = MmNumberOfPhysicalPages + MiFreeSwapPages + MiUsedSwapPages;
Spi->PeakCommitment = 0; /* FIXME */ Spi->PeakCommitment = MmPeakCommitment;
Spi->PageFaultCount = 0; /* FIXME */ Spi->PageFaultCount = 0; /* FIXME */
Spi->CopyOnWriteCount = 0; /* FIXME */ Spi->CopyOnWriteCount = 0; /* FIXME */
Spi->TransitionCount = 0; /* FIXME */ Spi->TransitionCount = 0; /* FIXME */

View file

@ -866,6 +866,33 @@ NTAPI
MmDeleteKernelStack(PVOID Stack, MmDeleteKernelStack(PVOID Stack,
BOOLEAN GuiStack); BOOLEAN GuiStack);
/* balance.c / pagefile.c******************************************************/
inline VOID UpdateTotalCommittedPages(LONG Delta)
{
/*
* Add up all the used "Committed" memory + pagefile.
* Not sure this is right. 8^\
* MmTotalCommittedPages should be adjusted consistently with
* other counters at different places.
*
MmTotalCommittedPages = MiMemoryConsumers[MC_SYSTEM].PagesUsed +
MiMemoryConsumers[MC_USER].PagesUsed +
MiUsedSwapPages;
*/
/* Update Commitment */
SIZE_T TotalCommittedPages = InterlockedExchangeAddSizeT(&MmTotalCommittedPages, Delta) + Delta;
/* Update Peak = max(Peak, Total) in a lockless way */
SIZE_T PeakCommitment = MmPeakCommitment;
while (TotalCommittedPages > PeakCommitment &&
InterlockedCompareExchangeSizeT(&MmPeakCommitment, TotalCommittedPages, PeakCommitment) != PeakCommitment)
{
PeakCommitment = MmPeakCommitment;
}
}
/* balance.c *****************************************************************/ /* balance.c *****************************************************************/
CODE_SEG("INIT") CODE_SEG("INIT")

View file

@ -81,6 +81,7 @@ MmReleasePageMemoryConsumer(ULONG Consumer, PFN_NUMBER Page)
} }
(void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed); (void)InterlockedDecrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
UpdateTotalCommittedPages(-1);
OldIrql = MiAcquirePfnLock(); OldIrql = MiAcquirePfnLock();
@ -284,6 +285,7 @@ MmRequestPageMemoryConsumer(ULONG Consumer, BOOLEAN CanWait,
/* Update the target */ /* Update the target */
InterlockedIncrementUL(&MiMemoryConsumers[Consumer].PagesUsed); InterlockedIncrementUL(&MiMemoryConsumers[Consumer].PagesUsed);
UpdateTotalCommittedPages(1);
/* /*
* Actually allocate the page. * Actually allocate the page.

View file

@ -295,6 +295,7 @@ MmFreeSwapPage(SWAPENTRY Entry)
MiFreeSwapPages++; MiFreeSwapPages++;
MiUsedSwapPages--; MiUsedSwapPages--;
UpdateTotalCommittedPages(-1);
KeReleaseGuardedMutex(&MmPageFileCreationLock); KeReleaseGuardedMutex(&MmPageFileCreationLock);
} }
@ -329,6 +330,8 @@ MmAllocSwapPage(VOID)
} }
MiUsedSwapPages++; MiUsedSwapPages++;
MiFreeSwapPages--; MiFreeSwapPages--;
UpdateTotalCommittedPages(1);
KeReleaseGuardedMutex(&MmPageFileCreationLock); KeReleaseGuardedMutex(&MmPageFileCreationLock);
entry = ENTRY_FROM_FILE_OFFSET(i, off + 1); entry = ENTRY_FROM_FILE_OFFSET(i, off + 1);