- 1 year ago
Any way to make program run only on a performance core (P-core)?
Yes, it is possible to make a program run only on a performance core (P-core) using the Windows API. Here are the steps to achieve this:
First, you need to identify the performance core. You can use the
GetLogicalProcessorInformationEx
function of the Windows API to get the information about the processor cores. This function returns an array ofSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
structures, which contains the information about each logical processor.Once you have the information about the logical processors, you can identify the performance core based on the
Relationship
andProcessor.Group
fields of theSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
structure. The performance cores are usually the ones that belong to the highest numbered group.Finally, you can set the affinity of the process to the performance core using the
SetProcessAffinityMask
function. This function takes the process handle and a bitmask that specifies the affinity of the process to the processor cores. You can use theSetThreadAffinityMask
function to set the affinity of individual threads.
Here is an example code snippet in C++ that demonstrates how to set the affinity of a process to the performance core:
c++#include <windows.h>
int main()
{
// Get information about the logical processors
DWORD bufferSize = 0;
GetLogicalProcessorInformationEx(RelationAll, nullptr, &bufferSize);
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(bufferSize);
GetLogicalProcessorInformationEx(RelationAll, buffer, &bufferSize);
// Find the performance core
DWORD maxGroup = 0;
DWORD maxNumber = 0;
for (DWORD i = 0; i < bufferSize / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX); i++)
{
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX& info = buffer[i];
if (info.Relationship == RelationProcessorCore && info.Processor.Group > maxGroup)
{
maxGroup = info.Processor.Group;
maxNumber = info.Processor.Number;
}
}
// Set the affinity of the process to the performance core
HANDLE process = GetCurrentProcess();
DWORD_PTR affinityMask = 1ULL << maxNumber;
SetProcessAffinityMask(process, affinityMask);
// Free the buffer
free(buffer);
// Run the program
// ...
}
Note that the above code only sets the affinity of the current process to the performance core. If you want to set the affinity of another process, you need to first obtain a handle to that process using the OpenProcess
function.