forked from btorpey/clocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SysTime.c
executable file
·60 lines (52 loc) · 1.57 KB
/
SysTime.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
55
56
57
58
59
#include "SysTime.h"
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#define ONE_BILLION 1000000000L
JNIEXPORT jlong JNICALL Java_SysTime_clocktime(JNIEnv *env, jclass jobj)
{
#if _POSIX_TIMERS > 0
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return (((jlong)ts.tv_sec) * ONE_BILLION) + ((jlong)ts.tv_nsec);
#else
#warning "_POSIX_TIMERS not defined, using gettimeofday"
struct timeval tv;
gettimeofday(&tv, NULL);
return (((jlong)tv.tv_sec) * ONE_BILLION) + ((jlong)tv.tv_usec*1000);
#endif
}
JNIEXPORT jlong JNICALL Java_SysTime_rdtsc(JNIEnv *env, jclass jobj)
{
unsigned int lo, hi;
asm volatile (
"rdtsc"
: "=a"(lo), "=d"(hi) /* outputs */
: "a"(0) /* inputs */
: "%ebx", "%ecx"); /* clobbers*/
long long x = ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
return (jlong) x;
}
JNIEXPORT jlong JNICALL Java_SysTime_cpuidrdtsc(JNIEnv *env, jclass jobj)
{
unsigned int lo, hi;
asm volatile (
"cpuid \n"
"rdtsc"
: "=a"(lo), "=d"(hi) /* outputs */
: "a"(0) /* inputs */
: "%ebx", "%ecx"); /* clobbers*/
long long x = ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
return (jlong) x;
}
JNIEXPORT jlong JNICALL Java_SysTime_rdtscp(JNIEnv *env, jclass jobj)
{
unsigned int lo, hi;
asm volatile (
"rdtscp"
: "=a"(lo), "=d"(hi) /* outputs */
: "a"(0) /* inputs */
: "%ebx", "%ecx"); /* clobbers*/
long long x = ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
return (jlong) x;
}