Skip to content

Commit

Permalink
Allow defining shared memory size at runtime (#285)
Browse files Browse the repository at this point in the history
* fix config API
* move config related function
* change default memory size
* fix compile error

---------

Co-authored-by: yunwei37 <[email protected]>
  • Loading branch information
NobinPegasus and yunwei37 authored Aug 9, 2024
1 parent 1f88756 commit b86c6a3
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 71 deletions.
3 changes: 2 additions & 1 deletion daemon/user/bpftime_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ bpftime_driver::bpftime_driver(daemon_config cfg, struct bpf_tracer_bpf *obj)
config = cfg;
object = obj;
bpftime_initialize_global_shm(shm_open_type::SHM_REMOVE_AND_CREATE);
set_agent_config_from_env();
auto config = get_agent_config_from_env();
bpftime_set_agent_config(config);
}

bpftime_driver::~bpftime_driver()
Expand Down
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
14 changes: 14 additions & 0 deletions runtime/include/bpftime_config.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#ifndef _CONFIG_MANAGER_HPP
#define _CONFIG_MANAGER_HPP

#include <cstdlib>
#include <string>

namespace bpftime
{
// Configuration for the bpftime runtime
// Initialize the configuration from the environment variables
struct agent_config {
bool debug = false;
// Enable JIT?
Expand All @@ -18,7 +23,16 @@ struct agent_config {
// self-defined or non-buildin supported map type, if will not be
// rejected
bool allow_non_buildin_map_types = false;

// memory size will determine the maximum size of the shared memory
// available for the eBPF programs and maps
// The value is in MB
int shm_memory_size = 20; // 20MB
};

// Get the bpftime configuration from the environment variables
const agent_config get_agent_config_from_env();

} // namespace bpftime

#endif
3 changes: 1 addition & 2 deletions runtime/include/bpftime_shm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ enum class bpf_prog_type {
};

extern const shm_open_type global_shm_open_type;
const bpftime::agent_config &set_agent_config_from_env();
const bpftime::agent_config &bpftime_get_agent_config();
void bpftime_set_agent_config(bpftime::agent_config &cfg);
void bpftime_set_agent_config(const bpftime::agent_config &cfg);

// Map ops for register external map types and operations
//
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 bpftime::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;
}
60 changes: 0 additions & 60 deletions runtime/src/bpftime_shm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,66 +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);
}
}

const bpftime::agent_config &bpftime::set_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;
}
const char *use_jit = getenv("BPFTIME_USE_JIT");
agent_config.jit_enabled = use_jit != nullptr;
agent_config.allow_non_buildin_map_types =
getenv("BPFTIME_ALLOW_EXTERNAL_MAPS") != nullptr;
bpftime_set_agent_config(agent_config);
return bpftime_get_agent_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
17 changes: 11 additions & 6 deletions runtime/src/bpftime_shm_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ bool bpftime_shm::is_exist_fake_fd(int fd) const
bpftime_shm::bpftime_shm(const char *shm_name, shm_open_type type)
: open_type(type)
{
size_t memory_size = get_agent_config().shm_memory_size;
if (type == shm_open_type::SHM_OPEN_ONLY) {
SPDLOG_DEBUG("start: bpftime_shm for client setup");
// open the shm
Expand All @@ -569,11 +570,13 @@ bpftime_shm::bpftime_shm(const char *shm_name, shm_open_type type)
.first;
SPDLOG_DEBUG("done: bpftime_shm for client setup");
} else if (type == shm_open_type::SHM_CREATE_OR_OPEN) {
SPDLOG_DEBUG("start: bpftime_shm for create or open setup");
SPDLOG_DEBUG(
"start: bpftime_shm for create or open setup for memory size {}",
memory_size);
segment = boost::interprocess::managed_shared_memory(
boost::interprocess::open_or_create,
// Allocate 200M bytes of memory by default
shm_name, 200 << 20);
// Allocate 20M bytes of memory by default
shm_name, memory_size << 20);

manager = segment.find_or_construct<bpftime::handler_manager>(
bpftime::DEFAULT_GLOBAL_HANDLER_NAME)(segment);
Expand All @@ -598,15 +601,17 @@ bpftime_shm::bpftime_shm(const char *shm_name, shm_open_type type)
segment.get_segment_manager()));
SPDLOG_DEBUG("done: bpftime_shm for open_or_create setup");
} else if (type == shm_open_type::SHM_REMOVE_AND_CREATE) {
SPDLOG_DEBUG("start: bpftime_shm for server setup");
SPDLOG_DEBUG(
"start: bpftime_shm for server setup for memory size {}",
memory_size);
boost::interprocess::shared_memory_object::remove(shm_name);
// create the shm
SPDLOG_DEBUG(
"done: bpftime_shm for server setup: remove installed segment");
segment = boost::interprocess::managed_shared_memory(
boost::interprocess::create_only,
// Allocate 20M bytes of memory by default
shm_name, 20 << 20);
shm_name, memory_size << 20);
SPDLOG_DEBUG("done: bpftime_shm for server setup: segment");

manager = segment.construct<bpftime::handler_manager>(
Expand Down Expand Up @@ -741,7 +746,7 @@ const bpftime::agent_config &bpftime_get_agent_config()
return shm_holder.global_shared_memory.get_agent_config();
}

void bpftime_set_agent_config(bpftime::agent_config &cfg)
void bpftime_set_agent_config(const bpftime::agent_config &cfg)
{
shm_holder.global_shared_memory.set_agent_config(cfg);
}
Expand Down
3 changes: 2 additions & 1 deletion runtime/syscall-server/syscall_server_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ void start_up()
spdlog::cfg::load_env_levels();
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S][%^%l%$][%t] %v");
bpftime_initialize_global_shm(shm_open_type::SHM_REMOVE_AND_CREATE);
const auto &agent_config = set_agent_config_from_env();
const auto agent_config = get_agent_config_from_env();
bpftime_set_agent_config(agent_config);
#ifdef ENABLE_BPFTIME_VERIFIER
std::vector<int32_t> helper_ids;
std::map<int32_t, bpftime::verifier::BpftimeHelperProrotype>
Expand Down
3 changes: 2 additions & 1 deletion tools/bpftimetool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ int main(int argc, char *argv[])
}
bpftime_initialize_global_shm(
shm_open_type::SHM_CREATE_OR_OPEN);
set_agent_config_from_env();
auto agent_config = get_agent_config_from_env();
bpftime_set_agent_config(agent_config);
auto filename = std::string(argv[2]);
return bpftime_import_global_shm_from_json(filename.c_str());
} else if (cmd == "remove") {
Expand Down

0 comments on commit b86c6a3

Please sign in to comment.