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>
master
Ran Benita 2018-07-05 18:13:14 +03:00
parent 110d17c6be
commit c8e17eede5
8 changed files with 50 additions and 146 deletions

View File

@ -22,130 +22,54 @@
* DEALINGS IN THE SOFTWARE. * 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 <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <sys/time.h>
#include "bench.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 void
bench_timer_reset(struct bench_timer *self) bench_start(struct bench *bench)
{ {
#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)
{
#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; struct timeval val;
(void) gettimeofday(&val, NULL); (void) gettimeofday(&val, NULL);
bench->start = (struct bench_time) {
set_bench_time(&self->start, val.tv_sec, val.tv_usec); .seconds = val.tv_sec,
#endif .microseconds = val.tv_usec,
};
} }
void 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; struct timeval val;
(void) gettimeofday(&val, NULL); (void) gettimeofday(&val, NULL);
bench->stop = (struct bench_time) {
set_bench_time(&self->stop, val.tv_sec, val.tv_usec); .seconds = val.tv_sec,
#endif .microseconds = val.tv_usec,
};
} }
void 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->seconds = bench->stop.seconds - bench->start.seconds;
result->milliseconds = self->stop.milliseconds - self->start.milliseconds; result->microseconds = bench->stop.microseconds - bench->start.microseconds;
if (result->microseconds < 0) {
result->microseconds += 1000000;
result->seconds--;
}
} }
char * char *
bench_timer_get_elapsed_time_str(struct bench_timer *self) bench_elapsed_str(const struct bench *bench)
{ {
struct bench_time elapsed; struct bench_time elapsed;
char *buf; char *buf;
int ret; int ret;
bench_timer_get_elapsed_time(self, &elapsed); bench_elapsed(bench, &elapsed);
normalize_bench_time(&elapsed); ret = asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.microseconds);
ret = asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.milliseconds);
assert(ret >= 0); assert(ret >= 0);
return buf; return buf;

View File

@ -25,28 +25,25 @@
#ifndef LIBXKBCOMMON_BENCH_H #ifndef LIBXKBCOMMON_BENCH_H
#define LIBXKBCOMMON_BENCH_H #define LIBXKBCOMMON_BENCH_H
#include <stdint.h>
struct bench_time { struct bench_time {
long seconds; long seconds;
long milliseconds; long microseconds;
}; };
struct bench_timer { struct bench {
struct bench_time start; struct bench_time start;
struct bench_time stop; struct bench_time stop;
#if defined(__MACH__) && __MACH__ == 1
uint64_t scaling_factor;
#endif
}; };
void bench_timer_reset(struct bench_timer *self); void
bench_start(struct bench *bench);
void
bench_stop(struct bench *bench);
void bench_timer_start(struct bench_timer *self); void
void bench_timer_stop(struct bench_timer *self); bench_elapsed(const struct bench *bench, struct bench_time *result);
/* The caller is responsibile to free() the returned string. */
void bench_timer_get_elapsed_time(struct bench_timer *self, struct bench_time *result); char *
/* It's caller's responsibility to release the returned string using free(). */ bench_elapsed_str(const struct bench *bench);
char *bench_timer_get_elapsed_time_str(struct bench_timer *self);
#endif /* LIBXKBCOMMON_BENCH_H */ #endif /* LIBXKBCOMMON_BENCH_H */

View File

