Getting CPU Cache Size
parent
c136939988
commit
605d39d72a
46
Info.hpp
46
Info.hpp
|
|
@ -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;
|
|
||||||
size_t size;
|
|
||||||
|
|
||||||
while(getline(&str, &size, cpuinfo) != -1)
|
while(getline(cpuinfo, line))
|
||||||
{
|
{
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue