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 <stdio.h>
4162N/A#include <stdint.h>
4162N/A#include <stdarg.h>
4162N/A#include <unistd.h>
4162N/A#include <errno.h>
4162N/A#include <string.h>
4162N/A#include <sys/resource.h>
4162N/A#include <sys/types.h>
4162N/A#include <dirent.h>
4162N/A#include <stdlib.h>
4162N/A#include <dlfcn.h>
4162N/A#include <pthread.h>
4162N/A#include "com_sun_management_UnixOperatingSystem.h"
4162N/A
4162N/Astruct ticks {
4162N/A uint64_t used;
4162N/A uint64_t usedKernel;
4162N/A uint64_t total;
4162N/A};
4162N/A
4162N/Atypedef struct ticks ticks;
4162N/A
4162N/Atypedef enum {
4162N/A CPU_LOAD_VM_ONLY,
4162N/A CPU_LOAD_GLOBAL,
4162N/A} CpuLoadTarget;
4162N/A
4162N/Astatic struct perfbuf {
4162N/A int nProcs;
4162N/A ticks jvmTicks;
4162N/A ticks cpuTicks;
4162N/A ticks *cpus;
4162N/A} counters;
4162N/A
4162N/A#define DEC_64 "%lld"
4162N/A
4162N/Astatic void next_line(FILE *f) {
4162N/A while (fgetc(f) != '\n');
4162N/A}
4162N/A
4162N/A/**
4162N/A * Return the total number of ticks since the system was booted.
4162N/A * If the usedTicks parameter is not NULL, it will be filled with
4162N/A * the number of ticks spent on actual processes (user, system or
4162N/A * nice processes) since system boot. Note that this is the total number
4162N/A * of "executed" ticks on _all_ CPU:s, that is on a n-way system it is
4162N/A * n times the number of ticks that has passed in clock time.
4162N/A *
4162N/A * Returns a negative value if the reading of the ticks failed.
4162N/A */
4162N/Astatic int get_totalticks(int which, ticks *pticks) {
4162N/A FILE *fh;
4162N/A uint64_t userTicks, niceTicks, systemTicks, idleTicks;
4162N/A int n;
4162N/A
4162N/A if((fh = fopen("/proc/stat", "r")) == NULL) {
4162N/A return -1;
4162N/A }
4162N/A
4162N/A n = fscanf(fh, "cpu " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64,
4162N/A &userTicks, &niceTicks, &systemTicks, &idleTicks);
4162N/A
4162N/A // Move to next line
4162N/A next_line(fh);
4162N/A
4162N/A //find the line for requested cpu faster to just iterate linefeeds?
4162N/A if (which != -1) {
4162N/A int i;
4162N/A for (i = 0; i < which; i++) {
4162N/A if (fscanf(fh, "cpu%*d " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64, &userTicks, &niceTicks, &systemTicks, &idleTicks) != 4) {
4162N/A fclose(fh);
4162N/A return -2;
4162N/A }
4162N/A next_line(fh);
4162N/A }
4162N/A n = fscanf(fh, "cpu%*d " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 "\n",
4162N/A &userTicks, &niceTicks, &systemTicks, &idleTicks);
4162N/A }
4162N/A
4162N/A fclose(fh);
4162N/A if (n != 4) {
4162N/A return -2;
4162N/A }
4162N/A
4162N/A pticks->used = userTicks + niceTicks;
4162N/A pticks->usedKernel = systemTicks;
4162N/A pticks->total = userTicks + niceTicks + systemTicks + idleTicks;
4162N/A
4162N/A return 0;
4162N/A}
4162N/A
4162N/Astatic int vread_statdata(const char *procfile, const char *fmt, va_list args) {
4162N/A FILE *f;
4162N/A int n;
4162N/A char buf[2048];
4162N/A
4162N/A if ((f = fopen(procfile, "r")) == NULL) {
4162N/A return -1;
4162N/A }
4162N/A
4162N/A if ((n = fread(buf, 1, sizeof(buf), f)) != -1) {
4162N/A char *tmp;
4162N/A
4162N/A buf[n-1] = '\0';
4162N/A /** skip through pid and exec name. the exec name _could be wacky_ (renamed) and
4162N/A * make scanf go mupp.
4162N/A */
4162N/A if ((tmp = strrchr(buf, ')')) != NULL) {
4162N/A // skip the ')' and the following space but check that the buffer is long enough
4162N/A tmp += 2;
4162N/A if (tmp < buf + n) {
4162N/A n = vsscanf(tmp, fmt, args);
4162N/A }
4162N/A }
4162N/A }
4162N/A
4162N/A fclose(f);
4162N/A
4162N/A return n;
4162N/A}
4162N/A
4162N/Astatic int read_statdata(const char *procfile, const char *fmt, ...) {
4162N/A int n;
4162N/A va_list args;
4162N/A
4162N/A va_start(args, fmt);
4162N/A n = vread_statdata(procfile, fmt, args);
4162N/A va_end(args);
4162N/A return n;
4162N/A}
4162N/A
4162N/A/** read user and system ticks from a named procfile, assumed to be in 'stat' format then. */
4162N/Astatic int read_ticks(const char *procfile, uint64_t *userTicks, uint64_t *systemTicks) {
4162N/A return read_statdata(procfile, "%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u "DEC_64" "DEC_64,
4162N/A userTicks, systemTicks
4162N/A );
4162N/A}
4162N/A
4162N/A/**
4162N/A * Return the number of ticks spent in any of the processes belonging
4162N/A * to the JVM on any CPU.
4162N/A */
4162N/Astatic int get_jvmticks(ticks *pticks) {
4162N/A uint64_t userTicks;
4162N/A uint64_t systemTicks;
4162N/A
4162N/A if (read_ticks("/proc/self/stat", &userTicks, &systemTicks) < 0) {
4162N/A return -1;
4162N/A }
4162N/A
4162N/A // get the total
4162N/A if (get_totalticks(-1, pticks) < 0) {
4162N/A return -1;
4162N/A }
4162N/A
4162N/A pticks->used = userTicks;
4162N/A pticks->usedKernel = systemTicks;
4162N/A
4162N/A return 0;
4162N/A}
4162N/A
4162N/A/**
4162N/A * This method must be called first, before any data can be gathererd.
4162N/A */
4162N/Aint perfInit() {
4162N/A static int initialized=1;
4162N/A
4162N/A if (!initialized) {
4162N/A int i;
4162N/A
4162N/A int n = sysconf(_SC_NPROCESSORS_ONLN);
4162N/A if (n <= 0) {
4162N/A n = 1;
4162N/A }
4162N/A
4162N/A counters.cpus = calloc(n,sizeof(ticks));
4162N/A if (counters.cpus != NULL) {
4162N/A // For the CPU load
4162N/A get_totalticks(-1, &counters.cpuTicks);
4162N/A
4162N/A for (i = 0; i < n; i++) {
4162N/A get_totalticks(i, &counters.cpus[i]);
4162N/A }
4162N/A // For JVM load
4162N/A get_jvmticks(&counters.jvmTicks);
4162N/A initialized = 1;
4162N/A }
4162N/A }
4162N/A
4162N/A return initialized ? 0 : -1;
4162N/A}
4162N/A
4162N/A#define MAX(a,b) (a>b?a:b)
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 load of the CPU as a double. 1.0 means the CPU process uses all
4162N/A * available time for user or system processes, 0.0 means the CPU uses all time
4162N/A * being idle.
4162N/A *
4162N/A * Returns a negative value if there is a problem in determining the CPU load.
4162N/A */
4162N/A
4162N/Astatic double get_cpuload_internal(int which, double *pkernelLoad, CpuLoadTarget target) {
4162N/A uint64_t udiff, kdiff, tdiff;
4162N/A ticks *pticks, tmp;
4162N/A double user_load = -1.0;
4162N/A int failed = 0;
4162N/A
4162N/A *pkernelLoad = 0.0;
4162N/A
4162N/A pthread_mutex_lock(&lock);
4162N/A
4162N/A if(perfInit() == 0) {
4162N/A
4162N/A if (target == CPU_LOAD_VM_ONLY) {
4162N/A pticks = &counters.jvmTicks;
4162N/A } else if (which == -1) {
4162N/A pticks = &counters.cpuTicks;
4162N/A } else {
4162N/A pticks = &counters.cpus[which];
4162N/A }
4162N/A
4162N/A tmp = *pticks;
4162N/A
4162N/A if (target == CPU_LOAD_VM_ONLY) {
4162N/A if (get_jvmticks(pticks) != 0) {
4162N/A failed = 1;
4162N/A }
4162N/A } else if (get_totalticks(which, pticks) < 0) {
4162N/A failed = 1;
4162N/A }
4162N/A
4162N/A if(!failed) {
4162N/A // seems like we sometimes end up with less kernel ticks when
4162N/A // reading /proc/self/stat a second time, timing issue between cpus?
4162N/A if (pticks->usedKernel < tmp.usedKernel) {
4162N/A kdiff = 0;
4162N/A } else {
4162N/A kdiff = pticks->usedKernel - tmp.usedKernel;
4162N/A }
4162N/A tdiff = pticks->total - tmp.total;
4162N/A udiff = pticks->used - tmp.used;
4162N/A
4162N/A if (tdiff == 0) {
4162N/A user_load = 0;
4162N/A } else {
4162N/A if (tdiff < (udiff + kdiff)) {
4162N/A tdiff = udiff + kdiff;
4162N/A }
4162N/A *pkernelLoad = (kdiff / (double)tdiff);
4162N/A // BUG9044876, normalize return values to sane values
4162N/A *pkernelLoad = MAX(*pkernelLoad, 0.0);
4162N/A *pkernelLoad = MIN(*pkernelLoad, 1.0);
4162N/A
4162N/A user_load = (udiff / (double)tdiff);
4162N/A user_load = MAX(user_load, 0.0);
4162N/A user_load = MIN(user_load, 1.0);
4162N/A }
4162N/A }
4162N/A }
4162N/A pthread_mutex_unlock(&lock);
4162N/A return user_load;
4162N/A}
4162N/A
4162N/Adouble get_cpu_load(int which) {
4162N/A double u, s;
4162N/A u = get_cpuload_internal(which, &s, CPU_LOAD_GLOBAL);
4162N/A if (u < 0) {
4162N/A return -1.0;
4162N/A }
4162N/A // Cap total systemload to 1.0
4162N/A return MIN((u + s), 1.0);
4162N/A}
4162N/A
4162N/Adouble get_process_load() {
4162N/A double u, s;
4162N/A u = get_cpuload_internal(-1, &s, CPU_LOAD_VM_ONLY);
4162N/A if (u < 0) {
4162N/A return -1.0;
4162N/A }
4162N/A return u + s;
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 if(perfInit() == 0) {
4162N/A return get_cpu_load(-1);
4162N/A } else {
4162N/A return -1.0;
4162N/A }
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 if(perfInit() == 0) {
4162N/A return get_process_load();
4162N/A } else {
4162N/A return -1.0;
4162N/A }
4162N/A}