mirror of
https://github.com/id-Software/DOOM-3-BFG.git
synced 2025-03-15 07:00:58 +00:00
Fix cpu clock-hz inaccuracy on linux
This commit is contained in:
parent
f2a7361fc0
commit
65f7a305c1
2 changed files with 5 additions and 49 deletions
|
@ -112,57 +112,15 @@ double Sys_ClockTicksPerSecond()
|
|||
static bool init = false;
|
||||
static double ret;
|
||||
|
||||
int fd, len, pos, end;
|
||||
char buf[ 4096 ];
|
||||
|
||||
if( init )
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
fd = open( "/proc/cpuinfo", O_RDONLY );
|
||||
if( fd == -1 )
|
||||
{
|
||||
common->Printf( "couldn't read /proc/cpuinfo\n" );
|
||||
ret = MeasureClockTicks();
|
||||
init = true;
|
||||
common->Printf( "measured CPU frequency: %g MHz\n", ret / 1000000.0 );
|
||||
return ret;
|
||||
}
|
||||
len = read( fd, buf, 4096 );
|
||||
close( fd );
|
||||
pos = 0;
|
||||
while( pos < len )
|
||||
{
|
||||
if( !idStr::Cmpn( buf + pos, "cpu MHz", 7 ) )
|
||||
{
|
||||
pos = strchr( buf + pos, ':' ) - buf + 2;
|
||||
end = strchr( buf + pos, '\n' ) - buf;
|
||||
if( pos < len && end < len )
|
||||
{
|
||||
buf[end] = '\0';
|
||||
ret = atof( buf + pos );
|
||||
}
|
||||
else
|
||||
{
|
||||
common->Printf( "failed parsing /proc/cpuinfo\n" );
|
||||
ret = MeasureClockTicks();
|
||||
init = true;
|
||||
common->Printf( "measured CPU frequency: %g MHz\n", ret / 1000000.0 );
|
||||
return ret;
|
||||
}
|
||||
common->Printf( "/proc/cpuinfo CPU frequency: %g MHz\n", ret );
|
||||
ret *= 1000000;
|
||||
init = true;
|
||||
return ret;
|
||||
}
|
||||
pos = strchr( buf + pos, '\n' ) - buf + 1;
|
||||
}
|
||||
common->Printf( "failed parsing /proc/cpuinfo\n" );
|
||||
ret = MeasureClockTicks();
|
||||
init = true;
|
||||
common->Printf( "measured CPU frequency: %g MHz\n", ret / 1000000.0 );
|
||||
return ret;
|
||||
common->Printf( "measured CPU frequency: %g MHz\n", ret / 1000000.0 );
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -264,11 +264,9 @@ double Sys_GetClockTicks()
|
|||
#else
|
||||
//#error unsupported CPU
|
||||
// RB begin
|
||||
struct timespec now;
|
||||
|
||||
clock_gettime( CLOCK_MONOTONIC, &now );
|
||||
|
||||
return now.tv_sec * 1000000000LL + now.tv_nsec;
|
||||
uint32_t lo, hi;
|
||||
__asm__ __volatile__ ( "rdtsc" : "=a" (lo), "=d" (hi));
|
||||
return ( ( ( uint64_t )hi ) << 32 ) | lo;
|
||||
// RB end
|
||||
#endif
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue