Last active
August 29, 2015 14:08
-
-
Save kimwalisch/a34bf029a492cba4112f to your computer and use it in GitHub Desktop.
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
| // Author: Kim Walisch | |
| // Released into public domain | |
| #include <windows.h> | |
| #include <cstdlib> | |
| typedef BOOL (WINAPI *LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD); | |
| /// Get the CPU's cache size in bytes. | |
| /// Works on Windows (MSVC, Intel C++ Compiler, Cygwin, ...). | |
| /// @param level 1 for L1, 2 for L2, 3 for L3. | |
| /// | |
| std::size_t get_cache_size(int level) | |
| { | |
| LPFN_GLPI glpi = (LPFN_GLPI) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation"); | |
| // GetLogicalProcessorInformation not supported | |
| if (glpi == NULL) | |
| return 0; | |
| DWORD buffer_bytes = 0; | |
| std::size_t cache_size = 0; | |
| glpi(0, &buffer_bytes); | |
| std::size_t size = buffer_bytes / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); | |
| SYSTEM_LOGICAL_PROCESSOR_INFORMATION* buffer = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION[size]; | |
| glpi(buffer, &buffer_bytes); | |
| for (std::size_t i = 0; i < size; i++) | |
| { | |
| if (buffer[i].Relationship == RelationCache && | |
| buffer[i].Cache.Level == level) | |
| { | |
| cache_size = buffer[i].Cache.Size; | |
| break; | |
| } | |
| } | |
| delete buffer; | |
| return cache_size; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment