From d156b9b21f031f935fed91e49cd82cb8b72e9534 Mon Sep 17 00:00:00 2001 From: Alexey Kopytov Date: Thu, 2 Feb 2017 15:25:54 +0300 Subject: [PATCH] Exclude queries executed by background threads from statistic reports. --- src/db_driver.c | 6 ++---- src/sb_counter.c | 5 +---- src/sysbench.c | 45 +++++++++++++++++++++++++++++++++++++++++---- src/sysbench.h | 6 ++++++ 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/db_driver.c b/src/db_driver.c index 3789afa..7c69b2e 100644 --- a/src/db_driver.c +++ b/src/db_driver.c @@ -192,10 +192,8 @@ static void db_init(void) /* Initialize timers if in debug mode */ if (db_globals.debug) { - exec_timers = (sb_timer_t *) malloc(sb_globals.threads * - sizeof(sb_timer_t)); - fetch_timers = (sb_timer_t *) malloc(sb_globals.threads * - sizeof(sb_timer_t)); + exec_timers = sb_alloc_per_thread_array(sizeof(sb_timer_t)); + fetch_timers = sb_alloc_per_thread_array(sizeof(sb_timer_t)); } db_reset_stats(); diff --git a/src/sb_counter.c b/src/sb_counter.c index 2b3735c..af2b176 100644 --- a/src/sb_counter.c +++ b/src/sb_counter.c @@ -34,10 +34,7 @@ int sb_counters_init(void) { SB_COMPILE_TIME_ASSERT(sizeof(sb_counters_t) % CK_MD_CACHELINE == 0); - sb_counters = sb_memalign(sb_globals.threads * sizeof(sb_counters_t), - CK_MD_CACHELINE); - - memset(sb_counters, 0, sb_globals.threads * sizeof(sb_counters_t)); + sb_counters = sb_alloc_per_thread_array(sizeof(sb_counters_t)); return sb_counters == NULL; } diff --git a/src/sysbench.c b/src/sysbench.c index eaed9d9..523844e 100644 --- a/src/sysbench.c +++ b/src/sysbench.c @@ -85,6 +85,12 @@ /* Wait at most this number of seconds for worker threads to initialize */ #define THREAD_INIT_TIMEOUT 30 +/* + Extra thread ID assigned to background threads. This may be used as an index + into per-thread arrays (see comment in sb_alloc_per_thread_array(). +*/ +#define SB_BACKGROUND_THREAD_ID sb_globals.threads + /* General options */ sb_arg_t general_args[] = { @@ -892,6 +898,8 @@ static void *eventgen_thread_proc(void *arg) (void)arg; /* unused */ + sb_tls_thread_id = SB_BACKGROUND_THREAD_ID; + /* Initialize thread-local RNG state */ sb_rand_thread_init(); @@ -959,6 +967,8 @@ static void *report_thread_proc(void *arg) (void)arg; /* unused */ + sb_tls_thread_id = SB_BACKGROUND_THREAD_ID; + /* Initialize thread-local RNG state */ sb_rand_thread_init(); @@ -1007,6 +1017,8 @@ static void *checkpoints_thread_proc(void *arg) (void)arg; /* unused */ + sb_tls_thread_id = SB_BACKGROUND_THREAD_ID; + /* Initialize thread-local RNG state */ sb_rand_thread_init(); @@ -1392,10 +1404,9 @@ static int init(void) } /* Initialize timers */ - timers = sb_memalign(sb_globals.threads * sizeof(sb_timer_t), - CK_MD_CACHELINE); - timers_copy = sb_memalign(sb_globals.threads * sizeof(sb_timer_t), - CK_MD_CACHELINE); + timers = sb_alloc_per_thread_array(sizeof(sb_timer_t)); + timers_copy = sb_alloc_per_thread_array(sizeof(sb_timer_t)); + if (timers == NULL || timers_copy == NULL) { log_text(LOG_FATAL, "Memory allocation failure"); @@ -1604,3 +1615,29 @@ void sb_print_test_options(void) if (current_test != NULL) sb_print_options(current_test->args); } + +/* + Allocate an array of objects of the specified size for all threads, both + worker and background ones. +*/ + +void *sb_alloc_per_thread_array(size_t size) +{ + /* + We want to exclude queries executed by background threads from statistics + generated for worker threads. + + To simplify code, we allocate all timers and counters for all worker threads + + possible background threads created by sysbench for statistic reports, + etc. When executing requests from background threads, extra array slots will + be used (it depends on the assigned ID for each thread). When aggregating + counters and timers, we only consider slots in the range [0, + sb_globals.threads - 1], i.e. ignore statistics generated by background + threads. Currently we assign the same single thread ID for all background + threads, so they also share the same single slot in each allocated array. + */ + void *ptr = sb_memalign((sb_globals.threads + 1) * size, CK_MD_CACHELINE); + memset(ptr, 0, size); + + return ptr; +} diff --git a/src/sysbench.h b/src/sysbench.h index c0ee442..8247d2c 100644 --- a/src/sysbench.h +++ b/src/sysbench.h @@ -232,4 +232,10 @@ void sb_report_intermediate(sb_stat_t *stat); /* Default cumulative reports handler */ void sb_report_cumulative(sb_stat_t *stat); +/* + Allocate an array of objects of the specified size for all threads, both + worker and background ones. +*/ +void *sb_alloc_per_thread_array(size_t size); + #endif