Getting CPU Cache Size

master
ColumbusTech 2017-11-24 21:27:52 +03:00
parent c136939988
commit 605d39d72a
1 changed files with 34 additions and 12 deletions

View File

@ -1,35 +1,57 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <fstream>
#include <cstring> #include <string>
#include <iostream>
namespace Columbus namespace Columbus
{ {
int GetCPUCount(); int GetCPUCount();
int GetCPUCacheSize();
int GetCPUCount() int GetCPUCount()
{ {
#ifdef __linux__ #ifdef __linux__
int counter = 0; int counter = 0;
std::ifstream cpuinfo("/proc/cpuinfo");
if (cpuinfo.is_open() == false) return 0;
FILE *cpuinfo = fopen("/proc/cpuinfo", "rb"); std::string line;
char* str; while(getline(cpuinfo, line))
size_t size;
while(getline(&str, &size, cpuinfo) != -1)
{ {
if (strstr(str, "processor") != 0) if (line.find("processor") != std::string::npos)
counter++; counter++;
} }
fclose(cpuinfo); cpuinfo.close();
return counter; return counter;
#endif #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
}
} }