src/os/bsd/vm/os_perf_bsd.cpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

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

mercurial