Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/management/SolarisOperatingSystem.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 <fcntl.h>26#include <kstat.h>27#include <procfs.h>28#include <unistd.h>29#include <stdlib.h>30#include <stdio.h>31#include <string.h>32#include <sys/sysinfo.h>33#include <sys/lwp.h>34#include <pthread.h>35#include <utmpx.h>36#include <dlfcn.h>37#include <sys/loadavg.h>38#include <jni.h>39#include "jvm.h"40#include "sun_management_OperatingSystemImpl.h"4142typedef struct {43kstat_t *kstat;44uint64_t last_idle;45uint64_t last_total;46double last_ratio;47} cpuload_t;4849static cpuload_t *cpu_loads = NULL;50static unsigned int num_cpus;51static kstat_ctl_t *kstat_ctrl = NULL;5253static void map_cpu_kstat_counters() {54kstat_t *kstat;55int i;5657// Get number of CPU(s)58if ((num_cpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {59num_cpus = 1;60}6162// Data structure for saving CPU load63if ((cpu_loads = calloc(num_cpus,sizeof(cpuload_t))) == NULL) {64return;65}6667// Get kstat cpu_stat counters for every CPU68// (loop over kstat to find our cpu_stat(s)69i = 0;70for (kstat = kstat_ctrl->kc_chain; kstat != NULL; kstat = kstat->ks_next) {71if (strncmp(kstat->ks_module, "cpu_stat", 8) == 0) {7273if (kstat_read(kstat_ctrl, kstat, NULL) == -1) {74// Failed to initialize kstat for this CPU so ignore it75continue;76}7778if (i == num_cpus) {79// Found more cpu_stats than reported CPUs80break;81}8283cpu_loads[i++].kstat = kstat;84}85}86}8788static int init_cpu_kstat_counters() {89static int initialized = 0;9091// Concurrence in this method is prevented by the lock in92// the calling method get_cpu_load();93if(!initialized) {94if ((kstat_ctrl = kstat_open()) != NULL) {95map_cpu_kstat_counters();96initialized = 1;97}98}99return initialized ? 0 : -1;100}101102static void update_cpu_kstat_counters() {103if(kstat_chain_update(kstat_ctrl) != 0) {104free(cpu_loads);105map_cpu_kstat_counters();106}107}108109int read_cpustat(cpuload_t *load, cpu_stat_t *cpu_stat) {110if (load->kstat == NULL) {111// no handle.112return -1;113}114if (kstat_read(kstat_ctrl, load->kstat, cpu_stat) == -1) {115// disabling for now, a kstat chain update is likely to happen next time116load->kstat = NULL;117return -1;118}119return 0;120}121122double get_single_cpu_load(unsigned int n) {123cpuload_t *load;124cpu_stat_t cpu_stat;125uint_t *usage;126uint64_t c_idle;127uint64_t c_total;128uint64_t d_idle;129uint64_t d_total;130int i;131132if (n >= num_cpus) {133return -1.0;134}135136load = &cpu_loads[n];137if (read_cpustat(load, &cpu_stat) < 0) {138return -1.0;139}140141usage = cpu_stat.cpu_sysinfo.cpu;142c_idle = usage[CPU_IDLE];143144for (c_total = 0, i = 0; i < CPU_STATES; i++) {145c_total += usage[i];146}147148// Calculate diff against previous snapshot149d_idle = c_idle - load->last_idle;150d_total = c_total - load->last_total;151152/** update if weve moved */153if (d_total > 0) {154// Save current values for next time around155load->last_idle = c_idle;156load->last_total = c_total;157load->last_ratio = (double) (d_total - d_idle) / d_total;158}159160return load->last_ratio;161}162163int get_info(const char *path, void *info, size_t s, off_t o) {164int fd;165int ret = 0;166if ((fd = open(path, O_RDONLY)) < 0) {167return -1;168}169if (pread(fd, info, s, o) != s) {170ret = -1;171}172close(fd);173return ret;174}175176#define MIN(a, b) ((a < b) ? a : b)177178static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;179180/**181* Return the cpu load (0-1) for proc number 'which' (or average all if which == -1)182*/183double get_cpu_load(int which) {184double load =.0;185186pthread_mutex_lock(&lock);187if(init_cpu_kstat_counters()==0) {188189update_cpu_kstat_counters();190191if (which == -1) {192unsigned int i;193double t;194195for (t = .0, i = 0; i < num_cpus; i++) {196t += get_single_cpu_load(i);197}198199// Cap total systemload to 1.0200load = MIN((t / num_cpus), 1.0);201} else {202load = MIN(get_single_cpu_load(which), 1.0);203}204} else {205load = -1.0;206}207pthread_mutex_unlock(&lock);208209return load;210}211212/**213* Return the cpu load (0-1) for the current process (i.e the JVM)214* or -1.0 if the get_info() call failed215*/216double get_process_load(void) {217psinfo_t info;218219// Get the percentage of "recent cpu usage" from all the lwp:s in the JVM:s220// process. This is returned as a value between 0.0 and 1.0 multiplied by 0x8000.221if (get_info("/proc/self/psinfo",&info.pr_pctcpu, sizeof(info.pr_pctcpu), offsetof(psinfo_t, pr_pctcpu)) == 0) {222return (double) info.pr_pctcpu / 0x8000;223}224return -1.0;225}226227JNIEXPORT jdouble JNICALL228Java_sun_management_OperatingSystemImpl_getSystemCpuLoad0229(JNIEnv *env, jobject dummy)230{231return get_cpu_load(-1);232}233234JNIEXPORT jdouble JNICALL235Java_sun_management_OperatingSystemImpl_getProcessCpuLoad236(JNIEnv *env, jobject dummy)237{238return get_process_load();239}240241242243