src/hotspot/os/bsd/os_perf_bsd.cpp
author alanb
Thu, 21 Mar 2019 08:41:10 +0000
branchniosocketimpl-branch
changeset 57275 222fa5ed1c91
parent 50879 d90c3cbf13df
child 58282 03fce7b04b42
permissions -rw-r--r--
Add @systemProperty ref to system property to configure old impl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     4
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     8
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    13
 * accompanied this code).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    14
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    18
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    21
 * questions.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    22
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    23
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    24
#include "precompiled.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
#include "memory/allocation.inline.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
#include "memory/resourceArea.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
#include "runtime/os.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
#include "runtime/os_perf.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
#include "vm_version_ext_x86.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
#ifdef __APPLE__
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
  #import <libproc.h>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
  #include <sys/time.h>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
  #include <sys/sysctl.h>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
  #include <mach/mach.h>
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
  #include <mach/task_info.h>
50879
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
    37
  #include <sys/socket.h>
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
    38
  #include <net/if.h>
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
    39
  #include <net/if_dl.h>
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
    40
  #include <net/route.h>
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
#endif
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
static const double NANOS_PER_SEC = 1000000000.0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
   friend class CPUPerformanceInterface;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
 private:
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
  long _total_cpu_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
  long _total_csr_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
  long _jvm_user_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
  long _jvm_system_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
  long _jvm_context_switches;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
  long _used_ticks;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
  long _total_ticks;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
  int  _active_processor_count;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
  bool now_in_nanos(long* resultp) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
    timeval current_time;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
    if (gettimeofday(&current_time, NULL) != 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
      // Error getting current time
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
      return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
    *resultp = current_time.tv_sec * NANOS_PER_SEC + 1000L * current_time.tv_usec;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
    return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
  double normalize(double value) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
    return MIN2<double>(MAX2<double>(value, 0.0), 1.0);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
  int cpu_load(int which_logical_cpu, double* cpu_load);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
  int context_switch_rate(double* rate);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
  int cpu_load_total_process(double* cpu_load);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
  int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
  CPUPerformance(const CPUPerformance& rhs); // no impl
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
  CPUPerformance& operator=(const CPUPerformance& rhs); // no impl
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
 public:
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
  CPUPerformance();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
  bool initialize();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
  ~CPUPerformance();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
};
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
CPUPerformanceInterface::CPUPerformance::CPUPerformance() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
  _total_cpu_nanos= 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
  _total_csr_nanos= 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
  _jvm_context_switches = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
  _jvm_user_nanos = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
  _jvm_system_nanos = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
  _used_ticks = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
  _total_ticks = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
  _active_processor_count = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
bool CPUPerformanceInterface::CPUPerformance::initialize() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
  return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
  return FUNCTIONALITY_NOT_IMPLEMENTED;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
#ifdef __APPLE__
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
  host_name_port_t host = mach_host_self();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
  host_flavor_t flavor = HOST_CPU_LOAD_INFO;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
  mach_msg_type_number_t host_info_count = HOST_CPU_LOAD_INFO_COUNT;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
  host_cpu_load_info_data_t cpu_load_info;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
  kern_return_t kr = host_statistics(host, flavor, (host_info_t)&cpu_load_info, &host_info_count);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
  if (kr != KERN_SUCCESS) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
  long 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];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
  long total_ticks = used_ticks + cpu_load_info.cpu_ticks[CPU_STATE_IDLE];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
  if (_used_ticks == 0 || _total_ticks == 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
    // First call, just set the values
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
    _used_ticks  = used_ticks;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
    _total_ticks = total_ticks;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
  long used_delta  = used_ticks - _used_ticks;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
  long total_delta = total_ticks - _total_ticks;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
  _used_ticks  = used_ticks;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
  _total_ticks = total_ticks;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   133
  if (total_delta == 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   134
    // Avoid division by zero
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
  *cpu_load = (double)used_delta / total_delta;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
  return OS_OK;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   141
#else
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   142
  return FUNCTIONALITY_NOT_IMPLEMENTED;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   143
#endif
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   144
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   145
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   146
int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   147
#ifdef __APPLE__
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   148
  int result = cpu_load_total_process(psystemTotalLoad);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   149
  mach_port_t task = mach_task_self();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   150
  mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   151
  task_info_data_t task_info_data;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   152
  kern_return_t kr = task_info(task, TASK_ABSOLUTETIME_INFO, (task_info_t)task_info_data, &task_info_count);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   153
  if (kr != KERN_SUCCESS) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
  task_absolutetime_info_t absolutetime_info = (task_absolutetime_info_t)task_info_data;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
  int active_processor_count = os::active_processor_count();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
  long jvm_user_nanos = absolutetime_info->total_user;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
  long jvm_system_nanos = absolutetime_info->total_system;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
  long total_cpu_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   163
  if(!now_in_nanos(&total_cpu_nanos)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   164
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   165
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
  if (_total_cpu_nanos == 0 || active_processor_count != _active_processor_count) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   168
    // First call or change in active processor count
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
    result = OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   171
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   172
  long delta_nanos = active_processor_count * (total_cpu_nanos - _total_cpu_nanos);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   173
  if (delta_nanos == 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   174
    // Avoid division by zero
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   175
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   176
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   177
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
  *pjvmUserLoad = normalize((double)(jvm_user_nanos - _jvm_user_nanos)/delta_nanos);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
  *pjvmKernelLoad = normalize((double)(jvm_system_nanos - _jvm_system_nanos)/delta_nanos);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
  _active_processor_count = active_processor_count;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
  _total_cpu_nanos = total_cpu_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
  _jvm_user_nanos = jvm_user_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
  _jvm_system_nanos = jvm_system_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   185
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   186
  return result;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   187
#else
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   188
  return FUNCTIONALITY_NOT_IMPLEMENTED;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   189
#endif
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   190
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   191
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   192
int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   193
#ifdef __APPLE__
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   194
  mach_port_t task = mach_task_self();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   195
  mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   196
  task_info_data_t task_info_data;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   197
  kern_return_t kr = task_info(task, TASK_EVENTS_INFO, (task_info_t)task_info_data, &task_info_count);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   198
  if (kr != KERN_SUCCESS) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   199
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   200
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   201
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   202
  int result = OS_OK;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   203
  if (_total_csr_nanos == 0 || _jvm_context_switches == 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   204
    // First call just set initial values.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   205
    result = OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   206
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   207
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   208
  long jvm_context_switches = ((task_events_info_t)task_info_data)->csw;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   209
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   210
  long total_csr_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   211
  if(!now_in_nanos(&total_csr_nanos)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   212
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   213
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   214
  double delta_in_sec = (double)(total_csr_nanos - _total_csr_nanos) / NANOS_PER_SEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   215
  if (delta_in_sec == 0.0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   216
    // Avoid division by zero
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   217
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   218
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   219
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   220
  *rate = (jvm_context_switches - _jvm_context_switches) / delta_in_sec;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   221
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   222
  _jvm_context_switches = jvm_context_switches;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   223
  _total_csr_nanos = total_csr_nanos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   224
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   225
  return result;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   226
#else
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   227
  return FUNCTIONALITY_NOT_IMPLEMENTED;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   228
#endif
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   229
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   230
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   231
CPUPerformanceInterface::CPUPerformanceInterface() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   232
  _impl = NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   233
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   234
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   235
bool CPUPerformanceInterface::initialize() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   236
  _impl = new CPUPerformanceInterface::CPUPerformance();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   237
  return _impl != NULL && _impl->initialize();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   238
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   239
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   240
CPUPerformanceInterface::~CPUPerformanceInterface() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   241
  if (_impl != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   242
    delete _impl;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   243
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   244
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   245
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   246
int CPUPerformanceInterface::cpu_load(int which_logical_cpu, double* cpu_load) const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   247
  return _impl->cpu_load(which_logical_cpu, cpu_load);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   248
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   249
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   250
int CPUPerformanceInterface::cpu_load_total_process(double* cpu_load) const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   251
  return _impl->cpu_load_total_process(cpu_load);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   252
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   253
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   254
int CPUPerformanceInterface::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   255
  return _impl->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotalLoad);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   256
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   257
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   258
int CPUPerformanceInterface::context_switch_rate(double* rate) const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   259
  return _impl->context_switch_rate(rate);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   260
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   261
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   262
class SystemProcessInterface::SystemProcesses : public CHeapObj<mtInternal> {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   263
  friend class SystemProcessInterface;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   264
 private:
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   265
  SystemProcesses();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   266
  bool initialize();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   267
  SystemProcesses(const SystemProcesses& rhs); // no impl
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   268
  SystemProcesses& operator=(const SystemProcesses& rhs); // no impl
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   269
  ~SystemProcesses();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   270
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   271
  //information about system processes
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   272
  int system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   273
};
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   274
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   275
SystemProcessInterface::SystemProcesses::SystemProcesses() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   276
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   277
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   278
bool SystemProcessInterface::SystemProcesses::initialize() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   279
  return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   280
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   281
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   282
SystemProcessInterface::SystemProcesses::~SystemProcesses() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   283
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   284
int SystemProcessInterface::SystemProcesses::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   285
  assert(system_processes != NULL, "system_processes pointer is NULL!");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   286
  assert(no_of_sys_processes != NULL, "system_processes counter pointer is NULL!");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   287
#ifdef __APPLE__
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   288
  pid_t* pids = NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   289
  int pid_count = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   290
  ResourceMark rm;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   291
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   292
  int try_count = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   293
  while (pids == NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   294
    // Find out buffer size
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   295
    size_t pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   296
    if (pids_bytes <= 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   297
      return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   298
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   299
    pid_count = pids_bytes / sizeof(pid_t);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   300
    pids = NEW_RESOURCE_ARRAY(pid_t, pid_count);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   301
    memset(pids, 0, pids_bytes);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   302
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   303
    pids_bytes = proc_listpids(PROC_ALL_PIDS, 0, pids, pids_bytes);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   304
    if (pids_bytes <= 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   305
       // couldn't fit buffer, retry.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   306
      FREE_RESOURCE_ARRAY(pid_t, pids, pid_count);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   307
      pids = NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   308
      try_count++;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   309
      if (try_count > 3) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   310
      return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   311
      }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   312
    } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   313
      pid_count = pids_bytes / sizeof(pid_t);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   314
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   315
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   316
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   317
  int process_count = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   318
  SystemProcess* next = NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   319
  for (int i = 0; i < pid_count; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   320
    pid_t pid = pids[i];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   321
    if (pid != 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   322
      char buffer[PROC_PIDPATHINFO_MAXSIZE];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   323
      memset(buffer, 0 , sizeof(buffer));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   324
      if (proc_pidpath(pid, buffer, sizeof(buffer)) != -1) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   325
        int length = strlen(buffer);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   326
        if (length > 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   327
          SystemProcess* current = new SystemProcess();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   328
          char * path = NEW_C_HEAP_ARRAY(char, length + 1, mtInternal);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   329
          strcpy(path, buffer);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   330
          current->set_path(path);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   331
          current->set_pid((int)pid);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   332
          current->set_next(next);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   333
          next = current;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   334
          process_count++;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   335
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   336
      }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   337
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   338
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   339
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   340
  *no_of_sys_processes = process_count;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   341
  *system_processes = next;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   342
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   343
  return OS_OK;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   344
#endif
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   345
  return FUNCTIONALITY_NOT_IMPLEMENTED;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   346
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   347
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   348
int SystemProcessInterface::system_processes(SystemProcess** system_procs, int* no_of_sys_processes) const {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   349
  return _impl->system_processes(system_procs, no_of_sys_processes);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   350
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   351
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   352
SystemProcessInterface::SystemProcessInterface() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   353
  _impl = NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   354
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   355
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   356
bool SystemProcessInterface::initialize() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   357
  _impl = new SystemProcessInterface::SystemProcesses();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   358
  return _impl != NULL && _impl->initialize();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   359
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   360
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   361
SystemProcessInterface::~SystemProcessInterface() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   362
  if (_impl != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   363
    delete _impl;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   364
 }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   365
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   366
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   367
CPUInformationInterface::CPUInformationInterface() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   368
  _cpu_info = NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   369
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   370
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   371
bool CPUInformationInterface::initialize() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   372
  _cpu_info = new CPUInformation();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   373
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   374
  if (NULL == _cpu_info) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   375
    return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   376
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   377
  _cpu_info->set_number_of_hardware_threads(VM_Version_Ext::number_of_threads());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   378
  _cpu_info->set_number_of_cores(VM_Version_Ext::number_of_cores());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   379
  _cpu_info->set_number_of_sockets(VM_Version_Ext::number_of_sockets());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   380
  _cpu_info->set_cpu_name(VM_Version_Ext::cpu_name());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   381
  _cpu_info->set_cpu_description(VM_Version_Ext::cpu_description());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   382
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   383
  return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   384
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   385
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   386
CPUInformationInterface::~CPUInformationInterface() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   387
  if (_cpu_info != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   388
    if (_cpu_info->cpu_name() != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   389
      const char* cpu_name = _cpu_info->cpu_name();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   390
      FREE_C_HEAP_ARRAY(char, cpu_name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   391
      _cpu_info->set_cpu_name(NULL);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   392
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   393
    if (_cpu_info->cpu_description() != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   394
      const char* cpu_desc = _cpu_info->cpu_description();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   395
      FREE_C_HEAP_ARRAY(char, cpu_desc);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   396
      _cpu_info->set_cpu_description(NULL);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   397
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   398
    delete _cpu_info;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   399
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   400
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   401
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   402
int CPUInformationInterface::cpu_information(CPUInformation& cpu_info) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   403
  if (NULL == _cpu_info) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   404
    return OS_ERR;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   405
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   406
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   407
  cpu_info = *_cpu_info; // shallow copy assignment
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   408
  return OS_OK;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   409
}
50879
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   410
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   411
class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   412
  friend class NetworkPerformanceInterface;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   413
 private:
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   414
  NetworkPerformance();
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   415
  NetworkPerformance(const NetworkPerformance& rhs); // no impl
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   416
  NetworkPerformance& operator=(const NetworkPerformance& rhs); // no impl
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   417
  bool initialize();
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   418
  ~NetworkPerformance();
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   419
  int network_utilization(NetworkInterface** network_interfaces) const;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   420
};
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   421
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   422
NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   423
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   424
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   425
bool NetworkPerformanceInterface::NetworkPerformance::initialize() {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   426
  return true;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   427
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   428
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   429
NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   430
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   431
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   432
int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   433
  size_t len;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   434
  int mib[] = {CTL_NET, PF_ROUTE, /* protocol number */ 0, /* address family */ 0, NET_RT_IFLIST2, /* NET_RT_FLAGS mask*/ 0};
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   435
  if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &len, NULL, 0) != 0) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   436
    return OS_ERR;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   437
  }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   438
  uint8_t* buf = NEW_RESOURCE_ARRAY(uint8_t, len);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   439
  if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &len, NULL, 0) != 0) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   440
    return OS_ERR;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   441
  }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   442
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   443
  size_t index = 0;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   444
  NetworkInterface* ret = NULL;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   445
  while (index < len) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   446
    if_msghdr* msghdr = reinterpret_cast<if_msghdr*>(buf + index);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   447
    index += msghdr->ifm_msglen;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   448
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   449
    if (msghdr->ifm_type != RTM_IFINFO2) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   450
      continue;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   451
    }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   452
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   453
    if_msghdr2* msghdr2 = reinterpret_cast<if_msghdr2*>(msghdr);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   454
    sockaddr_dl* sockaddr = reinterpret_cast<sockaddr_dl*>(msghdr2 + 1);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   455
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   456
    // The interface name is not necessarily NUL-terminated
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   457
    char name_buf[128];
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   458
    size_t name_len = MIN2(sizeof(name_buf) - 1, static_cast<size_t>(sockaddr->sdl_nlen));
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   459
    strncpy(name_buf, sockaddr->sdl_data, name_len);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   460
    name_buf[name_len] = '\0';
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   461
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   462
    uint64_t bytes_in = msghdr2->ifm_data.ifi_ibytes;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   463
    uint64_t bytes_out = msghdr2->ifm_data.ifi_obytes;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   464
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   465
    NetworkInterface* cur = new NetworkInterface(name_buf, bytes_in, bytes_out, ret);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   466
    ret = cur;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   467
  }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   468
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   469
  *network_interfaces = ret;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   470
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   471
  return OS_OK;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   472
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   473
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   474
NetworkPerformanceInterface::NetworkPerformanceInterface() {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   475
  _impl = NULL;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   476
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   477
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   478
NetworkPerformanceInterface::~NetworkPerformanceInterface() {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   479
  if (_impl != NULL) {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   480
    delete _impl;
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   481
  }
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   482
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   483
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   484
bool NetworkPerformanceInterface::initialize() {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   485
  _impl = new NetworkPerformanceInterface::NetworkPerformance();
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   486
  return _impl != NULL && _impl->initialize();
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   487
}
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   488
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   489
int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const {
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   490
  return _impl->network_utilization(network_interfaces);
d90c3cbf13df 8003209: JFR events for network utilization
rwestberg
parents: 50113
diff changeset
   491
}