8197408: Bad pointer comparison and small cleanup in os_linux.cpp

Reviewed-by: bobv, stuefe
This commit is contained in:
Robbin Ehn 2018-02-27 14:15:30 +01:00
parent 8241f85b85
commit 5287d9a366
4 changed files with 146 additions and 82 deletions

View file

@ -414,9 +414,9 @@ void OSContainer::init() {
} }
char * OSContainer::container_type() { const char * OSContainer::container_type() {
if (is_containerized()) { if (is_containerized()) {
return (char *)"cgroupv1"; return "cgroupv1";
} else { } else {
return NULL; return NULL;
} }

View file

@ -40,7 +40,7 @@ class OSContainer: AllStatic {
public: public:
static void init(); static void init();
static inline bool is_containerized(); static inline bool is_containerized();
static char * container_type(); static const char * container_type();
static jlong memory_limit_in_bytes(); static jlong memory_limit_in_bytes();
static jlong memory_and_swap_limit_in_bytes(); static jlong memory_and_swap_limit_in_bytes();

View file

@ -177,20 +177,17 @@ julong os::Linux::available_memory() {
if (OSContainer::is_containerized()) { if (OSContainer::is_containerized()) {
jlong mem_limit, mem_usage; jlong mem_limit, mem_usage;
if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) { if ((mem_limit = OSContainer::memory_limit_in_bytes()) < 1) {
if ((mem_usage = OSContainer::memory_usage_in_bytes()) > 0) { log_debug(os, container)("container memory limit %s: " JLONG_FORMAT ", using host value",
if (mem_limit > mem_usage) { mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);
avail_mem = (julong)mem_limit - (julong)mem_usage;
} else {
avail_mem = 0;
} }
if (mem_limit > 0 && (mem_usage = OSContainer::memory_usage_in_bytes()) < 1) {
log_debug(os, container)("container memory usage failed: " JLONG_FORMAT ", using host value", mem_usage);
}
if (mem_limit > 0 && mem_usage > 0 ) {
avail_mem = mem_limit > mem_usage ? (julong)mem_limit - (julong)mem_usage : 0;
log_trace(os)("available container memory: " JULONG_FORMAT, avail_mem); log_trace(os)("available container memory: " JULONG_FORMAT, avail_mem);
return avail_mem; return avail_mem;
} else {
log_debug(os,container)("container memory usage call failed: " JLONG_FORMAT, mem_usage);
}
} else {
log_debug(os,container)("container memory unlimited or failed: " JLONG_FORMAT, mem_limit);
} }
} }
@ -201,22 +198,18 @@ julong os::Linux::available_memory() {
} }
julong os::physical_memory() { julong os::physical_memory() {
jlong phys_mem = 0;
if (OSContainer::is_containerized()) { if (OSContainer::is_containerized()) {
jlong mem_limit; jlong mem_limit;
if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) { if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
log_trace(os)("total container memory: " JLONG_FORMAT, mem_limit); log_trace(os)("total container memory: " JLONG_FORMAT, mem_limit);
return (julong)mem_limit; return phys_mem;
} else {
if (mem_limit == OSCONTAINER_ERROR) {
log_debug(os,container)("container memory limit call failed");
}
if (mem_limit == -1) {
log_debug(os,container)("container memory unlimited, using host value");
}
} }
log_debug(os, container)("container memory limit %s: " JLONG_FORMAT ", using host value",
mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);
} }
jlong phys_mem = Linux::physical_memory(); phys_mem = Linux::physical_memory();
log_trace(os)("total system memory: " JLONG_FORMAT, phys_mem); log_trace(os)("total system memory: " JLONG_FORMAT, phys_mem);
return phys_mem; return phys_mem;
} }
@ -2135,37 +2128,29 @@ void os::Linux::print_full_memory_info(outputStream* st) {
} }
void os::Linux::print_container_info(outputStream* st) { void os::Linux::print_container_info(outputStream* st) {
if (OSContainer::is_containerized()) { if (!OSContainer::is_containerized()) {
return;
}
st->print("container (cgroup) information:\n"); st->print("container (cgroup) information:\n");
char *p = OSContainer::container_type(); const char *p_ct = OSContainer::container_type();
if (p == NULL) st->print("container_type: %s\n", p_ct != NULL ? p_ct : "failed");
st->print("container_type() failed\n");
else {
st->print("container_type: %s\n", p);
}
p = OSContainer::cpu_cpuset_cpus(); char *p = OSContainer::cpu_cpuset_cpus();
if (p == NULL) st->print("cpu_cpuset_cpus: %s\n", p != NULL ? p : "failed");
st->print("cpu_cpuset_cpus() failed\n");
else {
st->print("cpu_cpuset_cpus: %s\n", p);
free(p); free(p);
}
p = OSContainer::cpu_cpuset_memory_nodes(); p = OSContainer::cpu_cpuset_memory_nodes();
if (p < 0) st->print("cpu_memory_nodes: %s\n", p != NULL ? p : "failed");
st->print("cpu_memory_nodes() failed\n");
else {
st->print("cpu_memory_nodes: %s\n", p);
free(p); free(p);
}
int i = OSContainer::active_processor_count(); int i = OSContainer::active_processor_count();
if (i < 0) if (i > 0) {
st->print("active_processor_count() failed\n");
else
st->print("active_processor_count: %d\n", i); st->print("active_processor_count: %d\n", i);
} else {
st->print("active_processor_count: failed\n");
}
i = OSContainer::cpu_quota(); i = OSContainer::cpu_quota();
st->print("cpu_quota: %d\n", i); st->print("cpu_quota: %d\n", i);
@ -2192,7 +2177,6 @@ void os::Linux::print_container_info(outputStream* st) {
st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j); st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j);
st->cr(); st->cr();
} }
}
void os::print_memory_info(outputStream* st) { void os::print_memory_info(outputStream* st) {

View file

@ -0,0 +1,80 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test PlainRead
* @requires os.family == "linux"
* @library /testlibrary /test/lib
* @build sun.hotspot.WhiteBox
* @run driver ClassFileInstaller sun.hotspot.WhiteBox
* sun.hotspot.WhiteBox$WhiteBoxPermission
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI PlainRead
*/
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.Platform;
import sun.hotspot.WhiteBox;
public class PlainRead {
static public void match(OutputAnalyzer oa, String what, String value) {
oa.shouldMatch("^.*" + what + " *" + value + ".*$");
}
static public void noMatch(OutputAnalyzer oa, String what, String value) {
oa.shouldNotMatch("^.*" + what + " *" + value + ".*$");
}
static final String good_value = "(\\d+|-1|Unlimited)";
static final String bad_value = "(failed)";
static final String[] variables = {"Memory Limit is:", "CPU Shares is:", "CPU Quota is:", "CPU Period is:", "active_processor_count:"};
static public void isContainer(OutputAnalyzer oa) {
for (String v: variables) {
match(oa, v, good_value);
}
for (String v: variables) {
noMatch(oa, v, bad_value);
}
}
static public void isNotContainer(OutputAnalyzer oa) {
oa.shouldMatch("^.*Can't open /proc/self/mountinfo.*$");
}
public static void main(String[] args) throws Exception {
WhiteBox wb = WhiteBox.getWhiteBox();
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:os+container=trace", "-version");
OutputAnalyzer output = new OutputAnalyzer(pb.start());
if (wb.isContainerized()) {
System.out.println("Inside a cgroup, testing...");
isContainer(output);
} else {
System.out.println("Not in a cgroup, testing...");
isNotContainer(output);
}
}
}