bench: simplify the bench helpers Trim the API a bit. Also, just always use gettimeofday(), which is portable. Hopefully the system clock doesn't change while a benchmark is running. Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
diff --git a/bench/bench.c b/bench/bench.c
index 6aa43d9..30889ef 100644
--- a/bench/bench.c
+++ b/bench/bench.c
@@ -22,130 +22,54 @@
* DEALINGS IN THE SOFTWARE.
*/
-#if defined(HAVE_CLOCK_GETTIME)
-#define USE_CLOCK_GETTIME
-#include <time.h>
-#elif defined(__MACH__) && __MACH__ == 1
-#define USE_MACH_ABSOLUTE_TIME
-#include <mach/mach_time.h>
-#else
-/* gettimeofday() - a last resort */
-#include <sys/time.h>
-#endif
-
#include <assert.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
#include <stdio.h>
+#include <sys/time.h>
#include "bench.h"
-static void
-set_bench_time(struct bench_time *dest, long seconds, long milliseconds)
-{
- dest->seconds = seconds;
- dest->milliseconds = milliseconds;
-}
-
-static void
-normalize_bench_time(struct bench_time *obj)
-{
- if (obj->milliseconds >= 0) {
- return;
- }
- obj->milliseconds += 1000000;
- obj->seconds--;
-}
-
-void
-bench_timer_reset(struct bench_timer *self)
-{
-#if defined(USE_MACH_ABSOLUTE_TIME)
- mach_timebase_info_data_t info;
- if (mach_timebase_info(&info) == 0) {
- self->scaling_factor = info.numer / info.denom;
- }
-#endif
- self->start.seconds = 0L;
- self->start.milliseconds = 0L;
- self->stop.seconds = 0L;
- self->stop.milliseconds = 0L;
-}
-
void
-bench_timer_start(struct bench_timer *self)
+bench_start(struct bench *bench)
{
-#if defined(USE_CLOCK_GETTIME)
- struct timespec val;
-
- (void) clock_gettime(CLOCK_MONOTONIC, &val);
-
- /* With conversion from nanosecond to millisecond */
- set_bench_time(&self->start, val.tv_sec, val.tv_nsec / 1000);
-#elif defined(USE_MACH_ABSOLUTE_TIME)
- uint64_t val;
-
- val = mach_absolute_time();
-
- /* With conversion from nanosecond to millisecond */
- set_bench_time(&self->start,
- self->scaling_factor * val / 1000000000,
- self->scaling_factor * val % 1000000000 / 1000);
-#else
struct timeval val;
-
(void) gettimeofday(&val, NULL);
-
- set_bench_time(&self->start, val.tv_sec, val.tv_usec);
-#endif
+ bench->start = (struct bench_time) {
+ .seconds = val.tv_sec,
+ .microseconds = val.tv_usec,
+ };
}
void
-bench_timer_stop(struct bench_timer *self)
+bench_stop(struct bench *bench)
{
-#if defined(USE_CLOCK_GETTIME)
- struct timespec val;
-
- (void) clock_gettime(CLOCK_MONOTONIC, &val);
-
- /* With conversion from nanosecond to millisecond */
- set_bench_time(&self->stop, val.tv_sec, val.tv_nsec / 1000);
-#elif defined(USE_MACH_ABSOLUTE_TIME)
- uint64_t val;
-
- val = mach_absolute_time();
-
- /* With conversion from nanosecond to millisecond */
- set_bench_time(&self->stop,
- self->scaling_factor * val / 1000000000,
- self->scaling_factor * val % 1000000000 / 1000);
-#else
struct timeval val;
-
(void) gettimeofday(&val, NULL);
-
- set_bench_time(&self->stop, val.tv_sec, val.tv_usec);
-#endif
+ bench->stop = (struct bench_time) {
+ .seconds = val.tv_sec,
+ .microseconds = val.tv_usec,
+ };
}
void
-bench_timer_get_elapsed_time(struct bench_timer *self, struct bench_time *result)
+bench_elapsed(const struct bench *bench, struct bench_time *result)
{
- result->seconds = self->stop.seconds - self->start.seconds;
- result->milliseconds = self->stop.milliseconds - self->start.milliseconds;
+ result->seconds = bench->stop.seconds - bench->start.seconds;
+ result->microseconds = bench->stop.microseconds - bench->start.microseconds;
+ if (result->microseconds < 0) {
+ result->microseconds += 1000000;
+ result->seconds--;
+ }
}
char *
-bench_timer_get_elapsed_time_str(struct bench_timer *self)
+bench_elapsed_str(const struct bench *bench)
{
struct bench_time elapsed;
char *buf;
int ret;
- bench_timer_get_elapsed_time(self, &elapsed);
- normalize_bench_time(&elapsed);
- ret = asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.milliseconds);
+ bench_elapsed(bench, &elapsed);
+ ret = asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.microseconds);
assert(ret >= 0);
return buf;
diff --git a/bench/bench.h b/bench/bench.h
index adc15bb..facee3d 100644
--- a/bench/bench.h
+++ b/bench/bench.h
@@ -25,28 +25,25 @@
#ifndef LIBXKBCOMMON_BENCH_H
#define LIBXKBCOMMON_BENCH_H
-#include <stdint.h>
-
struct bench_time {
long seconds;
- long milliseconds;
+ long microseconds;
};
-struct bench_timer {
+struct bench {
struct bench_time start;
struct bench_time stop;
-#if defined(__MACH__) && __MACH__ == 1
- uint64_t scaling_factor;
-#endif
};
-void bench_timer_reset(struct bench_timer *self);
-
-void bench_timer_start(struct bench_timer *self);
-void bench_timer_stop(struct bench_timer *self);
+void
+bench_start(struct bench *bench);
+void
+bench_stop(struct bench *bench);
-void bench_timer_get_elapsed_time(struct bench_timer *self, struct bench_time *result);
-/* It's caller's responsibility to release the returned string using free(). */
-char *bench_timer_get_elapsed_time_str(struct bench_timer *self);
+void
+bench_elapsed(const struct bench *bench, struct bench_time *result);
+/* The caller is responsibile to free() the returned string. */
+char *
+bench_elapsed_str(const struct bench *bench);
#endif /* LIBXKBCOMMON_BENCH_H */
diff --git a/bench/compose.c b/bench/compose.c
index e2bf3e0..bb949d8 100644
--- a/bench/compose.c
+++ b/bench/compose.c
@@ -37,7 +37,7 @@ main(void)
char *path;
FILE *file;
struct xkb_compose_table *table;
- struct bench_timer timer;
+ struct bench bench;
char *elapsed;
ctx = test_get_context(CONTEXT_NO_FLAG);
@@ -55,9 +55,7 @@ main(void)
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
xkb_context_set_log_verbosity(ctx, 0);
- bench_timer_reset(&timer);
-
- bench_timer_start(&timer);
+ bench_start(&bench);
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
rewind(file);
table = xkb_compose_table_new_from_file(ctx, file, "",
@@ -66,12 +64,12 @@ main(void)
assert(table);
xkb_compose_table_unref(table);
}
- bench_timer_stop(&timer);
+ bench_stop(&bench);
fclose(file);
free(path);
- elapsed = bench_timer_get_elapsed_time_str(&timer);
+ elapsed = bench_elapsed_str(&bench);
fprintf(stderr, "compiled %d compose tables in %ss\n",
BENCHMARK_ITERATIONS, elapsed);
free(elapsed);
diff --git a/bench/key-proc.c b/bench/key-proc.c
index 56b396a..c09b389 100644
--- a/bench/key-proc.c
+++ b/bench/key-proc.c
@@ -30,7 +30,7 @@
#define BENCHMARK_ITERATIONS 20000000
static void
-bench(struct xkb_state *state)
+bench_key_proc(struct xkb_state *state)
{
int8_t keys[256] = { 0 };
xkb_keycode_t keycode;
@@ -57,7 +57,7 @@ main(void)
struct xkb_context *ctx;
struct xkb_keymap *keymap;
struct xkb_state *state;
- struct bench_timer timer;
+ struct bench bench;
char *elapsed;
ctx = test_get_context(0);
@@ -75,13 +75,11 @@ main(void)
srand(time(NULL));
- bench_timer_reset(&timer);
+ bench_start(&bench);
+ bench_key_proc(state);
+ bench_stop(&bench);
- bench_timer_start(&timer);
- bench(state);
- bench_timer_stop(&timer);
-
- elapsed = bench_timer_get_elapsed_time_str(&timer);
+ elapsed = bench_elapsed_str(&bench);
fprintf(stderr, "ran %d iterations in %ss\n",
BENCHMARK_ITERATIONS, elapsed);
free(elapsed);
diff --git a/bench/rules.c b/bench/rules.c
index c5d7dc8..751370f 100644
--- a/bench/rules.c
+++ b/bench/rules.c
@@ -39,7 +39,7 @@ main(int argc, char *argv[])
"evdev", "pc105", "us,il", ",", "ctrl:nocaps,grp:menu_toggle",
};
struct xkb_component_names kccgst;
- struct bench_timer timer;
+ struct bench bench;
char *elapsed;
ctx = test_get_context(0);
@@ -48,9 +48,7 @@ main(int argc, char *argv[])
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
xkb_context_set_log_verbosity(ctx, 0);
- bench_timer_reset(&timer);
-
- bench_timer_start(&timer);
+ bench_start(&bench);
for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
assert(xkb_components_from_rules(ctx, &rmlvo, &kccgst));
free(kccgst.keycodes);
@@ -58,9 +56,9 @@ main(int argc, char *argv[])
free(kccgst.compat);
free(kccgst.symbols);
}
- bench_timer_stop(&timer);
+ bench_stop(&bench);
- elapsed = bench_timer_get_elapsed_time_str(&timer);
+ elapsed = bench_elapsed_str(&bench);
fprintf(stderr, "processed %d rule files in %ss\n",
BENCHMARK_ITERATIONS, elapsed);
free(elapsed);
diff --git a/bench/rulescomp.c b/bench/rulescomp.c
index 650ccf3..aecb37f 100644
--- a/bench/rulescomp.c
+++ b/bench/rulescomp.c
@@ -33,7 +33,7 @@ main(int argc, char *argv[])
{
struct xkb_context *ctx;
struct xkb_keymap *keymap;
- struct bench_timer timer;
+ struct bench bench;
char *elapsed;
int i;
@@ -43,17 +43,15 @@ main(int argc, char *argv[])
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
xkb_context_set_log_verbosity(ctx, 0);
- bench_timer_reset(&timer);
-
- bench_timer_start(&timer);
+ bench_start(&bench);
for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
keymap = test_compile_rules(ctx, "evdev", "evdev", "us", "", "");
assert(keymap);
xkb_keymap_unref(keymap);
}
- bench_timer_stop(&timer);
+ bench_stop(&bench);
- elapsed = bench_timer_get_elapsed_time_str(&timer);
+ elapsed = bench_elapsed_str(&bench);
fprintf(stderr, "compiled %d keymaps in %ss\n",
BENCHMARK_ITERATIONS, elapsed);
free(elapsed);
diff --git a/configure.ac b/configure.ac
index 4da1e16..475cc4c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -100,12 +100,6 @@ AM_CONDITIONAL([HAVE_NO_UNDEFINED], [test "x$have_no_undefined" = xyes])
XORG_CHECK_LINKER_FLAGS([-Wl,--version-script="$srcdir/xkbcommon.map"], [have_version_script=yes])
AM_CONDITIONAL([HAVE_VERSION_SCRIPT], [test "x$have_version_script" = xyes])
-AC_CHECK_LIB(rt, clock_gettime,
- [AC_SUBST(RT_LIBS, "-lrt")],
- [AC_SUBST(RT_LIBS, "")],
- [-lrt])
-AC_CHECK_FUNCS([clock_gettime])
-
# Define a configuration option for the XKB config root
xkb_base=`$PKG_CONFIG --variable=xkb_base xkeyboard-config`
AS_IF([test "x$xkb_base" = x], [
diff --git a/meson.build b/meson.build
index bf4a5f8..7ee4f78 100644
--- a/meson.build
+++ b/meson.build
@@ -421,13 +421,10 @@ endif
# Benchmarks.
-# For clock_gettime, on some systems.
-rt_dep = cc.find_library('rt', required: false)
libxkbcommon_bench_internal = static_library(
'xkbcommon-bench-internal',
'bench/bench.c',
'bench/bench.h',
- dependencies: rt_dep,
link_with: libxkbcommon_test_internal,
)
bench_dep = declare_dependency(