Skip to content

Commit f2d5290

Browse files
committed
8367319: Add os interfaces to get machine and container values separately
Reviewed-by: eosterlund, sgehwolf
1 parent c44a99a commit f2d5290

20 files changed

+356
-157
lines changed

src/hotspot/os/aix/os_aix.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2012, 2025 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -258,10 +258,18 @@ bool os::free_memory(physical_memory_size_type& value) {
258258
return Aix::available_memory(value);
259259
}
260260

261+
bool os::Machine::free_memory(physical_memory_size_type& value) {
262+
return Aix::available_memory(value);
263+
}
264+
261265
bool os::available_memory(physical_memory_size_type& value) {
262266
return Aix::available_memory(value);
263267
}
264268

269+
bool os::Machine::available_memory(physical_memory_size_type& value) {
270+
return Aix::available_memory(value);
271+
}
272+
265273
bool os::Aix::available_memory(physical_memory_size_type& value) {
266274
os::Aix::meminfo_t mi;
267275
if (os::Aix::get_meminfo(&mi)) {
@@ -273,6 +281,10 @@ bool os::Aix::available_memory(physical_memory_size_type& value) {
273281
}
274282

275283
bool os::total_swap_space(physical_memory_size_type& value) {
284+
return Machine::total_swap_space(value);
285+
}
286+
287+
bool os::Machine::total_swap_space(physical_memory_size_type& value) {
276288
perfstat_memory_total_t memory_info;
277289
if (libperfstat::perfstat_memory_total(nullptr, &memory_info, sizeof(perfstat_memory_total_t), 1) == -1) {
278290
return false;
@@ -282,6 +294,10 @@ bool os::total_swap_space(physical_memory_size_type& value) {
282294
}
283295

284296
bool os::free_swap_space(physical_memory_size_type& value) {
297+
return Machine::free_swap_space(value);
298+
}
299+
300+
bool os::Machine::free_swap_space(physical_memory_size_type& value) {
285301
perfstat_memory_total_t memory_info;
286302
if (libperfstat::perfstat_memory_total(nullptr, &memory_info, sizeof(perfstat_memory_total_t), 1) == -1) {
287303
return false;
@@ -294,6 +310,10 @@ physical_memory_size_type os::physical_memory() {
294310
return Aix::physical_memory();
295311
}
296312

313+
physical_memory_size_type os::Machine::physical_memory() {
314+
return Aix::physical_memory();
315+
}
316+
297317
size_t os::rss() { return (size_t)0; }
298318

299319
// Cpu architecture string
@@ -2264,6 +2284,10 @@ int os::active_processor_count() {
22642284
return ActiveProcessorCount;
22652285
}
22662286

2287+
return Machine::active_processor_count();
2288+
}
2289+
2290+
int os::Machine::active_processor_count() {
22672291
int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN);
22682292
assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check");
22692293
return online_cpus;

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -137,10 +137,18 @@ bool os::available_memory(physical_memory_size_type& value) {
137137
return Bsd::available_memory(value);
138138
}
139139

140+
bool os::Machine::available_memory(physical_memory_size_type& value) {
141+
return Bsd::available_memory(value);
142+
}
143+
140144
bool os::free_memory(physical_memory_size_type& value) {
141145
return Bsd::available_memory(value);
142146
}
143147

148+
bool os::Machine::free_memory(physical_memory_size_type& value) {
149+
return Bsd::available_memory(value);
150+
}
151+
144152
// Available here means free. Note that this number is of no much use. As an estimate
145153
// for future memory pressure it is far too conservative, since MacOS will use a lot
146154
// of unused memory for caches, and return it willingly in case of needs.
@@ -181,6 +189,10 @@ void os::Bsd::print_uptime_info(outputStream* st) {
181189
}
182190

183191
bool os::total_swap_space(physical_memory_size_type& value) {
192+
return Machine::total_swap_space(value);
193+
}
194+
195+
bool os::Machine::total_swap_space(physical_memory_size_type& value) {
184196
#if defined(__APPLE__)
185197
struct xsw_usage vmusage;
186198
size_t size = sizeof(vmusage);
@@ -195,6 +207,10 @@ bool os::total_swap_space(physical_memory_size_type& value) {
195207
}
196208

197209
bool os::free_swap_space(physical_memory_size_type& value) {
210+
return Machine::free_swap_space(value);
211+
}
212+
213+
bool os::Machine::free_swap_space(physical_memory_size_type& value) {
198214
#if defined(__APPLE__)
199215
struct xsw_usage vmusage;
200216
size_t size = sizeof(vmusage);
@@ -212,6 +228,10 @@ physical_memory_size_type os::physical_memory() {
212228
return Bsd::physical_memory();
213229
}
214230

231+
physical_memory_size_type os::Machine::physical_memory() {
232+
return Bsd::physical_memory();
233+
}
234+
215235
size_t os::rss() {
216236
size_t rss = 0;
217237
#ifdef __APPLE__
@@ -2189,6 +2209,10 @@ int os::active_processor_count() {
21892209
return ActiveProcessorCount;
21902210
}
21912211

2212+
return Machine::active_processor_count();
2213+
}
2214+
2215+
int os::Machine::active_processor_count() {
21922216
return _processor_count;
21932217
}
21942218

src/hotspot/os/linux/cgroupSubsystem_linux.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -631,22 +631,20 @@ void CgroupSubsystemFactory::cleanup(CgroupInfo* cg_infos) {
631631
* return:
632632
* true if there were no errors. false otherwise.
633633
*/
634-
bool CgroupSubsystem::active_processor_count(int& value) {
635-
int cpu_count;
636-
int result = -1;
637-
634+
bool CgroupSubsystem::active_processor_count(double& value) {
638635
// We use a cache with a timeout to avoid performing expensive
639636
// computations in the event this function is called frequently.
640637
// [See 8227006].
641-
CachingCgroupController<CgroupCpuController>* contrl = cpu_controller();
642-
CachedMetric* cpu_limit = contrl->metrics_cache();
638+
CachingCgroupController<CgroupCpuController, double>* contrl = cpu_controller();
639+
CachedMetric<double>* cpu_limit = contrl->metrics_cache();
643640
if (!cpu_limit->should_check_metric()) {
644-
value = (int)cpu_limit->value();
645-
log_trace(os, container)("CgroupSubsystem::active_processor_count (cached): %d", value);
641+
value = cpu_limit->value();
642+
log_trace(os, container)("CgroupSubsystem::active_processor_count (cached): %.2f", value);
646643
return true;
647644
}
648645

649-
cpu_count = os::Linux::active_processor_count();
646+
int cpu_count = os::Linux::active_processor_count();
647+
double result = -1;
650648
if (!CgroupUtil::processor_count(contrl->controller(), cpu_count, result)) {
651649
return false;
652650
}
@@ -671,8 +669,8 @@ bool CgroupSubsystem::active_processor_count(int& value) {
671669
*/
672670
bool CgroupSubsystem::memory_limit_in_bytes(physical_memory_size_type upper_bound,
673671
physical_memory_size_type& value) {
674-
CachingCgroupController<CgroupMemoryController>* contrl = memory_controller();
675-
CachedMetric* memory_limit = contrl->metrics_cache();
672+
CachingCgroupController<CgroupMemoryController, physical_memory_size_type>* contrl = memory_controller();
673+
CachedMetric<physical_memory_size_type>* memory_limit = contrl->metrics_cache();
676674
if (!memory_limit->should_check_metric()) {
677675
value = memory_limit->value();
678676
return true;

src/hotspot/os/linux/cgroupSubsystem_linux.hpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -181,9 +181,10 @@ class CgroupController: public CHeapObj<mtInternal> {
181181
static bool limit_from_str(char* limit_str, physical_memory_size_type& value);
182182
};
183183

184+
template <typename MetricType>
184185
class CachedMetric : public CHeapObj<mtInternal>{
185186
private:
186-
volatile physical_memory_size_type _metric;
187+
volatile MetricType _metric;
187188
volatile jlong _next_check_counter;
188189
public:
189190
CachedMetric() {
@@ -193,8 +194,8 @@ class CachedMetric : public CHeapObj<mtInternal>{
193194
bool should_check_metric() {
194195
return os::elapsed_counter() > _next_check_counter;
195196
}
196-
physical_memory_size_type value() { return _metric; }
197-
void set_value(physical_memory_size_type value, jlong timeout) {
197+
MetricType value() { return _metric; }
198+
void set_value(MetricType value, jlong timeout) {
198199
_metric = value;
199200
// Metric is unlikely to change, but we want to remain
200201
// responsive to configuration changes. A very short grace time
@@ -205,19 +206,19 @@ class CachedMetric : public CHeapObj<mtInternal>{
205206
}
206207
};
207208

208-
template <class T>
209+
template <class T, typename MetricType>
209210
class CachingCgroupController : public CHeapObj<mtInternal> {
210211
private:
211212
T* _controller;
212-
CachedMetric* _metrics_cache;
213+
CachedMetric<MetricType>* _metrics_cache;
213214

214215
public:
215216
CachingCgroupController(T* cont) {
216217
_controller = cont;
217-
_metrics_cache = new CachedMetric();
218+
_metrics_cache = new CachedMetric<MetricType>();
218219
}
219220

220-
CachedMetric* metrics_cache() { return _metrics_cache; }
221+
CachedMetric<MetricType>* metrics_cache() { return _metrics_cache; }
221222
T* controller() { return _controller; }
222223
};
223224

@@ -277,7 +278,7 @@ class CgroupMemoryController: public CHeapObj<mtInternal> {
277278
class CgroupSubsystem: public CHeapObj<mtInternal> {
278279
public:
279280
bool memory_limit_in_bytes(physical_memory_size_type upper_bound, physical_memory_size_type& value);
280-
bool active_processor_count(int& value);
281+
bool active_processor_count(double& value);
281282

282283
virtual bool pids_max(uint64_t& value) = 0;
283284
virtual bool pids_current(uint64_t& value) = 0;
@@ -286,8 +287,8 @@ class CgroupSubsystem: public CHeapObj<mtInternal> {
286287
virtual char * cpu_cpuset_cpus() = 0;
287288
virtual char * cpu_cpuset_memory_nodes() = 0;
288289
virtual const char * container_type() = 0;
289-
virtual CachingCgroupController<CgroupMemoryController>* memory_controller() = 0;
290-
virtual CachingCgroupController<CgroupCpuController>* cpu_controller() = 0;
290+
virtual CachingCgroupController<CgroupMemoryController, physical_memory_size_type>* memory_controller() = 0;
291+
virtual CachingCgroupController<CgroupCpuController, double>* cpu_controller() = 0;
291292
virtual CgroupCpuacctController* cpuacct_controller() = 0;
292293

293294
bool cpu_quota(int& value);

src/hotspot/os/linux/cgroupUtil_linux.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2024, 2025, Red Hat, Inc.
3+
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
56
* This code is free software; you can redistribute it and/or modify it
@@ -25,9 +26,8 @@
2526
#include "cgroupUtil_linux.hpp"
2627
#include "os_linux.hpp"
2728

28-
bool CgroupUtil::processor_count(CgroupCpuController* cpu_ctrl, int upper_bound, int& value) {
29+
bool CgroupUtil::processor_count(CgroupCpuController* cpu_ctrl, int upper_bound, double& value) {
2930
assert(upper_bound > 0, "upper bound of cpus must be positive");
30-
int limit_count = upper_bound;
3131
int quota = -1;
3232
int period = -1;
3333
if (!cpu_ctrl->cpu_quota(quota)) {
@@ -37,20 +37,15 @@ bool CgroupUtil::processor_count(CgroupCpuController* cpu_ctrl, int upper_bound,
3737
return false;
3838
}
3939
int quota_count = 0;
40-
int result = upper_bound;
40+
double result = upper_bound;
4141

42-
if (quota > -1 && period > 0) {
43-
quota_count = ceilf((float)quota / (float)period);
44-
log_trace(os, container)("CPU Quota count based on quota/period: %d", quota_count);
42+
if (quota > 0 && period > 0) { // Use quotas
43+
double cpu_quota = static_cast<double>(quota) / period;
44+
log_trace(os, container)("CPU Quota based on quota/period: %.2f", cpu_quota);
45+
result = MIN2(result, cpu_quota);
4546
}
4647

47-
// Use quotas
48-
if (quota_count != 0) {
49-
limit_count = quota_count;
50-
}
51-
52-
result = MIN2(upper_bound, limit_count);
53-
log_trace(os, container)("OSContainer::active_processor_count: %d", result);
48+
log_trace(os, container)("OSContainer::active_processor_count: %.2f", result);
5449
value = result;
5550
return true;
5651
}
@@ -73,11 +68,11 @@ physical_memory_size_type CgroupUtil::get_updated_mem_limit(CgroupMemoryControll
7368

7469
// Get an updated cpu limit. The return value is strictly less than or equal to the
7570
// passed in 'lowest' value.
76-
int CgroupUtil::get_updated_cpu_limit(CgroupCpuController* cpu,
71+
double CgroupUtil::get_updated_cpu_limit(CgroupCpuController* cpu,
7772
int lowest,
7873
int upper_bound) {
7974
assert(lowest > 0 && lowest <= upper_bound, "invariant");
80-
int cpu_limit_val = -1;
75+
double cpu_limit_val = -1;
8176
if (CgroupUtil::processor_count(cpu, upper_bound, cpu_limit_val) && cpu_limit_val != upper_bound) {
8277
assert(cpu_limit_val <= upper_bound, "invariant");
8378
if (lowest > cpu_limit_val) {
@@ -172,7 +167,7 @@ void CgroupUtil::adjust_controller(CgroupCpuController* cpu) {
172167
assert(cg_path[0] == '/', "cgroup path must start with '/'");
173168
int host_cpus = os::Linux::active_processor_count();
174169
int lowest_limit = host_cpus;
175-
int cpus = get_updated_cpu_limit(cpu, lowest_limit, host_cpus);
170+
double cpus = get_updated_cpu_limit(cpu, lowest_limit, host_cpus);
176171
int orig_limit = lowest_limit != host_cpus ? lowest_limit : host_cpus;
177172
char* limit_cg_path = nullptr;
178173
while ((last_slash = strrchr(cg_path, '/')) != cg_path) {

src/hotspot/os/linux/cgroupUtil_linux.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2024, Red Hat, Inc.
3+
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
56
* This code is free software; you can redistribute it and/or modify it
@@ -31,7 +32,7 @@
3132
class CgroupUtil: AllStatic {
3233

3334
public:
34-
static bool processor_count(CgroupCpuController* cpu, int upper_bound, int& value);
35+
static bool processor_count(CgroupCpuController* cpu, int upper_bound, double& value);
3536
// Given a memory controller, adjust its path to a point in the hierarchy
3637
// that represents the closest memory limit.
3738
static void adjust_controller(CgroupMemoryController* m);
@@ -42,9 +43,7 @@ class CgroupUtil: AllStatic {
4243
static physical_memory_size_type get_updated_mem_limit(CgroupMemoryController* m,
4344
physical_memory_size_type lowest,
4445
physical_memory_size_type upper_bound);
45-
static int get_updated_cpu_limit(CgroupCpuController* c,
46-
int lowest,
47-
int upper_bound);
46+
static double get_updated_cpu_limit(CgroupCpuController* c, int lowest, int upper_bound);
4847
};
4948

5049
#endif // CGROUP_UTIL_LINUX_HPP

src/hotspot/os/linux/cgroupV1Subsystem_linux.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -328,8 +328,8 @@ CgroupV1Subsystem::CgroupV1Subsystem(CgroupV1Controller* cpuset,
328328
_pids(pids) {
329329
CgroupUtil::adjust_controller(memory);
330330
CgroupUtil::adjust_controller(cpu);
331-
_memory = new CachingCgroupController<CgroupMemoryController>(memory);
332-
_cpu = new CachingCgroupController<CgroupCpuController>(cpu);
331+
_memory = new CachingCgroupController<CgroupMemoryController, physical_memory_size_type>(memory);
332+
_cpu = new CachingCgroupController<CgroupCpuController, double>(cpu);
333333
}
334334

335335
bool CgroupV1Subsystem::is_containerized() {

0 commit comments

Comments
 (0)