[NTOSKRNL:CC] Implement CcAdjustWriteBehindThreadPool and CcAdjustWriteBehindThreadPoolIfNeeded

These two function are responsible for  calling threadpool,when needed
This commit is contained in:
Dibyamartanda Samanta 2024-08-17 15:32:37 +02:00 committed by CodingWorkshop Signing Team
parent 1c173364a1
commit e75a2adeec
Signed by: CodingWorkshop Signing Team
GPG Key ID: 6DC88369C82795D2

View File

@ -1175,44 +1175,7 @@ NTAPI CcLazyWriteScan()
CcScheduleLazyWriteScan(FALSE);
}
}
NTSTATUS CcWaitForCurrentLazyWriterActivity()
{
NTSTATUS result;
PWORK_QUEUE_ENTRY WorkQueueEntry = nullptr;
KEVENT Event = {0};
KIRQL irql = {0};
result = CcAllocateWorkQueueEntry(&WorkQueueEntry);
if (NT_SUCCESS(result))
{
WorkQueueEntry->Function = SetDone;
KeInitializeEvent(&Event, NotificationEvent, FALSE);
WorkQueueEntry->Parameters.Notification.Reason = (ULONG_PTR)&Event;
if ((PerfGlobalGroupMask.Masks[4] & 0x20000) != 0)
CcPerfLogWorkItemEnqueue(&CcPostTickWorkQueue, WorkQueueEntry, 0, 0);
irql = KeAcquireQueuedSpinLock(LockQueueMasterLock);
WorkQueueEntry->WorkQueueLinks.Flink = &CcPostTickWorkQueue;
WorkQueueEntry->WorkQueueLinks.Blink = CcPostTickWorkQueue.Blink;
CcPostTickWorkQueue.Blink->Flink = &WorkQueueEntry->WorkQueueLinks;
CcPostTickWorkQueue.Blink = &WorkQueueEntry->WorkQueueLinks;
LazyWriter.OtherWork = 1;
_InterlockedIncrement(&CcPostTickWorkItemCount);
CcScheduleLazyWriteScan(1, 1);
KeReleaseQueuedSpinLock(LockQueueMasterLock, irql);
result = KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
_InterlockedDecrement(&CcPostTickWorkItemCount);
}
return result;
}
VOID VECTORCALL CcReEngageWorkerThreads(
ULONG NormalThreadsToActivate,
ULONG ExtraWriteBehindThreadsToActivate
@ -1281,6 +1244,62 @@ VOID VECTORCALL CcReEngageWorkerThreads(
ExQueueWorkItem(currentExtraThreadEntry, CriticalWorkQueue);
}
}
VOID
NTAPI
CcAdjustWriteBehindThreadPool(
IN BOOLEAN IsThreadPriorityLow)
{
if (IsThreadPriorityLow)
{
CcMaxNumberOfWriteBehindThreads = 1;
if (CcAddExtraWriteBehindThreads)
{
CcAddExtraWriteBehindThreads = false;
}
}
else
{
CcMaxNumberOfWriteBehindThreads = (ULONG)-1;
if (!IsListEmpty(&CcRegularWorkQueue) && !CcQueueThrottle)
{
CcReEngageWorkerThreads(CcNumberofWorkerThreads, 0);
}
}
}
VOID
NTAPI
CcAdjustWriteBehindThreadPoolIfNeeded(
IN BOOLEAN NeedAdjustment)
{
BOOLEAN NeedBoost = false;
KSPIN_LOCK_QUEUE_NUMBER queueNumber = LockQueueMasterLock;
SpinLockGuard guard(queueNumber);
if (CcPostTickWorkItemCount != 0)
{
if (CcIsWriteBehindThreadpoolAtLowPriority())
{
CcAdjustWriteBehindThreadPool(false);
}
}
else
{
if (CcGlobalDirtyPageStatistics.DirtyPages > 0x2000 || NeedAdjustment)
{
if (CcIsWriteBehindThreadpoolAtLowPriority())
{
CcAdjustWriteBehindThreadPool(false);
}
}
else if (!CcExecutingWriteBehindWorkItems && IsListEmpty(&CcRegularWorkQueue))
{
CcAdjustWriteBehindThreadPool(true);
}
}
}
VOID
NTAPI
CcWorkerThread(PVOID Parameter)