-
Notifications
You must be signed in to change notification settings - Fork 0
/
freq.c
54 lines (42 loc) · 1.04 KB
/
freq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
unsigned long long int rdtsc(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
int freq()
{
struct timezone tz;
struct timeval tvstart, tvstop;
unsigned long long int cycles[2];
unsigned long microseconds;
int mhz;
memset(&tz, 0, sizeof(tz));
gettimeofday(&tvstart, &tz);
cycles[0] = rdtsc();
gettimeofday(&tvstart, &tz);
usleep(250000);
gettimeofday(&tvstop, &tz);
cycles[1] = rdtsc();
gettimeofday(&tvstop, &tz);
microseconds = ((tvstop.tv_sec-tvstart.tv_sec)*1000000) + (tvstop.tv_usec-tvstart.tv_usec);
mhz = (int) (cycles[1]-cycles[0]) / microseconds;
// printf("%i MHz\n",mhz);
return mhz;
}
// OCAML binding
#include <errno.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/compatibility.h>
#include <caml/fail.h>
value freq_get()
{
CAMLparam0();
CAMLreturn(Val_int(freq()));
}