About the perfInit() method in LinuxOperatingSystem.c
Poison
tianshuang.me at qq.com
Tue Feb 8 07:26:27 UTC 2022
In JDK8 com.sun.management.OperatingSystemMXBean#getSystemCpuLoad, when the operating system is Linux, the perfInit() method will be called for initialization, but will the initialized variable cause the code in the if block to not be executed?
Source code(https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/solaris/native/sun/management/LinuxOperatingSystem.c#L193):
/**
* This method must be called first, before any data can be gathererd.
*/
int perfInit() {
static int initialized=1;
if (!initialized) {
int i;
int n = sysconf(_SC_NPROCESSORS_ONLN);
if (n <= 0) {
n = 1;
}
counters.cpus = calloc(n,sizeof(ticks));
if (counters.cpus != NULL) {
// For the CPU load
get_totalticks(-1, &counters.cpuTicks);
for (i = 0; i < n; i++) {
get_totalticks(i, &counters.cpus[i]);
}
// For JVM load
get_jvmticks(&counters.jvmTicks);
initialized = 1;
}
}
return initialized ? 0 : -1;
}
Thanks.
More information about the jdk8u-dev
mailing list