diff --git a/emper/Runtime.cpp b/emper/Runtime.cpp index b0bd380bf3c4c22d87fd3e6c519f6ddadba6e97d..1c9f097e2d3656792e3cbca095a7ca7c86d74133 100644 --- a/emper/Runtime.cpp +++ b/emper/Runtime.cpp @@ -1,5 +1,5 @@ // SPDX-License-Identifier: LGPL-3.0-or-later -// Copyright © 2020-2021 Florian Schmaus, Florian Fischer +// Copyright © 2020-2022 Florian Schmaus, Florian Fischer #include "Runtime.hpp" #include @@ -36,6 +36,7 @@ #include "io/IoContext.hpp" #include "io/Stats.hpp" #include "lib/DebugUtil.hpp" +#include "lib/env.hpp" #include "log/LogBuffer.hpp" #include "stats/FromAnywhere.hpp" #include "stats/Worker.hpp" @@ -323,9 +324,12 @@ auto Runtime::workerLoop(Worker* worker) -> void* { } auto Runtime::getDefaultWorkerCount() -> workerid_t { - auto workerCountEnv = emper::lib::env::getUnsignedFromEnv("EMPER_WORKER_COUNT"); + static const std::string workerCountEnvVarName = "EMPER_WORKER_COUNT"; + auto workerCountEnv = emper::lib::env::getUnsignedFromEnv(workerCountEnvVarName); if (workerCountEnv) { - return workerCountEnv.value(); + auto value = workerCountEnv.value(); + DBG("Processed " << workerCountEnvVarName << "=" << value); + return value; } // The CPU count reported by sysconf(_SC_NPROCESSORS_ONLN), sysconf(_SC_NPROCESSORS_CONF) @@ -482,3 +486,27 @@ void Runtime::executeAndWait(std::function f) { fiberFinished.lock(); } + +auto Runtime::shouldPinWorkers() -> bool { + static const std::string pinWorkersEnvVarName = "EMPER_PIN_WORKERS"; + auto pinWorkersEnv = emper::lib::env::getBoolFromEnv(pinWorkersEnvVarName); + if (pinWorkersEnv) { + auto value = pinWorkersEnv.value(); + DBG("Processed " << pinWorkersEnvVarName << "=" << value); + return value; + } + + return true; +} + +auto Runtime::getDefaultPinningOffset() -> workerid_t { + static const std::string pinningOffsetEnvVarName = "EMPER_PINNING_OFFSET"; + auto pinningOffsetEnv = emper::lib::env::getUnsignedFromEnv(pinningOffsetEnvVarName); + if (pinningOffsetEnv) { + auto value = pinningOffsetEnv.value(); + DBG("Processed " << pinningOffsetEnvVarName << "=" << value); + return value; + } + + return 0; +} diff --git a/emper/Runtime.hpp b/emper/Runtime.hpp index 8261c8d59f210291706550206d7b4fd4e25ef4eb..d91d779dc8ab4694c86ddb445ebb92ced6e34e91 100644 --- a/emper/Runtime.hpp +++ b/emper/Runtime.hpp @@ -1,5 +1,5 @@ // SPDX-License-Identifier: LGPL-3.0-or-later -// Copyright © 2020-2021 Florian Schmaus +// Copyright © 2020-2022 Florian Schmaus #pragma once #include // for pthread_t @@ -25,8 +25,7 @@ #include "Scheduler.hpp" // for Scheduler #include "WakeupStrategy.hpp" #include "Worker.hpp" -#include "emper-common.h" // for workerid_t -#include "lib/env.hpp" +#include "emper-common.h" // for workerid_t #include "lib/sync/Latch.hpp" // for Latch #include "lib/sync/Semaphore.hpp" #include "sleep_strategy/WorkerSleepStrategy.hpp" @@ -121,13 +120,8 @@ class Runtime : public Logger { static auto getDefaultWorkerCount() -> workerid_t; - static auto shouldPinWorkers() -> bool { - return emper::lib::env::getBoolFromEnv("EMPER_PIN_WORKERS").value_or(true); - } - - static auto getDefaultPinningOffset() -> workerid_t { - return emper::lib::env::getUnsignedFromEnv("EMPER_PINNING_OFFSET").value_or(0); - } + static auto shouldPinWorkers() -> bool; + static auto getDefaultPinningOffset() -> workerid_t; protected: void addNewWorkerHook(const std::function& hook) { diff --git a/emper/lib/env.hpp b/emper/lib/env.hpp index 4d97aaecba060ff8227b19d3930a336a50495e02..d03784beb10caf45a0ae8f17189884345881202f 100644 --- a/emper/lib/env.hpp +++ b/emper/lib/env.hpp @@ -9,8 +9,7 @@ namespace emper::lib::env { -static auto getBoolFromEnv(const std::string&& key) -> std::optional { - DBG("parse " << key << " environment variable"); +static auto getBoolFromEnv(const std::string& key) -> std::optional { char* envVar = std::getenv(key.c_str()); if (!envVar) { return std::nullopt; @@ -29,8 +28,7 @@ static auto getBoolFromEnv(const std::string&& key) -> std::optional { } template -static auto getUnsignedFromEnv(const std::string&& key) -> std::optional { - DBG("parse " << key << " environment variable"); +static auto getUnsignedFromEnv(const std::string& key) -> std::optional { char* envVar = std::getenv(key.c_str()); if (!envVar) { return std::nullopt;