Path: blob/master/src/hotspot/os/bsd/os_perf_bsd.cpp
40949 views
/*1* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23#include "precompiled.hpp"24#include "memory/allocation.inline.hpp"25#include "memory/resourceArea.hpp"26#include "runtime/os.hpp"27#include "runtime/os_perf.hpp"28#include "utilities/globalDefinitions.hpp"29#include CPU_HEADER(vm_version_ext)3031#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#include <sys/socket.h>38#include <net/if.h>39#include <net/if_dl.h>40#include <net/route.h>41#endif4243static const double NANOS_PER_SEC = 1000000000.0;4445class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {46friend class CPUPerformanceInterface;47private:48long _total_cpu_nanos;49long _total_csr_nanos;50long _jvm_user_nanos;51long _jvm_system_nanos;52long _jvm_context_switches;53long _used_ticks;54long _total_ticks;55int _active_processor_count;5657bool now_in_nanos(long* resultp) {58struct timespec tp;59int status = clock_gettime(CLOCK_REALTIME, &tp);60assert(status == 0, "clock_gettime error: %s", os::strerror(errno));61if (status != 0) {62return false;63}64*resultp = tp.tv_sec * NANOS_PER_SEC + tp.tv_nsec;65return true;66}6768double normalize(double value) {69return MIN2<double>(MAX2<double>(value, 0.0), 1.0);70}71int cpu_load(int which_logical_cpu, double* cpu_load);72int context_switch_rate(double* rate);73int cpu_load_total_process(double* cpu_load);74int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);7576NONCOPYABLE(CPUPerformance);7778public:79CPUPerformance();80bool initialize();81~CPUPerformance();82};8384CPUPerformanceInterface::CPUPerformance::CPUPerformance() {85_total_cpu_nanos= 0;86_total_csr_nanos= 0;87_jvm_context_switches = 0;88_jvm_user_nanos = 0;89_jvm_system_nanos = 0;90_used_ticks = 0;91_total_ticks = 0;92_active_processor_count = 0;93}9495bool CPUPerformanceInterface::CPUPerformance::initialize() {96return true;97}9899CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {100}101102int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {103return FUNCTIONALITY_NOT_IMPLEMENTED;104}105106int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {107#ifdef __APPLE__108host_name_port_t host = mach_host_self();109host_flavor_t flavor = HOST_CPU_LOAD_INFO;110mach_msg_type_number_t host_info_count = HOST_CPU_LOAD_INFO_COUNT;111host_cpu_load_info_data_t cpu_load_info;112113kern_return_t kr = host_statistics(host, flavor, (host_info_t)&cpu_load_info, &host_info_count);114if (kr != KERN_SUCCESS) {115return OS_ERR;116}117118long 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];119long total_ticks = used_ticks + cpu_load_info.cpu_ticks[CPU_STATE_IDLE];120121if (_used_ticks == 0 || _total_ticks == 0) {122// First call, just set the values123_used_ticks = used_ticks;124_total_ticks = total_ticks;125return OS_ERR;126}127128long used_delta = used_ticks - _used_ticks;129long total_delta = total_ticks - _total_ticks;130131_used_ticks = used_ticks;132_total_ticks = total_ticks;133134if (total_delta == 0) {135// Avoid division by zero136return OS_ERR;137}138139*cpu_load = (double)used_delta / total_delta;140141return OS_OK;142#else143return FUNCTIONALITY_NOT_IMPLEMENTED;144#endif145}146147int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {148#ifdef __APPLE__149int result = cpu_load_total_process(psystemTotalLoad);150mach_port_t task = mach_task_self();151mach_msg_type_number_t task_info_count = TASK_INFO_MAX;152task_info_data_t task_info_data;153kern_return_t kr = task_info(task, TASK_ABSOLUTETIME_INFO, (task_info_t)task_info_data, &task_info_count);154if (kr != KERN_SUCCESS) {155return OS_ERR;156}157task_absolutetime_info_t absolutetime_info = (task_absolutetime_info_t)task_info_data;158159int active_processor_count = os::active_processor_count();160long jvm_user_nanos = absolutetime_info->total_user;161long jvm_system_nanos = absolutetime_info->total_system;162163long total_cpu_nanos;164if(!now_in_nanos(&total_cpu_nanos)) {165return OS_ERR;166}167168if (_total_cpu_nanos == 0 || active_processor_count != _active_processor_count) {169// First call or change in active processor count170result = OS_ERR;171}172173long delta_nanos = active_processor_count * (total_cpu_nanos - _total_cpu_nanos);174if (delta_nanos == 0) {175// Avoid division by zero176return OS_ERR;177}178179*pjvmUserLoad = normalize((double)(jvm_user_nanos - _jvm_user_nanos)/delta_nanos);180*pjvmKernelLoad = normalize((double)(jvm_system_nanos - _jvm_system_nanos)/delta_nanos);181182_active_processor_count = active_processor_count;183_total_cpu_nanos = total_cpu_nanos;184_jvm_user_nanos = jvm_user_nanos;185_jvm_system_nanos = jvm_system_nanos;186187return result;188#else189return FUNCTIONALITY_NOT_IMPLEMENTED;190#endif191}192193int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {194#ifdef __APPLE__195mach_port_t task = mach_task_self();196mach_msg_type_number_t task_info_count = TASK_INFO_MAX;197task_info_data_t task_info_data;198kern_return_t kr = task_info(task, TASK_EVENTS_INFO, (task_info_t)task_info_data, &task_info_count);199if (kr != KERN_SUCCESS) {200return OS_ERR;201}202203int result = OS_OK;204if (_total_csr_nanos == 0 || _jvm_context_switches == 0) {205// First call just set initial values.206result = OS_ERR;207}208209long jvm_context_switches = ((task_events_info_t)task_info_data)->csw;210211long total_csr_nanos;212if(!now_in_nanos(&total_csr_nanos)) {213return OS_ERR;214}215double delta_in_sec = (double)(total_csr_nanos - _total_csr_nanos) / NANOS_PER_SEC;216if (delta_in_sec == 0.0) {217// Avoid division by zero218return OS_ERR;219}220221*rate = (jvm_context_switches - _jvm_context_switches) / delta_in_sec;222223_jvm_context_switches = jvm_context_switches;224_total_csr_nanos = total_csr_nanos;225226return result;227#else228return FUNCTIONALITY_NOT_IMPLEMENTED;229#endif230}231232CPUPerformanceInterface::CPUPerformanceInterface() {233_impl = NULL;234}235236bool CPUPerformanceInterface::initialize() {237_impl = new CPUPerformanceInterface::CPUPerformance();238return _impl->initialize();239}240241CPUPerformanceInterface::~CPUPerformanceInterface() {242if (_impl != NULL) {243delete _impl;244}245}246247int CPUPerformanceInterface::cpu_load(int which_logical_cpu, double* cpu_load) const {248return _impl->cpu_load(which_logical_cpu, cpu_load);249}250251int CPUPerformanceInterface::cpu_load_total_process(double* cpu_load) const {252return _impl->cpu_load_total_process(cpu_load);253}254255int CPUPerformanceInterface::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) const {256return _impl->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotalLoad);257}258259int CPUPerformanceInterface::context_switch_rate(double* rate) const {260return _impl->context_switch_rate(rate);261}262263class SystemProcessInterface::SystemProcesses : public CHeapObj<mtInternal> {264friend class SystemProcessInterface;265private:266SystemProcesses();267bool initialize();268NONCOPYABLE(SystemProcesses);269~SystemProcesses();270271//information about system processes272int system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const;273};274275SystemProcessInterface::SystemProcesses::SystemProcesses() {276}277278bool SystemProcessInterface::SystemProcesses::initialize() {279return true;280}281282SystemProcessInterface::SystemProcesses::~SystemProcesses() {283}284int SystemProcessInterface::SystemProcesses::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const {285assert(system_processes != NULL, "system_processes pointer is NULL!");286assert(no_of_sys_processes != NULL, "system_processes counter pointer is NULL!");287#ifdef __APPLE__288pid_t* pids = NULL;289int pid_count = 0;290ResourceMark rm;291292int try_count = 0;293while (pids == NULL) {294// Find out buffer size295size_t pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);296if (pids_bytes <= 0) {297return OS_ERR;298}299pid_count = pids_bytes / sizeof(pid_t);300pids = NEW_RESOURCE_ARRAY(pid_t, pid_count);301memset(pids, 0, pids_bytes);302303pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, pids, pids_bytes);304if (pids_bytes <= 0) {305// couldn't fit buffer, retry.306FREE_RESOURCE_ARRAY(pid_t, pids, pid_count);307pids = NULL;308try_count++;309if (try_count > 3) {310return OS_ERR;311}312} else {313pid_count = pids_bytes / sizeof(pid_t);314}315}316317int process_count = 0;318SystemProcess* next = NULL;319for (int i = 0; i < pid_count; i++) {320pid_t pid = pids[i];321if (pid != 0) {322char buffer[PROC_PIDPATHINFO_MAXSIZE];323memset(buffer, 0 , sizeof(buffer));324if (proc_pidpath(pid, buffer, sizeof(buffer)) != -1) {325int length = strlen(buffer);326if (length > 0) {327SystemProcess* current = new SystemProcess();328char * path = NEW_C_HEAP_ARRAY(char, length + 1, mtInternal);329strcpy(path, buffer);330current->set_path(path);331current->set_pid((int)pid);332current->set_next(next);333next = current;334process_count++;335}336}337}338}339340*no_of_sys_processes = process_count;341*system_processes = next;342343return OS_OK;344#endif345return FUNCTIONALITY_NOT_IMPLEMENTED;346}347348int SystemProcessInterface::system_processes(SystemProcess** system_procs, int* no_of_sys_processes) const {349return _impl->system_processes(system_procs, no_of_sys_processes);350}351352SystemProcessInterface::SystemProcessInterface() {353_impl = NULL;354}355356bool SystemProcessInterface::initialize() {357_impl = new SystemProcessInterface::SystemProcesses();358return _impl->initialize();359}360361SystemProcessInterface::~SystemProcessInterface() {362if (_impl != NULL) {363delete _impl;364}365}366367CPUInformationInterface::CPUInformationInterface() {368_cpu_info = NULL;369}370371bool CPUInformationInterface::initialize() {372_cpu_info = new CPUInformation();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());378return true;379}380381CPUInformationInterface::~CPUInformationInterface() {382if (_cpu_info != NULL) {383if (_cpu_info->cpu_name() != NULL) {384const char* cpu_name = _cpu_info->cpu_name();385FREE_C_HEAP_ARRAY(char, cpu_name);386_cpu_info->set_cpu_name(NULL);387}388if (_cpu_info->cpu_description() != NULL) {389const char* cpu_desc = _cpu_info->cpu_description();390FREE_C_HEAP_ARRAY(char, cpu_desc);391_cpu_info->set_cpu_description(NULL);392}393delete _cpu_info;394}395}396397int CPUInformationInterface::cpu_information(CPUInformation& cpu_info) {398if (NULL == _cpu_info) {399return OS_ERR;400}401402cpu_info = *_cpu_info; // shallow copy assignment403return OS_OK;404}405406class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> {407friend class NetworkPerformanceInterface;408private:409NetworkPerformance();410NONCOPYABLE(NetworkPerformance);411bool initialize();412~NetworkPerformance();413int network_utilization(NetworkInterface** network_interfaces) const;414};415416NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() {417}418419bool NetworkPerformanceInterface::NetworkPerformance::initialize() {420return true;421}422423NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() {424}425426int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const {427size_t len;428int mib[] = {CTL_NET, PF_ROUTE, /* protocol number */ 0, /* address family */ 0, NET_RT_IFLIST2, /* NET_RT_FLAGS mask*/ 0};429if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &len, NULL, 0) != 0) {430return OS_ERR;431}432uint8_t* buf = NEW_RESOURCE_ARRAY(uint8_t, len);433if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &len, NULL, 0) != 0) {434return OS_ERR;435}436437size_t index = 0;438NetworkInterface* ret = NULL;439while (index < len) {440if_msghdr* msghdr = reinterpret_cast<if_msghdr*>(buf + index);441index += msghdr->ifm_msglen;442443if (msghdr->ifm_type != RTM_IFINFO2) {444continue;445}446447if_msghdr2* msghdr2 = reinterpret_cast<if_msghdr2*>(msghdr);448sockaddr_dl* sockaddr = reinterpret_cast<sockaddr_dl*>(msghdr2 + 1);449450// The interface name is not necessarily NUL-terminated451char name_buf[128];452size_t name_len = MIN2(sizeof(name_buf) - 1, static_cast<size_t>(sockaddr->sdl_nlen));453strncpy(name_buf, sockaddr->sdl_data, name_len);454name_buf[name_len] = '\0';455456uint64_t bytes_in = msghdr2->ifm_data.ifi_ibytes;457uint64_t bytes_out = msghdr2->ifm_data.ifi_obytes;458459NetworkInterface* cur = new NetworkInterface(name_buf, bytes_in, bytes_out, ret);460ret = cur;461}462463*network_interfaces = ret;464465return OS_OK;466}467468NetworkPerformanceInterface::NetworkPerformanceInterface() {469_impl = NULL;470}471472NetworkPerformanceInterface::~NetworkPerformanceInterface() {473if (_impl != NULL) {474delete _impl;475}476}477478bool NetworkPerformanceInterface::initialize() {479_impl = new NetworkPerformanceInterface::NetworkPerformance();480return _impl->initialize();481}482483int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const {484return _impl->network_utilization(network_interfaces);485}486487488