tagscan: damn it! forgot to remove debugging code
[k8muffin.git] / src / k8clock.c
blobddbf9f6b1feb9ed77d67fa154f6ae005f88cff1d
1 #include <ctype.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
6 #include "k8clock.h"
9 k8clock_type k8clock_initialized = K8CLOCK_ERROR;
10 k8clock_t clk;
13 #ifndef CLOCK_MONOTONIC_RAW
14 # define K8CLOCK_CLOCK_TYPE CLOCK_MONOTONIC_RAW
15 #else
16 # define K8CLOCK_CLOCK_TYPE CLOCK_MONOTONIC
17 #endif
20 static k8clock_type k8clock_check_source (void) {
21 FILE *fl = fopen("/sys/devices/system/clocksource/clocksource0/current_clocksource", "r");
22 char buf[128];
23 if (fl == NULL) {
24 fprintf(stderr, "WARNING: can't determine clock source!\n");
25 return K8CLOCK_OTHER;
27 memset(buf, 0, sizeof(buf));
28 if (fgets(buf, sizeof(buf)-1, fl) == NULL) {
29 fclose(fl);
30 fprintf(stderr, "WARNING: can't determine clock source!\n");
31 return K8CLOCK_OTHER;
33 fclose(fl);
34 while (buf[0] && isspace(buf[strlen(buf)-1])) buf[strlen(buf)-1] = 0;
35 //fprintf(stderr, "clock source: %s\n", buf);
36 return (strcasecmp(buf, "hpet") == 0 ? K8CLOCK_HPET : K8CLOCK_OTHER);
40 __attribute__((constructor)) static void k8clock_ctor (void) {
41 if (k8clock_initialized == K8CLOCK_ERROR) {
42 struct timespec cres;
43 k8clock_initialized = k8clock_check_source();
44 if (clock_getres(K8CLOCK_CLOCK_TYPE, &cres) != 0) {
45 fprintf(stderr, "ERROR: can't get clock resolution!\n");
46 k8clock_initialized = K8CLOCK_ERROR;
47 return;
49 //fprintf(stderr, "CLOCK_MONOTONIC: %ld:%ld\n", cres.tv_sec, cres.tv_nsec);
50 if (cres.tv_sec > 0 || cres.tv_nsec > (long)1000000*10 /*10 ms*/) {
51 fprintf(stderr, "ERROR: real-time clock resolution is too low!\n");
52 k8clock_initialized = K8CLOCK_ERROR;
53 return;
55 k8clock_init(&clk);
60 void k8clock_init (k8clock_t *clk) {
61 if (clk != NULL && k8clock_initialized != K8CLOCK_ERROR) {
62 if (clock_gettime(K8CLOCK_CLOCK_TYPE, &clk->stt) != 0) {
63 fprintf(stderr, "ERROR: can't get real-time clock value!\n");
69 uint64_t k8clock_ms (k8clock_t *clk) {
70 if (clk != NULL && k8clock_initialized != K8CLOCK_ERROR) {
71 struct timespec ts;
72 if (clock_gettime(K8CLOCK_CLOCK_TYPE, &ts) != 0) {
73 fprintf(stderr, "ERROR: can't get real-time clock value!\n");
74 return 0;
76 // ah, ignore nanoseconds in clk->stt here: we need only 'differential' time, and it can start with something weird
77 return ((int64_t)(ts.tv_sec-clk->stt.tv_sec))*1000+(ts.tv_nsec-clk->stt.tv_nsec)/1000000+1;
79 return 0;
83 uint64_t k8clock_micro (k8clock_t *clk) {
84 if (clk != NULL && k8clock_initialized != K8CLOCK_ERROR) {
85 struct timespec ts;
86 if (clock_gettime(K8CLOCK_CLOCK_TYPE, &ts) != 0) {
87 fprintf(stderr, "ERROR: can't get real-time clock value!\n");
88 return 0;
90 // ah, ignore nanoseconds in clk->stt here: we need only 'differential' time, and it can start with something weird
91 return ((int64_t)(ts.tv_sec-clk->stt.tv_sec))*1000000+(ts.tv_nsec-clk->stt.tv_nsec)/1000+1;
93 return 0;
97 uint64_t k8clock (void) {
98 return k8clock_ms(&clk);
102 uint64_t k8clockmicro (void) {
103 return k8clock_micro(&clk);