50113
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
#include "precompiled.hpp"
|
|
25 |
#include "memory/allocation.inline.hpp"
|
|
26 |
#include "memory/resourceArea.hpp"
|
|
27 |
#include "runtime/os.hpp"
|
|
28 |
#include "runtime/os_perf.hpp"
|
|
29 |
#include "vm_version_ext_x86.hpp"
|
|
30 |
|
|
31 |
#ifdef __APPLE__
|
|
32 |
#import <libproc.h>
|
|
33 |
#include <sys/time.h>
|
|
34 |
#include <sys/sysctl.h>
|
|
35 |
#include <mach/mach.h>
|
|
36 |
#include <mach/task_info.h>
|
|
37 |
#endif
|
|
38 |
|
|
39 |
static const double NANOS_PER_SEC = 1000000000.0;
|
|
40 |
|
|
41 |
class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
|
|
42 |
friend class CPUPerformanceInterface;
|
|
43 |
private:
|
|
44 |
long _total_cpu_nanos;
|
|
45 |
long _total_csr_nanos;
|
|
46 |
long _jvm_user_nanos;
|
|
47 |
long _jvm_system_nanos;
|
|
48 |
long _jvm_context_switches;
|
|
49 |
long _used_ticks;
|
|
50 |
long _total_ticks;
|
|
51 |
int _active_processor_count;
|
|
52 |
|
|
53 |
bool now_in_nanos(long* resultp) {
|
|
54 |
timeval current_time;
|
|
55 |
if (gettimeofday(¤t_time, NULL) != 0) {
|
|
56 |
// Error getting current time
|
|
57 |
return false;
|
|
58 |
}
|
|
59 |
*resultp = current_time.tv_sec * NANOS_PER_SEC + 1000L * current_time.tv_usec;
|
|
60 |
return true;
|
|
61 |
}
|
|
62 |
|
|
63 |
double normalize(double value) {
|
|
64 |
return MIN2<double>(MAX2<double>(value, 0.0), 1.0);
|
|
65 |
}
|
|
66 |
int cpu_load(int which_logical_cpu, double* cpu_load);
|
|
67 |
int context_switch_rate(double* rate);
|
|
68 |
int cpu_load_total_process(double* cpu_load);
|
|
69 |
int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);
|
|
70 |
|
|
71 |
CPUPerformance(const CPUPerformance& rhs); // no impl
|
|
72 |
CPUPerformance& operator=(const CPUPerformance& rhs); // no impl
|
|
73 |
public:
|
|
74 |
CPUPerformance();
|
|
75 |
bool initialize();
|
|
76 |
~CPUPerformance();
|
|
77 |
};
|
|
78 |
|
|
79 |
CPUPerformanceInterface::CPUPerformance::CPUPerformance() {
|
|
80 |
_total_cpu_nanos= 0;
|
|
81 |
_total_csr_nanos= 0;
|
|
82 |
_jvm_context_switches = 0;
|
|
83 |
_jvm_user_nanos = 0;
|
|
84 |
_jvm_system_nanos = 0;
|
|
85 |
_used_ticks = 0;
|
|
86 |
_total_ticks = 0;
|
|
87 |
_active_processor_count = 0;
|
|
88 |
}
|
|
89 |
|
|
90 |
bool CPUPerformanceInterface::CPUPerformance::initialize() {
|
|
91 |
return true;
|
|
92 |
}
|
|
93 |
|
|
94 |
CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {
|
|
95 |
}
|
|
96 |
|
|
97 |
int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {
|
|
98 |
return FUNCTIONALITY_NOT_IMPLEMENTED;
|
|
99 |
}
|
|
100 |
|
|
101 |
int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {
|
|
102 |
#ifdef __APPLE__
|
|
103 |
host_name_port_t host = mach_host_self();
|
|
104 |
host_flavor_t flavor = HOST_CPU_LOAD_INFO;
|
|
105 |
mach_msg_type_number_t host_info_count = HOST_CPU_LOAD_INFO_COUNT;
|
|
106 |
host_cpu_load_info_data_t cpu_load_info;
|
|
107 |
|
|
108 |
kern_return_t kr = host_statistics(host, flavor, (host_info_t)&cpu_load_info, &host_info_count);
|
|
109 |
if (kr != KERN_SUCCESS) {
|
|
110 |
return OS_ERR;
|
|
111 |
}
|
|
112 |
|
|
113 |
long used_ticks = cpu_load_info.cpu_ticks[CPU_STATE_USER] + cpu_load_info.cpu_ticks[CPU_STATE_NICE] + cpu_load_info.cpu_ticks[CPU_STATE_SYSTEM];
|
|
114 |
long total_ticks = used_ticks + cpu_load_info.cpu_ticks[CPU_STATE_IDLE];
|
|
115 |
|
|
116 |
if (_used_ticks == 0 || _total_ticks == 0) {
|
|
117 |
// First call, just set the values
|
|
118 |
_used_ticks = used_ticks;
|
|
119 |
_total_ticks = total_ticks;
|
|
120 |
return OS_ERR;
|
|
121 |
}
|
|
122 |
|
|
123 |
long used_delta = used_ticks - _used_ticks;
|
|
124 |
long total_delta = total_ticks - _total_ticks;
|
|
125 |
|
|
126 |
_used_ticks = used_ticks;
|
|
127 |
_total_ticks = total_ticks;
|
|
128 |
|
|
129 |
if (total_delta == 0) {
|
|
130 |
// Avoid division by zero
|
|
131 |
return OS_ERR;
|
|
132 |
}
|
|
133 |
|
|
134 |
*cpu_load = (double)used_delta / total_delta;
|
|
135 |
|
|
136 |
return OS_OK;
|
|
137 |
#else
|
|
138 |
return FUNCTIONALITY_NOT_IMPLEMENTED;
|
|
139 |
#endif
|
|
140 |
}
|
|
141 |
|
|
142 |
int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {
|
|
143 |
#ifdef __APPLE__
|
|
144 |
int result = cpu_load_total_process(psystemTotalLoad);
|
|
145 |
mach_port_t task = mach_task_self();
|
|
146 |
mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
|
|
147 |
task_info_data_t task_info_data;
|
|
148 |
kern_return_t kr = task_info(task, TASK_ABSOLUTETIME_INFO, (task_info_t)task_info_data, &task_info_count);
|
|
149 |
if (kr != KERN_SUCCESS) {
|
|
150 |
return OS_ERR;
|
|
151 |
}
|
|
152 |
task_absolutetime_info_t absolutetime_info = (task_absolutetime_info_t)task_info_data;
|
|
153 |
|
|
154 |
int active_processor_count = os::active_processor_count();
|
|
155 |
long jvm_user_nanos = absolutetime_info->total_user;
|
|
156 |
long jvm_system_nanos = absolutetime_info->total_system;
|
|
157 |
|
|
158 |
long total_cpu_nanos;
|
|
159 |
if(!now_in_nanos(&total_cpu_nanos)) {
|
|
160 |
return OS_ERR;
|
|
161 |
}
|
|
162 |
|
|
163 |
if (_total_cpu_nanos == 0 || active_processor_count != _active_processor_count) {
|
|
164 |
// First call or change in active processor count
|
|
165 |
result = OS_ERR;
|
|
166 |
}
|
|
167 |
|
|
168 |
long delta_nanos = active_processor_count * (total_cpu_nanos - _total_cpu_nanos);
|
|
169 |
if (delta_nanos == 0) {
|
|
170 |
// Avoid division by zero
|
|
171 |
return OS_ERR;
|
|
172 |
}
|
|
173 |
|
|
174 |
*pjvmUserLoad = normalize((double)(jvm_user_nanos - _jvm_user_nanos)/delta_nanos);
|
|
175 |
*pjvmKernelLoad = normalize((double)(jvm_system_nanos - _jvm_system_nanos)/delta_nanos);
|
|
176 |
|
|
177 |
_active_processor_count = active_processor_count;
|
|
178 |
_total_cpu_nanos = total_cpu_nanos;
|
|
179 |
_jvm_user_nanos = jvm_user_nanos;
|
|
180 |
_jvm_system_nanos = jvm_system_nanos;
|
|
181 |
|
|
182 |
return result;
|
|
183 |
#else
|
|
184 |
return FUNCTIONALITY_NOT_IMPLEMENTED;
|
|
185 |
#endif
|
|
186 |
}
|
|
187 |
|
|
188 |
int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {
|
|
189 |
#ifdef __APPLE__
|
|
190 |
mach_port_t task = mach_task_self();
|
|
191 |
mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
|
|
192 |
task_info_data_t task_info_data;
|
|
193 |
kern_return_t kr = task_info(task, TASK_EVENTS_INFO, (task_info_t)task_info_data, &task_info_count);
|
|
194 |
if (kr != KERN_SUCCESS) {
|
|
195 |
return OS_ERR;
|
|
196 |
}
|
|
197 |
|
|
198 |
int result = OS_OK;
|
|
199 |
if (_total_csr_nanos == 0 || _jvm_context_switches == 0) {
|
|
200 |
// First call just set initial values.
|
|
201 |
result = OS_ERR;
|
|
202 |
}
|
|
203 |
|
|
204 |
long jvm_context_switches = ((task_events_info_t)task_info_data)->csw;
|
|
205 |
|
|
206 |
long total_csr_nanos;
|
|
207 |
if(!now_in_nanos(&total_csr_nanos)) {
|
|
208 |
return OS_ERR;
|
|
209 |
}
|
|
210 |
double delta_in_sec = (double)(total_csr_nanos - _total_csr_nanos) / NANOS_PER_SEC;
|
|
211 |
if (delta_in_sec == 0.0) {
|
|
212 |
// Avoid division by zero
|
|
213 |
return OS_ERR;
|
|
214 |
}
|
|
215 |
|
|
216 |
*rate = (jvm_context_switches - _jvm_context_switches) / delta_in_sec;
|
|
217 |
|
|
218 |
_jvm_context_switches = jvm_context_switches;
|
|
219 |
_total_csr_nanos = total_csr_nanos;
|
|
220 |
|
|
221 |
return result;
|
|
222 |
#else
|
|
223 |
return FUNCTIONALITY_NOT_IMPLEMENTED;
|
|
224 |
#endif
|
|
225 |
}
|
|
226 |
|
|
227 |
CPUPerformanceInterface::CPUPerformanceInterface() {
|
|
228 |
_impl = NULL;
|
|
229 |
}
|
|
230 |
|
|
231 |
bool CPUPerformanceInterface::initialize() {
|
|
232 |
_impl = new CPUPerformanceInterface::CPUPerformance();
|
|
233 |
return _impl != NULL && _impl->initialize();
|
|
234 |
}
|
|
235 |
|
|
236 |
CPUPerformanceInterface::~CPUPerformanceInterface() {
|
|
237 |
if (_impl != NULL) {
|
|
238 |
delete _impl;
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
int CPUPerformanceInterface::cpu_load(int which_logical_cpu, double* cpu_load) const {
|
|
243 |
return _impl->cpu_load(which_logical_cpu, cpu_load);
|
|
244 |
}
|
|
245 |
|
|
246 |
int CPUPerformanceInterface::cpu_load_total_process(double* cpu_load) const {
|
|
247 |
return _impl->cpu_load_total_process(cpu_load);
|
|
248 |
}
|
|
249 |
|
|
250 |
int CPUPerformanceInterface::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) const {
|
|
251 |
return _impl->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotalLoad);
|
|
252 |
}
|
|
253 |
|
|
254 |
int CPUPerformanceInterface::context_switch_rate(double* rate) const {
|
|
255 |
return _impl->context_switch_rate(rate);
|
|
256 |
}
|
|
257 |
|
|
258 |
class SystemProcessInterface::SystemProcesses : public CHeapObj<mtInternal> {
|
|
259 |
friend class SystemProcessInterface;
|
|
260 |
private:
|
|
261 |
SystemProcesses();
|
|
262 |
bool initialize();
|
|
263 |
SystemProcesses(const SystemProcesses& rhs); // no impl
|
|
264 |
SystemProcesses& operator=(const SystemProcesses& rhs); // no impl
|
|
265 |
~SystemProcesses();
|
|
266 |
|
|
267 |
//information about system processes
|
|
268 |
int system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const;
|
|
269 |
};
|
|
270 |
|
|
271 |
SystemProcessInterface::SystemProcesses::SystemProcesses() {
|
|
272 |
}
|
|
273 |
|
|
274 |
bool SystemProcessInterface::SystemProcesses::initialize() {
|
|
275 |
return true;
|
|
276 |
}
|
|
277 |
|
|
278 |
SystemProcessInterface::SystemProcesses::~SystemProcesses() {
|
|
279 |
}
|
|
280 |
int SystemProcessInterface::SystemProcesses::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const {
|
|
281 |
assert(system_processes != NULL, "system_processes pointer is NULL!");
|
|
282 |
assert(no_of_sys_processes != NULL, "system_processes counter pointer is NULL!");
|
|
283 |
#ifdef __APPLE__
|
|
284 |
pid_t* pids = NULL;
|
|
285 |
int pid_count = 0;
|
|
286 |
ResourceMark rm;
|
|
287 |
|
|
288 |
int try_count = 0;
|
|
289 |
while (pids == NULL) {
|
|
290 |
// Find out buffer size
|
|
291 |
size_t pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
|
|
292 |
if (pids_bytes <= 0) {
|
|
293 |
return OS_ERR;
|
|
294 |
}
|
|
295 |
pid_count = pids_bytes / sizeof(pid_t);
|
|
296 |
pids = NEW_RESOURCE_ARRAY(pid_t, pid_count);
|
|
297 |
memset(pids, 0, pids_bytes);
|
|
298 |
|
|
299 |
pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, pids, pids_bytes);
|
|
300 |
if (pids_bytes <= 0) {
|
|
301 |
// couldn't fit buffer, retry.
|
|
302 |
FREE_RESOURCE_ARRAY(pid_t, pids, pid_count);
|
|
303 |
pids = NULL;
|
|
304 |
try_count++;
|
|
305 |
if (try_count > 3) {
|
|
306 |
return OS_ERR;
|
|
307 |
}
|
|
308 |
} else {
|
|
309 |
pid_count = pids_bytes / sizeof(pid_t);
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
int process_count = 0;
|
|
314 |
SystemProcess* next = NULL;
|
|
315 |
for (int i = 0; i < pid_count; i++) {
|
|
316 |
pid_t pid = pids[i];
|
|
317 |
if (pid != 0) {
|
|
318 |
char buffer[PROC_PIDPATHINFO_MAXSIZE];
|
|
319 |
memset(buffer, 0 , sizeof(buffer));
|
|
320 |
if (proc_pidpath(pid, buffer, sizeof(buffer)) != -1) {
|
|
321 |
int length = strlen(buffer);
|
|
322 |
if (length > 0) {
|
|
323 |
SystemProcess* current = new SystemProcess();
|
|
324 |
char * path = NEW_C_HEAP_ARRAY(char, length + 1, mtInternal);
|
|
325 |
strcpy(path, buffer);
|
|
326 |
current->set_path(path);
|
|
327 |
current->set_pid((int)pid);
|
|
328 |
current->set_next(next);
|
|
329 |
next = current;
|
|
330 |
process_count++;
|
|
331 |
}
|
|
332 |
}
|
|
333 |
}
|
|
334 |
}
|
|
335 |
|
|
336 |
*no_of_sys_processes = process_count;
|
|
337 |
*system_processes = next;
|
|
338 |
|
|
339 |
return OS_OK;
|
|
340 |
#endif
|
|
341 |
return FUNCTIONALITY_NOT_IMPLEMENTED;
|
|
342 |
}
|
|
343 |
|
|
344 |
int SystemProcessInterface::system_processes(SystemProcess** system_procs, int* no_of_sys_processes) const {
|
|
345 |
return _impl->system_processes(system_procs, no_of_sys_processes);
|
|
346 |
}
|
|
347 |
|
|
348 |
SystemProcessInterface::SystemProcessInterface() {
|
|
349 |
_impl = NULL;
|
|
350 |
}
|
|
351 |
|
|
352 |
bool SystemProcessInterface::initialize() {
|
|
353 |
_impl = new SystemProcessInterface::SystemProcesses();
|
|
354 |
return _impl != NULL && _impl->initialize();
|
|
355 |
}
|
|
356 |
|
|
357 |
SystemProcessInterface::~SystemProcessInterface() {
|
|
358 |
if (_impl != NULL) {
|
|
359 |
delete _impl;
|
|
360 |
}
|
|
361 |
}
|
|
362 |
|
|
363 |
CPUInformationInterface::CPUInformationInterface() {
|
|
364 |
_cpu_info = NULL;
|
|
365 |
}
|
|
366 |
|
|
367 |
bool CPUInformationInterface::initialize() {
|
|
368 |
_cpu_info = new CPUInformation();
|
|
369 |
|
|
370 |
if (NULL == _cpu_info) {
|
|
371 |
return false;
|
|
372 |
}
|
|
373 |
_cpu_info->set_number_of_hardware_threads(VM_Version_Ext::number_of_threads());
|
|
374 |
_cpu_info->set_number_of_cores(VM_Version_Ext::number_of_cores());
|
|
375 |
_cpu_info->set_number_of_sockets(VM_Version_Ext::number_of_sockets());
|
|
376 |
_cpu_info->set_cpu_name(VM_Version_Ext::cpu_name());
|
|
377 |
_cpu_info->set_cpu_description(VM_Version_Ext::cpu_description());
|
|
378 |
|
|
379 |
return true;
|
|
380 |
}
|
|
381 |
|
|
382 |
CPUInformationInterface::~CPUInformationInterface() {
|
|
383 |
if (_cpu_info != NULL) {
|
|
384 |
if (_cpu_info->cpu_name() != NULL) {
|
|
385 |
const char* cpu_name = _cpu_info->cpu_name();
|
|
386 |
FREE_C_HEAP_ARRAY(char, cpu_name);
|
|
387 |
_cpu_info->set_cpu_name(NULL);
|
|
388 |
}
|
|
389 |
if (_cpu_info->cpu_description() != NULL) {
|
|
390 |
const char* cpu_desc = _cpu_info->cpu_description();
|
|
391 |
FREE_C_HEAP_ARRAY(char, cpu_desc);
|
|
392 |
_cpu_info->set_cpu_description(NULL);
|
|
393 |
}
|
|
394 |
delete _cpu_info;
|
|
395 |
}
|
|
396 |
}
|
|
397 |
|
|
398 |
int CPUInformationInterface::cpu_information(CPUInformation& cpu_info) {
|
|
399 |
if (NULL == _cpu_info) {
|
|
400 |
return OS_ERR;
|
|
401 |
}
|
|
402 |
|
|
403 |
cpu_info = *_cpu_info; // shallow copy assignment
|
|
404 |
return OS_OK;
|
|
405 |
}
|