@ -37,7 +37,7 @@ main(void)
char *path; char *path;
FILE *file; FILE *file;
struct xkb_compose_table *table; struct xkb_compose_table *table;
struct bench_timer timer; struct bench bench;
char *elapsed; char *elapsed;
ctx = test_get_context(CONTEXT_NO_FLAG); 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_level(ctx, XKB_LOG_LEVEL_CRITICAL);
xkb_context_set_log_verbosity(ctx, 0); xkb_context_set_log_verbosity(ctx, 0);
bench_timer_reset(&timer); bench_start(&bench);
bench_timer_start(&timer);
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) { for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
rewind(file); rewind(file);
table = xkb_compose_table_new_from_file(ctx, file, "", table = xkb_compose_table_new_from_file(ctx, file, "",
@ -66,12 +64,12 @@ main(void)
assert(table); assert(table);
xkb_compose_table_unref(table); xkb_compose_table_unref(table);
} }
bench_timer_stop(&timer); bench_stop(&bench);
fclose(file); fclose(file);
free(path); free(path);
elapsed = bench_timer_get_elapsed_time_str(&timer); elapsed = bench_elapsed_str(&bench);
fprintf(stderr, "compiled %d compose tables in %ss\n", fprintf(stderr, "compiled %d compose tables in %ss\n",
BENCHMARK_ITERATIONS, elapsed); BENCHMARK_ITERATIONS, elapsed);
free(elapsed); free(elapsed);

View File

@ -30,7 +30,7 @@
#define BENCHMARK_ITERATIONS 20000000 #define BENCHMARK_ITERATIONS 20000000
static void static void
bench(struct xkb_state *state) bench_key_proc(struct xkb_state *state)
{ {
int8_t keys[256] = { 0 }; int8_t keys[256] = { 0 };
xkb_keycode_t keycode; xkb_keycode_t keycode;
@ -57,7 +57,7 @@ main(void)
struct xkb_context *ctx; struct xkb_context *ctx;
struct xkb_keymap *keymap; struct xkb_keymap *keymap;
struct xkb_state *state; struct xkb_state *state;
struct bench_timer timer; struct bench bench;
char *elapsed; char *elapsed;
ctx = test_get_context(0); ctx = test_get_context(0);
@ -75,13 +75,11 @@ main(void)
srand(time(NULL)); srand(time(NULL));
bench_timer_reset(&timer); bench_start(&bench);
bench_key_proc(state);
bench_stop(&bench);
bench_timer_start(&timer); elapsed = bench_elapsed_str(&bench);
bench(state);
bench_timer_stop(&timer);
elapsed = bench_timer_get_elapsed_time_str(&timer);
fprintf(stderr, "ran %d iterations in %ss\n", fprintf(stderr, "ran %d iterations in %ss\n",
BENCHMARK_ITERATIONS, elapsed); BENCHMARK_ITERATIONS, elapsed);
free(elapsed); free(elapsed);

View File

@ -39,7 +39,7 @@ main(int argc, char *argv[])
"evdev", "pc105", "us,il", ",", "ctrl:nocaps,grp:menu_toggle", "evdev", "pc105", "us,il", ",", "ctrl:nocaps,grp:menu_toggle",
}; };
struct xkb_component_names kccgst; struct xkb_component_names kccgst;
struct bench_timer timer; struct bench bench;
char *elapsed; char *elapsed;
ctx = test_get_context(0); 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_level(ctx, XKB_LOG_LEVEL_CRITICAL);
xkb_context_set_log_verbosity(ctx, 0); xkb_context_set_log_verbosity(ctx, 0);
bench_timer_reset(&timer); bench_start(&bench);
bench_timer_start(&timer);
for (i = 0; i < BENCHMARK_ITERATIONS; i++) { for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
assert(xkb_components_from_rules(ctx, &rmlvo, &kccgst)); assert(xkb_components_from_rules(ctx, &rmlvo, &kccgst));
free(kccgst.keycodes); free(kccgst.keycodes);
@ -58,9 +56,9 @@ main(int argc, char *argv[])
free(kccgst.compat); free(kccgst.compat);
free(kccgst.symbols); 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", fprintf(stderr, "processed %d rule files in %ss\n",
BENCHMARK_ITERATIONS, elapsed); BENCHMARK_ITERATIONS, elapsed);
free(elapsed); free(elapsed);

View File

@ -33,7 +33,7 @@ main(int argc, char *argv[])
{ {
struct xkb_context *ctx; struct xkb_context *ctx;
struct xkb_keymap *keymap; struct xkb_keymap *keymap;
struct bench_timer timer; struct bench bench;
char *elapsed; char *elapsed;
int i; 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_level(ctx, XKB_LOG_LEVEL_CRITICAL);
xkb_context_set_log_verbosity(ctx, 0); xkb_context_set_log_verbosity(ctx, 0);
bench_timer_reset(&timer); bench_start(&bench);
bench_timer_start(&timer);
for (i = 0; i < BENCHMARK_ITERATIONS; i++) { for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
keymap = test_compile_rules(ctx, "evdev", "evdev", "us", "", ""); keymap = test_compile_rules(ctx, "evdev", "evdev", "us", "", "");
assert(keymap); assert(keymap);
xkb_keymap_unref(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", fprintf(stderr, "compiled %d keymaps in %ss\n",
BENCHMARK_ITERATIONS, elapsed); BENCHMARK_ITERATIONS, elapsed);
free(elapsed); free(elapsed);

View File

@ -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]) 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]) 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 # Define a configuration option for the XKB config root
xkb_base=`$PKG_CONFIG --variable=xkb_base xkeyboard-config` xkb_base=`$PKG_CONFIG --variable=xkb_base xkeyboard-config`
AS_IF([test "x$xkb_base" = x], [ AS_IF([test "x$xkb_base" = x], [

View File

@ -421,13 +421,10 @@ endif
# Benchmarks. # Benchmarks.
# For clock_gettime, on some systems.
rt_dep = cc.find_library('rt', required: false)
libxkbcommon_bench_internal = static_library( libxkbcommon_bench_internal = static_library(
'xkbcommon-bench-internal', 'xkbcommon-bench-internal',
'bench/bench.c', 'bench/bench.c',
'bench/bench.h', 'bench/bench.h',
dependencies: rt_dep,
link_with: libxkbcommon_test_internal, link_with: libxkbcommon_test_internal,
) )
bench_dep = declare_dependency( bench_dep = declare_dependency(