Skip to content

Commit b635c7a

Browse files
committed
move os::linux calls out of cgroups
1 parent 8402891 commit b635c7a

File tree

8 files changed

+37
-33
lines changed

8 files changed

+37
-33
lines changed

src/hotspot/os/linux/cgroupSubsystem_linux.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "cgroupV2Subsystem_linux.hpp"
2929
#include "logging/log.hpp"
3030
#include "memory/allocation.hpp"
31-
#include "os_linux.hpp"
3231
#include "runtime/globals.hpp"
3332
#include "runtime/os.hpp"
3433
#include "utilities/globalDefinitions.hpp"
@@ -605,6 +604,11 @@ void CgroupSubsystemFactory::cleanup(CgroupInfo* cg_infos) {
605604
}
606605
}
607606

607+
void CgroupSubsystem::adjust_controllers(physical_memory_size_type upper_mem_bound, int upper_cpu_bound) {
608+
CgroupUtil::adjust_controller(memory_controller()->controller(), upper_mem_bound);
609+
CgroupUtil::adjust_controller(cpu_controller()->controller(), upper_cpu_bound);
610+
}
611+
608612
/* active_processor_count
609613
*
610614
* Calculate an appropriate number of active processors for the
@@ -631,7 +635,7 @@ void CgroupSubsystemFactory::cleanup(CgroupInfo* cg_infos) {
631635
* return:
632636
* true if there were no errors. false otherwise.
633637
*/
634-
bool CgroupSubsystem::active_processor_count(int& value) {
638+
bool CgroupSubsystem::active_processor_count(int (*cpu_bound_func)(), int& value) {
635639
int cpu_count;
636640
int result = -1;
637641

@@ -646,7 +650,7 @@ bool CgroupSubsystem::active_processor_count(int& value) {
646650
return true;
647651
}
648652

649-
cpu_count = os::Linux::active_processor_count();
653+
cpu_count = cpu_bound_func();
650654
if (!CgroupUtil::processor_count(contrl->controller(), cpu_count, result)) {
651655
return false;
652656
}

src/hotspot/os/linux/cgroupSubsystem_linux.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class CgroupMemoryController: public CHeapObj<mtInternal> {
277277
class CgroupSubsystem: public CHeapObj<mtInternal> {
278278
public:
279279
bool memory_limit_in_bytes(physical_memory_size_type upper_bound, physical_memory_size_type& value);
280-
bool active_processor_count(int& value);
280+
bool active_processor_count(int (*cpu_bound_func)(), int& value);
281281

282282
virtual bool pids_max(uint64_t& value) = 0;
283283
virtual bool pids_current(uint64_t& value) = 0;
@@ -290,6 +290,8 @@ class CgroupSubsystem: public CHeapObj<mtInternal> {
290290
virtual CachingCgroupController<CgroupCpuController>* cpu_controller() = 0;
291291
virtual CgroupCpuacctController* cpuacct_controller() = 0;
292292

293+
void adjust_controllers(physical_memory_size_type upper_mem_bound, int upper_cpu_bound);
294+
293295
bool cpu_quota(int& value);
294296
bool cpu_period(int& value);
295297
bool cpu_shares(int& value);

src/hotspot/os/linux/cgroupUtil_linux.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424

2525
#include "cgroupUtil_linux.hpp"
26-
#include "os_linux.hpp"
2726

2827
bool CgroupUtil::processor_count(CgroupCpuController* cpu_ctrl, int upper_bound, int& value) {
2928
assert(upper_bound > 0, "upper bound of cpus must be positive");
@@ -87,7 +86,7 @@ int CgroupUtil::get_updated_cpu_limit(CgroupCpuController* cpu,
8786
return lowest;
8887
}
8988

90-
void CgroupUtil::adjust_controller(CgroupMemoryController* mem) {
89+
void CgroupUtil::adjust_controller(CgroupMemoryController* mem, physical_memory_size_type upper_bound) {
9190
assert(mem->cgroup_path() != nullptr, "invariant");
9291
if (strstr(mem->cgroup_path(), "../") != nullptr) {
9392
log_warning(os, container)("Cgroup memory controller path at '%s' seems to have moved "
@@ -105,17 +104,16 @@ void CgroupUtil::adjust_controller(CgroupMemoryController* mem) {
105104
char* cg_path = os::strdup(orig);
106105
char* last_slash;
107106
assert(cg_path[0] == '/', "cgroup path must start with '/'");
108-
physical_memory_size_type phys_mem = os::Linux::physical_memory();
109107
char* limit_cg_path = nullptr;
110108
physical_memory_size_type limit = value_unlimited;
111-
physical_memory_size_type lowest_limit = phys_mem;
112-
lowest_limit = get_updated_mem_limit(mem, lowest_limit, phys_mem);
113-
physical_memory_size_type orig_limit = lowest_limit != phys_mem ? lowest_limit : phys_mem;
109+
physical_memory_size_type lowest_limit = upper_bound;
110+
lowest_limit = get_updated_mem_limit(mem, lowest_limit, upper_bound);
111+
physical_memory_size_type orig_limit = lowest_limit != upper_bound ? lowest_limit : upper_bound;
114112
while ((last_slash = strrchr(cg_path, '/')) != cg_path) {
115113
*last_slash = '\0'; // strip path
116114
// update to shortened path and try again
117115
mem->set_subsystem_path(cg_path);
118-
limit = get_updated_mem_limit(mem, lowest_limit, phys_mem);
116+
limit = get_updated_mem_limit(mem, lowest_limit, upper_bound);
119117
if (limit < lowest_limit) {
120118
lowest_limit = limit;
121119
os::free(limit_cg_path); // handles nullptr
@@ -124,13 +122,13 @@ void CgroupUtil::adjust_controller(CgroupMemoryController* mem) {
124122
}
125123
// need to check limit at mount point
126124
mem->set_subsystem_path("/");
127-
limit = get_updated_mem_limit(mem, lowest_limit, phys_mem);
125+
limit = get_updated_mem_limit(mem, lowest_limit, upper_bound);
128126
if (limit < lowest_limit) {
129127
lowest_limit = limit;
130128
os::free(limit_cg_path); // handles nullptr
131129
limit_cg_path = os::strdup("/");
132130
}
133-
assert(lowest_limit <= phys_mem, "limit must not exceed host memory");
131+
assert(lowest_limit <= upper_bound, "limit must not exceed upper bound");
134132
if (lowest_limit != orig_limit) {
135133
// we've found a lower limit anywhere in the hierarchy,
136134
// set the path to the limit path
@@ -152,7 +150,7 @@ void CgroupUtil::adjust_controller(CgroupMemoryController* mem) {
152150
os::free(limit_cg_path);
153151
}
154152

155-
void CgroupUtil::adjust_controller(CgroupCpuController* cpu) {
153+
void CgroupUtil::adjust_controller(CgroupCpuController* cpu, int upper_bound) {
156154
assert(cpu->cgroup_path() != nullptr, "invariant");
157155
if (strstr(cpu->cgroup_path(), "../") != nullptr) {
158156
log_warning(os, container)("Cgroup cpu controller path at '%s' seems to have moved "
@@ -170,26 +168,25 @@ void CgroupUtil::adjust_controller(CgroupCpuController* cpu) {
170168
char* cg_path = os::strdup(orig);
171169
char* last_slash;
172170
assert(cg_path[0] == '/', "cgroup path must start with '/'");
173-
int host_cpus = os::Linux::active_processor_count();
174-
int lowest_limit = host_cpus;
175-
int cpus = get_updated_cpu_limit(cpu, lowest_limit, host_cpus);
176-
int orig_limit = lowest_limit != host_cpus ? lowest_limit : host_cpus;
171+
int lowest_limit = upper_bound;
172+
int cpus = get_updated_cpu_limit(cpu, lowest_limit, upper_bound);
173+
int orig_limit = lowest_limit != upper_bound ? lowest_limit : upper_bound;
177174
char* limit_cg_path = nullptr;
178175
while ((last_slash = strrchr(cg_path, '/')) != cg_path) {
179176
*last_slash = '\0'; // strip path
180177
// update to shortened path and try again
181178
cpu->set_subsystem_path(cg_path);
182-
cpus = get_updated_cpu_limit(cpu, lowest_limit, host_cpus);
183-
if (cpus != host_cpus && cpus < lowest_limit) {
179+
cpus = get_updated_cpu_limit(cpu, lowest_limit, upper_bound);
180+
if (cpus != upper_bound && cpus < lowest_limit) {
184181
lowest_limit = cpus;
185182
os::free(limit_cg_path); // handles nullptr
186183
limit_cg_path = os::strdup(cg_path);
187184
}
188185
}
189186
// need to check limit at mount point
190187
cpu->set_subsystem_path("/");
191-
cpus = get_updated_cpu_limit(cpu, lowest_limit, host_cpus);
192-
if (cpus != host_cpus && cpus < lowest_limit) {
188+
cpus = get_updated_cpu_limit(cpu, lowest_limit, upper_bound);
189+
if (cpus != upper_bound && cpus < lowest_limit) {
193190
lowest_limit = cpus;
194191
os::free(limit_cg_path); // handles nullptr
195192
limit_cg_path = os::strdup(cg_path);

src/hotspot/os/linux/cgroupUtil_linux.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class CgroupUtil: AllStatic {
3434
static bool processor_count(CgroupCpuController* cpu, int upper_bound, int& value);
3535
// Given a memory controller, adjust its path to a point in the hierarchy
3636
// that represents the closest memory limit.
37-
static void adjust_controller(CgroupMemoryController* m);
37+
static void adjust_controller(CgroupMemoryController* m, physical_memory_size_type upper_bound);
3838
// Given a cpu controller, adjust its path to a point in the hierarchy
3939
// that represents the closest cpu limit.
40-
static void adjust_controller(CgroupCpuController* c);
40+
static void adjust_controller(CgroupCpuController* c, int upper_bound);
4141
private:
4242
static physical_memory_size_type get_updated_mem_limit(CgroupMemoryController* m,
4343
physical_memory_size_type lowest,

src/hotspot/os/linux/cgroupV1Subsystem_linux.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,6 @@ CgroupV1Subsystem::CgroupV1Subsystem(CgroupV1Controller* cpuset,
326326
_cpuset(cpuset),
327327
_cpuacct(cpuacct),
328328
_pids(pids) {
329-
CgroupUtil::adjust_controller(memory);
330-
CgroupUtil::adjust_controller(cpu);
331329
_memory = new CachingCgroupController<CgroupMemoryController>(memory);
332330
_cpu = new CachingCgroupController<CgroupCpuController>(cpu);
333331
}

src/hotspot/os/linux/cgroupV2Subsystem_linux.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ CgroupV2Subsystem::CgroupV2Subsystem(CgroupV2MemoryController * memory,
154154
CgroupV2CpuacctController* cpuacct,
155155
CgroupV2Controller unified) :
156156
_unified(unified) {
157-
CgroupUtil::adjust_controller(memory);
158-
CgroupUtil::adjust_controller(cpu);
159157
_memory = new CachingCgroupController<CgroupMemoryController>(memory);
160158
_cpu = new CachingCgroupController<CgroupCpuController>(cpu);
161159
_cpuacct = cpuacct;

src/hotspot/os/linux/osContainer_linux.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ void OSContainer::init() {
5959
if (cgroup_subsystem == nullptr) {
6060
return; // Required subsystem files not found or other error
6161
}
62+
// Adjust controller paths once subsystem is initialized
63+
physical_memory_size_type phys_mem = os::Linux::physical_memory();
64+
int host_cpus = os::Linux::active_processor_count();
65+
cgroup_subsystem->adjust_controllers(phys_mem, host_cpus);
66+
6267
/*
6368
* In order to avoid a false positive on is_containerized() on
6469
* Linux systems outside a container *and* to ensure compatibility
@@ -254,7 +259,7 @@ char * OSContainer::cpu_cpuset_memory_nodes() {
254259

255260
bool OSContainer::active_processor_count(int& value) {
256261
assert(cgroup_subsystem != nullptr, "cgroup subsystem not available");
257-
return cgroup_subsystem->active_processor_count(value);
262+
return cgroup_subsystem->active_processor_count(&os::Linux::active_processor_count, value);
258263
}
259264

260265
bool OSContainer::cpu_quota(int& value) {

test/hotspot/gtest/runtime/test_cgroupSubsystem_linux.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ TEST(cgroupTest, set_cgroupv1_subsystem_path_adjusted) {
479479
ccc->set_subsystem_path((char*)cpu.cgroup_path);
480480
EXPECT_TRUE(ccc->needs_hierarchy_adjustment());
481481

482-
CgroupUtil::adjust_controller(ccc);
482+
CgroupUtil::adjust_controller(ccc, 1);
483483
ASSERT_STREQ(cpu.expected_path, ccc->subsystem_path());
484484
EXPECT_FALSE(ccc->needs_hierarchy_adjustment());
485485

@@ -489,7 +489,7 @@ TEST(cgroupTest, set_cgroupv1_subsystem_path_adjusted) {
489489
cmc->set_subsystem_path((char*)memory.cgroup_path);
490490
EXPECT_TRUE(cmc->needs_hierarchy_adjustment());
491491

492-
CgroupUtil::adjust_controller(cmc);
492+
CgroupUtil::adjust_controller(cmc, (physical_memory_size_type)1024);
493493
ASSERT_STREQ(memory.expected_path, cmc->subsystem_path());
494494
EXPECT_FALSE(cmc->needs_hierarchy_adjustment());
495495
}
@@ -512,7 +512,7 @@ TEST(cgroupTest, set_cgroupv2_subsystem_path_adjusted) {
512512
true /* read-only mount */));
513513
EXPECT_TRUE(ccc->needs_hierarchy_adjustment());
514514

515-
CgroupUtil::adjust_controller(ccc);
515+
CgroupUtil::adjust_controller(ccc, 1);
516516
ASSERT_STREQ(cpu.expected_path, ccc->subsystem_path());
517517
EXPECT_FALSE(ccc->needs_hierarchy_adjustment());
518518

@@ -521,7 +521,7 @@ TEST(cgroupTest, set_cgroupv2_subsystem_path_adjusted) {
521521
true /* read-only mount */));
522522
EXPECT_TRUE(cmc->needs_hierarchy_adjustment());
523523

524-
CgroupUtil::adjust_controller(cmc);
524+
CgroupUtil::adjust_controller(cmc, (physical_memory_size_type)1024);
525525
ASSERT_STREQ(memory.expected_path, cmc->subsystem_path());
526526
EXPECT_FALSE(cmc->needs_hierarchy_adjustment());
527527
}

0 commit comments

Comments
 (0)