From e1d5fe0a55f46a53549450972c5b5506a43ca94c Mon Sep 17 00:00:00 2001 From: Sachin Keswani Date: Mon, 27 Jan 2020 15:50:31 -0800 Subject: [PATCH] Adding Processor Groups support for more than 64 processors --- src/Common/EncryptionThreadPool.c | 58 +++++++++++++++++++++++++-- src/Common/Zip.vcxproj | 6 +-- src/Crypto/Crypto.vcxproj | 4 +- src/ExpandVolume/ExpandVolume.vcxproj | 4 +- src/Format/Format.vcxproj | 4 +- src/Mount/Mount.vcxproj | 4 +- src/Setup/Portable.vcxproj | 4 +- src/Setup/Setup.vcxproj | 4 +- src/VeraCrypt.sln | 13 ++++-- 9 files changed, 79 insertions(+), 22 deletions(-) diff --git a/src/Common/EncryptionThreadPool.c b/src/Common/EncryptionThreadPool.c index 461f2847..c87da607 100644 --- a/src/Common/EncryptionThreadPool.c +++ b/src/Common/EncryptionThreadPool.c @@ -10,13 +10,19 @@ code distribution packages. */ +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined (WIN64) +#include +#include +#endif + #include "EncryptionThreadPool.h" #include "Pkcs5.h" #ifdef DEVICE_DRIVER #include "Driver/Ntdriver.h" #endif -#define TC_ENC_THREAD_POOL_MAX_THREAD_COUNT 64 +//Increasing the maximum number of threads in thread pool count +#define TC_ENC_THREAD_POOL_MAX_THREAD_COUNT 256 //64 #define TC_ENC_THREAD_POOL_QUEUE_SIZE (TC_ENC_THREAD_POOL_MAX_THREAD_COUNT * 2) #ifdef DEVICE_DRIVER @@ -111,6 +117,9 @@ static TC_MUTEX DequeueMutex; static TC_EVENT WorkItemReadyEvent; static TC_EVENT WorkItemCompletedEvent; +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined (WIN64) +static uint32 totalProcessors; +#endif #if defined(_WIN64) void EncryptDataUnitsCurrentThreadEx (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci) { @@ -163,6 +172,11 @@ static void SetWorkItemState (EncryptionThreadPoolWorkItem *workItem, WorkItemSt static TC_THREAD_PROC EncryptionThreadProc (void *threadArg) { +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) + GROUP_AFFINITY groupAffinity = { ~0ULL, *(int*)(threadArg), { 0, 0, 0 } }; + BOOL value = SetThreadGroupAffinity(GetCurrentThread(), &groupAffinity, NULL); +#endif + EncryptionThreadPoolWorkItem *workItem; while (!StopPending) @@ -263,12 +277,27 @@ static TC_THREAD_PROC EncryptionThreadProc (void *threadArg) BOOL EncryptionThreadPoolStart (size_t encryptionFreeCpuCount) { - size_t cpuCount, i; + //size_t cpuCount, i; + size_t cpuCount = 0, i = 0, processors = 0, totalProcessors = 0; + int threadProcessorGroups[128]; + #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined (WIN64) + for (i = 0; i < GetActiveProcessorGroupCount(); ++i) + { + processors = GetActiveProcessorCount(i); + totalProcessors += processors; + } + #endif if (ThreadPoolRunning) return TRUE; -#ifdef DEVICE_DRIVER +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) + SYSTEM_INFO sysInfo; + GetSystemInfo(&sysInfo); + cpuCount = (IsWindows7OrGreater) ? totalProcessors : sysInfo.dwNumberOfProcessors; +#endif + +/*#ifdef DEVICE_DRIVER cpuCount = GetCpuCount(); #else { @@ -277,7 +306,7 @@ BOOL EncryptionThreadPoolStart (size_t encryptionFreeCpuCount) cpuCount = sysInfo.dwNumberOfProcessors; } #endif - +*/ if (cpuCount > encryptionFreeCpuCount) cpuCount -= encryptionFreeCpuCount; @@ -337,10 +366,31 @@ BOOL EncryptionThreadPoolStart (size_t encryptionFreeCpuCount) for (ThreadCount = 0; ThreadCount < cpuCount; ++ThreadCount) { + #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) + // Determine which processor group to bind the thread to. + totalProcessors = 0U; + for (i = 0U; i < GetActiveProcessorGroupCount(); ++i) + { + totalProcessors += GetActiveProcessorCount(i); + if (totalProcessors >= ThreadCount) + { + threadProcessorGroups[ThreadCount] = i; + break; + } + } + #endif + +#ifdef DEVICE_DRIVER + if (!NT_SUCCESS(TCStartThread(EncryptionThreadProc, (void*)(&threadProcessorGroups[ThreadCount]), &ThreadHandles[ThreadCount]))) +#else + if (!(ThreadHandles[ThreadCount] = (HANDLE)_beginthreadex(NULL, 0, EncryptionThreadProc, (void*)(&threadProcessorGroups[ThreadCount]), 0, NULL))) +/* #ifdef DEVICE_DRIVER if (!NT_SUCCESS (TCStartThread (EncryptionThreadProc, NULL, &ThreadHandles[ThreadCount]))) #else if (!(ThreadHandles[ThreadCount] = (HANDLE) _beginthreadex (NULL, 0, EncryptionThreadProc, NULL, 0, NULL))) + +*/ #endif { EncryptionThreadPoolStop(); diff --git a/src/Common/Zip.vcxproj b/src/Common/Zip.vcxproj index 746f771d..b04ef3e2 100644 --- a/src/Common/Zip.vcxproj +++ b/src/Common/Zip.vcxproj @@ -166,7 +166,7 @@ StaticLibrary true Unicode - Windows7.1SDK + v142 false @@ -181,14 +181,14 @@ false false Unicode - Windows7.1SDK + v142 StaticLibrary false false Unicode - Windows7.1SDK + v142 diff --git a/src/Crypto/Crypto.vcxproj b/src/Crypto/Crypto.vcxproj index c6e0aac5..5196648d 100644 --- a/src/Crypto/Crypto.vcxproj +++ b/src/Crypto/Crypto.vcxproj @@ -32,12 +32,12 @@ StaticLibrary Unicode - Windows7.1SDK + v142 StaticLibrary Unicode - Windows7.1SDK + v142 StaticLibrary diff --git a/src/ExpandVolume/ExpandVolume.vcxproj b/src/ExpandVolume/ExpandVolume.vcxproj index 5fe967bb..24d49b1d 100644 --- a/src/ExpandVolume/ExpandVolume.vcxproj +++ b/src/ExpandVolume/ExpandVolume.vcxproj @@ -45,12 +45,12 @@ Application Unicode - Windows7.1SDK + v142 Application Unicode - Windows7.1SDK + v142 Application diff --git a/src/Format/Format.vcxproj b/src/Format/Format.vcxproj index 204654e1..c7767592 100644 --- a/src/Format/Format.vcxproj +++ b/src/Format/Format.vcxproj @@ -45,12 +45,12 @@ Application Unicode - Windows7.1SDK + v142 Application Unicode - Windows7.1SDK + v142 Application diff --git a/src/Mount/Mount.vcxproj b/src/Mount/Mount.vcxproj index 73a6641e..21bf9784 100644 --- a/src/Mount/Mount.vcxproj +++ b/src/Mount/Mount.vcxproj @@ -45,12 +45,12 @@ Application Unicode - Windows7.1SDK + v142 Application Unicode - Windows7.1SDK + v142 Application diff --git a/src/Setup/Portable.vcxproj b/src/Setup/Portable.vcxproj index e864515b..56f77584 100644 --- a/src/Setup/Portable.vcxproj +++ b/src/Setup/Portable.vcxproj @@ -23,7 +23,7 @@ Application Unicode - Windows7.1SDK + v142 Application @@ -33,7 +33,7 @@ Application Unicode - Windows7.1SDK + v142 diff --git a/src/Setup/Setup.vcxproj b/src/Setup/Setup.vcxproj index 922d3018..2d57e4fc 100644 --- a/src/Setup/Setup.vcxproj +++ b/src/Setup/Setup.vcxproj @@ -23,7 +23,7 @@ Application Unicode - Windows7.1SDK + v142 Application @@ -33,7 +33,7 @@ Application Unicode - Windows7.1SDK + v142 diff --git a/src/VeraCrypt.sln b/src/VeraCrypt.sln index 6f373bcd..f044dcf6 100644 --- a/src/VeraCrypt.sln +++ b/src/VeraCrypt.sln @@ -1,5 +1,7 @@ -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29709.97 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Crypto", "Crypto\Crypto.vcxproj", "{993245CF-6B70-47EE-91BB-39F8FC6DC0E7}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Driver", "Driver\Driver.vcxproj", "{EF5EF444-18D0-40D7-8DFA-775EC4448602}" @@ -440,6 +442,7 @@ Global {8B7F059F-E4C7-4E11-88F5-EE8B8433072E}.Release|Win32.ActiveCfg = Release|Win32 {8B7F059F-E4C7-4E11-88F5-EE8B8433072E}.Release|Win32.Build.0 = Release|Win32 {8B7F059F-E4C7-4E11-88F5-EE8B8433072E}.Release|x64.ActiveCfg = Release|Win32 + {8B7F059F-E4C7-4E11-88F5-EE8B8433072E}.Release|x64.Build.0 = Release|Win32 {8B7F059F-E4C7-4E11-88F5-EE8B8433072E}.Setup Debug|Win32.ActiveCfg = Release|Win32 {8B7F059F-E4C7-4E11-88F5-EE8B8433072E}.Setup Debug|x64.ActiveCfg = Release Loader|Win32 {8B7F059F-E4C7-4E11-88F5-EE8B8433072E}.Setup|Win32.ActiveCfg = Release|Win32 @@ -584,7 +587,8 @@ Global {6316EE71-0210-4CA4-BCC7-CFB7A3C090FC}.Portable|x64.ActiveCfg = Release|Win32 {6316EE71-0210-4CA4-BCC7-CFB7A3C090FC}.Release|Win32.ActiveCfg = Release|Win32 {6316EE71-0210-4CA4-BCC7-CFB7A3C090FC}.Release|Win32.Build.0 = Release|Win32 - {6316EE71-0210-4CA4-BCC7-CFB7A3C090FC}.Release|x64.ActiveCfg = Release|Win32 + {6316EE71-0210-4CA4-BCC7-CFB7A3C090FC}.Release|x64.ActiveCfg = Release|x64 + {6316EE71-0210-4CA4-BCC7-CFB7A3C090FC}.Release|x64.Build.0 = Release|x64 {6316EE71-0210-4CA4-BCC7-CFB7A3C090FC}.Setup Debug|Win32.ActiveCfg = Debug|Win32 {6316EE71-0210-4CA4-BCC7-CFB7A3C090FC}.Setup Debug|Win32.Build.0 = Debug|Win32 {6316EE71-0210-4CA4-BCC7-CFB7A3C090FC}.Setup Debug|x64.ActiveCfg = Debug|Win32 @@ -658,4 +662,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0D6A044D-CA1A-4F12-8CC8-99C7513192AE} + EndGlobalSection EndGlobal