1
0
Fork 0
mirror of https://github.com/anrieff/libcpuid synced 2024-12-16 16:35:45 +00:00

cpuid_tool: Added --clock-os key to enable queries to cpu_clock_by_os()

rdtsc.c: Ported CPU clocking code to Win32, debugged Registry stuff

git-svn-id: https://svn.code.sf.net/p/libcpuid/code/HEAD/libcpuid@30 3b4be424-7ac5-41d7-8526-f4ddcb85d872
This commit is contained in:
Veselin Georgiev 2008-11-21 19:05:18 +00:00
parent 9cf8005f3a
commit 2fb7b0522b
2 changed files with 16 additions and 14 deletions

View file

@ -81,6 +81,7 @@ typedef enum {
NEED_CODENAME,
NEED_FEATURES,
NEED_CLOCK,
NEED_CLOCK_OS,
NEED_CLOCK_RDTSC,
} output_data_switch;
@ -128,6 +129,7 @@ matchtable[] = {
{ NEED_CODENAME , "--codename" , 1},
{ NEED_FEATURES , "--flags" , 1},
{ NEED_CLOCK , "--clock" , 0},
{ NEED_CLOCK_OS , "--clock-os" , 0},
{ NEED_CLOCK_RDTSC , "--clock-rdtsc" , 1},
};
@ -378,6 +380,9 @@ static void print_info(output_data_switch query, struct cpu_raw_data_t* raw,
case NEED_CLOCK:
fprintf(fout, "%d\n", cpu_clock());
break;
case NEED_CLOCK_OS:
fprintf(fout, "%d\n", cpu_clock_by_os());
break;
case NEED_CLOCK_RDTSC:
fprintf(fout, "%d\n", cpu_clock_measure(400, 1));
break;

View file

@ -31,12 +31,13 @@
#include <windows.h>
static void sys_precise_clock(uint64_t *result)
{
double c, f;
LARGE_INTEGER freq, counter;
QueryPerformanceCounter(&counter);
QueryPerformanceFrequency(&freq);
double c = counter.QuadPart;
double f = freq.QuadPart;
result = (uint64_t) ( c * 1000000.0 / f );
c = (double) counter.QuadPart;
f = (double) freq.QuadPart;
*result = (uint64_t) ( c * 1000000.0 / f );
}
#else
/* assuming Linux, Mac OS or other POSIX */
@ -91,23 +92,19 @@ int cpu_clock_by_mark(struct cpu_mark_t* mark)
int cpu_clock_by_os(void)
{
HKEY key;
char buff[20];
DWORD size = 20;
int result;
DWORD result;
DWORD size = 4;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\~MHz", 0, KEY_READ, &key) != ERROR_SUCCESS)
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), 0, KEY_READ, &key) != ERROR_SUCCESS)
return -1;
if (RegQueryValueEx(key, "~MHz", NULL, NULL, (LPBYTE) buff, (LPDWORD) &size) != ERROR_SUCCESS) {
if (RegQueryValueEx(key, TEXT("~MHz"), NULL, NULL, (LPBYTE) &result, (LPDWORD) &size) != ERROR_SUCCESS) {
RegCloseKey(key);
return -1;
}
buff[size] = 0;
RegCloseKey(key);
if (1 != sscanf(buff, "%d", &result))
return -1;
return result;
return (int)result;
}
#else
/* Assuming Linux with /proc/cpuinfo */
@ -175,7 +172,7 @@ int cpu_clock_measure(int millis, int quad_check)
mark_t_subtract(&end[k], &begin[k], &temp);
results[k] = cpu_clock_by_mark(&temp);
}
if (n == 1) return results[k];
if (n == 1) return results[0];
mdiff = 0x7fffffff;
bi = bj = -1;
for (i = 0; i < 4; i++) {
@ -198,6 +195,6 @@ int cpu_clock(void)
int result;
result = cpu_clock_by_os();
if (result <= 0)
result = cpu_clock_measure(200, 1);
result = cpu_clock_measure(200, 0);
return result;
}