author | roland |
Wed, 03 Feb 2016 12:36:18 +0100 | |
changeset 36065 | 4f0e0cb7b016 |
parent 30355 | e37c7eba132f |
permissions | -rw-r--r-- |
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 <fcntl.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
27 |
#include <kstat.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
28 |
#include <procfs.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 <stdlib.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
31 |
#include <stdio.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
32 |
#include <string.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
33 |
#include <sys/sysinfo.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
34 |
#include <sys/lwp.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
35 |
#include <pthread.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
36 |
#include <utmpx.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
37 |
#include <dlfcn.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
38 |
#include <sys/loadavg.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
39 |
#include <jni.h> |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
40 |
#include "jvm.h" |
30355
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
25859
diff
changeset
|
41 |
#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
|
42 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
43 |
typedef struct { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
44 |
kstat_t *kstat; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
45 |
uint64_t last_idle; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
46 |
uint64_t last_total; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
47 |
double last_ratio; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
48 |
} cpuload_t; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
49 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
50 |
static cpuload_t *cpu_loads = NULL; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
51 |
static unsigned int num_cpus; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
52 |
static kstat_ctl_t *kstat_ctrl = NULL; |
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 void map_cpu_kstat_counters() { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
55 |
kstat_t *kstat; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
56 |
int i; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
57 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
58 |
// Get number of CPU(s) |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
59 |
if ((num_cpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
60 |
num_cpus = 1; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
61 |
} |
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 |
// Data structure for saving CPU load |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
64 |
if ((cpu_loads = calloc(num_cpus,sizeof(cpuload_t))) == NULL) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
65 |
return; |
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 |
// Get kstat cpu_stat counters for every CPU |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
69 |
// (loop over kstat to find our cpu_stat(s) |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
70 |
i = 0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
71 |
for (kstat = kstat_ctrl->kc_chain; kstat != NULL; kstat = kstat->ks_next) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
72 |
if (strncmp(kstat->ks_module, "cpu_stat", 8) == 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
73 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
74 |
if (kstat_read(kstat_ctrl, kstat, NULL) == -1) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
75 |
// Failed to initialize kstat for this CPU so ignore it |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
76 |
continue; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
77 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
78 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
79 |
if (i == num_cpus) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
80 |
// Found more cpu_stats than reported CPUs |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
81 |
break; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
82 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
83 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
84 |
cpu_loads[i++].kstat = kstat; |
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 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
87 |
} |
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 |
static int init_cpu_kstat_counters() { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
90 |
static int initialized = 0; |
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 |
// Concurrence in this method is prevented by the lock in |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
93 |
// the calling method get_cpu_load(); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
94 |
if(!initialized) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
95 |
if ((kstat_ctrl = kstat_open()) != NULL) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
96 |
map_cpu_kstat_counters(); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
97 |
initialized = 1; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
98 |
} |
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 |
return initialized ? 0 : -1; |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
103 |
static void update_cpu_kstat_counters() { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
104 |
if(kstat_chain_update(kstat_ctrl) != 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
105 |
free(cpu_loads); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
106 |
map_cpu_kstat_counters(); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
107 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
108 |
} |
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 |
int read_cpustat(cpuload_t *load, cpu_stat_t *cpu_stat) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
111 |
if (load->kstat == NULL) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
112 |
// no handle. |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
113 |
return -1; |
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 |
if (kstat_read(kstat_ctrl, load->kstat, cpu_stat) == -1) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
116 |
// disabling for now, a kstat chain update is likely to happen next time |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
117 |
load->kstat = NULL; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
118 |
return -1; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
119 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
120 |
return 0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
121 |
} |
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 |
double get_single_cpu_load(unsigned int n) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
124 |
cpuload_t *load; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
125 |
cpu_stat_t cpu_stat; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
126 |
uint_t *usage; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
127 |
uint64_t c_idle; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
128 |
uint64_t c_total; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
129 |
uint64_t d_idle; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
130 |
uint64_t d_total; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
131 |
int i; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
132 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
133 |
if (n >= num_cpus) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
134 |
return -1.0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
135 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
136 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
137 |
load = &cpu_loads[n]; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
138 |
if (read_cpustat(load, &cpu_stat) < 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
139 |
return -1.0; |
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 |
usage = cpu_stat.cpu_sysinfo.cpu; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
143 |
c_idle = usage[CPU_IDLE]; |
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 |
for (c_total = 0, i = 0; i < CPU_STATES; i++) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
146 |
c_total += usage[i]; |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
149 |
// Calculate diff against previous snapshot |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
150 |
d_idle = c_idle - load->last_idle; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
151 |
d_total = c_total - load->last_total; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
152 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
153 |
/** update if weve moved */ |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
154 |
if (d_total > 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
155 |
// Save current values for next time around |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
156 |
load->last_idle = c_idle; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
157 |
load->last_total = c_total; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
158 |
load->last_ratio = (double) (d_total - d_idle) / d_total; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
159 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
160 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
161 |
return load->last_ratio; |
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 |
int get_info(const char *path, void *info, size_t s, off_t o) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
165 |
int fd; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
166 |
int ret = 0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
167 |
if ((fd = open(path, O_RDONLY)) < 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
168 |
return -1; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
169 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
170 |
if (pread(fd, info, s, o) != s) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
171 |
ret = -1; |
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 |
close(fd); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
174 |
return ret; |
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 |
#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
|
178 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
179 |
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
|
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 |
* Return the cpu load (0-1) for proc number 'which' (or average all if which == -1) |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
183 |
*/ |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
184 |
double get_cpu_load(int which) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
185 |
double load =.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 |
pthread_mutex_lock(&lock); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
188 |
if(init_cpu_kstat_counters()==0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
189 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
190 |
update_cpu_kstat_counters(); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
191 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
192 |
if (which == -1) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
193 |
unsigned int i; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
194 |
double t; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
195 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
196 |
for (t = .0, i = 0; i < num_cpus; i++) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
197 |
t += get_single_cpu_load(i); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
198 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
199 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
200 |
// Cap total systemload to 1.0 |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
201 |
load = MIN((t / num_cpus), 1.0); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
202 |
} else { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
203 |
load = MIN(get_single_cpu_load(which), 1.0); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
204 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
205 |
} else { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
206 |
load = -1.0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
207 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
208 |
pthread_mutex_unlock(&lock); |
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 |
return load; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
211 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
212 |
|
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 |
* Return the cpu load (0-1) for the current process (i.e the JVM) |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
215 |
* or -1.0 if the get_info() call failed |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
216 |
*/ |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
217 |
double get_process_load(void) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
218 |
psinfo_t info; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
219 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
220 |
// Get the percentage of "recent cpu usage" from all the lwp:s in the JVM:s |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
221 |
// process. This is returned as a value between 0.0 and 1.0 multiplied by 0x8000. |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
222 |
if (get_info("/proc/self/psinfo",&info.pr_pctcpu, sizeof(info.pr_pctcpu), offsetof(psinfo_t, pr_pctcpu)) == 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
223 |
return (double) info.pr_pctcpu / 0x8000; |
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 -1.0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
226 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
227 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
228 |
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
|
229 |
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
|
230 |
(JNIEnv *env, jobject dummy) |
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 |
return get_cpu_load(-1); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
233 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
234 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
235 |
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
|
236 |
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
|
237 |
(JNIEnv *env, jobject dummy) |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
238 |
{ |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
239 |
return get_process_load(); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
240 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
241 |