From 605d39d72a19eb23d06369e6ccb5e9548441a234 Mon Sep 17 00:00:00 2001 From: ColumbusTech Date: Fri, 24 Nov 2017 21:27:52 +0300 Subject: [PATCH] Getting CPU Cache Size --- Info.hpp | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/Info.hpp b/Info.hpp index 46bf74c..7443c1e 100644 --- a/Info.hpp +++ b/Info.hpp @@ -1,35 +1,57 @@ #include -#include -#include +#include +#include +#include namespace Columbus { int GetCPUCount(); - - + int GetCPUCacheSize(); int GetCPUCount() { #ifdef __linux__ int counter = 0; + std::ifstream cpuinfo("/proc/cpuinfo"); + if (cpuinfo.is_open() == false) return 0; - FILE *cpuinfo = fopen("/proc/cpuinfo", "rb"); - - char* str; - size_t size; + std::string line; - while(getline(&str, &size, cpuinfo) != -1) + while(getline(cpuinfo, line)) { - if (strstr(str, "processor") != 0) + if (line.find("processor") != std::string::npos) counter++; } - - fclose(cpuinfo); + + cpuinfo.close(); return counter; #endif } + int GetCPUCacheSize() + { + #ifdef __linux__ + std::ifstream cpuinfo("/proc/cpuinfo"); + if (cpuinfo.is_open() == false) return 0; + + std::string line; + + while(getline(cpuinfo, line)) + { + if (line.find("cache size") != std::string::npos) + { + size_t sz = line.find(":") + 1; + size_t kb = line.find("KB"); + return std::atoi(line.substr(sz, kb - sz).c_str()); + } + } + + cpuinfo.close(); + return 0; + #endif + } + }