@@ -27,9 +27,21 @@ static bool exec_system_command(const std::string& command, const std::string& c
2727 return true ;
2828}
2929
30+ // / Writing the value 0 to a cgroup.procs file causes the writing process to be moved to the corresponding cgroup.
31+ static void move_to_cgroup (const std::string& cgroup_path)
32+ {
33+ std::ofstream output (cgroup_path + " /cgroup.procs" );
34+ if (output.fail ()) {
35+ fmt::print (" Could not open {} for writing. error=\" {}\"\n " , cgroup_path + " /cgroup.procs" , strerror (errno));
36+ }
37+ output.write (" 0\n " , 2 );
38+ }
39+
3040// / Moves processes from source cgroup to destination cgroup.
3141static bool move_procs_between_cgroups (const std::string& dst_path, const std::string& src_path)
3242{
43+ using namespace std ::chrono_literals;
44+
3345 std::ifstream source_file (src_path);
3446 if (source_file.fail ()) {
3547 fmt::print (" Could not open {} directory. error=\" {}\"\n " , src_path, strerror (errno));
@@ -44,6 +56,8 @@ static bool move_procs_between_cgroups(const std::string& dst_path, const std::s
4456 }
4557 destination_file << std::stoi (pid) << " \n " ;
4658 }
59+ std::this_thread::sleep_for (50ms);
60+
4761 return true ;
4862}
4963
@@ -56,6 +70,9 @@ bool configure_cgroups(const std::string& isol_cpus, const std::string& os_cpus)
5670 return false ;
5771 }
5872
73+ // / First move itself to root cgroup.
74+ move_to_cgroup (cgroup_path);
75+
5976 // / Create cgroup for OS tasks, call it 'housekeeping' cgroup.
6077 if (!os_cpus.empty ()) {
6178 std::string housekeeping = cgroup_path + " /housekeeping" ;
@@ -101,17 +118,9 @@ bool configure_cgroups(const std::string& isol_cpus, const std::string& os_cpus)
101118 if (!exec_system_command (set_cpus_cmd, isol_cgroup_path)) {
102119 return false ;
103120 }
104- std::string set_partition_cmd = " echo root > " + isol_cgroup_path + " /cpuset.cpus.partition" ;
105- if (!exec_system_command (set_partition_cmd, isol_cgroup_path)) {
106- return false ;
107- }
108121
109- // / Writing the value 0 to a cgroup.procs file causes the writing process to be moved to the corresponding cgroup.
110- std::ofstream output (isol_cgroup_path + " /cgroup.procs" );
111- if (output.fail ()) {
112- fmt::print (" Could not open {} for writing. error=\" {}\"\n " , isol_cgroup_path + " /cgroup.procs" , strerror (errno));
113- }
114- output.write (" 0\n " , 2 );
122+ // / Finally move itself to isolcated cgroup.
123+ move_to_cgroup (isol_cgroup_path);
115124
116125 return true ;
117126}
0 commit comments