< prev index next >

src/hotspot/os/bsd/os_perf_bsd.cpp

Print this page

  1 /*
  2  * Copyright (c) 2012, 2023, 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 #include "precompiled.hpp"
 25 #include "memory/allocation.inline.hpp"
 26 #include "memory/resourceArea.hpp"
 27 #include "runtime/os.hpp"
 28 #include "runtime/os_perf.hpp"
 29 #include "runtime/vm_version.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 
 32 #ifdef __APPLE__
 33   #import <libproc.h>
 34   #include <sys/time.h>
 35   #include <sys/sysctl.h>
 36   #include <mach/mach.h>
 37   #include <mach/task_info.h>
 38   #include <sys/socket.h>
 39   #include <net/if.h>
 40   #include <net/if_dl.h>
 41   #include <net/route.h>

 42 #endif
 43 
 44 static const double NANOS_PER_SEC = 1000000000.0;
 45 
 46 class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
 47    friend class CPUPerformanceInterface;
 48  private:
 49 #ifdef __APPLE__
 50   uint64_t _total_cpu_nanos;
 51   uint64_t _total_csr_nanos;
 52   uint64_t _jvm_user_nanos;
 53   uint64_t _jvm_system_nanos;
 54   long _jvm_context_switches;
 55   long _used_ticks;
 56   long _total_ticks;
 57   int  _active_processor_count;
 58 
 59   bool now_in_nanos(uint64_t* resultp) {
 60     struct timespec tp;
 61     int status = clock_gettime(CLOCK_REALTIME, &tp);
 62     assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
 63     if (status != 0) {
 64       return false;
 65     }
 66     *resultp = tp.tv_sec * NANOS_PER_SEC + tp.tv_nsec;
 67     return true;
 68   }
 69 #endif
 70 
 71   double normalize(double value) {
 72     return MIN2<double>(MAX2<double>(value, 0.0), 1.0);
 73   }
 74   int cpu_load(int which_logical_cpu, double* cpu_load);
 75   int context_switch_rate(double* rate);
 76   int cpu_load_total_process(double* cpu_load);
 77   int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);
 78 
 79   NONCOPYABLE(CPUPerformance);
 80 
 81  public:
 82   CPUPerformance();
 83   bool initialize();
 84   ~CPUPerformance();
 85 };
 86 
 87 CPUPerformanceInterface::CPUPerformance::CPUPerformance() {
 88 #ifdef __APPLE__
 89   _total_cpu_nanos= 0;
 90   _total_csr_nanos= 0;
 91   _jvm_context_switches = 0;
 92   _jvm_user_nanos = 0;
 93   _jvm_system_nanos = 0;
 94   _used_ticks = 0;
 95   _total_ticks = 0;
 96   _active_processor_count = 0;
 97 #endif
 98 }
 99 
100 bool CPUPerformanceInterface::CPUPerformance::initialize() {
101   return true;
102 }
103 
104 CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {
105 }
106 
107 int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {
108   return FUNCTIONALITY_NOT_IMPLEMENTED;
109 }
110 
111 int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {
112 #ifdef __APPLE__
113   host_name_port_t host = mach_host_self();

135 
136   _used_ticks  = used_ticks;
137   _total_ticks = total_ticks;
138 
139   if (total_delta == 0) {
140     // Avoid division by zero
141     return OS_ERR;
142   }
143 
144   *cpu_load = (double)used_delta / total_delta;
145 
146   return OS_OK;
147 #else
148   return FUNCTIONALITY_NOT_IMPLEMENTED;
149 #endif
150 }
151 
152 int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {
153 #ifdef __APPLE__
154   int result = cpu_load_total_process(psystemTotalLoad);
155   mach_port_t task = mach_task_self();
156   mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
157   task_info_data_t task_info_data;
158   kern_return_t kr = task_info(task, TASK_ABSOLUTETIME_INFO, (task_info_t)task_info_data, &task_info_count);
159   if (kr != KERN_SUCCESS) {
160     return OS_ERR;
161   }
162   task_absolutetime_info_t absolutetime_info = (task_absolutetime_info_t)task_info_data;
163 
164   int active_processor_count = os::active_processor_count();
165   uint64_t jvm_user_nanos = absolutetime_info->total_user;
166   uint64_t jvm_system_nanos = absolutetime_info->total_system;
167 
168   uint64_t total_cpu_nanos;
169   if(!now_in_nanos(&total_cpu_nanos)) {
170     return OS_ERR;
171   }
172 
173   if (_total_cpu_nanos == 0 || active_processor_count != _active_processor_count) {
174     // First call or change in active processor count
175     result = OS_ERR;
176   } else {
177     uint64_t delta_nanos = active_processor_count * (total_cpu_nanos - _total_cpu_nanos);
178     if (delta_nanos == 0) {
179       // Avoid division by zero
180       return OS_ERR;
181     }
182 
183     *pjvmUserLoad = normalize((double)(jvm_user_nanos - _jvm_user_nanos)/delta_nanos);
184     *pjvmKernelLoad = normalize((double)(jvm_system_nanos - _jvm_system_nanos)/delta_nanos);
185   }
186 
187   _active_processor_count = active_processor_count;
188   _total_cpu_nanos = total_cpu_nanos;
189   _jvm_user_nanos = jvm_user_nanos;
190   _jvm_system_nanos = jvm_system_nanos;
191 
192   return result;
193 #else
194   return FUNCTIONALITY_NOT_IMPLEMENTED;
195 #endif
196 }
197 
198 int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {
199 #ifdef __APPLE__
200   mach_port_t task = mach_task_self();
201   mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
202   task_info_data_t task_info_data;
203   kern_return_t kr = task_info(task, TASK_EVENTS_INFO, (task_info_t)task_info_data, &task_info_count);
204   if (kr != KERN_SUCCESS) {
205     return OS_ERR;
206   }
207 
208   int result = OS_OK;
209   if (_total_csr_nanos == 0 || _jvm_context_switches == 0) {
210     // First call just set initial values.

  1 /*
  2  * Copyright (c) 2012, 2024, 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 #include "precompiled.hpp"
 25 #include "memory/allocation.inline.hpp"
 26 #include "memory/resourceArea.hpp"
 27 #include "runtime/os.hpp"
 28 #include "runtime/os_perf.hpp"
 29 #include "runtime/vm_version.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 
 32 #ifdef __APPLE__
 33   #import <libproc.h>
 34   #include <sys/time.h>
 35   #include <sys/sysctl.h>
 36   #include <mach/mach.h>
 37   #include <mach/task_info.h>
 38   #include <sys/socket.h>
 39   #include <net/if.h>
 40   #include <net/if_dl.h>
 41   #include <net/route.h>
 42   #include <sys/times.h>
 43 #endif
 44 
 45 static const double NANOS_PER_SEC = 1000000000.0;
 46 
 47 class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
 48    friend class CPUPerformanceInterface;
 49  private:
 50 #ifdef __APPLE__
 51   uint64_t _jvm_real;
 52   uint64_t _total_csr_nanos;
 53   uint64_t _jvm_user;
 54   uint64_t _jvm_system;
 55   long _jvm_context_switches;
 56   long _used_ticks;
 57   long _total_ticks;
 58   int  _active_processor_count;
 59 
 60   bool now_in_nanos(uint64_t* resultp) {
 61     struct timespec tp;
 62     int status = clock_gettime(CLOCK_REALTIME, &tp);
 63     assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
 64     if (status != 0) {
 65       return false;
 66     }
 67     *resultp = tp.tv_sec * NANOS_PER_SEC + tp.tv_nsec;
 68     return true;
 69   }
 70 #endif
 71 
 72   double normalize(double value) {
 73     return MIN2<double>(MAX2<double>(value, 0.0), 1.0);
 74   }
 75   int cpu_load(int which_logical_cpu, double* cpu_load);
 76   int context_switch_rate(double* rate);
 77   int cpu_load_total_process(double* cpu_load);
 78   int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);
 79 
 80   NONCOPYABLE(CPUPerformance);
 81 
 82  public:
 83   CPUPerformance();
 84   bool initialize();
 85   ~CPUPerformance();
 86 };
 87 
 88 CPUPerformanceInterface::CPUPerformance::CPUPerformance() {
 89 #ifdef __APPLE__
 90   _jvm_real = 0;
 91   _total_csr_nanos= 0;
 92   _jvm_context_switches = 0;
 93   _jvm_user = 0;
 94   _jvm_system = 0;
 95   _used_ticks = 0;
 96   _total_ticks = 0;
 97   _active_processor_count = 0;
 98 #endif
 99 }
100 
101 bool CPUPerformanceInterface::CPUPerformance::initialize() {
102   return true;
103 }
104 
105 CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {
106 }
107 
108 int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {
109   return FUNCTIONALITY_NOT_IMPLEMENTED;
110 }
111 
112 int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {
113 #ifdef __APPLE__
114   host_name_port_t host = mach_host_self();

136 
137   _used_ticks  = used_ticks;
138   _total_ticks = total_ticks;
139 
140   if (total_delta == 0) {
141     // Avoid division by zero
142     return OS_ERR;
143   }
144 
145   *cpu_load = (double)used_delta / total_delta;
146 
147   return OS_OK;
148 #else
149   return FUNCTIONALITY_NOT_IMPLEMENTED;
150 #endif
151 }
152 
153 int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {
154 #ifdef __APPLE__
155   int result = cpu_load_total_process(psystemTotalLoad);
156 
157   struct tms buf;
158   clock_t jvm_real = times(&buf);
159   if (jvm_real == (clock_t) (-1)) {

160     return OS_ERR;
161   }

162 
163   int active_processor_count = os::active_processor_count();
164   uint64_t jvm_user = buf.tms_utime;
165   uint64_t jvm_system = buf.tms_stime;





166 
167   if (active_processor_count != _active_processor_count) {
168     // Change in active processor count
169     result = OS_ERR;
170   } else {
171     uint64_t delta = active_processor_count * (jvm_real - _jvm_real);
172     if (delta == 0) {
173       // Avoid division by zero
174       return OS_ERR;
175     }
176 
177     *pjvmUserLoad = normalize((double)(jvm_user - _jvm_user) / delta);
178     *pjvmKernelLoad = normalize((double)(jvm_system - _jvm_system) / delta);
179   }
180 
181   _active_processor_count = active_processor_count;
182   _jvm_real = jvm_real;
183   _jvm_user = jvm_user;
184   _jvm_system = jvm_system;
185 
186   return result;
187 #else
188   return FUNCTIONALITY_NOT_IMPLEMENTED;
189 #endif
190 }
191 
192 int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {
193 #ifdef __APPLE__
194   mach_port_t task = mach_task_self();
195   mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
196   task_info_data_t task_info_data;
197   kern_return_t kr = task_info(task, TASK_EVENTS_INFO, (task_info_t)task_info_data, &task_info_count);
198   if (kr != KERN_SUCCESS) {
199     return OS_ERR;
200   }
201 
202   int result = OS_OK;
203   if (_total_csr_nanos == 0 || _jvm_context_switches == 0) {
204     // First call just set initial values.
< prev index next >