Path: blob/master/src/hotspot/share/jfr/periodic/jfrOSInterface.cpp
41152 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*/2324#include "precompiled.hpp"25#include "jfr/jfrEvents.hpp"26#include "jfr/periodic/jfrNetworkUtilization.hpp"27#include "jfr/periodic/jfrOSInterface.hpp"28#include "memory/allocation.inline.hpp"29#include "memory/resourceArea.hpp"30#include "runtime/os.hpp"31#include "runtime/os_perf.hpp"32#include "runtime/vm_version.hpp"33#include "utilities/ostream.hpp"3435#include <stdlib.h> // for environment variables3637static JfrOSInterface* _instance = NULL;3839JfrOSInterface& JfrOSInterface::instance() {40return *_instance;41}4243JfrOSInterface* JfrOSInterface::create() {44assert(_instance == NULL, "invariant");45_instance = new JfrOSInterface();46return _instance;47}4849void JfrOSInterface::destroy() {50JfrNetworkUtilization::destroy();51if (_instance != NULL) {52delete _instance;53_instance = NULL;54}55}5657class JfrOSInterface::JfrOSInterfaceImpl : public JfrCHeapObj {58friend class JfrOSInterface;59private:60CPUInformationInterface* _cpu_info_interface;61CPUPerformanceInterface* _cpu_perf_interface;62SystemProcessInterface* _system_process_interface;63NetworkPerformanceInterface* _network_performance_interface;6465CPUInformationInterface* cpu_info_interface();66CPUPerformanceInterface* cpu_perf_interface();67SystemProcessInterface* system_process_interface();68NetworkPerformanceInterface* network_performance_interface();6970JfrOSInterfaceImpl();71bool initialize();72~JfrOSInterfaceImpl();7374// cpu info75int cpu_information(CPUInformation& cpu_info);76int cpu_load(int which_logical_cpu, double* cpu_load);77int context_switch_rate(double* rate);78int cpu_load_total_process(double* cpu_load);79int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotal);8081// os information82int os_version(char** os_version) const;8384// environment information85void generate_environment_variables_events();8687// system processes information88int system_processes(SystemProcess** system_processes, int* no_of_sys_processes);8990int network_utilization(NetworkInterface** network_interfaces);91};9293JfrOSInterface::JfrOSInterfaceImpl::JfrOSInterfaceImpl() : _cpu_info_interface(NULL),94_cpu_perf_interface(NULL),95_system_process_interface(NULL),96_network_performance_interface(NULL) {}9798template <typename T>99static T* create_interface() {100ResourceMark rm;101T* iface = new T();102if (iface != NULL) {103if (!iface->initialize()) {104delete iface;105iface = NULL;106}107}108return iface;109}110111CPUInformationInterface* JfrOSInterface::JfrOSInterfaceImpl::cpu_info_interface() {112if (_cpu_info_interface == NULL) {113_cpu_info_interface = create_interface<CPUInformationInterface>();114}115return _cpu_info_interface;116}117118CPUPerformanceInterface* JfrOSInterface::JfrOSInterfaceImpl::cpu_perf_interface() {119if (_cpu_perf_interface == NULL) {120_cpu_perf_interface = create_interface<CPUPerformanceInterface>();121}122return _cpu_perf_interface;123}124125SystemProcessInterface* JfrOSInterface::JfrOSInterfaceImpl::system_process_interface() {126if (_system_process_interface == NULL) {127_system_process_interface = create_interface<SystemProcessInterface>();128}129return _system_process_interface;130}131132NetworkPerformanceInterface* JfrOSInterface::JfrOSInterfaceImpl::network_performance_interface() {133if (_network_performance_interface == NULL) {134_network_performance_interface = create_interface<NetworkPerformanceInterface>();135}136return _network_performance_interface;137}138139bool JfrOSInterface::JfrOSInterfaceImpl::initialize() {140return true;141}142143JfrOSInterface::JfrOSInterfaceImpl::~JfrOSInterfaceImpl(void) {144if (_cpu_info_interface != NULL) {145delete _cpu_info_interface;146_cpu_info_interface = NULL;147}148if (_cpu_perf_interface != NULL) {149delete _cpu_perf_interface;150_cpu_perf_interface = NULL;151}152if (_system_process_interface != NULL) {153delete _system_process_interface;154_system_process_interface = NULL;155}156if (_network_performance_interface != NULL) {157delete _network_performance_interface;158_network_performance_interface = NULL;159}160}161162int JfrOSInterface::JfrOSInterfaceImpl::cpu_information(CPUInformation& cpu_info) {163CPUInformationInterface* const iface = cpu_info_interface();164return iface == NULL ? OS_ERR : iface->cpu_information(cpu_info);165}166167int JfrOSInterface::JfrOSInterfaceImpl::cpu_load(int which_logical_cpu, double* cpu_load) {168CPUPerformanceInterface* const iface = cpu_perf_interface();169return iface == NULL ? OS_ERR : iface->cpu_load(which_logical_cpu, cpu_load);170}171172int JfrOSInterface::JfrOSInterfaceImpl::context_switch_rate(double* rate) {173CPUPerformanceInterface* const iface = cpu_perf_interface();174return iface == NULL ? OS_ERR : iface->context_switch_rate(rate);175}176177int JfrOSInterface::JfrOSInterfaceImpl::cpu_load_total_process(double* cpu_load) {178CPUPerformanceInterface* const iface = cpu_perf_interface();179return iface == NULL ? OS_ERR : iface->cpu_load_total_process(cpu_load);180}181182int JfrOSInterface::JfrOSInterfaceImpl::cpu_loads_process(double* pjvmUserLoad,183double* pjvmKernelLoad,184double* psystemTotal) {185CPUPerformanceInterface* const iface = cpu_perf_interface();186return iface == NULL ? OS_ERR : iface->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotal);187}188189int JfrOSInterface::JfrOSInterfaceImpl::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) {190assert(system_processes != NULL, "system_processes pointer is NULL!");191assert(no_of_sys_processes != NULL, "no_of_sys_processes pointer is NULL!");192SystemProcessInterface* const iface = system_process_interface();193return iface == NULL ? OS_ERR : iface->system_processes(system_processes, no_of_sys_processes);194}195196int JfrOSInterface::JfrOSInterfaceImpl::network_utilization(NetworkInterface** network_interfaces) {197NetworkPerformanceInterface* const iface = network_performance_interface();198return iface == NULL ? OS_ERR : iface->network_utilization(network_interfaces);199}200201// assigned char* is RESOURCE_HEAP_ALLOCATED202// caller need to ensure proper ResourceMark placement.203int JfrOSInterface::JfrOSInterfaceImpl::os_version(char** os_version) const {204assert(os_version != NULL, "os_version pointer is NULL!");205stringStream os_ver_info;206os::print_os_info_brief(&os_ver_info);207*os_version = os_ver_info.as_string();208return OS_OK;209}210211JfrOSInterface::JfrOSInterface() {212_impl = NULL;213}214215bool JfrOSInterface::initialize() {216_impl = new JfrOSInterface::JfrOSInterfaceImpl();217return _impl != NULL && _impl->initialize();218}219220JfrOSInterface::~JfrOSInterface() {221if (_impl != NULL) {222delete _impl;223_impl = NULL;224}225}226227int JfrOSInterface::cpu_information(CPUInformation& cpu_info) {228return instance()._impl->cpu_information(cpu_info);229}230231int JfrOSInterface::cpu_load(int which_logical_cpu, double* cpu_load) {232return instance()._impl->cpu_load(which_logical_cpu, cpu_load);233}234235int JfrOSInterface::context_switch_rate(double* rate) {236return instance()._impl->context_switch_rate(rate);237}238239int JfrOSInterface::cpu_load_total_process(double* cpu_load) {240return instance()._impl->cpu_load_total_process(cpu_load);241}242243int JfrOSInterface::cpu_loads_process(double* jvm_user_load, double* jvm_kernel_load, double* system_total_load){244return instance()._impl->cpu_loads_process(jvm_user_load, jvm_kernel_load, system_total_load);245}246247int JfrOSInterface::os_version(char** os_version) {248return instance()._impl->os_version(os_version);249}250251const char* JfrOSInterface::virtualization_name() {252VirtualizationType vrt = VM_Version::get_detected_virtualization();253if (vrt == XenHVM) {254return "Xen hardware-assisted virtualization";255} else if (vrt == KVM) {256return "KVM virtualization";257} else if (vrt == VMWare) {258return "VMWare virtualization";259} else if (vrt == HyperV) {260return "Hyper-V virtualization";261} else if (vrt == HyperVRole) {262return "Hyper-V role";263} else if (vrt == PowerVM) {264return "PowerVM virtualization";265} else if (vrt == PowerKVM) {266return "Power KVM virtualization";267} else if (vrt == PowerFullPartitionMode) {268return "Power full partition";269}270271return "No virtualization detected";272}273274int JfrOSInterface::generate_initial_environment_variable_events() {275if (os::get_environ() == NULL) {276return OS_ERR;277}278279if (EventInitialEnvironmentVariable::is_enabled()) {280// One time stamp for all events, so they can be grouped together281JfrTicks time_stamp = JfrTicks::now();282for (char** p = os::get_environ(); *p != NULL; p++) {283char* variable = *p;284char* equal_sign = strchr(variable, '=');285if (equal_sign != NULL) {286// Extract key/value287ResourceMark rm;288ptrdiff_t key_length = equal_sign - variable;289char* key = NEW_RESOURCE_ARRAY(char, key_length + 1);290char* value = equal_sign + 1;291strncpy(key, variable, key_length);292key[key_length] = '\0';293EventInitialEnvironmentVariable event(UNTIMED);294event.set_endtime(time_stamp);295event.set_key(key);296event.set_value(value);297event.commit();298}299}300}301return OS_OK;302}303304int JfrOSInterface::system_processes(SystemProcess** sys_processes, int* no_of_sys_processes) {305return instance()._impl->system_processes(sys_processes, no_of_sys_processes);306}307308int JfrOSInterface::network_utilization(NetworkInterface** network_interfaces) {309return instance()._impl->network_utilization(network_interfaces);310}311312313