Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/solaris/vm/os_perf_solaris.cpp
32285 views
/*1* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jvm.h"26#include "memory/allocation.inline.hpp"27#include "runtime/os.hpp"28#include "runtime/os_perf.hpp"29#include "os_solaris.inline.hpp"30#include "utilities/macros.hpp"3132#ifdef TARGET_ARCH_aarch3233# include "vm_version_ext_aarch32.hpp"34#endif35#ifdef TARGET_ARCH_x8636# include "vm_version_ext_x86.hpp"37#endif38#ifdef TARGET_ARCH_sparc39# include "vm_version_ext_sparc.hpp"40#endif41#ifdef TARGET_ARCH_zero42# include "vm_version_ext_zero.hpp"43#endif44#ifdef TARGET_ARCH_arm45# include "vm_version_ext_arm.hpp"46#endif47#ifdef TARGET_ARCH_ppc48# include "vm_version_ext_ppc.hpp"49#endif5051#include <sys/types.h>52#include <procfs.h>53#include <dirent.h>54#include <errno.h>55#include <stdio.h>56#include <stdlib.h>57#include <strings.h>58#include <unistd.h>59#include <fcntl.h>60#include <kstat.h>61#include <unistd.h>62#include <string.h>63#include <sys/sysinfo.h>64#include <sys/lwp.h>65#include <pthread.h>66#include <time.h>67#include <utmpx.h>68#include <dlfcn.h>69#include <sys/loadavg.h>70#include <limits.h>7172static const double NANOS_PER_SEC = 1000000000.0;7374struct CPUPerfTicks {75kstat_t* kstat;76uint64_t last_idle;77uint64_t last_total;78double last_ratio;79};8081struct CPUPerfCounters {82int nProcs;83CPUPerfTicks* jvmTicks;84kstat_ctl_t* kstat_ctrl;85};8687static int get_info(const char* path, void* info, size_t s, off_t o) {88assert(path != NULL, "path is NULL!");89assert(info != NULL, "info is NULL!");9091int fd = -1;9293if ((fd = open(path, O_RDONLY)) < 0) {94return OS_ERR;95}96if (pread(fd, info, s, o) != s) {97close(fd);98return OS_ERR;99}100close(fd);101return OS_OK;102}103104static int get_psinfo2(void* info, size_t s, off_t o) {105return get_info("/proc/self/psinfo", info, s, o);106}107108static int get_psinfo(psinfo_t* info) {109return get_psinfo2(info, sizeof(*info), 0);110}111112static int get_psinfo(char* file, psinfo_t* info) {113assert(file != NULL, "file is NULL!");114assert(info != NULL, "info is NULL!");115return get_info(file, info, sizeof(*info), 0);116}117118119static int get_usage(prusage_t* usage) {120assert(usage != NULL, "usage is NULL!");121return get_info("/proc/self/usage", usage, sizeof(*usage), 0);122}123124static int read_cpustat(kstat_ctl_t* kstat_ctrl, CPUPerfTicks* load, cpu_stat_t* cpu_stat) {125assert(kstat_ctrl != NULL, "kstat_ctrl pointer is NULL!");126assert(load != NULL, "load pointer is NULL!");127assert(cpu_stat != NULL, "cpu_stat pointer is NULL!");128129if (load->kstat == NULL) {130// no handle.131return OS_ERR;132}133if (kstat_read(kstat_ctrl, load->kstat, cpu_stat) == OS_ERR) {134// disable handle for this CPU135load->kstat = NULL;136return OS_ERR;137}138return OS_OK;139}140141static double get_cpu_load(int which_logical_cpu, CPUPerfCounters* counters) {142assert(counters != NULL, "counters pointer is NULL!");143144cpu_stat_t cpu_stat = {0};145146if (which_logical_cpu >= counters->nProcs) {147return .0;148}149150CPUPerfTicks load = counters->jvmTicks[which_logical_cpu];151if (read_cpustat(counters->kstat_ctrl, &load, &cpu_stat) != OS_OK) {152return .0;153}154155uint_t* usage = cpu_stat.cpu_sysinfo.cpu;156if (usage == NULL) {157return .0;158}159160uint64_t c_idle = usage[CPU_IDLE];161uint64_t c_total = 0;162163for (int i = 0; i < CPU_STATES; i++) {164c_total += usage[i];165}166167// Calculate diff against previous snapshot168uint64_t d_idle = c_idle - load.last_idle;169uint64_t d_total = c_total - load.last_total;170171/** update if weve moved */172if (d_total > 0) {173// Save current values for next time around174load.last_idle = c_idle;175load.last_total = c_total;176load.last_ratio = (double) (d_total - d_idle) / d_total;177}178179return load.last_ratio;180}181182static int get_boot_time(uint64_t* time) {183assert(time != NULL, "time pointer is NULL!");184setutxent();185for(;;) {186struct utmpx* u;187if ((u = getutxent()) == NULL) {188break;189}190if (u->ut_type == BOOT_TIME) {191*time = u->ut_xtime;192endutxent();193return OS_OK;194}195}196endutxent();197return OS_ERR;198}199200static int get_noof_context_switches(CPUPerfCounters* counters, uint64_t* switches) {201assert(switches != NULL, "switches pointer is NULL!");202assert(counters != NULL, "counter pointer is NULL!");203*switches = 0;204uint64_t s = 0;205206// Collect data from all CPUs207for (int i = 0; i < counters->nProcs; i++) {208cpu_stat_t cpu_stat = {0};209CPUPerfTicks load = counters->jvmTicks[i];210211if (read_cpustat(counters->kstat_ctrl, &load, &cpu_stat) == OS_OK) {212s += cpu_stat.cpu_sysinfo.pswitch;213} else {214//fail fast...215return OS_ERR;216}217}218*switches = s;219return OS_OK;220}221222static int perf_context_switch_rate(CPUPerfCounters* counters, double* rate) {223assert(counters != NULL, "counters is NULL!");224assert(rate != NULL, "rate pointer is NULL!");225static pthread_mutex_t contextSwitchLock = PTHREAD_MUTEX_INITIALIZER;226static uint64_t lastTime = 0;227static uint64_t lastSwitches = 0;228static double lastRate = 0.0;229230uint64_t lt = 0;231int res = 0;232233if (lastTime == 0) {234uint64_t tmp;235if (get_boot_time(&tmp) < 0) {236return OS_ERR;237}238lt = tmp * 1000;239}240241res = OS_OK;242243pthread_mutex_lock(&contextSwitchLock);244{245246uint64_t sw = 0;247clock_t t, d;248249if (lastTime == 0) {250lastTime = lt;251}252253t = clock();254d = t - lastTime;255256if (d == 0) {257*rate = lastRate;258} else if (get_noof_context_switches(counters, &sw)== OS_OK) {259*rate = ((double)(sw - lastSwitches) / d) * 1000;260lastRate = *rate;261lastSwitches = sw;262lastTime = t;263} else {264*rate = 0.0;265res = OS_ERR;266}267if (*rate < 0.0) {268*rate = 0.0;269lastRate = 0.0;270}271}272pthread_mutex_unlock(&contextSwitchLock);273return res;274}275276277278class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {279friend class CPUPerformanceInterface;280private:281CPUPerfCounters _counters;282int cpu_load(int which_logical_cpu, double* cpu_load);283int context_switch_rate(double* rate);284int cpu_load_total_process(double* cpu_load);285int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);286287CPUPerformance();288~CPUPerformance();289bool initialize();290};291292CPUPerformanceInterface::CPUPerformance::CPUPerformance() {293_counters.nProcs = 0;294_counters.jvmTicks = NULL;295_counters.kstat_ctrl = NULL;296}297298bool CPUPerformanceInterface::CPUPerformance::initialize() {299// initialize kstat control structure,300_counters.kstat_ctrl = kstat_open();301assert(_counters.kstat_ctrl != NULL, "error initializing kstat control structure!");302303if (NULL == _counters.kstat_ctrl) {304return false;305}306307// Get number of CPU(s)308if ((_counters.nProcs = sysconf(_SC_NPROCESSORS_ONLN)) == OS_ERR) {309// ignore error?310_counters.nProcs = 1;311}312313assert(_counters.nProcs > 0, "no CPUs detected in sysconf call!");314if (_counters.nProcs == 0) {315return false;316}317318// Data structure(s) for saving CPU load (one per CPU)319size_t tick_array_size = _counters.nProcs * sizeof(CPUPerfTicks);320_counters.jvmTicks = (CPUPerfTicks*)NEW_C_HEAP_ARRAY(char, tick_array_size, mtInternal);321if (NULL == _counters.jvmTicks) {322return false;323}324memset(_counters.jvmTicks, 0, tick_array_size);325326// Get kstat cpu_stat counters for every CPU327// loop over kstat to find our cpu_stat(s)328int i = 0;329for (kstat_t* kstat = _counters.kstat_ctrl->kc_chain; kstat != NULL; kstat = kstat->ks_next) {330if (strncmp(kstat->ks_module, "cpu_stat", 8) == 0) {331if (kstat_read(_counters.kstat_ctrl, kstat, NULL) == OS_ERR) {332continue;333}334if (i == _counters.nProcs) {335// more cpu_stats than reported CPUs336break;337}338_counters.jvmTicks[i++].kstat = kstat;339}340}341return true;342}343344CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {345if (_counters.jvmTicks != NULL) {346FREE_C_HEAP_ARRAY(char, _counters.jvmTicks, mtInternal);347}348if (_counters.kstat_ctrl != NULL) {349kstat_close(_counters.kstat_ctrl);350}351}352353int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {354assert(cpu_load != NULL, "cpu_load pointer is NULL!");355double t = .0;356if (-1 == which_logical_cpu) {357for (int i = 0; i < _counters.nProcs; i++) {358t += get_cpu_load(i, &_counters);359}360// Cap total systemload to 1.0361t = MIN2<double>((t / _counters.nProcs), 1.0);362} else {363t = MIN2<double>(get_cpu_load(which_logical_cpu, &_counters), 1.0);364}365366*cpu_load = t;367return OS_OK;368}369370int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {371assert(cpu_load != NULL, "cpu_load pointer is NULL!");372373psinfo_t info;374375// Get the percentage of "recent cpu usage" from all the lwp:s in the JVM:s376// process. This is returned as a value between 0.0 and 1.0 multiplied by 0x8000.377if (get_psinfo2(&info.pr_pctcpu, sizeof(info.pr_pctcpu), offsetof(psinfo_t, pr_pctcpu)) != 0) {378*cpu_load = 0.0;379return OS_ERR;380}381*cpu_load = (double) info.pr_pctcpu / 0x8000;382return OS_OK;383}384385int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {386assert(pjvmUserLoad != NULL, "pjvmUserLoad not inited");387assert(pjvmKernelLoad != NULL, "pjvmKernelLoad not inited");388assert(psystemTotalLoad != NULL, "psystemTotalLoad not inited");389390static uint64_t lastTime;391static uint64_t lastUser, lastKernel;392static double lastUserRes, lastKernelRes;393394pstatus_t pss;395psinfo_t info;396397*pjvmKernelLoad = *pjvmUserLoad = *psystemTotalLoad = 0;398if (get_info("/proc/self/status", &pss.pr_utime, sizeof(timestruc_t)*2, offsetof(pstatus_t, pr_utime)) != 0) {399return OS_ERR;400}401402if (get_psinfo(&info) != 0) {403return OS_ERR;404}405406// get the total time in user, kernel and total time407// check ratios for 'lately' and multiply the 'recent load'.408uint64_t time = (info.pr_time.tv_sec * NANOS_PER_SEC) + info.pr_time.tv_nsec;409uint64_t user = (pss.pr_utime.tv_sec * NANOS_PER_SEC) + pss.pr_utime.tv_nsec;410uint64_t kernel = (pss.pr_stime.tv_sec * NANOS_PER_SEC) + pss.pr_stime.tv_nsec;411uint64_t diff = time - lastTime;412double load = (double) info.pr_pctcpu / 0x8000;413414if (diff > 0) {415lastUserRes = (load * (user - lastUser)) / diff;416lastKernelRes = (load * (kernel - lastKernel)) / diff;417418// BUG9182835 - patch for clamping these values to sane ones.419lastUserRes = MIN2<double>(1, lastUserRes);420lastUserRes = MAX2<double>(0, lastUserRes);421lastKernelRes = MIN2<double>(1, lastKernelRes);422lastKernelRes = MAX2<double>(0, lastKernelRes);423}424425double t = .0;426cpu_load(-1, &t);427// clamp at user+system and 1.0428if (lastUserRes + lastKernelRes > t) {429t = MIN2<double>(lastUserRes + lastKernelRes, 1.0);430}431432*pjvmUserLoad = lastUserRes;433*pjvmKernelLoad = lastKernelRes;434*psystemTotalLoad = t;435436lastTime = time;437lastUser = user;438lastKernel = kernel;439440return OS_OK;441}442443int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {444return perf_context_switch_rate(&_counters, rate);445}446447CPUPerformanceInterface::CPUPerformanceInterface() {448_impl = NULL;449}450451bool CPUPerformanceInterface::initialize() {452_impl = new CPUPerformanceInterface::CPUPerformance();453return _impl != NULL && _impl->initialize();454}455456CPUPerformanceInterface::~CPUPerformanceInterface(void) {457if (_impl != NULL) {458delete _impl;459}460}461462int CPUPerformanceInterface::cpu_load(int which_logical_cpu, double* const cpu_load) const {463return _impl->cpu_load(which_logical_cpu, cpu_load);464}465466int CPUPerformanceInterface::cpu_load_total_process(double* const cpu_load) const {467return _impl->cpu_load_total_process(cpu_load);468}469470int CPUPerformanceInterface::cpu_loads_process(double* const pjvmUserLoad, double* const pjvmKernelLoad, double* const psystemTotalLoad) const {471return _impl->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotalLoad);472}473474int CPUPerformanceInterface::context_switch_rate(double* const rate) const {475return _impl->context_switch_rate(rate);476}477478class SystemProcessInterface::SystemProcesses : public CHeapObj<mtInternal> {479friend class SystemProcessInterface;480private:481class ProcessIterator : public CHeapObj<mtInternal> {482friend class SystemProcessInterface::SystemProcesses;483private:484DIR* _dir;485struct dirent* _entry;486bool _valid;487488ProcessIterator();489~ProcessIterator();490bool initialize();491492bool is_valid() const { return _valid; }493bool is_valid_entry(struct dirent* const entry) const;494bool is_dir(const char* const name) const;495char* allocate_string(const char* const str) const;496int current(SystemProcess* const process_info);497int next_process();498};499500ProcessIterator* _iterator;501SystemProcesses();502bool initialize();503~SystemProcesses();504505//information about system processes506int system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const;507};508509bool SystemProcessInterface::SystemProcesses::ProcessIterator::is_dir(const char* name) const {510struct stat64 mystat;511int ret_val = 0;512513ret_val = ::stat64(name, &mystat);514515if (ret_val < 0) {516return false;517}518ret_val = S_ISDIR(mystat.st_mode);519return ret_val > 0;520}521522// if it has a numeric name, is a directory and has a 'psinfo' file in it523bool SystemProcessInterface::SystemProcesses::ProcessIterator::is_valid_entry(struct dirent* entry) const {524// ignore the "." and ".." directories525if ((strcmp(entry->d_name, ".") == 0) ||526(strcmp(entry->d_name, "..") == 0)) {527return false;528}529530char buffer[PATH_MAX] = {0};531uint64_t size = 0;532bool result = false;533FILE *fp = NULL;534535if (atoi(entry->d_name) != 0) {536jio_snprintf(buffer, PATH_MAX, "/proc/%s", entry->d_name);537538if (is_dir(buffer)) {539memset(buffer, 0, PATH_MAX);540jio_snprintf(buffer, PATH_MAX, "/proc/%s/psinfo", entry->d_name);541if ((fp = fopen(buffer, "r")) != NULL) {542int nread = 0;543psinfo_t psinfo_data;544if ((nread = fread(&psinfo_data, 1, sizeof(psinfo_t), fp)) != -1) {545// only considering system process owned by root546if (psinfo_data.pr_uid == 0) {547result = true;548}549}550}551}552}553554if (fp != NULL) {555fclose(fp);556}557558return result;559}560561char* SystemProcessInterface::SystemProcesses::ProcessIterator::allocate_string(const char* str) const {562if (str != NULL) {563size_t len = strlen(str);564char* tmp = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);565strncpy(tmp, str, len);566tmp[len] = '\0';567return tmp;568}569return NULL;570}571572int SystemProcessInterface::SystemProcesses::ProcessIterator::current(SystemProcess* process_info) {573if (!is_valid()) {574return OS_ERR;575}576577char psinfo_path[PATH_MAX] = {0};578jio_snprintf(psinfo_path, PATH_MAX, "/proc/%s/psinfo", _entry->d_name);579580FILE *fp = NULL;581if ((fp = fopen(psinfo_path, "r")) == NULL) {582return OS_ERR;583}584585int nread = 0;586psinfo_t psinfo_data;587if ((nread = fread(&psinfo_data, 1, sizeof(psinfo_t), fp)) == -1) {588fclose(fp);589return OS_ERR;590}591592char *exe_path = NULL;593if ((psinfo_data.pr_fname != NULL) &&594(psinfo_data.pr_psargs != NULL)) {595char *path_substring = strstr(psinfo_data.pr_psargs, psinfo_data.pr_fname);596if (path_substring != NULL) {597int len = path_substring - psinfo_data.pr_psargs;598exe_path = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);599if (exe_path != NULL) {600jio_snprintf(exe_path, len, "%s", psinfo_data.pr_psargs);601exe_path[len] = '\0';602}603}604}605606process_info->set_pid(atoi(_entry->d_name));607process_info->set_name(allocate_string(psinfo_data.pr_fname));608process_info->set_path(allocate_string(exe_path));609process_info->set_command_line(allocate_string(psinfo_data.pr_psargs));610611if (exe_path != NULL) {612FREE_C_HEAP_ARRAY(char, exe_path, mtInternal);613}614615if (fp != NULL) {616fclose(fp);617}618619return OS_OK;620}621622int SystemProcessInterface::SystemProcesses::ProcessIterator::next_process() {623if (!is_valid()) {624return OS_ERR;625}626627do {628_entry = os::readdir(_dir);629if (_entry == NULL) {630// Error or reached end. Could use errno to distinguish those cases.631_valid = false;632return OS_ERR;633}634} while(!is_valid_entry(_entry));635636_valid = true;637return OS_OK;638}639640SystemProcessInterface::SystemProcesses::ProcessIterator::ProcessIterator() {641_dir = NULL;642_entry = NULL;643_valid = false;644}645646bool SystemProcessInterface::SystemProcesses::ProcessIterator::initialize() {647_dir = os::opendir("/proc");648_entry = NULL;649_valid = true;650next_process();651652return true;653}654655SystemProcessInterface::SystemProcesses::ProcessIterator::~ProcessIterator() {656if (_dir != NULL) {657os::closedir(_dir);658}659}660661SystemProcessInterface::SystemProcesses::SystemProcesses() {662_iterator = NULL;663}664665bool SystemProcessInterface::SystemProcesses::initialize() {666_iterator = new SystemProcessInterface::SystemProcesses::ProcessIterator();667return _iterator != NULL && _iterator->initialize();668}669670SystemProcessInterface::SystemProcesses::~SystemProcesses() {671if (_iterator != NULL) {672delete _iterator;673}674}675676int SystemProcessInterface::SystemProcesses::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const {677assert(system_processes != NULL, "system_processes pointer is NULL!");678assert(no_of_sys_processes != NULL, "system_processes counter pointer is NULL!");679assert(_iterator != NULL, "iterator is NULL!");680681// initialize pointers682*no_of_sys_processes = 0;683*system_processes = NULL;684685while (_iterator->is_valid()) {686SystemProcess* tmp = new SystemProcess();687_iterator->current(tmp);688689//if already existing head690if (*system_processes != NULL) {691//move "first to second"692tmp->set_next(*system_processes);693}694// new head695*system_processes = tmp;696// increment697(*no_of_sys_processes)++;698// step forward699_iterator->next_process();700}701return OS_OK;702}703704int SystemProcessInterface::system_processes(SystemProcess** system_procs, int* const no_of_sys_processes) const {705return _impl->system_processes(system_procs, no_of_sys_processes);706}707708SystemProcessInterface::SystemProcessInterface() {709_impl = NULL;710}711712bool SystemProcessInterface::initialize() {713_impl = new SystemProcessInterface::SystemProcesses();714return _impl != NULL && _impl->initialize();715716}717718SystemProcessInterface::~SystemProcessInterface() {719if (_impl != NULL) {720delete _impl;721}722}723724CPUInformationInterface::CPUInformationInterface() {725_cpu_info = NULL;726}727728bool CPUInformationInterface::initialize() {729_cpu_info = new CPUInformation();730if (_cpu_info == NULL) {731return false;732}733_cpu_info->set_number_of_hardware_threads(VM_Version_Ext::number_of_threads());734_cpu_info->set_number_of_cores(VM_Version_Ext::number_of_cores());735_cpu_info->set_number_of_sockets(VM_Version_Ext::number_of_sockets());736_cpu_info->set_cpu_name(VM_Version_Ext::cpu_name());737_cpu_info->set_cpu_description(VM_Version_Ext::cpu_description());738return true;739}740741CPUInformationInterface::~CPUInformationInterface() {742if (_cpu_info != NULL) {743if (_cpu_info->cpu_name() != NULL) {744const char* cpu_name = _cpu_info->cpu_name();745FREE_C_HEAP_ARRAY(char, cpu_name, mtInternal);746_cpu_info->set_cpu_name(NULL);747}748if (_cpu_info->cpu_description() != NULL) {749const char* cpu_desc = _cpu_info->cpu_description();750FREE_C_HEAP_ARRAY(char, cpu_desc, mtInternal);751_cpu_info->set_cpu_description(NULL);752}753delete _cpu_info;754}755}756757int CPUInformationInterface::cpu_information(CPUInformation& cpu_info) {758if (_cpu_info == NULL) {759return OS_ERR;760}761762cpu_info = *_cpu_info; // shallow copy assignment763return OS_OK;764}765766class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> {767friend class NetworkPerformanceInterface;768private:769NetworkPerformance();770NetworkPerformance(const NetworkPerformance& rhs); // no impl771NetworkPerformance& operator=(const NetworkPerformance& rhs); // no impl772bool initialize();773~NetworkPerformance();774int network_utilization(NetworkInterface** network_interfaces) const;775};776777NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() {778779}780781bool NetworkPerformanceInterface::NetworkPerformance::initialize() {782return true;783}784785NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() {786787}788789int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const790{791kstat_ctl_t* ctl = kstat_open();792if (ctl == NULL) {793return OS_ERR;794}795796NetworkInterface* ret = NULL;797for (kstat_t* k = ctl->kc_chain; k != NULL; k = k->ks_next) {798if (strcmp(k->ks_class, "net") != 0) {799continue;800}801if (strcmp(k->ks_module, "link") != 0) {802continue;803}804805if (kstat_read(ctl, k, NULL) == -1) {806return OS_ERR;807}808809uint64_t bytes_in = UINT64_MAX;810uint64_t bytes_out = UINT64_MAX;811for (int i = 0; i < k->ks_ndata; ++i) {812kstat_named_t* data = &reinterpret_cast<kstat_named_t*>(k->ks_data)[i];813if (strcmp(data->name, "rbytes64") == 0) {814bytes_in = data->value.ui64;815}816else if (strcmp(data->name, "obytes64") == 0) {817bytes_out = data->value.ui64;818}819}820821if ((bytes_in != UINT64_MAX) && (bytes_out != UINT64_MAX)) {822NetworkInterface* cur = new NetworkInterface(k->ks_name, bytes_in, bytes_out, ret);823ret = cur;824}825}826827kstat_close(ctl);828*network_interfaces = ret;829830return OS_OK;831}832833NetworkPerformanceInterface::NetworkPerformanceInterface() {834_impl = NULL;835}836837NetworkPerformanceInterface::~NetworkPerformanceInterface() {838if (_impl != NULL) {839delete _impl;840}841}842843bool NetworkPerformanceInterface::initialize() {844_impl = new NetworkPerformanceInterface::NetworkPerformance();845return _impl != NULL && _impl->initialize();846}847848int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const {849return _impl->network_utilization(network_interfaces);850}851852853