Fixed CPU and time issues for linux from: http://sourceforge.net/p/udt/discussion/393036/thread/0a4330b9/
This commit is contained in:
parent
0b24133f5d
commit
74a3d791f8
|
@ -43,6 +43,7 @@ written by
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
#ifdef MACOSX
|
#ifdef MACOSX
|
||||||
#include <mach/mach_time.h>
|
#include <mach/mach_time.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -123,7 +124,7 @@ void CTimer::rdtsc(uint64_t &x)
|
||||||
x = mach_absolute_time();
|
x = mach_absolute_time();
|
||||||
#else
|
#else
|
||||||
// use system call to read time clock for other archs
|
// use system call to read time clock for other archs
|
||||||
x = getTime();
|
x = getTime() * s_ullCPUFrequency;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,18 +132,48 @@ uint64_t CTimer::readCPUFrequency()
|
||||||
{
|
{
|
||||||
uint64_t frequency = 1; // 1 tick per microsecond.
|
uint64_t frequency = 1; // 1 tick per microsecond.
|
||||||
|
|
||||||
#if defined(LINUX) && (defined(I386) || defined(AMD64))
|
#if defined(LINUX)
|
||||||
uint64_t t1, t2;
|
// extract cpu frequency from /proc/cpuinfo
|
||||||
|
float mhz = 0;
|
||||||
|
char str[256], p = NULL;
|
||||||
|
int find = 0;
|
||||||
|
FILE * fd = fopen("/proc/cpuinfo", "r");
|
||||||
|
|
||||||
rdtsc(t1);
|
if (fd == NULL) {
|
||||||
timespec ts;
|
// perror("fopen /proc/cpuinfo");
|
||||||
ts.tv_sec = 0;
|
} else {
|
||||||
ts.tv_nsec = 100000000;
|
while (fgets(str, 256, fd)) {
|
||||||
nanosleep(&ts, NULL);
|
if (strncmp("cpu MHz", str, sizeof("cpu MHz")-1) == 0) {
|
||||||
rdtsc(t2);
|
find = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CPU clocks per microsecond
|
fclose(fd);
|
||||||
frequency = (t2 - t1) / 100000;
|
}
|
||||||
|
|
||||||
|
if (find) {
|
||||||
|
p = str;
|
||||||
|
while (p++ != ':');
|
||||||
|
|
||||||
|
sscanf(p, "%f", &mhz);
|
||||||
|
frequency = (uint64_t)mhz;
|
||||||
|
///printf("linux cpu MHz: %f, %lld\n", mhz, frequency);
|
||||||
|
} else {
|
||||||
|
printf("Warning!!! /proc/cpuinfo cpu MHz unknown\n");
|
||||||
|
// original behavior
|
||||||
|
uint64_t t1, t2;
|
||||||
|
|
||||||
|
rdtsc(t1);
|
||||||
|
timespec ts;
|
||||||
|
ts.tv_sec = 0;
|
||||||
|
ts.tv_nsec = 100000000;
|
||||||
|
nanosleep(&ts, NULL);
|
||||||
|
rdtsc(t2);
|
||||||
|
|
||||||
|
// CPU clocks per microsecond
|
||||||
|
frequency = (t2 - t1) / 100000;
|
||||||
|
}
|
||||||
#elif defined(WINDOWS)
|
#elif defined(WINDOWS)
|
||||||
int64_t ccf;
|
int64_t ccf;
|
||||||
if (QueryPerformanceFrequency((LARGE_INTEGER *)&ccf))
|
if (QueryPerformanceFrequency((LARGE_INTEGER *)&ccf))
|
||||||
|
|
Loading…
Reference in New Issue