Skip to content

Commit

Permalink
gnb: improve cgroup management
Browse files Browse the repository at this point in the history
  • Loading branch information
sauka authored and Pavel Harbanau committed Dec 15, 2023
1 parent dd8db14 commit bcbc26e
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/support/sysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ static bool exec_system_command(const std::string& command, const std::string& c
return true;
}

/// Writing the value 0 to a cgroup.procs file causes the writing process to be moved to the corresponding cgroup.
static void move_to_cgroup(const std::string& cgroup_path)
{
std::ofstream output(cgroup_path + "/cgroup.procs");
if (output.fail()) {
fmt::print("Could not open {} for writing. error=\"{}\"\n", cgroup_path + "/cgroup.procs", strerror(errno));
}
output.write("0\n", 2);
}

/// Moves processes from source cgroup to destination cgroup.
static bool move_procs_between_cgroups(const std::string& dst_path, const std::string& src_path)
{
using namespace std::chrono_literals;

std::ifstream source_file(src_path);
if (source_file.fail()) {
fmt::print("Could not open {} directory. error=\"{}\"\n", src_path, strerror(errno));
Expand All @@ -44,6 +56,8 @@ static bool move_procs_between_cgroups(const std::string& dst_path, const std::s
}
destination_file << std::stoi(pid) << "\n";
}
std::this_thread::sleep_for(50ms);

return true;
}

Expand All @@ -56,6 +70,9 @@ bool configure_cgroups(const std::string& isol_cpus, const std::string& os_cpus)
return false;
}

/// First move itself to root cgroup.
move_to_cgroup(cgroup_path);

/// Create cgroup for OS tasks, call it 'housekeeping' cgroup.
if (!os_cpus.empty()) {
std::string housekeeping = cgroup_path + "/housekeeping";
Expand Down Expand Up @@ -101,17 +118,9 @@ bool configure_cgroups(const std::string& isol_cpus, const std::string& os_cpus)
if (!exec_system_command(set_cpus_cmd, isol_cgroup_path)) {
return false;
}
std::string set_partition_cmd = "echo root > " + isol_cgroup_path + "/cpuset.cpus.partition";
if (!exec_system_command(set_partition_cmd, isol_cgroup_path)) {
return false;
}

/// Writing the value 0 to a cgroup.procs file causes the writing process to be moved to the corresponding cgroup.
std::ofstream output(isol_cgroup_path + "/cgroup.procs");
if (output.fail()) {
fmt::print("Could not open {} for writing. error=\"{}\"\n", isol_cgroup_path + "/cgroup.procs", strerror(errno));
}
output.write("0\n", 2);
/// Finally move itself to isolcated cgroup.
move_to_cgroup(isol_cgroup_path);

return true;
}
Expand Down

0 comments on commit bcbc26e

Please sign in to comment.