author | alanb |
Tue, 15 May 2018 09:33:02 +0100 | |
changeset 50109 | ea54197f4fe4 |
parent 47216 | 71c04702a3d5 |
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 <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; |
32417
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
80 |
uint64_t iowTicks = 0, irqTicks = 0, sirqTicks= 0; |
9559
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
81 |
int n; |
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 |
if((fh = fopen("/proc/stat", "r")) == NULL) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
84 |
return -1; |
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 |
|
32417
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
87 |
n = fscanf(fh, "cpu " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " " |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
88 |
DEC_64 " " DEC_64, |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
89 |
&userTicks, &niceTicks, &systemTicks, &idleTicks, |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
90 |
&iowTicks, &irqTicks, &sirqTicks); |
9559
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 |
// Move to next line |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
93 |
next_line(fh); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
94 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
95 |
//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
|
96 |
if (which != -1) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
97 |
int i; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
98 |
for (i = 0; i < which; i++) { |
32417
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
99 |
if (fscanf(fh, "cpu%*d " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " " |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
100 |
DEC_64 " " DEC_64 " " DEC_64, |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
101 |
&userTicks, &niceTicks, &systemTicks, &idleTicks, |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
102 |
&iowTicks, &irqTicks, &sirqTicks) < 4) { |
9559
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
103 |
fclose(fh); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
104 |
return -2; |
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 |
next_line(fh); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
107 |
} |
32417
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
108 |
n = fscanf(fh, "cpu%*d " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " " |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
109 |
DEC_64 " " DEC_64 " " DEC_64 "\n", |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
110 |
&userTicks, &niceTicks, &systemTicks, &idleTicks, |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
111 |
&iowTicks, &irqTicks, &sirqTicks); |
9559
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
112 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
113 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
114 |
fclose(fh); |
32417
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
115 |
if (n < 4) { |
9559
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
116 |
return -2; |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
119 |
pticks->used = userTicks + niceTicks; |
32417
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
120 |
pticks->usedKernel = systemTicks + irqTicks + sirqTicks; |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
121 |
pticks->total = userTicks + niceTicks + systemTicks + idleTicks + |
6859107fc6c3
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux
dbuck
parents:
30355
diff
changeset
|
122 |
iowTicks + irqTicks + sirqTicks; |
9559
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
123 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
124 |
return 0; |
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 |
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
|
128 |
FILE *f; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
129 |
int n; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
130 |
char buf[2048]; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
131 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
132 |
if ((f = fopen(procfile, "r")) == NULL) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
133 |
return -1; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
134 |
} |
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 |
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
|
137 |
char *tmp; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
138 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
139 |
buf[n-1] = '\0'; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
140 |
/** 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
|
141 |
* make scanf go mupp. |
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 |
if ((tmp = strrchr(buf, ')')) != NULL) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
144 |
// 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
|
145 |
tmp += 2; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
146 |
if (tmp < buf + n) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
147 |
n = vsscanf(tmp, fmt, args); |
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 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
150 |
} |
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 |
fclose(f); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
153 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
154 |
return n; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
155 |
} |
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 |
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
|
158 |
int n; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
159 |
va_list args; |
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 |
va_start(args, fmt); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
162 |
n = vread_statdata(procfile, fmt, args); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
163 |
va_end(args); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
164 |
return n; |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
167 |
/** 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
|
168 |
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
|
169 |
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
|
170 |
userTicks, systemTicks |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
171 |
); |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
174 |
/** |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
175 |
* 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
|
176 |
* to the JVM on any CPU. |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
177 |
*/ |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
178 |
static int get_jvmticks(ticks *pticks) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
179 |
uint64_t userTicks; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
180 |
uint64_t systemTicks; |
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 |
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
|
183 |
return -1; |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
186 |
// get the total |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
187 |
if (get_totalticks(-1, pticks) < 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
188 |
return -1; |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
191 |
pticks->used = userTicks; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
192 |
pticks->usedKernel = systemTicks; |
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 |
return 0; |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
197 |
/** |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
198 |
* 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
|
199 |
*/ |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
200 |
int perfInit() { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
201 |
static int initialized=1; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
202 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
203 |
if (!initialized) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
204 |
int i; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
205 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
206 |
int n = sysconf(_SC_NPROCESSORS_ONLN); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
207 |
if (n <= 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
208 |
n = 1; |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
211 |
counters.cpus = calloc(n,sizeof(ticks)); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
212 |
if (counters.cpus != NULL) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
213 |
// For the CPU load |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
214 |
get_totalticks(-1, &counters.cpuTicks); |
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 |
for (i = 0; i < n; i++) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
217 |
get_totalticks(i, &counters.cpus[i]); |
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 |
// For JVM load |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
220 |
get_jvmticks(&counters.jvmTicks); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
221 |
initialized = 1; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
222 |
} |
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 initialized ? 0 : -1; |
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 |
#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
|
229 |
#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
|
230 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
231 |
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
|
232 |
|
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 |
* 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
|
235 |
* 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
|
236 |
* being idle. |
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 |
* 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
|
239 |
*/ |
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 |
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
|
242 |
uint64_t udiff, kdiff, tdiff; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
243 |
ticks *pticks, tmp; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
244 |
double user_load = -1.0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
245 |
int failed = 0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
246 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
247 |
*pkernelLoad = 0.0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
248 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
249 |
pthread_mutex_lock(&lock); |
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 |
if(perfInit() == 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
252 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
253 |
if (target == CPU_LOAD_VM_ONLY) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
254 |
pticks = &counters.jvmTicks; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
255 |
} else if (which == -1) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
256 |
pticks = &counters.cpuTicks; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
257 |
} else { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
258 |
pticks = &counters.cpus[which]; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
259 |
} |
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 |
tmp = *pticks; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
262 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
263 |
if (target == CPU_LOAD_VM_ONLY) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
264 |
if (get_jvmticks(pticks) != 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
265 |
failed = 1; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
266 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
267 |
} else if (get_totalticks(which, pticks) < 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
268 |
failed = 1; |
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 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
271 |
if(!failed) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
272 |
// 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
|
273 |
// 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
|
274 |
if (pticks->usedKernel < tmp.usedKernel) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
275 |
kdiff = 0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
276 |
} else { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
277 |
kdiff = pticks->usedKernel - tmp.usedKernel; |
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 |
tdiff = pticks->total - tmp.total; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
280 |
udiff = pticks->used - tmp.used; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
281 |
|
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
282 |
if (tdiff == 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
283 |
user_load = 0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
284 |
} else { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
285 |
if (tdiff < (udiff + kdiff)) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
286 |
tdiff = udiff + kdiff; |
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 |
*pkernelLoad = (kdiff / (double)tdiff); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
289 |
// BUG9044876, normalize return values to sane values |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
290 |
*pkernelLoad = MAX(*pkernelLoad, 0.0); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
291 |
*pkernelLoad = MIN(*pkernelLoad, 1.0); |
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 |
user_load = (udiff / (double)tdiff); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
294 |
user_load = MAX(user_load, 0.0); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
295 |
user_load = MIN(user_load, 1.0); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
296 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
297 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
298 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
299 |
pthread_mutex_unlock(&lock); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
300 |
return user_load; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
301 |
} |
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 |
double get_cpu_load(int which) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
304 |
double u, s; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
305 |
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
|
306 |
if (u < 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
307 |
return -1.0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
308 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
309 |
// Cap total systemload to 1.0 |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
310 |
return MIN((u + s), 1.0); |
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 |
double get_process_load() { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
314 |
double u, s; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
315 |
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
|
316 |
if (u < 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
317 |
return -1.0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
318 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
319 |
return u + s; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
320 |
} |
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 |
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
|
323 |
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
|
324 |
(JNIEnv *env, jobject dummy) |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
325 |
{ |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
326 |
if(perfInit() == 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
327 |
return get_cpu_load(-1); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
328 |
} else { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
329 |
return -1.0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
330 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
331 |
} |
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 |
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
|
334 |
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
|
335 |
(JNIEnv *env, jobject dummy) |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
336 |
{ |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
337 |
if(perfInit() == 0) { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
338 |
return get_process_load(); |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
339 |
} else { |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
340 |
return -1.0; |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
341 |
} |
24c0a7a7297f
7028071: Add two attributes to the OperatingSystemMXBean to provide CPU Load info
fparain
parents:
diff
changeset
|
342 |
} |