Skip to content
Snippets Groups Projects
Commit fc8408b6 authored by Dominic Kempf's avatar Dominic Kempf
Browse files

Provide TSC frequency regardless of dmesg availability

parent 30b24c52
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,7 @@ namespace Dune {
#if __linux__
TSC::Counter get_tsc_frequency()
TSC::Counter get_tsc_frequency_from_dmesg()
{
int pipe_fds[2];
if (pipe(pipe_fds) < 0)
......@@ -99,7 +99,7 @@ namespace Dune {
DUNE_THROW(TSCError,"Child process failed with return status " << WEXITSTATUS(status));
if (result < 0)
DUNE_THROW(TSCError,"Could not find TSC frequency information in kernel log");
DUNE_THROW(TSCNotInKernelLog,"Could not find TSC frequency information in kernel log");
}
......@@ -109,6 +109,42 @@ namespace Dune {
}
TSC::Counter get_tsc_frequency_from_cpuinfo()
{
double result = -1.0;
try {
auto cpuinfo = std::ifstream("/proc/cpuinfo");
auto re = std::regex("bogomips\\s:\\s(\\d+\\.\\d+)$");
using std::getline;
for (std::string line ; getline(cpuinfo,line) ; )
{
if (auto match = std::smatch() ; std::regex_match(line,match,re))
{
result = 5e5 * std::stod(match[1].str());
break;
}
}
}
catch(std::exception& e) {
DUNE_THROW(TSCError,"error getting TSC frequency from /proc/cpuinfo: " << e.what());
}
if (result < 0)
DUNE_THROW(TSCError,"could not find bogomips value in /proc/cpuinfo");
return result;
}
TSC::Counter get_tsc_frequency()
{
try {
return get_tsc_frequency_from_dmesg();
}
catch (TSCNotInKernelLog&) {
return get_tsc_frequency_from_cpuinfo();
}
}
#elif __APPLE__
......
......@@ -11,6 +11,10 @@ namespace Dune {
: public Exception
{};
class TSCNotInKernelLog
: public TSCError
{};
class TSC
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment