Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/management/LinuxOperatingSystem.c
32287 views
/*1* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdio.h>26#include <stdint.h>27#include <stdarg.h>28#include <unistd.h>29#include <errno.h>30#include <string.h>31#include <sys/resource.h>32#include <sys/types.h>33#include <dirent.h>34#include <stdlib.h>35#include <dlfcn.h>36#include <pthread.h>37#include "sun_management_OperatingSystemImpl.h"3839struct ticks {40uint64_t used;41uint64_t usedKernel;42uint64_t total;43};4445typedef struct ticks ticks;4647typedef enum {48CPU_LOAD_VM_ONLY,49CPU_LOAD_GLOBAL,50} CpuLoadTarget;5152static struct perfbuf {53int nProcs;54ticks jvmTicks;55ticks cpuTicks;56ticks *cpus;57} counters;5859#define DEC_64 "%lld"6061static void next_line(FILE *f) {62while (fgetc(f) != '\n');63}6465/**66* Return the total number of ticks since the system was booted.67* If the usedTicks parameter is not NULL, it will be filled with68* the number of ticks spent on actual processes (user, system or69* nice processes) since system boot. Note that this is the total number70* of "executed" ticks on _all_ CPU:s, that is on a n-way system it is71* n times the number of ticks that has passed in clock time.72*73* Returns a negative value if the reading of the ticks failed.74*/75static int get_totalticks(int which, ticks *pticks) {76FILE *fh;77uint64_t userTicks, niceTicks, systemTicks, idleTicks;78uint64_t iowTicks = 0, irqTicks = 0, sirqTicks= 0;79int n;8081if((fh = fopen("/proc/stat", "r")) == NULL) {82return -1;83}8485n = fscanf(fh, "cpu " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " "86DEC_64 " " DEC_64,87&userTicks, &niceTicks, &systemTicks, &idleTicks,88&iowTicks, &irqTicks, &sirqTicks);8990// Move to next line91next_line(fh);9293//find the line for requested cpu faster to just iterate linefeeds?94if (which != -1) {95int i;96for (i = 0; i < which; i++) {97if (fscanf(fh, "cpu%*d " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " "98DEC_64 " " DEC_64 " " DEC_64,99&userTicks, &niceTicks, &systemTicks, &idleTicks,100&iowTicks, &irqTicks, &sirqTicks) < 4) {101fclose(fh);102return -2;103}104next_line(fh);105}106n = fscanf(fh, "cpu%*d " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " "107DEC_64 " " DEC_64 " " DEC_64 "\n",108&userTicks, &niceTicks, &systemTicks, &idleTicks,109&iowTicks, &irqTicks, &sirqTicks);110}111112fclose(fh);113if (n < 4) {114return -2;115}116117pticks->used = userTicks + niceTicks;118pticks->usedKernel = systemTicks + irqTicks + sirqTicks;119pticks->total = userTicks + niceTicks + systemTicks + idleTicks +120iowTicks + irqTicks + sirqTicks;121122return 0;123}124125static int vread_statdata(const char *procfile, const char *fmt, va_list args) {126FILE *f;127int n;128char buf[2048];129130if ((f = fopen(procfile, "r")) == NULL) {131return -1;132}133134if ((n = fread(buf, 1, sizeof(buf), f)) != -1) {135char *tmp;136137buf[n-1] = '\0';138/** skip through pid and exec name. the exec name _could be wacky_ (renamed) and139* make scanf go mupp.140*/141if ((tmp = strrchr(buf, ')')) != NULL) {142// skip the ')' and the following space but check that the buffer is long enough143tmp += 2;144if (tmp < buf + n) {145n = vsscanf(tmp, fmt, args);146}147}148}149150fclose(f);151152return n;153}154155static int read_statdata(const char *procfile, const char *fmt, ...) {156int n;157va_list args;158159va_start(args, fmt);160n = vread_statdata(procfile, fmt, args);161va_end(args);162return n;163}164165/** read user and system ticks from a named procfile, assumed to be in 'stat' format then. */166static int read_ticks(const char *procfile, uint64_t *userTicks, uint64_t *systemTicks) {167return read_statdata(procfile, "%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u "DEC_64" "DEC_64,168userTicks, systemTicks169);170}171172/**173* Return the number of ticks spent in any of the processes belonging174* to the JVM on any CPU.175*/176static int get_jvmticks(ticks *pticks) {177uint64_t userTicks;178uint64_t systemTicks;179180if (read_ticks("/proc/self/stat", &userTicks, &systemTicks) < 0) {181return -1;182}183184// get the total185if (get_totalticks(-1, pticks) < 0) {186return -1;187}188189pticks->used = userTicks;190pticks->usedKernel = systemTicks;191192return 0;193}194195/**196* This method must be called first, before any data can be gathererd.197*/198int perfInit() {199static int initialized = 0;200201if (!initialized) {202int i;203204// We need to allocate counters for all CPUs, including ones that205// are currently offline as they could be turned online later.206int n = sysconf(_SC_NPROCESSORS_CONF);207if (n <= 0) {208n = 1;209}210211counters.cpus = calloc(n,sizeof(ticks));212counters.nProcs = n;213if (counters.cpus != NULL) {214// For the CPU load215get_totalticks(-1, &counters.cpuTicks);216217for (i = 0; i < n; i++) {218get_totalticks(i, &counters.cpus[i]);219}220// For JVM load221get_jvmticks(&counters.jvmTicks);222initialized = 1;223}224}225226return initialized ? 0 : -1;227}228229#define MAX(a,b) (a>b?a:b)230#define MIN(a,b) (a<b?a:b)231232static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;233234/**235* Return the load of the CPU as a double. 1.0 means the CPU process uses all236* available time for user or system processes, 0.0 means the CPU uses all time237* being idle.238*239* Returns a negative value if there is a problem in determining the CPU load.240*/241242static double get_cpuload_internal(int which, double *pkernelLoad, CpuLoadTarget target) {243uint64_t udiff, kdiff, tdiff;244ticks *pticks, tmp;245double user_load = -1.0;246int failed = 0;247248*pkernelLoad = 0.0;249250pthread_mutex_lock(&lock);251252if(perfInit() == 0) {253254if (target == CPU_LOAD_VM_ONLY) {255pticks = &counters.jvmTicks;256} else if (which == -1) {257pticks = &counters.cpuTicks;258} else {259pticks = &counters.cpus[which];260}261262tmp = *pticks;263264if (target == CPU_LOAD_VM_ONLY) {265if (get_jvmticks(pticks) != 0) {266failed = 1;267}268} else if (get_totalticks(which, pticks) < 0) {269failed = 1;270}271272if(!failed) {273// seems like we sometimes end up with less kernel ticks when274// reading /proc/self/stat a second time, timing issue between cpus?275if (pticks->usedKernel < tmp.usedKernel) {276kdiff = 0;277} else {278kdiff = pticks->usedKernel - tmp.usedKernel;279}280tdiff = pticks->total - tmp.total;281udiff = pticks->used - tmp.used;282283if (tdiff == 0) {284user_load = 0;285} else {286if (tdiff < (udiff + kdiff)) {287tdiff = udiff + kdiff;288}289*pkernelLoad = (kdiff / (double)tdiff);290// BUG9044876, normalize return values to sane values291*pkernelLoad = MAX(*pkernelLoad, 0.0);292*pkernelLoad = MIN(*pkernelLoad, 1.0);293294user_load = (udiff / (double)tdiff);295user_load = MAX(user_load, 0.0);296user_load = MIN(user_load, 1.0);297}298}299}300pthread_mutex_unlock(&lock);301return user_load;302}303304double get_cpu_load(int which) {305double u, s;306u = get_cpuload_internal(which, &s, CPU_LOAD_GLOBAL);307if (u < 0) {308return -1.0;309}310// Cap total systemload to 1.0311return MIN((u + s), 1.0);312}313314double get_process_load() {315double u, s;316u = get_cpuload_internal(-1, &s, CPU_LOAD_VM_ONLY);317if (u < 0) {318return -1.0;319}320return u + s;321}322323JNIEXPORT jdouble JNICALL324Java_sun_management_OperatingSystemImpl_getSystemCpuLoad0325(JNIEnv *env, jobject dummy)326{327if (perfInit() == 0) {328return get_cpu_load(-1);329} else {330return -1.0;331}332}333334JNIEXPORT jdouble JNICALL335Java_sun_management_OperatingSystemImpl_getProcessCpuLoad336(JNIEnv *env, jobject dummy)337{338if (perfInit() == 0) {339return get_process_load();340} else {341return -1.0;342}343}344345JNIEXPORT jdouble JNICALL346Java_sun_management_OperatingSystemImpl_getSingleCpuLoad0347(JNIEnv *env, jobject mbean, jint cpu_number)348{349if (perfInit() == 0 && cpu_number >= 0 && cpu_number < counters.nProcs) {350return get_cpu_load(cpu_number);351} else {352return -1.0;353}354}355356JNIEXPORT jint JNICALL357Java_sun_management_OperatingSystemImpl_getHostConfiguredCpuCount0358(JNIEnv *env, jobject mbean)359{360if (perfInit() == 0) {361return counters.nProcs;362} else {363return -1;364}365}366367368