Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/management/OperatingSystemImpl.java
32287 views
1
/*
2
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.management;
27
28
import jdk.internal.platform.Metrics;
29
import java.util.concurrent.TimeUnit;
30
31
/**
32
* Implementation class for the operating system.
33
* Standard and committed hotspot-specific metrics if any.
34
*
35
* ManagementFactory.getOperatingSystemMXBean() returns an instance
36
* of this class.
37
*/
38
class OperatingSystemImpl extends BaseOperatingSystemImpl
39
implements com.sun.management.UnixOperatingSystemMXBean {
40
41
private static final int MAX_ATTEMPTS_NUMBER = 10;
42
private final Metrics containerMetrics;
43
44
OperatingSystemImpl(VMManagement vm) {
45
super(vm);
46
this.containerMetrics = jdk.internal.platform.Container.metrics();
47
}
48
49
public long getTotalSwapSpaceSize() {
50
if (containerMetrics != null) {
51
long limit = containerMetrics.getMemoryAndSwapLimit();
52
// The memory limit metrics is not available if JVM runs on Linux host (not in a docker container)
53
// or if a docker container was started without specifying a memory limit (without '--memory='
54
// Docker option). In latter case there is no limit on how much memory the container can use and
55
// it can use as much memory as the host's OS allows.
56
long memLimit = containerMetrics.getMemoryLimit();
57
if (limit >= 0 && memLimit >= 0) {
58
return limit - memLimit;
59
}
60
}
61
return getTotalSwapSpaceSize0();
62
}
63
64
public long getFreeSwapSpaceSize() {
65
if (containerMetrics != null) {
66
long memSwapLimit = containerMetrics.getMemoryAndSwapLimit();
67
long memLimit = containerMetrics.getMemoryLimit();
68
if (memSwapLimit >= 0 && memLimit >= 0) {
69
long deltaLimit = memSwapLimit - memLimit;
70
// Return 0 when memSwapLimit == memLimit, which means no swap space is allowed.
71
// And the same for memSwapLimit < memLimit.
72
if (deltaLimit <= 0) {
73
return 0;
74
}
75
for (int attempt = 0; attempt < MAX_ATTEMPTS_NUMBER; attempt++) {
76
long memSwapUsage = containerMetrics.getMemoryAndSwapUsage();
77
long memUsage = containerMetrics.getMemoryUsage();
78
if (memSwapUsage > 0 && memUsage > 0) {
79
// We read "memory usage" and "memory and swap usage" not atomically,
80
// and it's possible to get the negative value when subtracting these two.
81
// If this happens just retry the loop for a few iterations.
82
long deltaUsage = memSwapUsage - memUsage;
83
if (deltaUsage >= 0) {
84
long freeSwap = deltaLimit - deltaUsage;
85
if (freeSwap >= 0) {
86
return freeSwap;
87
}
88
}
89
}
90
}
91
}
92
}
93
return getFreeSwapSpaceSize0();
94
}
95
96
public long getFreePhysicalMemorySize() {
97
if (containerMetrics != null) {
98
long usage = containerMetrics.getMemoryUsage();
99
long limit = containerMetrics.getMemoryLimit();
100
if (usage > 0 && limit >= 0) {
101
return limit - usage;
102
}
103
}
104
return getFreePhysicalMemorySize0();
105
}
106
107
public long getTotalPhysicalMemorySize() {
108
if (containerMetrics != null) {
109
long limit = containerMetrics.getMemoryLimit();
110
if (limit >= 0) {
111
return limit;
112
}
113
}
114
return getTotalPhysicalMemorySize0();
115
}
116
117
public double getSystemCpuLoad() {
118
if (containerMetrics != null) {
119
long quota = containerMetrics.getCpuQuota();
120
if (quota > 0) {
121
long periodLength = containerMetrics.getCpuPeriod();
122
long numPeriods = containerMetrics.getCpuNumPeriods();
123
long usageNanos = containerMetrics.getCpuUsage();
124
if (periodLength > 0 && numPeriods > 0 && usageNanos > 0) {
125
long elapsedNanos = TimeUnit.MICROSECONDS.toNanos(periodLength * numPeriods);
126
double systemLoad = (double) usageNanos / elapsedNanos;
127
// Ensure the return value is in the range 0.0 -> 1.0
128
systemLoad = Math.max(0.0, systemLoad);
129
systemLoad = Math.min(1.0, systemLoad);
130
return systemLoad;
131
}
132
return -1;
133
} else {
134
// If CPU quotas are not active then find the average system load for
135
// all online CPUs that are allowed to run this container.
136
137
// If the cpuset is the same as the host's one there is no need to iterate over each CPU
138
if (isCpuSetSameAsHostCpuSet()) {
139
return getSystemCpuLoad0();
140
} else {
141
int[] cpuSet = containerMetrics.getEffectiveCpuSetCpus();
142
if (cpuSet != null && cpuSet.length > 0) {
143
double systemLoad = 0.0;
144
for (int cpu : cpuSet) {
145
double cpuLoad = getSingleCpuLoad0(cpu);
146
if (cpuLoad < 0) {
147
return -1;
148
}
149
systemLoad += cpuLoad;
150
}
151
return systemLoad / cpuSet.length;
152
}
153
return -1;
154
}
155
}
156
}
157
return getSystemCpuLoad0();
158
}
159
160
private boolean isCpuSetSameAsHostCpuSet() {
161
if (containerMetrics != null) {
162
return containerMetrics.getCpuSetCpus().length == getHostConfiguredCpuCount0();
163
}
164
return false;
165
}
166
167
public native long getCommittedVirtualMemorySize();
168
private native long getTotalSwapSpaceSize0();
169
private native long getFreeSwapSpaceSize0();
170
public native long getProcessCpuTime();
171
private native long getFreePhysicalMemorySize0();
172
private native long getTotalPhysicalMemorySize0();
173
public native long getOpenFileDescriptorCount();
174
public native long getMaxFileDescriptorCount();
175
private native double getSystemCpuLoad0();
176
public native double getProcessCpuLoad();
177
private native double getSingleCpuLoad0(int cpuNum);
178
private native int getHostConfiguredCpuCount0();
179
180
static {
181
initialize();
182
}
183
private static native void initialize();
184
}
185
186