From a4bf3e1099a4c07b12218a8d3492ed25c1e6cc23 Mon Sep 17 00:00:00 2001 From: Tomasz Zawadzki Date: Tue, 18 May 2021 05:20:38 -0400 Subject: [PATCH] scheduler_dynamic: exit early from _get_thread_load() _get_thread_load() is function used to determine the load of a thread based on relation of busy/idle tsc from previous scheduling period. In order to avoid division by 0 calculating the percentage, we can simply exit early determining that thread was not doing any work. Having this check here will make sure that no matter the changes in event framework, scheduler dynamic will work. Removed the place that updated last_stats if they weren't yet updated at least once (first scheduling period iteration). In this case after change to _get_thread_load() will be the same, as only the latest iteration will be used to calculate thread load. Signed-off-by: Tomasz Zawadzki Change-Id: I75f0f12f024675f2473a26e30596d6eb28093d46 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/7917 Tested-by: SPDK CI Jenkins Community-CI: Mellanox Build Bot Reviewed-by: Shuhei Matsumoto Reviewed-by: Ben Walker Reviewed-by: Konrad Sztyber --- lib/event/scheduler_dynamic.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/event/scheduler_dynamic.c b/lib/event/scheduler_dynamic.c index 03003a0ab..b864363d8 100644 --- a/lib/event/scheduler_dynamic.c +++ b/lib/event/scheduler_dynamic.c @@ -74,6 +74,10 @@ _get_thread_load(struct spdk_lw_thread *lw_thread) lw_thread->last_stats.busy_tsc = lw_thread->snapshot_stats.busy_tsc; lw_thread->last_stats.idle_tsc = lw_thread->snapshot_stats.idle_tsc; + if (busy == 0) { + /* No work was done, exit before possible division by 0. */ + return 0; + } /* return percentage of time thread was busy */ return busy * 100 / (busy + idle); } @@ -157,17 +161,6 @@ balance(struct spdk_scheduler_core_info *cores_info, int cores_count, thread = spdk_thread_get_from_ctx(lw_thread); cpumask = spdk_thread_get_cpumask(thread); - if (lw_thread->last_stats.busy_tsc + lw_thread->last_stats.idle_tsc == 0) { - lw_thread->last_stats.busy_tsc = lw_thread->snapshot_stats.busy_tsc; - lw_thread->last_stats.idle_tsc = lw_thread->snapshot_stats.idle_tsc; - - if (i != g_main_lcore) { - busy_threads_present = true; - } - - continue; - } - thread_busy = lw_thread->snapshot_stats.busy_tsc - lw_thread->last_stats.busy_tsc; load = _get_thread_load(lw_thread);