2
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
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 "runtime/vm_version.hpp"
30
#include "utilities/globalDefinitions.hpp"
35
#include <sys/sysctl.h>
36
#include <mach/mach.h>
37
#include <mach/task_info.h>
38
#include <sys/socket.h>
40
#include <net/if_dl.h>
41
#include <net/route.h>
42
#include <sys/times.h>
45
static const double NANOS_PER_SEC = 1000000000.0;
47
class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
48
friend class CPUPerformanceInterface;
52
uint64_t _total_csr_nanos;
55
long _jvm_context_switches;
58
int _active_processor_count;
60
bool now_in_nanos(uint64_t* resultp) {
62
int status = clock_gettime(CLOCK_REALTIME, &tp);
63
assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
67
*resultp = tp.tv_sec * NANOS_PER_SEC + tp.tv_nsec;
72
double normalize(double value) {
73
return MIN2<double>(MAX2<double>(value, 0.0), 1.0);
75
int cpu_load(int which_logical_cpu, double* cpu_load);
76
int context_switch_rate(double* rate);
77
int cpu_load_total_process(double* cpu_load);
78
int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);
80
NONCOPYABLE(CPUPerformance);
88
CPUPerformanceInterface::CPUPerformance::CPUPerformance() {
92
_jvm_context_switches = 0;
97
_active_processor_count = 0;
101
bool CPUPerformanceInterface::CPUPerformance::initialize() {
105
CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {
108
int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {
109
return FUNCTIONALITY_NOT_IMPLEMENTED;
112
int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {
114
host_name_port_t host = mach_host_self();
115
host_flavor_t flavor = HOST_CPU_LOAD_INFO;
116
mach_msg_type_number_t host_info_count = HOST_CPU_LOAD_INFO_COUNT;
117
host_cpu_load_info_data_t cpu_load_info;
119
kern_return_t kr = host_statistics(host, flavor, (host_info_t)&cpu_load_info, &host_info_count);
120
if (kr != KERN_SUCCESS) {
124
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];
125
long total_ticks = used_ticks + cpu_load_info.cpu_ticks[CPU_STATE_IDLE];
127
if (_used_ticks == 0 || _total_ticks == 0) {
128
// First call, just set the values
129
_used_ticks = used_ticks;
130
_total_ticks = total_ticks;
134
long used_delta = used_ticks - _used_ticks;
135
long total_delta = total_ticks - _total_ticks;
137
_used_ticks = used_ticks;
138
_total_ticks = total_ticks;
140
if (total_delta == 0) {
141
// Avoid division by zero
145
*cpu_load = (double)used_delta / total_delta;
149
return FUNCTIONALITY_NOT_IMPLEMENTED;
153
int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {
155
int result = cpu_load_total_process(psystemTotalLoad);
158
clock_t jvm_real = times(&buf);
159
if (jvm_real == (clock_t) (-1)) {
163
int active_processor_count = os::active_processor_count();
164
uint64_t jvm_user = buf.tms_utime;
165
uint64_t jvm_system = buf.tms_stime;
167
if (active_processor_count != _active_processor_count) {
168
// Change in active processor count
171
uint64_t delta = active_processor_count * (jvm_real - _jvm_real);
173
// Avoid division by zero
177
*pjvmUserLoad = normalize((double)(jvm_user - _jvm_user) / delta);
178
*pjvmKernelLoad = normalize((double)(jvm_system - _jvm_system) / delta);
181
_active_processor_count = active_processor_count;
182
_jvm_real = jvm_real;
183
_jvm_user = jvm_user;
184
_jvm_system = jvm_system;
188
return FUNCTIONALITY_NOT_IMPLEMENTED;
192
int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {
194
mach_port_t task = mach_task_self();
195
mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
196
task_info_data_t task_info_data;
197
kern_return_t kr = task_info(task, TASK_EVENTS_INFO, (task_info_t)task_info_data, &task_info_count);
198
if (kr != KERN_SUCCESS) {
203
if (_total_csr_nanos == 0 || _jvm_context_switches == 0) {
204
// First call just set initial values.
208
long jvm_context_switches = ((task_events_info_t)task_info_data)->csw;
210
uint64_t total_csr_nanos;
211
if(!now_in_nanos(&total_csr_nanos)) {
214
double delta_in_sec = (double)(total_csr_nanos - _total_csr_nanos) / NANOS_PER_SEC;
215
if (delta_in_sec == 0.0) {
216
// Avoid division by zero
220
*rate = (jvm_context_switches - _jvm_context_switches) / delta_in_sec;
222
_jvm_context_switches = jvm_context_switches;
223
_total_csr_nanos = total_csr_nanos;
227
return FUNCTIONALITY_NOT_IMPLEMENTED;
231
CPUPerformanceInterface::CPUPerformanceInterface() {
235
bool CPUPerformanceInterface::initialize() {
236
_impl = new CPUPerformanceInterface::CPUPerformance();
237
return _impl->initialize();
240
CPUPerformanceInterface::~CPUPerformanceInterface() {
241
if (_impl != nullptr) {
246
int CPUPerformanceInterface::cpu_load(int which_logical_cpu, double* cpu_load) const {
247
return _impl->cpu_load(which_logical_cpu, cpu_load);
250
int CPUPerformanceInterface::cpu_load_total_process(double* cpu_load) const {
251
return _impl->cpu_load_total_process(cpu_load);
254
int CPUPerformanceInterface::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) const {
255
return _impl->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotalLoad);
258
int CPUPerformanceInterface::context_switch_rate(double* rate) const {
259
return _impl->context_switch_rate(rate);
262
class SystemProcessInterface::SystemProcesses : public CHeapObj<mtInternal> {
263
friend class SystemProcessInterface;
267
NONCOPYABLE(SystemProcesses);
270
//information about system processes
271
int system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const;
274
SystemProcessInterface::SystemProcesses::SystemProcesses() {
277
bool SystemProcessInterface::SystemProcesses::initialize() {
281
SystemProcessInterface::SystemProcesses::~SystemProcesses() {
283
int SystemProcessInterface::SystemProcesses::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const {
284
assert(system_processes != nullptr, "system_processes pointer is null!");
285
assert(no_of_sys_processes != nullptr, "system_processes counter pointer is null!");
287
pid_t* pids = nullptr;
292
while (pids == nullptr) {
293
// Find out buffer size
294
size_t pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, nullptr, 0);
295
if (pids_bytes <= 0) {
298
pid_count = pids_bytes / sizeof(pid_t);
299
pids = NEW_RESOURCE_ARRAY(pid_t, pid_count);
300
memset(pids, 0, pids_bytes);
302
pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, pids, pids_bytes);
303
if (pids_bytes <= 0) {
304
// couldn't fit buffer, retry.
305
FREE_RESOURCE_ARRAY(pid_t, pids, pid_count);
312
pid_count = pids_bytes / sizeof(pid_t);
316
int process_count = 0;
317
SystemProcess* next = nullptr;
318
for (int i = 0; i < pid_count; i++) {
321
char buffer[PROC_PIDPATHINFO_MAXSIZE];
322
memset(buffer, 0 , sizeof(buffer));
323
if (proc_pidpath(pid, buffer, sizeof(buffer)) != -1) {
324
int length = strlen(buffer);
326
SystemProcess* current = new SystemProcess();
327
char * path = NEW_C_HEAP_ARRAY(char, length + 1, mtInternal);
328
strcpy(path, buffer);
329
current->set_path(path);
330
current->set_pid((int)pid);
331
current->set_next(next);
339
*no_of_sys_processes = process_count;
340
*system_processes = next;
344
return FUNCTIONALITY_NOT_IMPLEMENTED;
347
int SystemProcessInterface::system_processes(SystemProcess** system_procs, int* no_of_sys_processes) const {
348
return _impl->system_processes(system_procs, no_of_sys_processes);
351
SystemProcessInterface::SystemProcessInterface() {
355
bool SystemProcessInterface::initialize() {
356
_impl = new SystemProcessInterface::SystemProcesses();
357
return _impl->initialize();
360
SystemProcessInterface::~SystemProcessInterface() {
361
if (_impl != nullptr) {
366
CPUInformationInterface::CPUInformationInterface() {
370
bool CPUInformationInterface::initialize() {
371
_cpu_info = new CPUInformation();
372
VM_Version::initialize_cpu_information();
373
_cpu_info->set_number_of_hardware_threads(VM_Version::number_of_threads());
374
_cpu_info->set_number_of_cores(VM_Version::number_of_cores());
375
_cpu_info->set_number_of_sockets(VM_Version::number_of_sockets());
376
_cpu_info->set_cpu_name(VM_Version::cpu_name());
377
_cpu_info->set_cpu_description(VM_Version::cpu_description());
381
CPUInformationInterface::~CPUInformationInterface() {
382
if (_cpu_info != nullptr) {
383
if (_cpu_info->cpu_name() != nullptr) {
384
const char* cpu_name = _cpu_info->cpu_name();
385
FREE_C_HEAP_ARRAY(char, cpu_name);
386
_cpu_info->set_cpu_name(nullptr);
388
if (_cpu_info->cpu_description() != nullptr) {
389
const char* cpu_desc = _cpu_info->cpu_description();
390
FREE_C_HEAP_ARRAY(char, cpu_desc);
391
_cpu_info->set_cpu_description(nullptr);
397
int CPUInformationInterface::cpu_information(CPUInformation& cpu_info) {
398
if (nullptr == _cpu_info) {
402
cpu_info = *_cpu_info; // shallow copy assignment
406
class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> {
407
friend class NetworkPerformanceInterface;
409
NetworkPerformance();
410
NONCOPYABLE(NetworkPerformance);
412
~NetworkPerformance();
413
int network_utilization(NetworkInterface** network_interfaces) const;
416
NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() {
419
bool NetworkPerformanceInterface::NetworkPerformance::initialize() {
423
NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() {
426
int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const {
428
int mib[] = {CTL_NET, PF_ROUTE, /* protocol number */ 0, /* address family */ 0, NET_RT_IFLIST2, /* NET_RT_FLAGS mask*/ 0};
429
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), nullptr, &len, nullptr, 0) != 0) {
432
uint8_t* buf = NEW_RESOURCE_ARRAY(uint8_t, len);
433
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &len, nullptr, 0) != 0) {
438
NetworkInterface* ret = nullptr;
439
while (index < len) {
440
if_msghdr* msghdr = reinterpret_cast<if_msghdr*>(buf + index);
441
index += msghdr->ifm_msglen;
443
if (msghdr->ifm_type != RTM_IFINFO2) {
447
if_msghdr2* msghdr2 = reinterpret_cast<if_msghdr2*>(msghdr);
448
sockaddr_dl* sockaddr = reinterpret_cast<sockaddr_dl*>(msghdr2 + 1);
450
// The interface name is not necessarily NUL-terminated
452
size_t name_len = MIN2(sizeof(name_buf) - 1, static_cast<size_t>(sockaddr->sdl_nlen));
453
strncpy(name_buf, sockaddr->sdl_data, name_len);
454
name_buf[name_len] = '\0';
456
uint64_t bytes_in = msghdr2->ifm_data.ifi_ibytes;
457
uint64_t bytes_out = msghdr2->ifm_data.ifi_obytes;
459
NetworkInterface* cur = new NetworkInterface(name_buf, bytes_in, bytes_out, ret);
463
*network_interfaces = ret;
468
NetworkPerformanceInterface::NetworkPerformanceInterface() {
472
NetworkPerformanceInterface::~NetworkPerformanceInterface() {
473
if (_impl != nullptr) {
478
bool NetworkPerformanceInterface::initialize() {
479
_impl = new NetworkPerformanceInterface::NetworkPerformance();
480
return _impl->initialize();
483
int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const {
484
return _impl->network_utilization(network_interfaces);