Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/bsd/vm/os_perf_bsd.cpp
32284 views
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
#ifdef AARCH64
30
#include "vm_version_ext_aarch64.hpp"
31
#else
32
#include "vm_version_ext_x86.hpp"
33
#endif
34
35
#ifdef __APPLE__
36
#import <libproc.h>
37
#include <sys/time.h>
38
#include <sys/sysctl.h>
39
#include <mach/mach.h>
40
#include <mach/task_info.h>
41
#include <sys/socket.h>
42
#include <net/if.h>
43
#include <net/if_dl.h>
44
#include <net/route.h>
45
#endif
46
47
static const double NANOS_PER_SEC = 1000000000.0;
48
49
class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
50
friend class CPUPerformanceInterface;
51
private:
52
long _total_cpu_nanos;
53
long _total_csr_nanos;
54
long _jvm_user_nanos;
55
long _jvm_system_nanos;
56
long _jvm_context_switches;
57
long _used_ticks;
58
long _total_ticks;
59
int _active_processor_count;
60
61
bool now_in_nanos(long* resultp) {
62
timeval current_time;
63
if (gettimeofday(&current_time, NULL) != 0) {
64
// Error getting current time
65
return false;
66
}
67
*resultp = (long)(current_time.tv_sec * NANOS_PER_SEC + 1000L * current_time.tv_usec);
68
return true;
69
}
70
71
double normalize(double value) {
72
return MIN2<double>(MAX2<double>(value, 0.0), 1.0);
73
}
74
int cpu_load(int which_logical_cpu, double* cpu_load);
75
int context_switch_rate(double* rate);
76
int cpu_load_total_process(double* cpu_load);
77
int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);
78
79
CPUPerformance(const CPUPerformance& rhs); // no impl
80
CPUPerformance& operator=(const CPUPerformance& rhs); // no impl
81
public:
82
CPUPerformance();
83
bool initialize();
84
~CPUPerformance();
85
};
86
87
CPUPerformanceInterface::CPUPerformance::CPUPerformance() {
88
_total_cpu_nanos= 0;
89
_total_csr_nanos= 0;
90
_jvm_context_switches = 0;
91
_jvm_user_nanos = 0;
92
_jvm_system_nanos = 0;
93
_used_ticks = 0;
94
_total_ticks = 0;
95
_active_processor_count = 0;
96
}
97
98
bool CPUPerformanceInterface::CPUPerformance::initialize() {
99
return true;
100
}
101
102
CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {
103
}
104
105
int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {
106
return FUNCTIONALITY_NOT_IMPLEMENTED;
107
}
108
109
int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {
110
#ifdef __APPLE__
111
host_name_port_t host = mach_host_self();
112
host_flavor_t flavor = HOST_CPU_LOAD_INFO;
113
mach_msg_type_number_t host_info_count = HOST_CPU_LOAD_INFO_COUNT;
114
host_cpu_load_info_data_t cpu_load_info;
115
116
kern_return_t kr = host_statistics(host, flavor, (host_info_t)&cpu_load_info, &host_info_count);
117
if (kr != KERN_SUCCESS) {
118
return OS_ERR;
119
}
120
121
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];
122
long total_ticks = used_ticks + cpu_load_info.cpu_ticks[CPU_STATE_IDLE];
123
124
if (_used_ticks == 0 || _total_ticks == 0) {
125
// First call, just set the values
126
_used_ticks = used_ticks;
127
_total_ticks = total_ticks;
128
return OS_ERR;
129
}
130
131
long used_delta = used_ticks - _used_ticks;
132
long total_delta = total_ticks - _total_ticks;
133
134
_used_ticks = used_ticks;
135
_total_ticks = total_ticks;
136
137
if (total_delta == 0) {
138
// Avoid division by zero
139
return OS_ERR;
140
}
141
142
*cpu_load = (double)used_delta / total_delta;
143
144
return OS_OK;
145
#else
146
return FUNCTIONALITY_NOT_IMPLEMENTED;
147
#endif
148
}
149
150
int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {
151
#ifdef __APPLE__
152
int result = cpu_load_total_process(psystemTotalLoad);
153
mach_port_t task = mach_task_self();
154
mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
155
task_info_data_t task_info_data;
156
kern_return_t kr = task_info(task, TASK_ABSOLUTETIME_INFO, (task_info_t)task_info_data, &task_info_count);
157
if (kr != KERN_SUCCESS) {
158
return OS_ERR;
159
}
160
task_absolutetime_info_t absolutetime_info = (task_absolutetime_info_t)task_info_data;
161
162
int active_processor_count = os::active_processor_count();
163
long jvm_user_nanos = absolutetime_info->total_user;
164
long jvm_system_nanos = absolutetime_info->total_system;
165
166
long total_cpu_nanos;
167
if(!now_in_nanos(&total_cpu_nanos)) {
168
return OS_ERR;
169
}
170
171
if (_total_cpu_nanos == 0 || active_processor_count != _active_processor_count) {
172
// First call or change in active processor count
173
result = OS_ERR;
174
}
175
176
long delta_nanos = active_processor_count * (total_cpu_nanos - _total_cpu_nanos);
177
if (delta_nanos == 0) {
178
// Avoid division by zero
179
return OS_ERR;
180
}
181
182
*pjvmUserLoad = normalize((double)(jvm_user_nanos - _jvm_user_nanos)/delta_nanos);
183
*pjvmKernelLoad = normalize((double)(jvm_system_nanos - _jvm_system_nanos)/delta_nanos);
184
185
_active_processor_count = active_processor_count;
186
_total_cpu_nanos = total_cpu_nanos;
187
_jvm_user_nanos = jvm_user_nanos;
188
_jvm_system_nanos = jvm_system_nanos;
189
190
return result;
191
#else
192
return FUNCTIONALITY_NOT_IMPLEMENTED;
193
#endif
194
}
195
196
int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {
197
#ifdef __APPLE__
198
mach_port_t task = mach_task_self();
199
mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
200
task_info_data_t task_info_data;
201
kern_return_t kr = task_info(task, TASK_EVENTS_INFO, (task_info_t)task_info_data, &task_info_count);
202
if (kr != KERN_SUCCESS) {
203
return OS_ERR;
204
}
205
206
int result = OS_OK;
207
if (_total_csr_nanos == 0 || _jvm_context_switches == 0) {
208
// First call just set initial values.
209
result = OS_ERR;
210
}
211
212
long jvm_context_switches = ((task_events_info_t)task_info_data)->csw;
213
214
long total_csr_nanos;
215
if(!now_in_nanos(&total_csr_nanos)) {
216
return OS_ERR;
217
}
218
double delta_in_sec = (double)(total_csr_nanos - _total_csr_nanos) / NANOS_PER_SEC;
219
if (delta_in_sec == 0.0) {
220
// Avoid division by zero
221
return OS_ERR;
222
}
223
224
*rate = (jvm_context_switches - _jvm_context_switches) / delta_in_sec;
225
226
_jvm_context_switches = jvm_context_switches;
227
_total_csr_nanos = total_csr_nanos;
228
229
return result;
230
#else
231
return FUNCTIONALITY_NOT_IMPLEMENTED;
232
#endif
233
}
234
235
CPUPerformanceInterface::CPUPerformanceInterface() {
236
_impl = NULL;
237
}
238
239
bool CPUPerformanceInterface::initialize() {
240
_impl = new CPUPerformanceInterface::CPUPerformance();
241
return _impl != NULL && _impl->initialize();
242
}
243
244
CPUPerformanceInterface::~CPUPerformanceInterface() {
245
if (_impl != NULL) {
246
delete _impl;
247
}
248
}
249
250
int CPUPerformanceInterface::cpu_load(int which_logical_cpu, double* cpu_load) const {
251
return _impl->cpu_load(which_logical_cpu, cpu_load);
252
}
253
254
int CPUPerformanceInterface::cpu_load_total_process(double* cpu_load) const {
255
return _impl->cpu_load_total_process(cpu_load);
256
}
257
258
int CPUPerformanceInterface::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) const {
259
return _impl->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotalLoad);
260
}
261
262
int CPUPerformanceInterface::context_switch_rate(double* rate) const {
263
return _impl->context_switch_rate(rate);
264
}
265
266
class SystemProcessInterface::SystemProcesses : public CHeapObj<mtInternal> {
267
friend class SystemProcessInterface;
268
private:
269
SystemProcesses();
270
bool initialize();
271
SystemProcesses(const SystemProcesses& rhs); // no impl
272
SystemProcesses& operator=(const SystemProcesses& rhs); // no impl
273
~SystemProcesses();
274
275
//information about system processes
276
int system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const;
277
};
278
279
SystemProcessInterface::SystemProcesses::SystemProcesses() {
280
}
281
282
bool SystemProcessInterface::SystemProcesses::initialize() {
283
return true;
284
}
285
286
SystemProcessInterface::SystemProcesses::~SystemProcesses() {
287
}
288
int SystemProcessInterface::SystemProcesses::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const {
289
assert(system_processes != NULL, "system_processes pointer is NULL!");
290
assert(no_of_sys_processes != NULL, "system_processes counter pointer is NULL!");
291
#ifdef __APPLE__
292
pid_t* pids = NULL;
293
int pid_count = 0;
294
ResourceMark rm;
295
296
int try_count = 0;
297
while (pids == NULL) {
298
// Find out buffer size
299
size_t pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
300
if (pids_bytes <= 0) {
301
return OS_ERR;
302
}
303
pid_count = pids_bytes / sizeof(pid_t);
304
pids = NEW_RESOURCE_ARRAY(pid_t, pid_count);
305
memset(pids, 0, pids_bytes);
306
307
pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, pids, pids_bytes);
308
if (pids_bytes <= 0) {
309
// couldn't fit buffer, retry.
310
FREE_RESOURCE_ARRAY(pid_t, pids, pid_count);
311
pids = NULL;
312
try_count++;
313
if (try_count > 3) {
314
return OS_ERR;
315
}
316
} else {
317
pid_count = pids_bytes / sizeof(pid_t);
318
}
319
}
320
321
int process_count = 0;
322
SystemProcess* next = NULL;
323
for (int i = 0; i < pid_count; i++) {
324
pid_t pid = pids[i];
325
if (pid != 0) {
326
char buffer[PROC_PIDPATHINFO_MAXSIZE];
327
memset(buffer, 0 , sizeof(buffer));
328
if (proc_pidpath(pid, buffer, sizeof(buffer)) != -1) {
329
int length = strlen(buffer);
330
if (length > 0) {
331
SystemProcess* current = new SystemProcess();
332
char * path = NEW_C_HEAP_ARRAY(char, length + 1, mtInternal);
333
strcpy(path, buffer);
334
current->set_path(path);
335
current->set_pid((int)pid);
336
current->set_next(next);
337
next = current;
338
process_count++;
339
}
340
}
341
}
342
}
343
344
*no_of_sys_processes = process_count;
345
*system_processes = next;
346
347
return OS_OK;
348
#endif
349
return FUNCTIONALITY_NOT_IMPLEMENTED;
350
}
351
352
int SystemProcessInterface::system_processes(SystemProcess** system_procs, int* no_of_sys_processes) const {
353
return _impl->system_processes(system_procs, no_of_sys_processes);
354
}
355
356
SystemProcessInterface::SystemProcessInterface() {
357
_impl = NULL;
358
}
359
360
bool SystemProcessInterface::initialize() {
361
_impl = new SystemProcessInterface::SystemProcesses();
362
return _impl != NULL && _impl->initialize();
363
}
364
365
SystemProcessInterface::~SystemProcessInterface() {
366
if (_impl != NULL) {
367
delete _impl;
368
}
369
}
370
371
CPUInformationInterface::CPUInformationInterface() {
372
_cpu_info = NULL;
373
}
374
375
bool CPUInformationInterface::initialize() {
376
_cpu_info = new CPUInformation();
377
378
if (NULL == _cpu_info) {
379
return false;
380
}
381
_cpu_info->set_number_of_hardware_threads(VM_Version_Ext::number_of_threads());
382
_cpu_info->set_number_of_cores(VM_Version_Ext::number_of_cores());
383
_cpu_info->set_number_of_sockets(VM_Version_Ext::number_of_sockets());
384
_cpu_info->set_cpu_name(VM_Version_Ext::cpu_name());
385
_cpu_info->set_cpu_description(VM_Version_Ext::cpu_description());
386
387
return true;
388
}
389
390
CPUInformationInterface::~CPUInformationInterface() {
391
if (_cpu_info != NULL) {
392
if (_cpu_info->cpu_name() != NULL) {
393
const char* cpu_name = _cpu_info->cpu_name();
394
FREE_C_HEAP_ARRAY(char, cpu_name, mtInternal);
395
_cpu_info->set_cpu_name(NULL);
396
}
397
if (_cpu_info->cpu_description() != NULL) {
398
const char* cpu_desc = _cpu_info->cpu_description();
399
FREE_C_HEAP_ARRAY(char, cpu_desc, mtInternal);
400
_cpu_info->set_cpu_description(NULL);
401
}
402
delete _cpu_info;
403
}
404
}
405
406
int CPUInformationInterface::cpu_information(CPUInformation& cpu_info) {
407
if (NULL == _cpu_info) {
408
return OS_ERR;
409
}
410
411
cpu_info = *_cpu_info; // shallow copy assignment
412
return OS_OK;
413
}
414
415
class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> {
416
friend class NetworkPerformanceInterface;
417
private:
418
NetworkPerformance();
419
NetworkPerformance(const NetworkPerformance& rhs); // no impl
420
NetworkPerformance& operator=(const NetworkPerformance& rhs); // no impl
421
bool initialize();
422
~NetworkPerformance();
423
int network_utilization(NetworkInterface** network_interfaces) const;
424
};
425
426
NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() {
427
}
428
429
bool NetworkPerformanceInterface::NetworkPerformance::initialize() {
430
return true;
431
}
432
433
NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() {
434
}
435
436
int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const {
437
size_t len;
438
int mib[] = {CTL_NET, PF_ROUTE, /* protocol number */ 0, /* address family */ 0, NET_RT_IFLIST2, /* NET_RT_FLAGS mask*/ 0};
439
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &len, NULL, 0) != 0) {
440
return OS_ERR;
441
}
442
uint8_t* buf = NEW_RESOURCE_ARRAY(uint8_t, len);
443
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &len, NULL, 0) != 0) {
444
return OS_ERR;
445
}
446
447
size_t index = 0;
448
NetworkInterface* ret = NULL;
449
while (index < len) {
450
if_msghdr* msghdr = reinterpret_cast<if_msghdr*>(buf + index);
451
index += msghdr->ifm_msglen;
452
453
if (msghdr->ifm_type != RTM_IFINFO2) {
454
continue;
455
}
456
457
if_msghdr2* msghdr2 = reinterpret_cast<if_msghdr2*>(msghdr);
458
sockaddr_dl* sockaddr = reinterpret_cast<sockaddr_dl*>(msghdr2 + 1);
459
460
// The interface name is not necessarily NUL-terminated
461
char name_buf[128];
462
size_t name_len = MIN2(sizeof(name_buf) - 1, static_cast<size_t>(sockaddr->sdl_nlen));
463
strncpy(name_buf, sockaddr->sdl_data, name_len);
464
name_buf[name_len] = '\0';
465
466
uint64_t bytes_in = msghdr2->ifm_data.ifi_ibytes;
467
uint64_t bytes_out = msghdr2->ifm_data.ifi_obytes;
468
469
NetworkInterface* cur = new NetworkInterface(name_buf, bytes_in, bytes_out, ret);
470
ret = cur;
471
}
472
473
*network_interfaces = ret;
474
475
return OS_OK;
476
}
477
478
NetworkPerformanceInterface::NetworkPerformanceInterface() {
479
_impl = NULL;
480
}
481
482
NetworkPerformanceInterface::~NetworkPerformanceInterface() {
483
if (_impl != NULL) {
484
delete _impl;
485
}
486
}
487
488
bool NetworkPerformanceInterface::initialize() {
489
_impl = new NetworkPerformanceInterface::NetworkPerformance();
490
return _impl != NULL && _impl->initialize();
491
}
492
493
int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const {
494
return _impl->network_utilization(network_interfaces);
495
}
496
497