diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-09-21 03:48:23 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-09-21 03:48:23 +0000 |
commit | 5f446ac4c9d98c7819ff836c0666c798f351ae2c (patch) | |
tree | bdd7fa89e4241c1a3ced8ff4a7074d779df2a053 /src | |
parent | 0b12bb29adc33255b97d2fb995ef145f0127d950 (diff) | |
download | libsandglass-5f446ac4c9d98c7819ff836c0666c798f351ae2c.tar.xz |
Make baseline calculations more accurate for SANDGLASS_REALTICKS.
Perform the baseline calculation directly in the sandglass_bench() macro, so
that the baseline is calculated in the same place (and therefore optimization
level, etc.) as the benchmarked code.
Diffstat (limited to 'src')
-rw-r--r-- | src/sandglass.c | 17 | ||||
-rw-r--r-- | src/sandglass.h | 26 |
2 files changed, 25 insertions, 18 deletions
diff --git a/src/sandglass.c b/src/sandglass.c index b9316e5..6db28a3 100644 --- a/src/sandglass.c +++ b/src/sandglass.c @@ -189,16 +189,6 @@ sandglass_elapse(sandglass_t *sandglass) /* Magical correction for timespec-based grains */ sandglass->grains += sandglass->adjustment; - 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; } @@ -213,13 +203,6 @@ sandglass_real_gettime(sandglass_t *sandglass) case SANDGLASS_MONOTONIC: switch (sandglass->attributes.resolution) { case SANDGLASS_REALTICKS: -#ifdef SANDGLASS_TSC - sandglass->grains = sandglass_get_tsc(); - break; -#else - return -1; -#endif - case SANDGLASS_CPUTIME: #ifdef SANDGLASS_TSC sandglass->grains = sandglass_get_tsc(); diff --git a/src/sandglass.h b/src/sandglass.h index 7073fb9..0d5e5e5 100644 --- a/src/sandglass.h +++ b/src/sandglass.h @@ -102,8 +102,12 @@ struct sandglass_t /* Adjustment to be added for negative (i.e. overflowed) grains counts */ long adjustment; - /* For SANDGLASS_TICKS looping support */ + /* For SANDGLASS_REALTICKS looping support */ unsigned int i, loops; + + /* A field used by sandglass_bench() to store the overhead of + sandglass_begin()/_elapse(), and of looping */ + long baseline; }; typedef struct sandglass_t sandglass_t; @@ -141,7 +145,23 @@ int sandglass_elapse(sandglass_t *sandglass); */ #define sandglass_bench(sandglass, routine) \ do { \ + /* Warm up the cache for these functions */ \ + sandglass_begin(sandglass); \ + sandglass_elapse(sandglass); \ + \ + /* Time an empty loop for our baseline */ \ + sandglass_begin(sandglass); \ + for ((sandglass)->i = 0; \ + (sandglass)->i < (sandglass)->loops; \ + ++(sandglass)->i) { \ + SANDGLASS_NO_UNROLL(); \ + } \ + sandglass_elapse(sandglass); \ + (sandglass)->baseline = (sandglass)->grains; \ + \ + /* Warm up the cache for our routine */ \ routine; \ + /* Time our routine in a loop */ \ sandglass_begin(sandglass); \ for ((sandglass)->i = 0; \ (sandglass)->i < (sandglass)->loops; \ @@ -150,6 +170,10 @@ int sandglass_elapse(sandglass_t *sandglass); routine; \ } \ sandglass_elapse(sandglass); \ + \ + /* Subtract the baseline and divide by the loop count */ \ + (sandglass)->grains -= (sandglass)->baseline; \ + (sandglass)->grains /= (sandglass)->loops; \ } while (0) #ifdef __cplusplus |