From 3ae19eaf0134465795d54cf54d963784157462d5 Mon Sep 17 00:00:00 2001 From: Dibyamartanda Samanta Date: Sat, 17 Aug 2024 20:13:20 +0200 Subject: [PATCH] [NTOSKRNL::CC] Implement CcOkToAddWriteBehindThread --- NTOSKRNL/CC/cclazywriter.cpp | 89 ++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/NTOSKRNL/CC/cclazywriter.cpp b/NTOSKRNL/CC/cclazywriter.cpp index 62fbd49..126b549 100644 --- a/NTOSKRNL/CC/cclazywriter.cpp +++ b/NTOSKRNL/CC/cclazywriter.cpp @@ -13,6 +13,95 @@ extern "C" +/* Move Typedef Later to cctypes.hpp */ +typedef struct _WRITE_BEHIND_THROUGHPUT +{ + ULONG PagesYetToWrite; + ULONG Throughput; +} WRITE_BEHIND_THROUGHPUT, *PWRITE_BEHIND_THROUGHPUT; + +BOOLEAN +CcOkToAddWriteBehindThread(VOID) +{ + ULONG ActiveExtraWriteBehindThreads = {0}; + PWRITE_BEHIND_THROUGHPUT ThroughputStats = nullptr; + ULONG PagesYetToWrite = {0}; + ULONG Throughput = {0}; + ULONG PreviousThroughput = {0}; + LONG ThroughputTrend = {0}; + BOOLEAN Result = false; + + ActiveExtraWriteBehindThreads = CcActiveExtraWriteBehindThreads; + ThroughputStats = CcThroughputStats; + PagesYetToWrite = CcPagesYetToWrite; + + Throughput = ThroughputStats[ActiveExtraWriteBehindThreads].PagesYetToWrite; + PreviousThroughput = 0; + + if (Throughput >= PagesYetToWrite) + { + Throughput -= PagesYetToWrite; + } + else + { + Throughput = 0; + } + + ThroughputStats[ActiveExtraWriteBehindThreads].PagesYetToWrite = PagesYetToWrite; + Result = true; + + if (ActiveExtraWriteBehindThreads > 0) + { + PreviousThroughput = ThroughputStats[ActiveExtraWriteBehindThreads - 1].Throughput; + } + + ThroughputStats[ActiveExtraWriteBehindThreads].Throughput = Throughput; + + if (Throughput > 0) + { + ThroughputTrend = CcThroughputTrend; + + if (Throughput < PreviousThroughput) + { + if (ThroughputTrend > 0) + ThroughputTrend = 0; + ThroughputTrend--; + } + else + { + if (ThroughputTrend < 0) + ThroughputTrend = 0; + ThroughputTrend++; + } + + CcThroughputTrend = ThroughputTrend; + + if (ThroughputTrend == 3) + { + CcThroughputTrend = 0; + Result = true; + + if (ActiveExtraWriteBehindThreads < CcMaxExtraWriteBehindThreads) + { + ThroughputStats[ActiveExtraWriteBehindThreads + 1].Throughput = 0; + ThroughputStats[ActiveExtraWriteBehindThreads + 1].PagesYetToWrite = PagesYetToWrite; + } + } + else if (ThroughputTrend == -3) + { + CcThroughputTrend = 0; + Result = true; + + if (ActiveExtraWriteBehindThreads > 0) + { + ThroughputStats[ActiveExtraWriteBehindThreads - 1].Throughput = 0; + ThroughputStats[ActiveExtraWriteBehindThreads - 1].PagesYetToWrite = PagesYetToWrite; + } + } + } + + return Result; +} VOID NTAPI