jdk/src/jdk.management/linux/native/libmanagement_ext/UnixOperatingSystem.c
author sjiang
Fri, 17 Apr 2015 09:40:02 +0200
changeset 30355 e37c7eba132f
parent 25859 jdk/src/java.management/unix/native/libmanagement/LinuxOperatingSystem.c@3317bb8137f4
child 32417 6859107fc6c3
permissions -rw-r--r--
8042901: Allow com.sun.management to be in a different module to java.lang.management Reviewed-by: mchung, dfuchs, erikj, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     1
/*
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 25859
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     4
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    10
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    15
 * accompanied this code).
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    16
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    20
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    23
 * questions.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    24
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    25
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    26
#include <stdio.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    27
#include <stdint.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    28
#include <stdarg.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    29
#include <unistd.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    30
#include <errno.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    31
#include <string.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    32
#include <sys/resource.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    33
#include <sys/types.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    34
#include <dirent.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    35
#include <stdlib.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    36
#include <dlfcn.h>
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    37
#include <pthread.h>
22964
59c522d83ed9 8034856: gcc warnings compiling src/solaris/native/sun/security/pkcs11
alanb
parents: 22583
diff changeset
    38
#include <inttypes.h>
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 25859
diff changeset
    39
#include "com_sun_management_internal_OperatingSystemImpl.h"
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    40
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    41
struct ticks {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    42
    uint64_t  used;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    43
    uint64_t  usedKernel;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    44
    uint64_t  total;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    45
};
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    46
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    47
typedef struct ticks ticks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    48
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    49
typedef enum {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    50
    CPU_LOAD_VM_ONLY,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    51
    CPU_LOAD_GLOBAL,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    52
} CpuLoadTarget;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    53
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    54
static struct perfbuf {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    55
    int   nProcs;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    56
    ticks jvmTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    57
    ticks cpuTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    58
    ticks *cpus;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    59
} counters;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    60
22964
59c522d83ed9 8034856: gcc warnings compiling src/solaris/native/sun/security/pkcs11
alanb
parents: 22583
diff changeset
    61
#define DEC_64 "%"SCNd64
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    62
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    63
static void next_line(FILE *f) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    64
    while (fgetc(f) != '\n');
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    65
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    66
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    67
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    68
 * Return the total number of ticks since the system was booted.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    69
 * If the usedTicks parameter is not NULL, it will be filled with
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    70
 * the number of ticks spent on actual processes (user, system or
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    71
 * nice processes) since system boot. Note that this is the total number
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    72
 * of "executed" ticks on _all_ CPU:s, that is on a n-way system it is
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    73
 * n times the number of ticks that has passed in clock time.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    74
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    75
 * Returns a negative value if the reading of the ticks failed.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    76
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    77
static int get_totalticks(int which, ticks *pticks) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    78
    FILE         *fh;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    79
    uint64_t        userTicks, niceTicks, systemTicks, idleTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    80
    int             n;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    81
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    82
    if((fh = fopen("/proc/stat", "r")) == NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    83
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    84
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    85
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    86
    n = fscanf(fh, "cpu " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    87
           &userTicks, &niceTicks, &systemTicks, &idleTicks);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    88
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    89
    // Move to next line
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    90
    next_line(fh);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    91
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    92
    //find the line for requested cpu faster to just iterate linefeeds?
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    93
    if (which != -1) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    94
        int i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    95
        for (i = 0; i < which; i++) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    96
            if (fscanf(fh, "cpu%*d " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64, &userTicks, &niceTicks, &systemTicks, &idleTicks) != 4) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    97
                fclose(fh);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    98
                return -2;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
    99
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   100
            next_line(fh);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   101
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   102
        n = fscanf(fh, "cpu%*d " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 "\n",
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   103
           &userTicks, &niceTicks, &systemTicks, &idleTicks);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   104
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   105
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   106
    fclose(fh);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   107
    if (n != 4) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   108
        return -2;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   109
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   110
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   111
    pticks->used       = userTicks + niceTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   112
    pticks->usedKernel = systemTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   113
    pticks->total      = userTicks + niceTicks + systemTicks + idleTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   114
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   115
    return 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   116
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   117
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   118
static int vread_statdata(const char *procfile, const char *fmt, va_list args) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   119
    FILE    *f;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   120
    int     n;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   121
    char     buf[2048];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   122
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   123
    if ((f = fopen(procfile, "r")) == NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   124
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   125
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   126
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   127
    if ((n = fread(buf, 1, sizeof(buf), f)) != -1) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   128
    char *tmp;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   129
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   130
    buf[n-1] = '\0';
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   131
    /** skip through pid and exec name. the exec name _could be wacky_ (renamed) and
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   132
     *  make scanf go mupp.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   133
     */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   134
    if ((tmp = strrchr(buf, ')')) != NULL) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   135
        // skip the ')' and the following space but check that the buffer is long enough
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   136
        tmp += 2;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   137
        if (tmp < buf + n) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   138
        n = vsscanf(tmp, fmt, args);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   139
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   140
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   141
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   142
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   143
    fclose(f);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   144
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   145
    return n;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   146
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   147
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   148
static int read_statdata(const char *procfile, const char *fmt, ...) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   149
    int       n;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   150
    va_list args;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   151
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   152
    va_start(args, fmt);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   153
    n = vread_statdata(procfile, fmt, args);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   154
    va_end(args);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   155
    return n;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   156
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   157
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   158
/** read user and system ticks from a named procfile, assumed to be in 'stat' format then. */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   159
static int read_ticks(const char *procfile, uint64_t *userTicks, uint64_t *systemTicks) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   160
    return read_statdata(procfile, "%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u "DEC_64" "DEC_64,
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   161
             userTicks, systemTicks
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   162
             );
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   163
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   164
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   165
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   166
 * Return the number of ticks spent in any of the processes belonging
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   167
 * to the JVM on any CPU.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   168
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   169
static int get_jvmticks(ticks *pticks) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   170
    uint64_t userTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   171
    uint64_t systemTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   172
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   173
    if (read_ticks("/proc/self/stat", &userTicks, &systemTicks) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   174
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   175
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   176
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   177
    // get the total
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   178
    if (get_totalticks(-1, pticks) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   179
        return -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   180
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   181
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   182
    pticks->used       = userTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   183
    pticks->usedKernel = systemTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   184
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   185
    return 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   186
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   187
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   188
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   189
 * This method must be called first, before any data can be gathererd.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   190
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   191
int perfInit() {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   192
    static int initialized=1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   193
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   194
    if (!initialized) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   195
        int  i;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   196
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   197
        int n = sysconf(_SC_NPROCESSORS_ONLN);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   198
        if (n <= 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   199
            n = 1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   200
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   201
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   202
        counters.cpus = calloc(n,sizeof(ticks));
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   203
        if (counters.cpus != NULL)  {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   204
            // For the CPU load
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   205
            get_totalticks(-1, &counters.cpuTicks);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   206
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   207
            for (i = 0; i < n; i++) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   208
                get_totalticks(i, &counters.cpus[i]);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   209
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   210
            // For JVM load
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   211
            get_jvmticks(&counters.jvmTicks);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   212
            initialized = 1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   213
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   214
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   215
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   216
    return initialized ? 0 : -1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   217
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   218
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   219
#define MAX(a,b) (a>b?a:b)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   220
#define MIN(a,b) (a<b?a:b)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   221
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   222
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   223
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   224
/**
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   225
 * Return the load of the CPU as a double. 1.0 means the CPU process uses all
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   226
 * available time for user or system processes, 0.0 means the CPU uses all time
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   227
 * being idle.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   228
 *
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   229
 * Returns a negative value if there is a problem in determining the CPU load.
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   230
 */
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   231
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   232
static double get_cpuload_internal(int which, double *pkernelLoad, CpuLoadTarget target) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   233
    uint64_t udiff, kdiff, tdiff;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   234
    ticks *pticks, tmp;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   235
    double user_load = -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   236
    int failed = 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   237
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   238
    *pkernelLoad = 0.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   239
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   240
    pthread_mutex_lock(&lock);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   241
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   242
    if(perfInit() == 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   243
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   244
        if (target == CPU_LOAD_VM_ONLY) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   245
            pticks = &counters.jvmTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   246
        } else if (which == -1) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   247
            pticks = &counters.cpuTicks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   248
        } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   249
            pticks = &counters.cpus[which];
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   250
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   251
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   252
        tmp = *pticks;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   253
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   254
        if (target == CPU_LOAD_VM_ONLY) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   255
            if (get_jvmticks(pticks) != 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   256
                failed = 1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   257
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   258
        } else if (get_totalticks(which, pticks) < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   259
            failed = 1;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   260
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   261
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   262
        if(!failed) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   263
            // seems like we sometimes end up with less kernel ticks when
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   264
            // reading /proc/self/stat a second time, timing issue between cpus?
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   265
            if (pticks->usedKernel < tmp.usedKernel) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   266
                kdiff = 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   267
            } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   268
                kdiff = pticks->usedKernel - tmp.usedKernel;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   269
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   270
            tdiff = pticks->total - tmp.total;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   271
            udiff = pticks->used - tmp.used;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   272
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   273
            if (tdiff == 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   274
                user_load = 0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   275
            } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   276
                if (tdiff < (udiff + kdiff)) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   277
                    tdiff = udiff + kdiff;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   278
                }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   279
                *pkernelLoad = (kdiff / (double)tdiff);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   280
                // BUG9044876, normalize return values to sane values
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   281
                *pkernelLoad = MAX(*pkernelLoad, 0.0);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   282
                *pkernelLoad = MIN(*pkernelLoad, 1.0);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   283
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   284
                user_load = (udiff / (double)tdiff);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   285
                user_load = MAX(user_load, 0.0);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   286
                user_load = MIN(user_load, 1.0);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   287
            }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   288
        }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   289
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   290
    pthread_mutex_unlock(&lock);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   291
    return user_load;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   292
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   293
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   294
double get_cpu_load(int which) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   295
    double u, s;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   296
    u = get_cpuload_internal(which, &s, CPU_LOAD_GLOBAL);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   297
    if (u < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   298
        return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   299
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   300
    // Cap total systemload to 1.0
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   301
    return MIN((u + s), 1.0);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   302
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   303
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   304
double get_process_load() {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   305
    double u, s;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   306
    u = get_cpuload_internal(-1, &s, CPU_LOAD_VM_ONLY);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   307
    if (u < 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   308
        return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   309
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   310
    return u + s;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   311
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   312
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   313
JNIEXPORT jdouble JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 25859
diff changeset
   314
Java_com_sun_management_internal_OperatingSystemImpl_getSystemCpuLoad0
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   315
(JNIEnv *env, jobject dummy)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   316
{
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   317
    if(perfInit() == 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   318
        return get_cpu_load(-1);
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   319
    } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   320
        return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   321
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   322
}
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   323
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   324
JNIEXPORT jdouble JNICALL
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 25859
diff changeset
   325
Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0
9559
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   326
(JNIEnv *env, jobject dummy)
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   327
{
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   328
    if(perfInit() == 0) {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   329
        return get_process_load();
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   330
    } else {
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   331
        return -1.0;
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   332
    }
24c0a7a7297f 7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff changeset
   333
}