Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/periodic/jfrOSInterface.cpp
38920 views
/*1* Copyright (c) 2012, 2019, 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 "utilities/ostream.hpp"3334#include <stdlib.h> // for environment variables35#ifdef __APPLE__36#include <crt_externs.h>37#define environ (*_NSGetEnviron())38#endif3940#ifndef environ41extern char** environ;42#endif4344static JfrOSInterface* _instance = NULL;4546JfrOSInterface& JfrOSInterface::instance() {47return *_instance;48}4950JfrOSInterface* JfrOSInterface::create() {51assert(_instance == NULL, "invariant");52_instance = new JfrOSInterface();53return _instance;54}5556void JfrOSInterface::destroy() {57JfrNetworkUtilization::destroy();58if (_instance != NULL) {59delete _instance;60_instance = NULL;61}62}6364class JfrOSInterface::JfrOSInterfaceImpl : public JfrCHeapObj {65friend class JfrOSInterface;66private:67CPUInformationInterface* _cpu_info_interface;68CPUPerformanceInterface* _cpu_perf_interface;69SystemProcessInterface* _system_process_interface;70NetworkPerformanceInterface* _network_performance_interface;7172JfrOSInterfaceImpl();73bool initialize();74~JfrOSInterfaceImpl();7576// cpu info77int cpu_information(CPUInformation& cpu_info);78int cpu_load(int which_logical_cpu, double* cpu_load);79int context_switch_rate(double* rate);80int cpu_load_total_process(double* cpu_load);81int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotal);8283// os information84int os_version(char** os_version) const;8586// environment information87void generate_environment_variables_events();8889// system processes information90int system_processes(SystemProcess** system_processes, int* no_of_sys_processes);9192int network_utilization(NetworkInterface** network_interfaces) const;93};9495JfrOSInterface::JfrOSInterfaceImpl::JfrOSInterfaceImpl() : _cpu_info_interface(NULL),96_cpu_perf_interface(NULL),97_system_process_interface(NULL) {}9899bool JfrOSInterface::JfrOSInterfaceImpl::initialize() {100_cpu_info_interface = new CPUInformationInterface();101if (!(_cpu_info_interface != NULL && _cpu_info_interface->initialize())) {102return false;103}104_cpu_perf_interface = new CPUPerformanceInterface();105if (!(_cpu_perf_interface != NULL && _cpu_perf_interface->initialize())) {106return false;107}108_system_process_interface = new SystemProcessInterface();109if (!(_system_process_interface != NULL && _system_process_interface->initialize())) {110return false;111}112_network_performance_interface = new NetworkPerformanceInterface();113return _network_performance_interface != NULL && _network_performance_interface->initialize();114}115116JfrOSInterface::JfrOSInterfaceImpl::~JfrOSInterfaceImpl(void) {117if (_cpu_info_interface != NULL) {118delete _cpu_info_interface;119_cpu_info_interface = NULL;120}121if (_cpu_perf_interface != NULL) {122delete _cpu_perf_interface;123_cpu_perf_interface = NULL;124}125if (_system_process_interface != NULL) {126delete _system_process_interface;127_system_process_interface = NULL;128}129if (_network_performance_interface != NULL) {130delete _network_performance_interface;131_network_performance_interface = NULL;132}133}134135int JfrOSInterface::JfrOSInterfaceImpl::cpu_load(int which_logical_cpu, double* cpu_load) {136return _cpu_perf_interface->cpu_load(which_logical_cpu, cpu_load);137}138139int JfrOSInterface::JfrOSInterfaceImpl::context_switch_rate(double* rate) {140return _cpu_perf_interface->context_switch_rate(rate);141}142143int JfrOSInterface::JfrOSInterfaceImpl::cpu_load_total_process(double* cpu_load) {144return _cpu_perf_interface->cpu_load_total_process(cpu_load);145}146147int JfrOSInterface::JfrOSInterfaceImpl::cpu_loads_process(double* pjvmUserLoad,148double* pjvmKernelLoad,149double* psystemTotal) {150return _cpu_perf_interface->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotal);151}152153int JfrOSInterface::JfrOSInterfaceImpl::cpu_information(CPUInformation& cpu_info) {154return _cpu_info_interface->cpu_information(cpu_info);155}156157int JfrOSInterface::JfrOSInterfaceImpl::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) {158assert(system_processes != NULL, "system_processes pointer is NULL!");159assert(no_of_sys_processes != NULL, "no_of_sys_processes pointer is NULL!");160return _system_process_interface->system_processes(system_processes, no_of_sys_processes);161}162163int JfrOSInterface::JfrOSInterfaceImpl::network_utilization(NetworkInterface** network_interfaces) const {164return _network_performance_interface->network_utilization(network_interfaces);165}166167// assigned char* is RESOURCE_HEAP_ALLOCATED168// caller need to ensure proper ResourceMark placement.169int JfrOSInterface::JfrOSInterfaceImpl::os_version(char** os_version) const {170assert(os_version != NULL, "os_version pointer is NULL!");171stringStream os_ver_info;172os::print_os_info_brief(&os_ver_info);173*os_version = os_ver_info.as_string();174return OS_OK;175}176177JfrOSInterface::JfrOSInterface() {178_impl = NULL;179}180181bool JfrOSInterface::initialize() {182_impl = new JfrOSInterface::JfrOSInterfaceImpl();183return _impl != NULL && _impl->initialize();184}185186JfrOSInterface::~JfrOSInterface() {187if (_impl != NULL) {188delete _impl;189_impl = NULL;190}191}192193int JfrOSInterface::cpu_information(CPUInformation& cpu_info) {194return instance()._impl->cpu_information(cpu_info);195}196197int JfrOSInterface::cpu_load(int which_logical_cpu, double* cpu_load) {198return instance()._impl->cpu_load(which_logical_cpu, cpu_load);199}200201int JfrOSInterface::context_switch_rate(double* rate) {202return instance()._impl->context_switch_rate(rate);203}204205int JfrOSInterface::cpu_load_total_process(double* cpu_load) {206return instance()._impl->cpu_load_total_process(cpu_load);207}208209int JfrOSInterface::cpu_loads_process(double* jvm_user_load, double* jvm_kernel_load, double* system_total_load){210return instance()._impl->cpu_loads_process(jvm_user_load, jvm_kernel_load, system_total_load);211}212213int JfrOSInterface::os_version(char** os_version) {214return instance()._impl->os_version(os_version);215}216217int JfrOSInterface::generate_initial_environment_variable_events() {218if (environ == NULL) {219return OS_ERR;220}221222if (EventInitialEnvironmentVariable::is_enabled()) {223// One time stamp for all events, so they can be grouped together224JfrTicks time_stamp = JfrTicks::now();225for (char** p = environ; *p != NULL; p++) {226char* variable = *p;227char* equal_sign = strchr(variable, '=');228if (equal_sign != NULL) {229// Extract key/value230ResourceMark rm;231ptrdiff_t key_length = equal_sign - variable;232char* key = NEW_RESOURCE_ARRAY(char, key_length + 1);233char* value = equal_sign + 1;234strncpy(key, variable, key_length);235key[key_length] = '\0';236EventInitialEnvironmentVariable event(UNTIMED);237event.set_endtime(time_stamp);238event.set_key(key);239event.set_value(value);240event.commit();241}242}243}244return OS_OK;245}246247int JfrOSInterface::system_processes(SystemProcess** sys_processes, int* no_of_sys_processes) {248return instance()._impl->system_processes(sys_processes, no_of_sys_processes);249}250251int JfrOSInterface::network_utilization(NetworkInterface** network_interfaces) {252return instance()._impl->network_utilization(network_interfaces);253}254255256