summaryrefslogtreecommitdiffstats
path: root/src/sandglass.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-09-17 13:33:06 +0000
committerTavian Barnes <tavianator@gmail.com>2009-09-17 13:33:06 +0000
commit9a358c3cdf8eb34754cc5544e2a5e0f67c022f8d (patch)
treea351b5a08f614e3ceeb7f63c7cf68f68377e54c5 /src/sandglass.c
parenta9f27bf33c6b6b6e9aac0eb1e69743120a11daa1 (diff)
downloadlibsandglass-9a358c3cdf8eb34754cc5544e2a5e0f67c022f8d.tar.xz
Improve sandglass_tsc_resolution(), and fix timespec-based grains counts.
Diffstat (limited to 'src/sandglass.c')
-rw-r--r--src/sandglass.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/sandglass.c b/src/sandglass.c
index 57729f9..3399b73 100644
--- a/src/sandglass.c
+++ b/src/sandglass.c
@@ -184,6 +184,9 @@ sandglass_elapse(sandglass_t *sandglass)
return -1;
sandglass->grains -= oldgrains;
+ /* Magical correction for timespec-based grains */
+ if (sandglass->grains < 0)
+ sandglass->grains += 2000000000L;
sandglass->grains /= sandglass->loops;
return 0;
@@ -219,12 +222,11 @@ sandglass_real_gettime(sandglass_t *sandglass)
if (sysconf(_SC_MONOTONIC_CLOCK) > 0) {
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
return -1;
- sandglass->grains = ts.tv_nsec;
} else {
if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
return -1;
- sandglass->grains = ts.tv_nsec;
}
+ sandglass->grains = sandglass_timespec_grains(&ts);
break;
default:
@@ -242,13 +244,12 @@ sandglass_real_gettime(sandglass_t *sandglass)
if (sysconf(_SC_THREAD_CPUTIME) > 0) {
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0)
return -1;
- sandglass->grains = ts.tv_nsec;
} else if (sysconf(_SC_CPUTIME) > 0) {
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) != 0)
return -1;
- sandglass->grains = ts.tv_nsec;
} else
return -1;
+ sandglass->grains = sandglass_timespec_grains(&ts);
break;
case SANDGLASS_SYSTEM: