1 /*
   2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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.
   8  *
   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).
  14  *
  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.
  18  *
  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
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jfr/periodic/jfrOSInterface.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "runtime/os.hpp"
  30 #include "runtime/os_perf.hpp"
  31 #include "utilities/ostream.hpp"
  32 #include "trace/tracing.hpp"
  33 
  34 #include <stdlib.h> // for environment variables
  35 #ifdef __APPLE__
  36 #include <crt_externs.h>
  37 #define environ (*_NSGetEnviron())
  38 #endif
  39 
  40 #ifndef environ
  41 extern char** environ;
  42 #endif
  43 
  44 static JfrOSInterface* _instance = NULL;
  45 
  46 JfrOSInterface& JfrOSInterface::instance() {
  47   return *_instance;
  48 }
  49 
  50 JfrOSInterface* JfrOSInterface::create() {
  51   assert(_instance == NULL, "invariant");
  52   _instance = new JfrOSInterface();
  53   return _instance;
  54 }
  55 
  56 void JfrOSInterface::destroy() {
  57   if (_instance != NULL) {
  58     delete _instance;
  59     _instance = NULL;
  60   }
  61 }
  62 
  63 class JfrOSInterface::JfrOSInterfaceImpl : public JfrCHeapObj {
  64   friend class JfrOSInterface;
  65  private:
  66   CPUInformationInterface* _cpu_info_interface;
  67   CPUPerformanceInterface* _cpu_perf_interface;
  68   SystemProcessInterface*  _system_process_interface;
  69 
  70   // stub helper
  71   void functionality_not_implemented(char** str) const;
  72 
  73   JfrOSInterfaceImpl();
  74   bool initialize();
  75   ~JfrOSInterfaceImpl();
  76 
  77   // cpu info
  78   int cpu_information(CPUInformation& cpu_info);
  79   int cpu_load(int which_logical_cpu, double* cpu_load);
  80   int context_switch_rate(double* rate);
  81   int cpu_load_total_process(double* cpu_load);
  82   int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotal);
  83 
  84   // os information
  85   int os_version(char** os_version) const;
  86 
  87   // environment information
  88   void generate_environment_variables_events();
  89 
  90    // system processes information
  91   int system_processes(SystemProcess** system_processes, int* no_of_sys_processes);
  92 };
  93 
  94 JfrOSInterface::JfrOSInterfaceImpl::JfrOSInterfaceImpl() : _cpu_info_interface(NULL),
  95                                                            _cpu_perf_interface(NULL),
  96                                                            _system_process_interface(NULL) {}
  97 
  98 bool JfrOSInterface::JfrOSInterfaceImpl::initialize() {
  99   _cpu_info_interface = new CPUInformationInterface();
 100   bool success = _cpu_info_interface != NULL && _cpu_info_interface->initialize();
 101   if (!success) {
 102     return false;
 103   }
 104   _cpu_perf_interface = new CPUPerformanceInterface();
 105   success = _cpu_perf_interface != NULL && _cpu_perf_interface->initialize();
 106   if (!success) {
 107     return false;
 108   }
 109   _system_process_interface = new SystemProcessInterface();
 110   success = _system_process_interface != NULL && _system_process_interface->initialize();
 111   return success;
 112 }
 113 
 114 JfrOSInterface::JfrOSInterfaceImpl::~JfrOSInterfaceImpl(void) {
 115   if (_cpu_info_interface != NULL) {
 116     delete _cpu_info_interface;
 117     _cpu_info_interface = NULL;
 118   }
 119   if (_cpu_perf_interface != NULL) {
 120     delete _cpu_perf_interface;
 121     _cpu_perf_interface = NULL;
 122   }
 123   if (_system_process_interface != NULL) {
 124     delete _system_process_interface;
 125     _system_process_interface = NULL;
 126   }
 127 }
 128 
 129 int JfrOSInterface::JfrOSInterfaceImpl::cpu_load(int which_logical_cpu, double* cpu_load) {
 130   return _cpu_perf_interface->cpu_load(which_logical_cpu, cpu_load);
 131 }
 132 
 133 int JfrOSInterface::JfrOSInterfaceImpl::context_switch_rate(double* rate) {
 134   return _cpu_perf_interface->context_switch_rate(rate);
 135 }
 136 
 137 int JfrOSInterface::JfrOSInterfaceImpl::cpu_load_total_process(double* cpu_load) {
 138   return _cpu_perf_interface->cpu_load_total_process(cpu_load);
 139 }
 140 
 141 int JfrOSInterface::JfrOSInterfaceImpl::cpu_loads_process(double* pjvmUserLoad,
 142                                                           double* pjvmKernelLoad,
 143                                                           double* psystemTotal) {
 144   return _cpu_perf_interface->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotal);
 145 }
 146 
 147 int JfrOSInterface::JfrOSInterfaceImpl::cpu_information(CPUInformation& cpu_info) {
 148   return _cpu_info_interface->cpu_information(cpu_info);
 149 }
 150 
 151 int JfrOSInterface::JfrOSInterfaceImpl::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) {
 152   assert(system_processes != NULL, "system_processes pointer is NULL!");
 153   assert(no_of_sys_processes != NULL, "no_of_sys_processes pointer is NULL!");
 154   return _system_process_interface->system_processes(system_processes, no_of_sys_processes);
 155 }
 156 
 157 // assigned char* is RESOURCE_HEAP_ALLOCATED
 158 // caller need to ensure proper ResourceMark placement.
 159 int JfrOSInterface::JfrOSInterfaceImpl::os_version(char** os_version) const {
 160   assert(os_version != NULL, "os_version pointer is NULL!");
 161   stringStream os_ver_info;
 162   os::print_os_info_brief(&os_ver_info);
 163   *os_version = os_ver_info.as_string();
 164   return OS_OK;
 165 }
 166 
 167 void JfrOSInterface::JfrOSInterfaceImpl::functionality_not_implemented(char** str) const {
 168   assert(str != NULL, "address to string is NULL!");
 169   const char* not_impl = "Functionality_not_implemented";
 170   const size_t not_impl_len = strlen(not_impl);
 171   *str = NEW_C_HEAP_ARRAY(char, not_impl_len+1, mtTracing);
 172   strncpy(*str, not_impl, not_impl_len);
 173   (*str)[not_impl_len] = '\0';
 174 }
 175 
 176 JfrOSInterface::JfrOSInterface() {
 177   _impl = NULL;
 178 }
 179 
 180 bool JfrOSInterface::initialize() {
 181   _impl = new JfrOSInterface::JfrOSInterfaceImpl();
 182   return _impl != NULL && _impl->initialize();
 183 }
 184 
 185 JfrOSInterface::~JfrOSInterface() {
 186   if (_impl != NULL) {
 187     delete _impl;
 188     _impl = NULL;
 189   }
 190 }
 191 
 192 int JfrOSInterface::cpu_information(CPUInformation& cpu_info) {
 193   return instance()._impl->cpu_information(cpu_info);
 194 }
 195 
 196 int JfrOSInterface::cpu_load(int which_logical_cpu, double* cpu_load) {
 197   return instance()._impl->cpu_load(which_logical_cpu, cpu_load);
 198 }
 199 
 200 int JfrOSInterface::context_switch_rate(double* rate) {
 201   return instance()._impl->context_switch_rate(rate);
 202 }
 203 
 204 int JfrOSInterface::cpu_load_total_process(double* cpu_load) {
 205   return instance()._impl->cpu_load_total_process(cpu_load);
 206 }
 207 
 208 int JfrOSInterface::cpu_loads_process(double* jvm_user_load, double* jvm_kernel_load, double* system_total_load){
 209   return instance()._impl->cpu_loads_process(jvm_user_load, jvm_kernel_load, system_total_load);
 210 }
 211 
 212 int JfrOSInterface::os_version(char** os_version) {
 213   return instance()._impl->os_version(os_version);
 214 }
 215 
 216 int JfrOSInterface::generate_initial_environment_variable_events() {
 217   if (environ == NULL) {
 218     return OS_ERR;
 219   }
 220 
 221   if (EventInitialEnvironmentVariable::is_enabled()) {
 222     // One time stamp for all events, so they can be grouped together
 223     JfrTraceTime time_stamp(Tracing::time());
 224     for (char** p = environ; *p != NULL; p++) {
 225       char* variable = *p;
 226       char* equal_sign = strchr(variable, '=');
 227       if (equal_sign != NULL) {
 228         // Extract key/value
 229         ResourceMark rm;
 230         ptrdiff_t key_length = equal_sign - variable;
 231         char* key = NEW_RESOURCE_ARRAY(char, key_length + 1);
 232         char* value = equal_sign + 1;
 233         strncpy(key, variable, key_length);
 234         key[key_length] = '\0';
 235         EventInitialEnvironmentVariable event(UNTIMED);
 236         event.set_endtime(time_stamp);
 237         event.set_key(key);
 238         event.set_value(value);
 239         event.commit();
 240       }
 241     }
 242   }
 243   return OS_OK;
 244 }
 245 
 246 int JfrOSInterface::system_processes(SystemProcess** sys_processes, int* no_of_sys_processes) {
 247   return instance()._impl->system_processes(sys_processes, no_of_sys_processes);
 248 }