Created
April 11, 2025 04:52
-
-
Save eao197/1bbe345f8d268136541d87231da9be74 to your computer and use it in GitHub Desktop.
Эксперимент получения информации об CPU Set в Windows 10/11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define NOMINMAX | |
#include <windows.h> | |
#include <iostream> | |
#include <stdexcept> | |
#include <vector> | |
#include <print> | |
#include <format> | |
void | |
do_work() | |
{ | |
std::print( "GetActiveProcessorCount(ALL_PROCESSOR_GROUPS): {}\n", | |
GetActiveProcessorCount(ALL_PROCESSOR_GROUPS) ); | |
std::print( "GetActiveProcessorCount(0): {}\n", | |
GetActiveProcessorCount(0) ); | |
{ | |
ULONG required_sets_count{}; | |
if( !GetSystemCpuSetInformation( | |
nullptr, | |
0, | |
&required_sets_count, | |
GetCurrentProcess(), | |
0 /* flags */ ) ) | |
{ | |
const auto last_error = GetLastError(); | |
if( ERROR_INSUFFICIENT_BUFFER != last_error ) | |
throw std::runtime_error{ | |
std::format( | |
"unable to get number of CPU Sets, " | |
"GetSystemCpuSetInformation failed (GetLastError={})", | |
last_error ) | |
}; | |
} | |
if( !required_sets_count ) | |
{ | |
std::print( "There is no System CPU Sets to retrive\n" ); | |
return; | |
} | |
std::print( "System CPU Sets to retrive: {}\n", | |
required_sets_count ); | |
std::vector< SYSTEM_CPU_SET_INFORMATION > cpu_sets; | |
cpu_sets.resize( static_cast<std::size_t>(required_sets_count) ); | |
if( !GetSystemCpuSetInformation( | |
cpu_sets.data(), | |
cpu_sets.size(), | |
&required_sets_count, | |
GetCurrentProcess(), | |
0 /* flags */ ) ) | |
{ | |
throw std::runtime_error{ | |
std::format( | |
"unable to get CPU Sets information, " | |
"GetSystemCpuSetInformation failed (GetLastError={})", | |
GetLastError() ) | |
}; | |
} | |
std::print( "{} CPU Set(s) retrieved\n", required_sets_count ); | |
for( std::size_t i = 0; | |
i != static_cast<std::size_t>(required_sets_count); | |
++i ) | |
{ | |
const auto & cs = cpu_sets[ i ]; | |
std::print( "CPU Set #{}, type: {}\n", | |
i, | |
static_cast<int>(cs.Type)); | |
if( CpuSetInformation == cs.Type ) | |
{ | |
std::print( " Id: {}, Group: {}, " | |
"LogicalProcessorIndex: {}, CoreIndex: {}, " | |
"AllocationFlag: {}\n", | |
cs.CpuSet.Id, | |
cs.CpuSet.Group, | |
cs.CpuSet.LogicalProcessorIndex, | |
cs.CpuSet.CoreIndex, | |
cs.CpuSet.AllocationTag ); | |
} | |
} | |
} | |
ULONG required_id_count{}; | |
if( !GetProcessDefaultCpuSets( | |
GetCurrentProcess(), | |
nullptr, | |
0, | |
&required_id_count ) ) | |
{ | |
throw std::runtime_error{ | |
"unable to get number of CPU Set IDs, " | |
"GetProcessDefaultCpuSets failed" | |
}; | |
} | |
else | |
std::print( "CPU Set IDs to retrive: {} (GetLastError={})\n", | |
required_id_count, | |
GetLastError() ); | |
if( !required_id_count ) | |
{ | |
std::print( "There is no default CPU Sets for the process!\n" ); | |
return; | |
} | |
std::vector< ULONG > cpu_set_ids; | |
cpu_set_ids.resize( static_cast<std::size_t>(required_id_count) ); | |
if( !GetProcessDefaultCpuSets( | |
GetCurrentProcess(), | |
cpu_set_ids.data(), | |
cpu_set_ids.size(), | |
&required_id_count ) ) | |
{ | |
throw std::runtime_error{ | |
"unable to CPU Set IDs, " | |
"GetProcessDefaultCpuSets failed" | |
}; | |
} | |
} | |
int main() | |
{ | |
try | |
{ | |
do_work(); | |
} | |
catch( const std::exception & x ) | |
{ | |
std::cerr << "main: exception caught: " << x.what() << std::endl; | |
return 2; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment