Skip to content

Commit

Permalink
move config related function
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Aug 9, 2024
1 parent 82c1cc0 commit 8703d93
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 70 deletions.
1 change: 1 addition & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set(sources
src/bpftime_shm_internal.cpp
src/bpftime_shm_json.cpp
src/bpftime_prog.cpp
src/bpftime_config.cpp
src/ufunc.cpp
src/bpf_helper.cpp
src/platform_utils.cpp
Expand Down
42 changes: 6 additions & 36 deletions runtime/include/bpftime_config.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef _CONFIG_MANAGER_HPP
#define _CONFIG_MANAGER_HPP

#include <cstdlib>
#include <string>


namespace bpftime
{
struct agent_config {
Expand All @@ -25,42 +29,8 @@ struct agent_config {
int shm_memory_size = 50; // 50MB
};

inline const agent_config get_agent_config_from_env()
{
bpftime::agent_config agent_config;
if (const char *custom_helpers = getenv("BPFTIME_HELPER_GROUPS");
custom_helpers != nullptr) {
agent_config.enable_kernel_helper_group =
agent_config.enable_ufunc_helper_group =
agent_config.enable_shm_maps_helper_group =
false;
auto helpers_sv = std::string_view(custom_helpers);
process_helper_sv(helpers_sv, ',', agent_config);
} else {
SPDLOG_INFO(
"Enabling helper groups ufunc, kernel, shm_map by default");
agent_config.enable_kernel_helper_group =
agent_config.enable_shm_maps_helper_group =
agent_config.enable_ufunc_helper_group = true;
}
if (getenv("BPFTIME_DISABLE_JIT") != nullptr) {
agent_config.jit_enabled = false;
}
if (getenv("BPFTIME_ALLOW_EXTERNAL_MAPS") != nullptr) {
agent_config.allow_non_buildin_map_types = true;
}
const char* shm_memory_size_str = getenv("BPFTIME_SHM_MEMORY_MB");
if (shm_memory_size_str != nullptr) {
try {
agent_config.shm_memory_size = std::stoi(shm_memory_size_str);
} catch (...) {
SPDLOG_ERROR(
"Invalid value for BPFTIME_SHM_MEMORY_SIZE: {}",
shm_memory_size_str);
}
}
return agent_config;
}
// Get the bpftime configuration from the environment variables
const agent_config get_agent_config_from_env();

} // namespace bpftime

Expand Down
77 changes: 77 additions & 0 deletions runtime/src/bpftime_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "bpftime_config.hpp"
#include "spdlog/spdlog.h"
#include <string_view>

using namespace bpftime;

static void process_token(const std::string_view &token, agent_config &config)
{
if (token == "ufunc") {
SPDLOG_INFO("Enabling ufunc helper group");
config.enable_ufunc_helper_group = true;
} else if (token == "kernel") {
SPDLOG_INFO("Enabling kernel helper group");
config.enable_kernel_helper_group = true;
} else if (token == "shm_map") {
SPDLOG_INFO("Enabling shm_map helper group");
config.enable_shm_maps_helper_group = true;
} else {
spdlog::warn("Unknown helper group: {}", token);
}
}

static void process_helper_sv(const std::string_view &str, const char delimiter,
agent_config &config)
{
std::string::size_type start = 0;
std::string::size_type end = str.find(delimiter);

while (end != std::string::npos) {
process_token(str.substr(start, end - start), config);
start = end + 1;
end = str.find(delimiter, start);
}

// Handle the last token, if any
if (start < str.size()) {
process_token(str.substr(start), config);
}
}

const agent_config get_agent_config_from_env()
{
bpftime::agent_config agent_config;
if (const char *custom_helpers = getenv("BPFTIME_HELPER_GROUPS");
custom_helpers != nullptr) {
agent_config.enable_kernel_helper_group =
agent_config.enable_ufunc_helper_group =
agent_config.enable_shm_maps_helper_group =
false;
auto helpers_sv = std::string_view(custom_helpers);
process_helper_sv(helpers_sv, ',', agent_config);
} else {
SPDLOG_INFO(
"Enabling helper groups ufunc, kernel, shm_map by default");
agent_config.enable_kernel_helper_group =
agent_config.enable_shm_maps_helper_group =
agent_config.enable_ufunc_helper_group = true;
}
if (getenv("BPFTIME_DISABLE_JIT") != nullptr) {
agent_config.jit_enabled = false;
}
if (getenv("BPFTIME_ALLOW_EXTERNAL_MAPS") != nullptr) {
agent_config.allow_non_buildin_map_types = true;
}
const char *shm_memory_size_str = getenv("BPFTIME_SHM_MEMORY_MB");
if (shm_memory_size_str != nullptr) {
try {
agent_config.shm_memory_size =
std::stoi(shm_memory_size_str);
} catch (...) {
SPDLOG_ERROR(
"Invalid value for BPFTIME_SHM_MEMORY_SIZE: {}",
shm_memory_size_str);
}
}
return agent_config;
}
34 changes: 0 additions & 34 deletions runtime/src/bpftime_shm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,40 +592,6 @@ extern "C" uint64_t map_val(uint64_t map_ptr)
return (uint64_t)handler.map_lookup_elem(key.data());
}

static void process_token(const std::string_view &token, agent_config &config)
{
if (token == "ufunc") {
SPDLOG_INFO("Enabling ufunc helper group");
config.enable_ufunc_helper_group = true;
} else if (token == "kernel") {
SPDLOG_INFO("Enabling kernel helper group");
config.enable_kernel_helper_group = true;
} else if (token == "shm_map") {
SPDLOG_INFO("Enabling shm_map helper group");
config.enable_shm_maps_helper_group = true;
} else {
spdlog::warn("Unknown helper group: {}", token);
}
}

static void process_helper_sv(const std::string_view &str, const char delimiter,
agent_config &config)
{
std::string::size_type start = 0;
std::string::size_type end = str.find(delimiter);

while (end != std::string::npos) {
process_token(str.substr(start, end - start), config);
start = end + 1;
end = str.find(delimiter, start);
}

// Handle the last token, if any
if (start < str.size()) {
process_token(str.substr(start), config);
}
}

int bpftime_add_custom_perf_event(int type, const char *attach_argument)
{
return shm_holder.global_shared_memory.add_custom_perf_event(
Expand Down

0 comments on commit 8703d93

Please sign in to comment.