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