From 01eed4db668dd7b009ff5c120ef7146812cef606 Mon Sep 17 00:00:00 2001 From: Alexey Kopytov Date: Tue, 4 Jul 2017 11:21:21 +0300 Subject: [PATCH] Remove busy-wait in --rate mode to avoid CPU hogging. In the bounded rate (--rate) mode, if the queue is empty, sleep for a certain amount of time depending on the requested rate and the number of worker threads instead of spinning on the queue. Clean up some code duplication by removing sb_next_event(). --- src/sysbench.c | 43 ++----------------------------------------- 1 file changed, 2 insertions(+), 41 deletions(-) diff --git a/src/sysbench.c b/src/sysbench.c index ab5549d..a4712d0 100644 --- a/src/sysbench.c +++ b/src/sysbench.c @@ -703,7 +703,7 @@ bool sb_more_events(int thread_id) while (!ck_ring_dequeue_spmc(&queue_ring, queue_ring_buffer, &ptr) && !ck_pr_load_int(&queue_is_full)) - ck_pr_stall(); + usleep(500000.0 * sb_globals.threads / sb_globals.tx_rate); if (ck_pr_load_int(&queue_is_full)) { @@ -721,45 +721,6 @@ bool sb_more_events(int thread_id) return true; } -/* - Get the next event, or return an 'empty' event with type = SB_REQ_TYPE_NULL, - if there are no more events to execute. -*/ - -sb_event_t sb_next_event(sb_test_t *test, int thread_id) -{ - uint64_t queue_start_time = 0; - - /* If we are in tx_rate mode, we take events from queue */ - if (sb_globals.tx_rate > 0) - { - void *ptr = NULL; - - while (!ck_ring_dequeue_spmc(&queue_ring, queue_ring_buffer, &ptr) && - !ck_pr_load_int(&queue_is_full)) - ck_pr_stall(); - - if (ck_pr_load_int(&queue_is_full)) - { - log_text(LOG_FATAL, "Event queue is full. Terminating the worker thread"); - - sb_event_t event; - event.type = SB_REQ_TYPE_NULL; - - return event; - } - - queue_start_time = ((uint64_t *) ptr)[0]; - - ck_pr_inc_int(&sb_globals.concurrency); - - timers[thread_id].queue_time = sb_timer_value(&sb_exec_timer) - - queue_start_time; - } - - return test->ops.next_event(thread_id); -} - void sb_event_start(int thread_id) { @@ -796,7 +757,7 @@ static int thread_run(sb_test_t *test, int thread_id) while (sb_more_events(thread_id) && rc == 0) { - event = sb_next_event(test, thread_id); + event = test->ops.next_event(thread_id); if (event.type == SB_REQ_TYPE_NULL) break;