Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/os/bsd/vm/os_perf_bsd.cpp
48785 views
/*1* Copyright (c) 2012, 2018, 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 "vm_version_ext_x86.hpp"2930#ifdef __APPLE__31#import <libproc.h>32#include <sys/time.h>33#include <sys/sysctl.h>34#include <mach/mach.h>35#include <mach/task_info.h>36#include <sys/socket.h>37#include <net/if.h>38#include <net/if_dl.h>39#include <net/route.h>40#endif4142static const double NANOS_PER_SEC = 1000000000.0;4344class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {45friend class CPUPerformanceInterface;46private:47long _total_cpu_nanos;48long _total_csr_nanos;49long _jvm_user_nanos;50long _jvm_system_nanos;51long _jvm_context_switches;52long _used_ticks;53long _total_ticks;54int _active_processor_count;5556bool now_in_nanos(long* resultp) {57timeval current_time;58if (gettimeofday(¤t_time, NULL) != 0) {59// Error getting current time60return false;61}62*resultp = (long)(current_time.tv_sec * NANOS_PER_SEC + 1000L * current_time.tv_usec);63return true;64}6566double normalize(double value) {67return MIN2<double>(MAX2<double>(value, 0.0), 1.0);68}69int cpu_load(int which_logical_cpu, double* cpu_load);70int context_switch_rate(double* rate);71int cpu_load_total_process(double* cpu_load);72int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);7374CPUPerformance(const CPUPerformance& rhs); // no impl75CPUPerformance& operator=(const CPUPerformance& rhs); // no impl76public:77CPUPerformance();78bool initialize();79~CPUPerformance();80};8182CPUPerformanceInterface::CPUPerformance::CPUPerformance() {83_total_cpu_nanos= 0;84_total_csr_nanos= 0;85_jvm_context_switches = 0;86_jvm_user_nanos = 0;87_jvm_system_nanos = 0;88_used_ticks = 0;89_total_ticks = 0;90_active_processor_count = 0;91}9293bool CPUPerformanceInterface::CPUPerformance::initialize() {94return true;95}9697CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {98}99100int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {101return FUNCTIONALITY_NOT_IMPLEMENTED;102}103104int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {105#ifdef __APPLE__106host_name_port_t host = mach_host_self();107host_flavor_t flavor = HOST_CPU_LOAD_INFO;108mach_msg_type_number_t host_info_count = HOST_CPU_LOAD_INFO_COUNT;109host_cpu_load_info_data_t cpu_load_info;110111kern_return_t kr = host_statistics(host, flavor, (host_info_t)&cpu_load_info, &host_info_count);112if (kr != KERN_SUCCESS) {113return OS_ERR;114}115116long 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];117long total_ticks = used_ticks + cpu_load_info.cpu_ticks[CPU_STATE_IDLE];118119if (_used_ticks == 0 || _total_ticks == 0) {120// First call, just set the values121_used_ticks = used_ticks;122_total_ticks = total_ticks;123return OS_ERR;124}125126long used_delta = used_ticks - _used_ticks;127long total_delta = total_ticks - _total_ticks;128129_used_ticks = used_ticks;130_total_ticks = total_ticks;131132if (total_delta == 0) {133// Avoid division by zero134return OS_ERR;135}136137*cpu_load = (double)used_delta / total_delta;138139return OS_OK;140#else141return FUNCTIONALITY_NOT_IMPLEMENTED;142#endif143}144145int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {146#ifdef __APPLE__147int result = cpu_load_total_process(psystemTotalLoad);148mach_port_t task = mach_task_self();149mach_msg_type_number_t task_info_count = TASK_INFO_MAX;150task_info_data_t task_info_data;151kern_return_t kr = task_info(task, TASK_ABSOLUTETIME_INFO, (task_info_t)task_info_data, &task_info_count);152if (kr != KERN_SUCCESS) {153return OS_ERR;154}155task_absolutetime_info_t absolutetime_info = (task_absolutetime_info_t)task_info_data;156157int active_processor_count = os::active_processor_count();158long jvm_user_nanos = absolutetime_info->total_user;159long jvm_system_nanos = absolutetime_info->total_system;160161long total_cpu_nanos;162if(!now_in_nanos(&total_cpu_nanos)) {163return OS_ERR;164}165166if (_total_cpu_nanos == 0 || active_processor_count != _active_processor_count) {167// First call or change in active processor count168result = OS_ERR;169}170171long delta_nanos = active_processor_count * (total_cpu_nanos - _total_cpu_nanos);172if (delta_nanos == 0) {173// Avoid division by zero174return OS_ERR;175}176177*pjvmUserLoad = normalize((double)(jvm_user_nanos - _jvm_user_nanos)/delta_nanos);178*pjvmKernelLoad = normalize((double)(jvm_system_nanos - _jvm_system_nanos)/delta_nanos);179180_active_processor_count = active_processor_count;181_total_cpu_nanos = total_cpu_nanos;182_jvm_user_nanos = jvm_user_nanos;183_jvm_system_nanos = jvm_system_nanos;184185return result;186#else187return FUNCTIONALITY_NOT_IMPLEMENTED;188#endif189}190191int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {192#ifdef __APPLE__193mach_port_t task = mach_task_self();194mach_msg_type_number_t task_info_count = TASK_INFO_MAX;195task_info_data_t task_info_data;196kern_return_t kr = task_info(task, TASK_EVENTS_INFO, (task_info_t)task_info_data, &task_info_count);197if (kr != KERN_SUCCESS) {198return OS_ERR;199}200201int result = OS_OK;202if (_total_csr_nanos == 0 || _jvm_context_switches == 0) {203// First call just set initial values.204result = OS_ERR;205}206207long jvm_context_switches = ((task_events_info_t)task_info_data)->csw;208209long total_csr_nanos;210if(!now_in_nanos(&total_csr_nanos)) {211return OS_ERR;212}213double delta_in_sec = (double)(total_csr_nanos - _total_csr_nanos) / NANOS_PER_SEC;214if (delta_in_sec == 0.0) {215// Avoid division by zero216return OS_ERR;217}218219*rate = (jvm_context_switches - _jvm_context_switches) / delta_in_sec;220221_jvm_context_switches = jvm_context_switches;222_total_csr_nanos = total_csr_nanos;223224return result;225#else226return FUNCTIONALITY_NOT_IMPLEMENTED;227#endif228}229230CPUPerformanceInterface::CPUPerformanceInterface() {231_impl = NULL;232}233234bool CPUPerformanceInterface::initialize() {235_impl = new CPUPerformanceInterface::CPUPerformance();236return _impl != NULL && _impl->initialize();237}238239CPUPerformanceInterface::~CPUPerformanceInterface() {240if (_impl != NULL) {241delete _impl;242}243}244245int CPUPerformanceInterface::cpu_load(int which_logical_cpu, double* cpu_load) const {246return _impl->cpu_load(which_logical_cpu, cpu_load);247}248249int CPUPerformanceInterface::cpu_load_total_process(double* cpu_load) const {250return _impl->cpu_load_total_process(cpu_load);251}252253int CPUPerformanceInterface::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) const {254return _impl->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotalLoad);255}256257int CPUPerformanceInterface::context_switch_rate(double* rate) const {258return _impl->context_switch_rate(rate);259}260261class SystemProcessInterface::SystemProcesses : public CHeapObj<mtInternal> {262friend class SystemProcessInterface;263private:264SystemProcesses();265bool initialize();266SystemProcesses(const SystemProcesses& rhs); // no impl267SystemProcesses& operator=(const SystemProcesses& rhs); // no impl268~SystemProcesses();269270//information about system processes271int system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const;272};273274SystemProcessInterface::SystemProcesses::SystemProcesses() {275}276277bool SystemProcessInterface::SystemProcesses::initialize() {278return true;279}280281SystemProcessInterface::SystemProcesses::~SystemProcesses() {282}283int SystemProcessInterface::SystemProcesses::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const {284assert(system_processes != NULL, "system_processes pointer is NULL!");285assert(no_of_sys_processes != NULL, "system_processes counter pointer is NULL!");286#ifdef __APPLE__287pid_t* pids = NULL;288int pid_count = 0;289ResourceMark rm;290291int try_count = 0;292while (pids == NULL) {293// Find out buffer size294size_t pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);295if (pids_bytes <= 0) {296return OS_ERR;297}298pid_count = pids_bytes / sizeof(pid_t);299pids = NEW_RESOURCE_ARRAY(pid_t, pid_count);300memset(pids, 0, pids_bytes);301302pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, pids, pids_bytes);303if (pids_bytes <= 0) {304// couldn't fit buffer, retry.305FREE_RESOURCE_ARRAY(pid_t, pids, pid_count);306pids = NULL;307try_count++;308if (try_count > 3) {309return OS_ERR;310}311} else {312pid_count = pids_bytes / sizeof(pid_t);313}314}315316int process_count = 0;317SystemProcess* next = NULL;318for (int i = 0; i < pid_count; i++) {319pid_t pid = pids[i];320if (pid != 0) {321char buffer[PROC_PIDPATHINFO_MAXSIZE];322memset(buffer, 0 , sizeof(buffer));323if (proc_pidpath(pid, buffer, sizeof(buffer)) != -1) {324int length = strlen(buffer);325if (length > 0) {326SystemProcess* current = new SystemProcess();327char * path = NEW_C_HEAP_ARRAY(char, length + 1, mtInternal);328strcpy(path, buffer);329current->set_path(path);330current->set_pid((int)pid);331current->set_next(next);332next = current;333process_count++;334}335}336}337}338339*no_of_sys_processes = process_count;340*system_processes = next;341342return OS_OK;343#endif344return FUNCTIONALITY_NOT_IMPLEMENTED;345}346347int SystemProcessInterface::system_processes(SystemProcess** system_procs, int* no_of_sys_processes) const {348return _impl->system_processes(system_procs, no_of_sys_processes);349}350351SystemProcessInterface::SystemProcessInterface() {352_impl = NULL;353}354355bool SystemProcessInterface::initialize() {356_impl = new SystemProcessInterface::SystemProcesses();357return _impl != NULL && _impl->initialize();358}359360SystemProcessInterface::~SystemProcessInterface() {361if (_impl != NULL) {362delete _impl;363}364}365366CPUInformationInterface::CPUInformationInterface() {367_cpu_info = NULL;368}369370bool CPUInformationInterface::initialize() {371_cpu_info = new CPUInformation();372373if (NULL == _cpu_info) {374return false;375}376_cpu_info->set_number_of_hardware_threads(VM_Version_Ext::number_of_threads());377_cpu_info->set_number_of_cores(VM_Version_Ext::number_of_cores());378_cpu_info->set_number_of_sockets(VM_Version_Ext::number_of_sockets());379_cpu_info->set_cpu_name(VM_Version_Ext::cpu_name());380_cpu_info->set_cpu_description(VM_Version_Ext::cpu_description());381382return true;383}384385CPUInformationInterface::~CPUInformationInterface() {386if (_cpu_info != NULL) {387if (_cpu_info->cpu_name() != NULL) {388const char* cpu_name = _cpu_info->cpu_name();389FREE_C_HEAP_ARRAY(char, cpu_name, mtInternal);390_cpu_info->set_cpu_name(NULL);391}392if (_cpu_info->cpu_description() != NULL) {393const char* cpu_desc = _cpu_info->cpu_description();394FREE_C_HEAP_ARRAY(char, cpu_desc, mtInternal);395_cpu_info->set_cpu_description(NULL);396}397delete _cpu_info;398}399}400401int CPUInformationInterface::cpu_information(CPUInformation& cpu_info) {402if (NULL == _cpu_info) {403return OS_ERR;404}405406cpu_info = *_cpu_info; // shallow copy assignment407return OS_OK;408}409410class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> {411friend class NetworkPerformanceInterface;412private:413NetworkPerformance();414NetworkPerformance(const NetworkPerformance& rhs); // no impl415NetworkPerformance& operator=(const NetworkPerformance& rhs); // no impl416bool initialize();417~NetworkPerformance();418int network_utilization(NetworkInterface** network_interfaces) const;419};420421NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() {422}423424bool NetworkPerformanceInterface::NetworkPerformance::initialize() {425return true;426}427428NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() {429}430431int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const {432size_t len;433int mib[] = {CTL_NET, PF_ROUTE, /* protocol number */ 0, /* address family */ 0, NET_RT_IFLIST2, /* NET_RT_FLAGS mask*/ 0};434if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &len, NULL, 0) != 0) {435return OS_ERR;436}437uint8_t* buf = NEW_RESOURCE_ARRAY(uint8_t, len);438if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &len, NULL, 0) != 0) {439return OS_ERR;440}441442size_t index = 0;443NetworkInterface* ret = NULL;444while (index < len) {445if_msghdr* msghdr = reinterpret_cast<if_msghdr*>(buf + index);446index += msghdr->ifm_msglen;447448if (msghdr->ifm_type != RTM_IFINFO2) {449continue;450}451452if_msghdr2* msghdr2 = reinterpret_cast<if_msghdr2*>(msghdr);453sockaddr_dl* sockaddr = reinterpret_cast<sockaddr_dl*>(msghdr2 + 1);454455// The interface name is not necessarily NUL-terminated456char name_buf[128];457size_t name_len = MIN2(sizeof(name_buf) - 1, static_cast<size_t>(sockaddr->sdl_nlen));458strncpy(name_buf, sockaddr->sdl_data, name_len);459name_buf[name_len] = '\0';460461uint64_t bytes_in = msghdr2->ifm_data.ifi_ibytes;462uint64_t bytes_out = msghdr2->ifm_data.ifi_obytes;463464NetworkInterface* cur = new NetworkInterface(name_buf, bytes_in, bytes_out, ret);465ret = cur;466}467468*network_interfaces = ret;469470return OS_OK;471}472473NetworkPerformanceInterface::NetworkPerformanceInterface() {474_impl = NULL;475}476477NetworkPerformanceInterface::~NetworkPerformanceInterface() {478if (_impl != NULL) {479delete _impl;480}481}482483bool NetworkPerformanceInterface::initialize() {484_impl = new NetworkPerformanceInterface::NetworkPerformance();485return _impl != NULL && _impl->initialize();486}487488int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const {489return _impl->network_utilization(network_interfaces);490}491492493