diff options
-rw-r--r-- | src/sandglass.c | 14 | ||||
-rw-r--r-- | tests/monotonic-realticks.c | 6 |
2 files changed, 15 insertions, 5 deletions
diff --git a/src/sandglass.c b/src/sandglass.c index c3435d0..b9316e5 100644 --- a/src/sandglass.c +++ b/src/sandglass.c @@ -179,15 +179,25 @@ int sandglass_elapse(sandglass_t *sandglass) { long oldgrains = sandglass->grains; + sandglass_t baseline; if (sandglass_real_gettime(sandglass) != 0) return -1; sandglass->grains -= oldgrains; - /* Magical correction for timespec-based grains */ if (sandglass->grains < 0) + /* Magical correction for timespec-based grains */ sandglass->grains += sandglass->adjustment; - sandglass->grains /= sandglass->loops; + + if (sandglass->attributes.resolution == SANDGLASS_REALTICKS) { + baseline.attributes.incrementation = SANDGLASS_MONOTONIC; + baseline.attributes.resolution = SANDGLASS_CPUTIME; + baseline.loops = sandglass->loops; + + sandglass_bench(&baseline, { }); + sandglass->grains -= baseline.grains; + sandglass->grains /= sandglass->loops; + } return 0; } diff --git a/tests/monotonic-realticks.c b/tests/monotonic-realticks.c index 119b3bb..eb9e9f6 100644 --- a/tests/monotonic-realticks.c +++ b/tests/monotonic-realticks.c @@ -30,16 +30,16 @@ main() { sandglass_t sandglass; sandglass_attributes_t attr = { SANDGLASS_MONOTONIC, SANDGLASS_REALTICKS }; - struct timespec tosleep = { .tv_sec = 0, .tv_nsec = 111111111L }; + unsigned int i = 0; if (sandglass_create(&sandglass, &attr, &attr) != 0) { perror("sandglass_create()"); return EXIT_FAILURE; } - sandglass_bench(&sandglass, sandglass_spin(&tosleep)); + sandglass_bench(&sandglass, ++i); - printf("%g\n", sandglass.grains/sandglass.resolution); + printf("%ld\n", sandglass.grains); return EXIT_SUCCESS; } |