Created
February 16, 2014 07:43
-
-
Save mikaelhg/9030759 to your computer and use it in GitHub Desktop.
Run JDK check for Linux clock monotonicity manually
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc check_clock.c -ldl -o check_clock | |
// ripped from http://hg.openjdk.java.net/jdk8u/jdk8u-gate/hotspot/file/9c2ddd17626e/src/os/linux/vm/os_linux.cpp | |
#include <dlfcn.h> | |
#include <stdio.h> | |
#include <linux/time.h> | |
typedef int clockid_t; | |
int main(int argc, const char* argv[]) { | |
// we do dlopen's in this particular order due to bug in linux | |
// dynamical loader (see 6348968) leading to crash on exit | |
void* handle = dlopen("librt.so.1", RTLD_LAZY); | |
if (handle == NULL) { | |
handle = dlopen("librt.so", RTLD_LAZY); | |
} | |
if (handle) { | |
int (*clock_getres_func)(clockid_t, struct timespec*) = | |
(int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres"); | |
int (*clock_gettime_func)(clockid_t, struct timespec*) = | |
(int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime"); | |
if (clock_getres_func && clock_gettime_func) { | |
// See if monotonic clock is supported by the kernel. Note that some | |
// early implementations simply return kernel jiffies (updated every | |
// 1/100 or 1/1000 second). It would be bad to use such a low res clock | |
// for nano time (though the monotonic property is still nice to have). | |
// It's fixed in newer kernels, however clock_getres() still returns | |
// 1/HZ. We check if clock_getres() works, but will ignore its reported | |
// resolution for now. Hopefully as people move to new kernels, this | |
// won't be a problem. | |
struct timespec res; | |
struct timespec tp; | |
if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 && | |
clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) { | |
printf("monotonic clock supported\n"); | |
return; | |
} else { | |
printf("monotonic clock NOT supported\n"); | |
// close librt if there is no monotonic clock | |
dlclose(handle); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment