2021-01-22 09:38:38 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
#include "spdk/likely.h"
|
|
|
|
#include "spdk/event.h"
|
|
|
|
#include "spdk/log.h"
|
|
|
|
#include "spdk/env.h"
|
|
|
|
|
2021-05-06 14:33:09 +00:00
|
|
|
#include "spdk/thread.h"
|
2021-01-22 09:38:38 +00:00
|
|
|
#include "spdk_internal/event.h"
|
|
|
|
|
|
|
|
static uint32_t g_next_lcore = SPDK_ENV_LCORE_ID_ANY;
|
|
|
|
static uint32_t g_main_lcore;
|
2020-12-17 09:10:53 +00:00
|
|
|
static bool g_core_mngmnt_available;
|
2021-01-22 09:38:38 +00:00
|
|
|
|
2021-03-03 12:32:51 +00:00
|
|
|
struct core_stats {
|
|
|
|
uint64_t busy;
|
|
|
|
uint64_t idle;
|
2021-05-24 17:32:21 +00:00
|
|
|
uint32_t thread_count;
|
2021-03-03 12:32:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct core_stats *g_cores;
|
|
|
|
|
2021-01-22 09:38:38 +00:00
|
|
|
#define SCHEDULER_THREAD_BUSY 100
|
2020-12-17 09:10:53 +00:00
|
|
|
#define SCHEDULER_LOAD_LIMIT 50
|
2021-01-22 09:38:38 +00:00
|
|
|
|
2021-01-25 15:56:31 +00:00
|
|
|
static uint32_t
|
|
|
|
_get_next_target_core(void)
|
|
|
|
{
|
|
|
|
uint32_t target_lcore;
|
|
|
|
|
|
|
|
if (g_next_lcore == SPDK_ENV_LCORE_ID_ANY) {
|
|
|
|
g_next_lcore = spdk_env_get_first_core();
|
|
|
|
}
|
|
|
|
|
|
|
|
target_lcore = g_next_lcore;
|
|
|
|
g_next_lcore = spdk_env_get_next_core(g_next_lcore);
|
|
|
|
|
|
|
|
return target_lcore;
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:38:38 +00:00
|
|
|
static uint8_t
|
|
|
|
_get_thread_load(struct spdk_lw_thread *lw_thread)
|
|
|
|
{
|
|
|
|
uint64_t busy, idle;
|
|
|
|
|
2021-05-07 15:30:00 +00:00
|
|
|
busy = lw_thread->current_stats.busy_tsc;
|
|
|
|
idle = lw_thread->current_stats.idle_tsc;
|
2021-01-22 09:38:38 +00:00
|
|
|
|
2021-05-18 09:20:38 +00:00
|
|
|
if (busy == 0) {
|
|
|
|
/* No work was done, exit before possible division by 0. */
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-22 09:38:38 +00:00
|
|
|
/* return percentage of time thread was busy */
|
|
|
|
return busy * 100 / (busy + idle);
|
|
|
|
}
|
|
|
|
|
2021-05-26 13:00:36 +00:00
|
|
|
typedef void (*_foreach_fn)(struct spdk_lw_thread *lw_thread);
|
|
|
|
|
|
|
|
static void
|
|
|
|
_foreach_thread(struct spdk_scheduler_core_info *cores_info, _foreach_fn fn)
|
|
|
|
{
|
|
|
|
struct spdk_scheduler_core_info *core;
|
|
|
|
uint32_t i, j;
|
|
|
|
|
|
|
|
SPDK_ENV_FOREACH_CORE(i) {
|
|
|
|
core = &cores_info[i];
|
|
|
|
for (j = 0; j < core->threads_count; j++) {
|
|
|
|
fn(core->threads[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 13:09:37 +00:00
|
|
|
static void
|
|
|
|
_move_thread(struct spdk_lw_thread *lw_thread, uint32_t dst_core)
|
|
|
|
{
|
|
|
|
struct core_stats *dst = &g_cores[dst_core];
|
|
|
|
struct core_stats *src = &g_cores[lw_thread->lcore];
|
|
|
|
uint64_t busy_tsc = lw_thread->current_stats.busy_tsc;
|
|
|
|
|
|
|
|
if (src == dst) {
|
|
|
|
/* Don't modify stats if thread is already on that core. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->busy += spdk_min(UINT64_MAX - dst->busy, busy_tsc);
|
|
|
|
dst->idle -= spdk_min(dst->idle, busy_tsc);
|
|
|
|
dst->thread_count++;
|
|
|
|
|
|
|
|
src->busy -= spdk_min(src->busy, busy_tsc);
|
|
|
|
src->idle += spdk_min(UINT64_MAX - src->busy, busy_tsc);
|
|
|
|
assert(src->thread_count > 0);
|
|
|
|
src->thread_count--;
|
|
|
|
|
|
|
|
lw_thread->lcore = dst_core;
|
|
|
|
}
|
|
|
|
|
2021-03-05 08:05:36 +00:00
|
|
|
static uint32_t
|
|
|
|
_find_optimal_core(struct spdk_lw_thread *lw_thread)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t target_lcore;
|
|
|
|
struct spdk_thread *thread = spdk_thread_get_from_ctx(lw_thread);
|
|
|
|
struct spdk_cpuset *cpumask = spdk_thread_get_cpumask(thread);
|
|
|
|
uint64_t thread_busy = lw_thread->current_stats.busy_tsc;
|
|
|
|
|
|
|
|
/* Find a core that can fit the thread. */
|
|
|
|
for (i = 0; i < spdk_env_get_core_count(); i++) {
|
|
|
|
target_lcore = _get_next_target_core();
|
|
|
|
|
|
|
|
/* Ignore cores outside cpumask. */
|
|
|
|
if (!spdk_cpuset_get_cpu(cpumask, target_lcore)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do not use main core if it is too busy for new thread. */
|
|
|
|
if (target_lcore == g_main_lcore && thread_busy > g_cores[g_main_lcore].idle) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return target_lcore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no better core is found, remain on the same one. */
|
|
|
|
return lw_thread->lcore;
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:38:38 +00:00
|
|
|
static int
|
|
|
|
init(struct spdk_governor *governor)
|
|
|
|
{
|
2020-12-17 09:10:53 +00:00
|
|
|
int rc;
|
|
|
|
|
2021-01-22 09:38:38 +00:00
|
|
|
g_main_lcore = spdk_env_get_current_core();
|
|
|
|
|
2020-12-17 09:10:53 +00:00
|
|
|
rc = _spdk_governor_set("dpdk_governor");
|
|
|
|
g_core_mngmnt_available = !rc;
|
|
|
|
|
2021-03-03 12:32:51 +00:00
|
|
|
g_cores = calloc(spdk_env_get_last_core() + 1, sizeof(struct core_stats));
|
|
|
|
if (g_cores == NULL) {
|
|
|
|
SPDK_ERRLOG("Failed to allocate memory for dynamic scheduler core stats.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:38:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-17 09:10:53 +00:00
|
|
|
static int
|
|
|
|
deinit(struct spdk_governor *governor)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
int rc = 0;
|
|
|
|
|
2021-03-03 12:32:51 +00:00
|
|
|
free(g_cores);
|
|
|
|
g_cores = NULL;
|
|
|
|
|
2020-12-17 09:10:53 +00:00
|
|
|
if (!g_core_mngmnt_available) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (governor->deinit_core) {
|
|
|
|
SPDK_ENV_FOREACH_CORE(i) {
|
|
|
|
rc = governor->deinit_core(i);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Failed to deinitialize governor for core %d\n", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (governor->deinit) {
|
|
|
|
rc = governor->deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-05-26 13:00:36 +00:00
|
|
|
static void
|
|
|
|
_balance_thread(struct spdk_lw_thread *lw_thread)
|
|
|
|
{
|
|
|
|
uint32_t target_lcore;
|
|
|
|
uint8_t load;
|
|
|
|
|
|
|
|
load = _get_thread_load(lw_thread);
|
|
|
|
if (load < SCHEDULER_LOAD_LIMIT) {
|
|
|
|
/* This thread is idle, move it to the main core. */
|
|
|
|
_move_thread(lw_thread, g_main_lcore);
|
|
|
|
} else {
|
|
|
|
/* This thread is active. */
|
|
|
|
target_lcore = _find_optimal_core(lw_thread);
|
|
|
|
_move_thread(lw_thread, target_lcore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:38:38 +00:00
|
|
|
static void
|
|
|
|
balance(struct spdk_scheduler_core_info *cores_info, int cores_count,
|
|
|
|
struct spdk_governor *governor)
|
|
|
|
{
|
2020-12-04 11:41:34 +00:00
|
|
|
struct spdk_reactor *reactor;
|
2021-01-22 09:38:38 +00:00
|
|
|
struct spdk_scheduler_core_info *core;
|
2021-03-03 12:32:51 +00:00
|
|
|
struct core_stats *main_core;
|
2021-05-26 13:00:36 +00:00
|
|
|
uint32_t i;
|
2020-12-17 09:10:53 +00:00
|
|
|
int rc;
|
|
|
|
bool busy_threads_present = false;
|
2021-01-22 09:38:38 +00:00
|
|
|
|
2020-12-04 10:47:08 +00:00
|
|
|
SPDK_ENV_FOREACH_CORE(i) {
|
2021-05-24 17:32:21 +00:00
|
|
|
g_cores[i].thread_count = cores_info[i].threads_count;
|
2021-03-03 12:32:51 +00:00
|
|
|
g_cores[i].busy = cores_info[i].current_busy_tsc;
|
|
|
|
g_cores[i].idle = cores_info[i].current_idle_tsc;
|
2020-12-04 10:47:08 +00:00
|
|
|
}
|
2021-03-03 12:32:51 +00:00
|
|
|
main_core = &g_cores[g_main_lcore];
|
2020-12-04 10:47:08 +00:00
|
|
|
|
2020-12-17 09:10:53 +00:00
|
|
|
/* Distribute active threads across all cores and move idle threads to main core */
|
2021-05-26 13:00:36 +00:00
|
|
|
_foreach_thread(cores_info, _balance_thread);
|
2020-12-17 09:10:53 +00:00
|
|
|
|
2020-12-04 11:41:34 +00:00
|
|
|
/* Switch unused cores to interrupt mode and switch cores to polled mode
|
|
|
|
* if they will be used after rebalancing */
|
|
|
|
SPDK_ENV_FOREACH_CORE(i) {
|
|
|
|
reactor = spdk_reactor_get(i);
|
|
|
|
core = &cores_info[i];
|
|
|
|
/* We can switch mode only if reactor already does not have any threads */
|
2021-05-24 17:32:21 +00:00
|
|
|
if (g_cores[i].thread_count == 0 && TAILQ_EMPTY(&reactor->threads)) {
|
2020-12-04 11:41:34 +00:00
|
|
|
core->interrupt_mode = true;
|
2021-05-24 17:32:21 +00:00
|
|
|
} else if (g_cores[i].thread_count != 0) {
|
2020-12-04 11:41:34 +00:00
|
|
|
core->interrupt_mode = false;
|
2021-05-25 09:36:15 +00:00
|
|
|
if (i != g_main_lcore) {
|
|
|
|
/* If a thread is present on non g_main_lcore,
|
|
|
|
* it has to be busy. */
|
|
|
|
busy_threads_present = true;
|
|
|
|
}
|
2020-12-04 11:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 09:10:53 +00:00
|
|
|
if (!g_core_mngmnt_available) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change main core frequency if needed */
|
|
|
|
if (busy_threads_present) {
|
|
|
|
rc = governor->set_core_freq_max(g_main_lcore);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("setting default frequency for core %u failed\n", g_main_lcore);
|
|
|
|
}
|
2021-03-03 12:32:51 +00:00
|
|
|
} else if (main_core->busy > main_core->idle) {
|
2020-12-17 09:10:53 +00:00
|
|
|
rc = governor->core_freq_up(g_main_lcore);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("increasing frequency for core %u failed\n", g_main_lcore);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rc = governor->core_freq_down(g_main_lcore);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("lowering frequency for core %u failed\n", g_main_lcore);
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 09:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct spdk_scheduler scheduler_dynamic = {
|
|
|
|
.name = "dynamic",
|
|
|
|
.init = init,
|
2020-12-17 09:10:53 +00:00
|
|
|
.deinit = deinit,
|
2021-01-22 09:38:38 +00:00
|
|
|
.balance = balance,
|
|
|
|
};
|
|
|
|
|
|
|
|
SPDK_SCHEDULER_REGISTER(scheduler_dynamic);
|