4162N/A/*
4162N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4162N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4162N/A *
4162N/A * This code is free software; you can redistribute it and/or modify it
4162N/A * under the terms of the GNU General Public License version 2 only, as
4162N/A * published by the Free Software Foundation. Oracle designates this
4162N/A * particular file as subject to the "Classpath" exception as provided
4162N/A * by Oracle in the LICENSE file that accompanied this code.
4162N/A *
4162N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4162N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4162N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4162N/A * version 2 for more details (a copy is included in the LICENSE file that
4162N/A * accompanied this code).
4162N/A *
4162N/A * You should have received a copy of the GNU General Public License version
4162N/A * 2 along with this work; if not, write to the Free Software Foundation,
4162N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4162N/A *
4162N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4162N/A * or visit www.oracle.com if you need additional information or have any
4162N/A * questions.
4162N/A */
4162N/A
4162N/A#include <fcntl.h>
4162N/A#include <kstat.h>
4162N/A#include <procfs.h>
4162N/A#include <unistd.h>
4162N/A#include <stdlib.h>
4162N/A#include <stdio.h>
4162N/A#include <string.h>
4162N/A#include <sys/sysinfo.h>
4162N/A#include <sys/lwp.h>
4162N/A#include <pthread.h>
4162N/A#include <utmpx.h>
4162N/A#include <dlfcn.h>
4162N/A#include <sys/loadavg.h>
4162N/A#include <jni.h>
4162N/A#include "jvm.h"
4162N/A#include "com_sun_management_UnixOperatingSystem.h"
4162N/A
4162N/Atypedef struct {
4162N/A kstat_t *kstat;
4162N/A uint64_t last_idle;
4162N/A uint64_t last_total;
4162N/A double last_ratio;
4162N/A} cpuload_t;
4162N/A
4162N/Astatic cpuload_t *cpu_loads = NULL;
4162N/Astatic unsigned int num_cpus;
4162N/Astatic kstat_ctl_t *kstat_ctrl = NULL;
4162N/A
4162N/Astatic void map_cpu_kstat_counters() {
4162N/A kstat_t *kstat;
4162N/A int i;
4162N/A
4162N/A // Get number of CPU(s)
4162N/A if ((num_cpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
4162N/A num_cpus = 1;
4162N/A }
4162N/A
4162N/A // Data structure for saving CPU load
4162N/A if ((cpu_loads = calloc(num_cpus,sizeof(cpuload_t))) == NULL) {
4162N/A return;
4162N/A }
4162N/A
4162N/A // Get kstat cpu_stat counters for every CPU
4162N/A // (loop over kstat to find our cpu_stat(s)
4162N/A i = 0;
4162N/A for (kstat = kstat_ctrl->kc_chain; kstat != NULL; kstat = kstat->ks_next) {
4162N/A if (strncmp(kstat->ks_module, "cpu_stat", 8) == 0) {
4162N/A
4162N/A if (kstat_read(kstat_ctrl, kstat, NULL) == -1) {
4162N/A // Failed to initialize kstat for this CPU so ignore it
4162N/A continue;
4162N/A }
4162N/A
4162N/A if (i == num_cpus) {
4162N/A // Found more cpu_stats than reported CPUs
4162N/A break;
4162N/A }
4162N/A
4162N/A cpu_loads[i++].kstat = kstat;
4162N/A }
4162N/A }
4162N/A}
4162N/A
4162N/Astatic int init_cpu_kstat_counters() {
4162N/A static int initialized = 0;
4162N/A
4162N/A // Concurrence in this method is prevented by the lock in
4162N/A // the calling method get_cpu_load();
4162N/A if(!initialized) {
4162N/A if ((kstat_ctrl = kstat_open()) != NULL) {
4162N/A map_cpu_kstat_counters();
4162N/A initialized = 1;
4162N/A }
4162N/A }
4162N/A return initialized ? 0 : -1;
4162N/A}
4162N/A
4162N/Astatic void update_cpu_kstat_counters() {
4162N/A if(kstat_chain_update(kstat_ctrl) != 0) {
4162N/A free(cpu_loads);
4162N/A map_cpu_kstat_counters();
4162N/A }
4162N/A}
4162N/A
4162N/Aint read_cpustat(cpuload_t *load, cpu_stat_t *cpu_stat) {
4162N/A if (load->kstat == NULL) {
4162N/A // no handle.
4162N/A return -1;
4162N/A }
4162N/A if (kstat_read(kstat_ctrl, load->kstat, cpu_stat) == -1) {
4162N/A // disabling for now, a kstat chain update is likely to happen next time
4162N/A load->kstat = NULL;
4162N/A return -1;
4162N/A }
4162N/A return 0;
4162N/A}
4162N/A
4162N/Adouble get_single_cpu_load(unsigned int n) {
4162N/A cpuload_t *load;
4162N/A cpu_stat_t cpu_stat;
4162N/A uint_t *usage;
4162N/A uint64_t c_idle;
4162N/A uint64_t c_total;
4162N/A uint64_t d_idle;
4162N/A uint64_t d_total;
4162N/A int i;
4162N/A
4162N/A if (n >= num_cpus) {
4162N/A return -1.0;
4162N/A }
4162N/A
4162N/A load = &cpu_loads[n];
4162N/A if (read_cpustat(load, &cpu_stat) < 0) {
4162N/A return -1.0;
4162N/A }
4162N/A
4162N/A usage = cpu_stat.cpu_sysinfo.cpu;
4162N/A c_idle = usage[CPU_IDLE];
4162N/A
4162N/A for (c_total = 0, i = 0; i < CPU_STATES; i++) {
4162N/A c_total += usage[i];
4162N/A }
4162N/A
4162N/A // Calculate diff against previous snapshot
4162N/A d_idle = c_idle - load->last_idle;
4162N/A d_total = c_total - load->last_total;
4162N/A
4162N/A /** update if weve moved */
4162N/A if (d_total > 0) {
4162N/A // Save current values for next time around
4162N/A load->last_idle = c_idle;
4162N/A load->last_total = c_total;
4162N/A load->last_ratio = (double) (d_total - d_idle) / d_total;
4162N/A }
4162N/A
4162N/A return load->last_ratio;
4162N/A}
4162N/A
4162N/Aint get_info(const char *path, void *info, size_t s, off_t o) {
4162N/A int fd;
4162N/A int ret = 0;
4162N/A if ((fd = open(path, O_RDONLY)) < 0) {
4162N/A return -1;
4162N/A }
4162N/A if (pread(fd, info, s, o) != s) {
4162N/A ret = -1;
4162N/A }
4162N/A close(fd);
4162N/A return ret;
4162N/A}
4162N/A
4162N/A#define MIN(a, b) ((a < b) ? a : b)
4162N/A
4162N/Astatic pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
4162N/A
4162N/A/**
4162N/A * Return the cpu load (0-1) for proc number 'which' (or average all if which == -1)
4162N/A */
4162N/Adouble get_cpu_load(int which) {
4162N/A double load =.0;
4162N/A
4162N/A pthread_mutex_lock(&lock);
4162N/A if(init_cpu_kstat_counters()==0) {
4162N/A
4162N/A update_cpu_kstat_counters();
4162N/A
4162N/A if (which == -1) {
4162N/A unsigned int i;
4162N/A double t;
4162N/A
4162N/A for (t = .0, i = 0; i < num_cpus; i++) {
4162N/A t += get_single_cpu_load(i);
4162N/A }
4162N/A
4162N/A // Cap total systemload to 1.0
4162N/A load = MIN((t / num_cpus), 1.0);
4162N/A } else {
4162N/A load = MIN(get_single_cpu_load(which), 1.0);
4162N/A }
4162N/A } else {
4162N/A load = -1.0;
4162N/A }
4162N/A pthread_mutex_unlock(&lock);
4162N/A
4162N/A return load;
4162N/A}
4162N/A
4162N/A/**
4162N/A * Return the cpu load (0-1) for the current process (i.e the JVM)
4162N/A * or -1.0 if the get_info() call failed
4162N/A */
4162N/Adouble get_process_load(void) {
4162N/A psinfo_t info;
4162N/A
4162N/A // Get the percentage of "recent cpu usage" from all the lwp:s in the JVM:s
4162N/A // process. This is returned as a value between 0.0 and 1.0 multiplied by 0x8000.
4162N/A if (get_info("/proc/self/psinfo",&info.pr_pctcpu, sizeof(info.pr_pctcpu), offsetof(psinfo_t, pr_pctcpu)) == 0) {
4162N/A return (double) info.pr_pctcpu / 0x8000;
4162N/A }
4162N/A return -1.0;
4162N/A}
4162N/A
4162N/AJNIEXPORT jdouble JNICALL
4162N/AJava_com_sun_management_UnixOperatingSystem_getSystemCpuLoad
4162N/A(JNIEnv *env, jobject dummy)
4162N/A{
4162N/A return get_cpu_load(-1);
4162N/A}
4162N/A
4162N/AJNIEXPORT jdouble JNICALL
4162N/AJava_com_sun_management_UnixOperatingSystem_getProcessCpuLoad
4162N/A(JNIEnv *env, jobject dummy)
4162N/A{
4162N/A return get_process_load();
4162N/A}
4162N/A