Skip to content

Commit a4add5b

Browse files
committed
Modify OSContainer API to use ssize_t/size_t
Where previously a -2 (OSCONTAINER_ERROR) was returned, we map it to values -1 or 0 depending on the use. Therefore, only values in range [-1,SSIZE_MAX] are possible after this change.
1 parent 27a5c56 commit a4add5b

11 files changed

+764
-531
lines changed

src/hotspot/os/linux/cgroupSubsystem_linux.cpp

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -661,18 +661,17 @@ int CgroupSubsystem::active_processor_count() {
661661
*
662662
* return:
663663
* memory limit in bytes or
664-
* -1 for unlimited
665-
* OSCONTAINER_ERROR for not supported
664+
* -1 for unlimited (or unavailable)
666665
*/
667-
jlong CgroupSubsystem::memory_limit_in_bytes() {
666+
ssize_t CgroupSubsystem::memory_limit_in_bytes() {
668667
CachingCgroupController<CgroupMemoryController>* contrl = memory_controller();
669668
CachedMetric* memory_limit = contrl->metrics_cache();
670669
if (!memory_limit->should_check_metric()) {
671670
return memory_limit->value();
672671
}
673672
size_t phys_mem = os::Linux::physical_memory();
674673
log_trace(os, container)("total physical memory: %zu", phys_mem);
675-
jlong mem_limit = contrl->controller()->read_memory_limit_in_bytes(phys_mem);
674+
ssize_t mem_limit = contrl->controller()->read_memory_limit_in_bytes(phys_mem);
676675
// Update cached metric to avoid re-reading container settings too often
677676
memory_limit->set_value(mem_limit, OSCONTAINER_CACHE_TIMEOUT);
678677
return mem_limit;
@@ -720,36 +719,35 @@ bool CgroupController::read_string(const char* filename, char* buf, size_t buf_s
720719
return true;
721720
}
722721

723-
bool CgroupController::read_number(const char* filename, julong* result) {
722+
bool CgroupController::read_number(const char* filename, size_t& result) {
724723
char buf[1024];
725724
bool is_ok = read_string(filename, buf, 1024);
726725
if (!is_ok) {
727726
return false;
728727
}
729-
int matched = sscanf(buf, JULONG_FORMAT, result);
728+
int matched = sscanf(buf, "%zu", &result);
730729
if (matched == 1) {
731730
return true;
732731
}
733732
return false;
734733
}
735734

736-
bool CgroupController::read_number_handle_max(const char* filename, jlong* result) {
735+
bool CgroupController::read_number_handle_max(const char* filename, ssize_t& result) {
737736
char buf[1024];
738737
bool is_ok = read_string(filename, buf, 1024);
739738
if (!is_ok) {
740739
return false;
741740
}
742-
jlong val = limit_from_str(buf);
743-
if (val == OSCONTAINER_ERROR) {
741+
ssize_t val = 0;
742+
if (!limit_from_str(buf, val)) {
744743
return false;
745744
}
746-
*result = val;
745+
result = val;
747746
return true;
748747
}
749748

750-
bool CgroupController::read_numerical_key_value(const char* filename, const char* key, julong* result) {
749+
bool CgroupController::read_numerical_key_value(const char* filename, const char* key, size_t& result) {
751750
assert(key != nullptr, "key must be given");
752-
assert(result != nullptr, "result pointer must not be null");
753751
assert(filename != nullptr, "file to search in must be given");
754752
const char* s_path = subsystem_path();
755753
if (s_path == nullptr) {
@@ -787,7 +785,7 @@ bool CgroupController::read_numerical_key_value(const char* filename, const char
787785
&& after_key != '\n') {
788786
// Skip key, skip space
789787
const char* value_substr = line + key_len + 1;
790-
int matched = sscanf(value_substr, JULONG_FORMAT, result);
788+
int matched = sscanf(value_substr, "%zu", &result);
791789
found_match = matched == 1;
792790
if (found_match) {
793791
break;
@@ -798,12 +796,12 @@ bool CgroupController::read_numerical_key_value(const char* filename, const char
798796
if (found_match) {
799797
return true;
800798
}
801-
log_debug(os, container)("Type %s (key == %s) not found in file %s", JULONG_FORMAT,
799+
log_debug(os, container)("Type %s (key == %s) not found in file %s", "%zu",
802800
key, absolute_path);
803801
return false;
804802
}
805803

806-
bool CgroupController::read_numerical_tuple_value(const char* filename, bool use_first, jlong* result) {
804+
bool CgroupController::read_numerical_tuple_value(const char* filename, bool use_first, ssize_t& result) {
807805
char buf[1024];
808806
bool is_ok = read_string(filename, buf, 1024);
809807
if (!is_ok) {
@@ -814,66 +812,68 @@ bool CgroupController::read_numerical_tuple_value(const char* filename, bool use
814812
if (matched != 1) {
815813
return false;
816814
}
817-
jlong val = limit_from_str(token);
818-
if (val == OSCONTAINER_ERROR) {
815+
ssize_t val = 0;
816+
if (!limit_from_str(token, val)) {
819817
return false;
820818
}
821-
*result = val;
819+
result = val;
822820
return true;
823821
}
824822

825-
jlong CgroupController::limit_from_str(char* limit_str) {
823+
bool CgroupController::limit_from_str(char* limit_str, ssize_t& value) {
826824
if (limit_str == nullptr) {
827-
return OSCONTAINER_ERROR;
825+
return false;
828826
}
829827
// Unlimited memory in cgroups is the literal string 'max' for
830828
// some controllers, for example the pids controller.
831829
if (strcmp("max", limit_str) == 0) {
832-
return (jlong)-1;
830+
value = (ssize_t)-1;
831+
return true;
833832
}
834-
julong limit;
835-
if (sscanf(limit_str, JULONG_FORMAT, &limit) != 1) {
836-
return OSCONTAINER_ERROR;
833+
size_t limit;
834+
if (sscanf(limit_str, "%zu", &limit) != 1) {
835+
return false;
837836
}
838-
return (jlong)limit;
837+
value = static_cast<ssize_t>(limit);
838+
return true;
839839
}
840840

841841
// CgroupSubsystem implementations
842842

843-
jlong CgroupSubsystem::memory_and_swap_limit_in_bytes() {
843+
ssize_t CgroupSubsystem::memory_and_swap_limit_in_bytes() {
844844
size_t phys_mem = os::Linux::physical_memory();
845845
size_t host_swap = os::Linux::host_swap();
846846
return memory_controller()->controller()->memory_and_swap_limit_in_bytes(phys_mem, host_swap);
847847
}
848848

849-
jlong CgroupSubsystem::memory_and_swap_usage_in_bytes() {
849+
ssize_t CgroupSubsystem::memory_and_swap_usage_in_bytes() {
850850
size_t phys_mem = os::Linux::physical_memory();
851851
size_t host_swap = os::Linux::host_swap();
852852
return memory_controller()->controller()->memory_and_swap_usage_in_bytes(phys_mem, host_swap);
853853
}
854854

855-
jlong CgroupSubsystem::memory_soft_limit_in_bytes() {
855+
ssize_t CgroupSubsystem::memory_soft_limit_in_bytes() {
856856
size_t phys_mem = os::Linux::physical_memory();
857857
return memory_controller()->controller()->memory_soft_limit_in_bytes(phys_mem);
858858
}
859859

860-
jlong CgroupSubsystem::memory_throttle_limit_in_bytes() {
860+
ssize_t CgroupSubsystem::memory_throttle_limit_in_bytes() {
861861
return memory_controller()->controller()->memory_throttle_limit_in_bytes();
862862
}
863863

864-
jlong CgroupSubsystem::memory_usage_in_bytes() {
864+
ssize_t CgroupSubsystem::memory_usage_in_bytes() {
865865
return memory_controller()->controller()->memory_usage_in_bytes();
866866
}
867867

868-
jlong CgroupSubsystem::memory_max_usage_in_bytes() {
868+
ssize_t CgroupSubsystem::memory_max_usage_in_bytes() {
869869
return memory_controller()->controller()->memory_max_usage_in_bytes();
870870
}
871871

872-
jlong CgroupSubsystem::rss_usage_in_bytes() {
872+
ssize_t CgroupSubsystem::rss_usage_in_bytes() {
873873
return memory_controller()->controller()->rss_usage_in_bytes();
874874
}
875875

876-
jlong CgroupSubsystem::cache_usage_in_bytes() {
876+
ssize_t CgroupSubsystem::cache_usage_in_bytes() {
877877
return memory_controller()->controller()->cache_usage_in_bytes();
878878
}
879879

@@ -889,7 +889,7 @@ int CgroupSubsystem::cpu_shares() {
889889
return cpu_controller()->controller()->cpu_shares();
890890
}
891891

892-
jlong CgroupSubsystem::cpu_usage_in_micros() {
892+
ssize_t CgroupSubsystem::cpu_usage_in_micros() {
893893
return cpuacct_controller()->cpu_usage_in_micros();
894894
}
895895

src/hotspot/os/linux/cgroupSubsystem_linux.hpp

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,25 @@
7272
#define CONTAINER_READ_NUMBER_CHECKED(controller, filename, log_string, retval) \
7373
{ \
7474
bool is_ok; \
75-
is_ok = controller->read_number(filename, &retval); \
75+
is_ok = controller->read_number(filename, retval); \
7676
if (!is_ok) { \
7777
log_trace(os, container)(log_string " failed: %d", OSCONTAINER_ERROR); \
78-
return OSCONTAINER_ERROR; \
78+
return false; \
7979
} \
80-
log_trace(os, container)(log_string " is: " JULONG_FORMAT, retval); \
80+
log_trace(os, container)(log_string " is: %zu", retval); \
81+
return true; \
8182
}
8283

8384
#define CONTAINER_READ_NUMBER_CHECKED_MAX(controller, filename, log_string, retval) \
8485
{ \
8586
bool is_ok; \
86-
is_ok = controller->read_number_handle_max(filename, &retval); \
87+
is_ok = controller->read_number_handle_max(filename, retval); \
8788
if (!is_ok) { \
8889
log_trace(os, container)(log_string " failed: %d", OSCONTAINER_ERROR); \
89-
return OSCONTAINER_ERROR; \
90+
return false; \
9091
} \
91-
log_trace(os, container)(log_string " is: " JLONG_FORMAT, retval); \
92+
log_trace(os, container)(log_string " is: %zd", retval); \
93+
return true; \
9294
}
9395

9496
#define CONTAINER_READ_STRING_CHECKED(controller, filename, log_string, retval, buf_size) \
@@ -113,21 +115,21 @@ class CgroupController: public CHeapObj<mtInternal> {
113115
const char* mount_point() { return _mount_point; }
114116
virtual bool needs_hierarchy_adjustment() { return false; }
115117

116-
/* Read a numerical value as unsigned long
118+
/* Read a numerical value as size_t
117119
*
118120
* returns: false if any error occurred. true otherwise and
119-
* the parsed value is set in the provided julong pointer.
121+
* the parsed value is set in the provided size_t reference.
120122
*/
121-
bool read_number(const char* filename, julong* result);
123+
bool read_number(const char* filename, size_t& result);
122124

123125
/* Convenience method to deal with numbers as well as the string 'max'
124126
* in interface files. Otherwise same as read_number().
125127
*
126128
* returns: false if any error occurred. true otherwise and
127-
* the parsed value (which might be negative) is being set in
128-
* the provided jlong pointer.
129+
* the parsed value in the range [-1,SSIZE_MAX] is being set in
130+
* the provided ssize_t reference. -1 means the value is unlimited.
129131
*/
130-
bool read_number_handle_max(const char* filename, jlong* result);
132+
bool read_number_handle_max(const char* filename, ssize_t& result);
131133

132134
/* Read a string of at most buf_size - 1 characters from the interface file.
133135
* The provided buffer must be at least buf_size in size so as to account
@@ -145,26 +147,27 @@ class CgroupController: public CHeapObj<mtInternal> {
145147
* parsing interface files like cpu.max which contain such tuples.
146148
*
147149
* returns: false if any error occurred. true otherwise and the parsed
148-
* value of the appropriate tuple entry set in the provided jlong pointer.
150+
* value of the appropriate tuple entry set in the provided ssize_t reference
151+
* in the range [-1,SSIZE_MAX]. -1 means unlimited tuple value.
149152
*/
150-
bool read_numerical_tuple_value(const char* filename, bool use_first, jlong* result);
153+
bool read_numerical_tuple_value(const char* filename, bool use_first, ssize_t& result);
151154

152155
/* Read a numerical value from a multi-line interface file. The matched line is
153156
* determined by the provided 'key'. The associated numerical value is being set
154-
* via the passed in julong pointer. Example interface file 'memory.stat'
157+
* via the passed in size_t reference. Example interface file 'memory.stat'
155158
*
156159
* returns: false if any error occurred. true otherwise and the parsed value is
157-
* being set in the provided julong pointer.
160+
* being set in the provided size_t reference.
158161
*/
159-
bool read_numerical_key_value(const char* filename, const char* key, julong* result);
162+
bool read_numerical_key_value(const char* filename, const char* key, size_t& result);
160163

161164
private:
162-
static jlong limit_from_str(char* limit_str);
165+
static bool limit_from_str(char* limit_str, ssize_t& value);
163166
};
164167

165168
class CachedMetric : public CHeapObj<mtInternal>{
166169
private:
167-
volatile jlong _metric;
170+
volatile ssize_t _metric;
168171
volatile jlong _next_check_counter;
169172
public:
170173
CachedMetric() {
@@ -174,8 +177,8 @@ class CachedMetric : public CHeapObj<mtInternal>{
174177
bool should_check_metric() {
175178
return os::elapsed_counter() > _next_check_counter;
176179
}
177-
jlong value() { return _metric; }
178-
void set_value(jlong value, jlong timeout) {
180+
ssize_t value() { return _metric; }
181+
void set_value(ssize_t value, jlong timeout) {
179182
_metric = value;
180183
// Metric is unlikely to change, but we want to remain
181184
// responsive to configuration changes. A very short grace time
@@ -219,7 +222,7 @@ class CgroupCpuController: public CHeapObj<mtInternal> {
219222
// Pure virtual class representing version agnostic CPU accounting controllers
220223
class CgroupCpuacctController: public CHeapObj<mtInternal> {
221224
public:
222-
virtual jlong cpu_usage_in_micros() = 0;
225+
virtual ssize_t cpu_usage_in_micros() = 0;
223226
virtual bool needs_hierarchy_adjustment() = 0;
224227
virtual bool is_read_only() = 0;
225228
virtual const char* subsystem_path() = 0;
@@ -231,15 +234,15 @@ class CgroupCpuacctController: public CHeapObj<mtInternal> {
231234
// Pure virtual class representing version agnostic memory controllers
232235
class CgroupMemoryController: public CHeapObj<mtInternal> {
233236
public:
234-
virtual jlong read_memory_limit_in_bytes(size_t host_mem) = 0;
235-
virtual jlong memory_usage_in_bytes() = 0;
236-
virtual jlong memory_and_swap_limit_in_bytes(size_t host_mem, size_t host_swap) = 0;
237-
virtual jlong memory_and_swap_usage_in_bytes(size_t host_mem, size_t host_swap) = 0;
238-
virtual jlong memory_soft_limit_in_bytes(size_t host_mem) = 0;
239-
virtual jlong memory_throttle_limit_in_bytes() = 0;
240-
virtual jlong memory_max_usage_in_bytes() = 0;
241-
virtual jlong rss_usage_in_bytes() = 0;
242-
virtual jlong cache_usage_in_bytes() = 0;
237+
virtual ssize_t read_memory_limit_in_bytes(size_t host_mem) = 0;
238+
virtual ssize_t memory_usage_in_bytes() = 0;
239+
virtual ssize_t memory_and_swap_limit_in_bytes(size_t host_mem, size_t host_swap) = 0;
240+
virtual ssize_t memory_and_swap_usage_in_bytes(size_t host_mem, size_t host_swap) = 0;
241+
virtual ssize_t memory_soft_limit_in_bytes(size_t host_mem) = 0;
242+
virtual ssize_t memory_throttle_limit_in_bytes() = 0;
243+
virtual ssize_t memory_max_usage_in_bytes() = 0;
244+
virtual ssize_t rss_usage_in_bytes() = 0;
245+
virtual ssize_t cache_usage_in_bytes() = 0;
243246
virtual void print_version_specific_info(outputStream* st, size_t host_mem) = 0;
244247
virtual bool needs_hierarchy_adjustment() = 0;
245248
virtual bool is_read_only() = 0;
@@ -251,11 +254,11 @@ class CgroupMemoryController: public CHeapObj<mtInternal> {
251254

252255
class CgroupSubsystem: public CHeapObj<mtInternal> {
253256
public:
254-
jlong memory_limit_in_bytes();
257+
ssize_t memory_limit_in_bytes();
255258
int active_processor_count();
256259

257-
virtual jlong pids_max() = 0;
258-
virtual jlong pids_current() = 0;
260+
virtual ssize_t pids_max() = 0;
261+
virtual ssize_t pids_current() = 0;
259262
virtual bool is_containerized() = 0;
260263

261264
virtual char * cpu_cpuset_cpus() = 0;
@@ -269,16 +272,16 @@ class CgroupSubsystem: public CHeapObj<mtInternal> {
269272
int cpu_period();
270273
int cpu_shares();
271274

272-
jlong cpu_usage_in_micros();
275+
ssize_t cpu_usage_in_micros();
273276

274-
jlong memory_usage_in_bytes();
275-
jlong memory_and_swap_limit_in_bytes();
276-
jlong memory_and_swap_usage_in_bytes();
277-
jlong memory_soft_limit_in_bytes();
278-
jlong memory_throttle_limit_in_bytes();
279-
jlong memory_max_usage_in_bytes();
280-
jlong rss_usage_in_bytes();
281-
jlong cache_usage_in_bytes();
277+
ssize_t memory_usage_in_bytes();
278+
ssize_t memory_and_swap_limit_in_bytes();
279+
ssize_t memory_and_swap_usage_in_bytes();
280+
ssize_t memory_soft_limit_in_bytes();
281+
ssize_t memory_throttle_limit_in_bytes();
282+
ssize_t memory_max_usage_in_bytes();
283+
ssize_t rss_usage_in_bytes();
284+
ssize_t cache_usage_in_bytes();
282285
void print_version_specific_info(outputStream* st);
283286
};
284287

0 commit comments

Comments
 (0)