1 /*
  2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "jvm.h"
 27 #include "memory/allocation.inline.hpp"
 28 #include "runtime/os.hpp"
 29 #include "runtime/os_perf.hpp"
 30 #include "os_solaris.inline.hpp"
 31 #include "utilities/macros.hpp"
 32 
 33 #include CPU_HEADER(vm_version_ext)
 34 
 35 #include <sys/types.h>
 36 #include <procfs.h>
 37 #include <dirent.h>
 38 #include <errno.h>
 39 #include <stdio.h>
 40 #include <stdlib.h>
 41 #include <strings.h>
 42 #include <unistd.h>
 43 #include <fcntl.h>
 44 #include <kstat.h>
 45 #include <unistd.h>
 46 #include <string.h>
 47 #include <sys/sysinfo.h>
 48 #include <sys/lwp.h>
 49 #include <pthread.h>
 50 #include <time.h>
 51 #include <utmpx.h>
 52 #include <dlfcn.h>
 53 #include <sys/loadavg.h>
 54 #include <limits.h>
 55 
 56 static const double NANOS_PER_SEC = 1000000000.0;
 57 
 58 struct CPUPerfTicks {
 59   kstat_t* kstat;
 60   uint64_t last_idle;
 61   uint64_t last_total;
 62   double   last_ratio;
 63 };
 64 
 65 struct CPUPerfCounters {
 66   int           nProcs;
 67   CPUPerfTicks* jvmTicks;
 68   kstat_ctl_t*  kstat_ctrl;
 69 };
 70 
 71 static int get_info(const char* path, void* info, size_t s, off_t o) {
 72   assert(path != NULL, "path is NULL!");
 73   assert(info != NULL, "info is NULL!");
 74 
 75   int fd = -1;
 76 
 77   if ((fd = os::open(path, O_RDONLY, 0)) < 0) {
 78     return OS_ERR;
 79   }
 80   if (pread(fd, info, s, o) != s) {
 81     close(fd);
 82     return OS_ERR;
 83   }
 84   close(fd);
 85   return OS_OK;
 86 }
 87 
 88 static int get_psinfo2(void* info, size_t s, off_t o) {
 89   return get_info("/proc/self/psinfo", info, s, o);
 90 }
 91 
 92 static int get_psinfo(psinfo_t* info) {
 93   return get_psinfo2(info, sizeof(*info), 0);
 94 }
 95 
 96 static int get_psinfo(char* file, psinfo_t* info) {
 97   assert(file != NULL, "file is NULL!");
 98   assert(info != NULL, "info is NULL!");
 99   return get_info(file, info, sizeof(*info), 0);
100 }
101 
102 
103 static int get_usage(prusage_t* usage) {
104   assert(usage != NULL, "usage is NULL!");
105   return get_info("/proc/self/usage", usage, sizeof(*usage), 0);
106 }
107 
108 static int read_cpustat(kstat_ctl_t* kstat_ctrl, CPUPerfTicks* load, cpu_stat_t* cpu_stat) {
109   assert(kstat_ctrl != NULL, "kstat_ctrl pointer is NULL!");
110   assert(load != NULL, "load pointer is NULL!");
111   assert(cpu_stat != NULL, "cpu_stat pointer is NULL!");
112 
113   if (load->kstat == NULL) {
114     // no handle.
115     return OS_ERR;
116   }
117   if (kstat_read(kstat_ctrl, load->kstat, cpu_stat) == OS_ERR) {
118     // disable handle for this CPU
119      load->kstat = NULL;
120      return OS_ERR;
121   }
122   return OS_OK;
123 }
124 
125 static double get_cpu_load(int which_logical_cpu, CPUPerfCounters* counters) {
126   assert(counters != NULL, "counters pointer is NULL!");
127 
128   cpu_stat_t  cpu_stat = {0};
129 
130   if (which_logical_cpu >= counters->nProcs) {
131     return .0;
132   }
133 
134   CPUPerfTicks load = counters->jvmTicks[which_logical_cpu];
135   if (read_cpustat(counters->kstat_ctrl, &load, &cpu_stat) != OS_OK) {
136     return .0;
137   }
138 
139   uint_t* usage = cpu_stat.cpu_sysinfo.cpu;
140   if (usage == NULL) {
141     return .0;
142   }
143 
144   uint64_t c_idle  = usage[CPU_IDLE];
145   uint64_t c_total = 0;
146 
147   for (int i = 0; i < CPU_STATES; i++) {
148     c_total += usage[i];
149   }
150 
151   // Calculate diff against previous snapshot
152   uint64_t d_idle  = c_idle - load.last_idle;
153   uint64_t d_total = c_total - load.last_total;
154 
155   /** update if weve moved */
156   if (d_total > 0) {
157     // Save current values for next time around
158     load.last_idle  = c_idle;
159     load.last_total = c_total;
160     load.last_ratio = (double) (d_total - d_idle) / d_total;
161   }
162 
163   return load.last_ratio;
164 }
165 
166 static int get_boot_time(uint64_t* time) {
167   assert(time != NULL, "time pointer is NULL!");
168   setutxent();
169   for(;;) {
170     struct utmpx* u;
171     if ((u = getutxent()) == NULL) {
172       break;
173     }
174     if (u->ut_type == BOOT_TIME) {
175       *time = u->ut_xtime;
176       endutxent();
177       return OS_OK;
178     }
179   }
180   endutxent();
181   return OS_ERR;
182 }
183 
184 static int get_noof_context_switches(CPUPerfCounters* counters, uint64_t* switches) {
185   assert(switches != NULL, "switches pointer is NULL!");
186   assert(counters != NULL, "counter pointer is NULL!");
187   *switches = 0;
188   uint64_t s = 0;
189 
190   // Collect data from all CPUs
191   for (int i = 0; i < counters->nProcs; i++) {
192     cpu_stat_t cpu_stat = {0};
193     CPUPerfTicks load = counters->jvmTicks[i];
194 
195     if (read_cpustat(counters->kstat_ctrl, &load, &cpu_stat) == OS_OK) {
196       s += cpu_stat.cpu_sysinfo.pswitch;
197     } else {
198       //fail fast...
199       return OS_ERR;
200     }
201   }
202   *switches = s;
203   return OS_OK;
204 }
205 
206 static int perf_context_switch_rate(CPUPerfCounters* counters, double* rate) {
207   assert(counters != NULL, "counters is NULL!");
208   assert(rate != NULL, "rate pointer is NULL!");
209   static pthread_mutex_t contextSwitchLock = PTHREAD_MUTEX_INITIALIZER;
210   static uint64_t lastTime = 0;
211   static uint64_t lastSwitches = 0;
212   static double   lastRate = 0.0;
213 
214   uint64_t lt = 0;
215   int res = 0;
216 
217   if (lastTime == 0) {
218     uint64_t tmp;
219     if (get_boot_time(&tmp) < 0) {
220       return OS_ERR;
221     }
222     lt = tmp * 1000;
223   }
224 
225   res = OS_OK;
226 
227   pthread_mutex_lock(&contextSwitchLock);
228   {
229 
230     uint64_t sw = 0;
231     clock_t t, d;
232 
233     if (lastTime == 0) {
234       lastTime = lt;
235     }
236 
237     t = clock();
238     d = t - lastTime;
239 
240     if (d == 0) {
241       *rate = lastRate;
242     } else if (get_noof_context_switches(counters, &sw)== OS_OK) {
243       *rate      = ((double)(sw - lastSwitches) / d) * 1000;
244       lastRate     = *rate;
245       lastSwitches = sw;
246       lastTime     = t;
247     } else {
248       *rate = 0.0;
249       res   = OS_ERR;
250     }
251     if (*rate < 0.0) {
252       *rate = 0.0;
253       lastRate = 0.0;
254     }
255   }
256   pthread_mutex_unlock(&contextSwitchLock);
257   return res;
258  }
259 
260 
261 
262 class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
263    friend class CPUPerformanceInterface;
264  private:
265   CPUPerfCounters _counters;
266   int cpu_load(int which_logical_cpu, double* cpu_load);
267   int context_switch_rate(double* rate);
268   int cpu_load_total_process(double* cpu_load);
269   int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);
270 
271   CPUPerformance();
272   ~CPUPerformance();
273   bool initialize();
274 };
275 
276 CPUPerformanceInterface::CPUPerformance::CPUPerformance() {
277   _counters.nProcs = 0;
278   _counters.jvmTicks = NULL;
279   _counters.kstat_ctrl = NULL;
280 }
281 
282 bool CPUPerformanceInterface::CPUPerformance::initialize() {
283   // initialize kstat control structure,
284   _counters.kstat_ctrl = kstat_open();
285   assert(_counters.kstat_ctrl != NULL, "error initializing kstat control structure!");
286 
287   if (NULL == _counters.kstat_ctrl) {
288     return false;
289   }
290 
291   // Get number of CPU(s)
292   if ((_counters.nProcs = sysconf(_SC_NPROCESSORS_ONLN)) == OS_ERR) {
293     // ignore error?
294     _counters.nProcs = 1;
295   }
296 
297   assert(_counters.nProcs > 0, "no CPUs detected in sysconf call!");
298   if (_counters.nProcs == 0) {
299     return false;
300   }
301 
302   // Data structure(s) for saving CPU load (one per CPU)
303   size_t array_entry_count = _counters.nProcs;
304   _counters.jvmTicks = NEW_C_HEAP_ARRAY(CPUPerfTicks, array_entry_count, mtInternal);
305   if (NULL == _counters.jvmTicks) {
306     return false;
307   }
308   memset(_counters.jvmTicks, 0, array_entry_count * sizeof(*_counters.jvmTicks));
309 
310   // Get kstat cpu_stat counters for every CPU
311   // loop over kstat to find our cpu_stat(s)
312   int i = 0;
313   for (kstat_t* kstat = _counters.kstat_ctrl->kc_chain; kstat != NULL; kstat = kstat->ks_next) {
314     if (strncmp(kstat->ks_module, "cpu_stat", 8) == 0) {
315       if (kstat_read(_counters.kstat_ctrl, kstat, NULL) == OS_ERR) {
316         continue;
317       }
318       if (i == _counters.nProcs) {
319         // more cpu_stats than reported CPUs
320         break;
321       }
322       _counters.jvmTicks[i++].kstat = kstat;
323     }
324   }
325   return true;
326 }
327 
328 CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {
329   FREE_C_HEAP_ARRAY(char, _counters.jvmTicks);
330   if (_counters.kstat_ctrl != NULL) {
331     kstat_close(_counters.kstat_ctrl);
332   }
333 }
334 
335 int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {
336   assert(cpu_load != NULL, "cpu_load pointer is NULL!");
337   double t = .0;
338   if (-1 == which_logical_cpu) {
339     for (int i = 0; i < _counters.nProcs; i++) {
340       t += get_cpu_load(i, &_counters);
341     }
342     // Cap total systemload to 1.0
343     t = MIN2<double>((t / _counters.nProcs), 1.0);
344   } else {
345     t = MIN2<double>(get_cpu_load(which_logical_cpu, &_counters), 1.0);
346   }
347 
348   *cpu_load = t;
349   return OS_OK;
350 }
351 
352 int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {
353   assert(cpu_load != NULL, "cpu_load pointer is NULL!");
354 
355   psinfo_t info;
356 
357   // Get the percentage of "recent cpu usage" from all the lwp:s in the JVM:s
358   // process. This is returned as a value between 0.0 and 1.0 multiplied by 0x8000.
359   if (get_psinfo2(&info.pr_pctcpu, sizeof(info.pr_pctcpu), offsetof(psinfo_t, pr_pctcpu)) != 0) {
360     *cpu_load = 0.0;
361     return OS_ERR;
362   }
363   *cpu_load = (double) info.pr_pctcpu / 0x8000;
364   return OS_OK;
365 }
366 
367 int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {
368   assert(pjvmUserLoad != NULL, "pjvmUserLoad not inited");
369   assert(pjvmKernelLoad != NULL, "pjvmKernelLoad not inited");
370   assert(psystemTotalLoad != NULL, "psystemTotalLoad not inited");
371 
372   static uint64_t lastTime;
373   static uint64_t lastUser, lastKernel;
374   static double lastUserRes, lastKernelRes;
375 
376   pstatus_t pss;
377   psinfo_t  info;
378 
379   *pjvmKernelLoad = *pjvmUserLoad = *psystemTotalLoad = 0;
380   if (get_info("/proc/self/status", &pss.pr_utime, sizeof(timestruc_t)*2, offsetof(pstatus_t, pr_utime)) != 0) {
381     return OS_ERR;
382   }
383 
384   if (get_psinfo(&info) != 0) {
385     return OS_ERR;
386   }
387 
388   // get the total time in user, kernel and total time
389   // check ratios for 'lately' and multiply the 'recent load'.
390   uint64_t time   = (info.pr_time.tv_sec * NANOS_PER_SEC) + info.pr_time.tv_nsec;
391   uint64_t user   = (pss.pr_utime.tv_sec * NANOS_PER_SEC) + pss.pr_utime.tv_nsec;
392   uint64_t kernel = (pss.pr_stime.tv_sec * NANOS_PER_SEC) + pss.pr_stime.tv_nsec;
393   uint64_t diff   = time - lastTime;
394   double load     = (double) info.pr_pctcpu / 0x8000;
395 
396   if (diff > 0) {
397     lastUserRes = (load * (user - lastUser)) / diff;
398     lastKernelRes = (load * (kernel - lastKernel)) / diff;
399 
400     // BUG9182835 - patch for clamping these values to sane ones.
401     lastUserRes   = MIN2<double>(1, lastUserRes);
402     lastUserRes   = MAX2<double>(0, lastUserRes);
403     lastKernelRes = MIN2<double>(1, lastKernelRes);
404     lastKernelRes = MAX2<double>(0, lastKernelRes);
405   }
406 
407   double t = .0;
408   cpu_load(-1, &t);
409   // clamp at user+system and 1.0
410   if (lastUserRes + lastKernelRes > t) {
411     t = MIN2<double>(lastUserRes + lastKernelRes, 1.0);
412   }
413 
414   *pjvmUserLoad   = lastUserRes;
415   *pjvmKernelLoad = lastKernelRes;
416   *psystemTotalLoad = t;
417 
418   lastTime   = time;
419   lastUser   = user;
420   lastKernel = kernel;
421 
422   return OS_OK;
423 }
424 
425 int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {
426   return perf_context_switch_rate(&_counters, rate);
427 }
428 
429 CPUPerformanceInterface::CPUPerformanceInterface() {
430   _impl = NULL;
431 }
432 
433 bool CPUPerformanceInterface::initialize() {
434   _impl = new CPUPerformanceInterface::CPUPerformance();
435   return _impl != NULL && _impl->initialize();
436 }
437 
438 CPUPerformanceInterface::~CPUPerformanceInterface(void) {
439   if (_impl != NULL) {
440     delete _impl;
441   }
442 }
443 
444 int CPUPerformanceInterface::cpu_load(int which_logical_cpu, double* cpu_load) const {
445   return _impl->cpu_load(which_logical_cpu, cpu_load);
446 }
447 
448 int CPUPerformanceInterface::cpu_load_total_process(double* cpu_load) const {
449   return _impl->cpu_load_total_process(cpu_load);
450 }
451 
452 int CPUPerformanceInterface::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) const {
453   return _impl->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotalLoad);
454 }
455 
456 int CPUPerformanceInterface::context_switch_rate(double* rate) const {
457   return _impl->context_switch_rate(rate);
458 }
459 
460 class SystemProcessInterface::SystemProcesses : public CHeapObj<mtInternal> {
461   friend class SystemProcessInterface;
462  private:
463   class ProcessIterator : public CHeapObj<mtInternal> {
464     friend class SystemProcessInterface::SystemProcesses;
465    private:
466     DIR*           _dir;
467     struct dirent* _entry;
468     bool           _valid;
469 
470     ProcessIterator();
471     ~ProcessIterator();
472     bool initialize();
473 
474     bool is_valid() const { return _valid; }
475     bool is_valid_entry(struct dirent* const entry) const;
476     bool is_dir(const char* const name) const;
477     char* allocate_string(const char* const str) const;
478     int current(SystemProcess* const process_info);
479     int next_process();
480   };
481 
482   ProcessIterator* _iterator;
483   SystemProcesses();
484   bool initialize();
485   ~SystemProcesses();
486 
487   //information about system processes
488   int system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const;
489 };
490 
491 bool SystemProcessInterface::SystemProcesses::ProcessIterator::is_dir(const char* name) const {
492   struct stat64 mystat;
493   int ret_val = 0;
494 
495   ret_val = ::stat64(name, &mystat);
496 
497   if (ret_val < 0) {
498     return false;
499   }
500   ret_val = S_ISDIR(mystat.st_mode);
501   return ret_val > 0;
502 }
503 
504 // if it has a numeric name, is a directory and has a 'psinfo' file in it
505 bool SystemProcessInterface::SystemProcesses::ProcessIterator::is_valid_entry(struct dirent* entry) const {
506   // ignore the "." and ".." directories
507   if ((strcmp(entry->d_name, ".") == 0) ||
508       (strcmp(entry->d_name, "..") == 0)) {
509     return false;
510   }
511 
512   char buffer[PATH_MAX] = {0};
513   uint64_t size = 0;
514   bool result = false;
515   FILE *fp = NULL;
516 
517   if (atoi(entry->d_name) != 0) {
518     jio_snprintf(buffer, PATH_MAX, "/proc/%s", entry->d_name);
519 
520     if (is_dir(buffer)) {
521       memset(buffer, 0, PATH_MAX);
522       jio_snprintf(buffer, PATH_MAX, "/proc/%s/psinfo", entry->d_name);
523       if ((fp = fopen(buffer, "r")) != NULL) {
524         int nread = 0;
525         psinfo_t psinfo_data;
526         if ((nread = fread(&psinfo_data, 1, sizeof(psinfo_t), fp)) != -1) {
527           // only considering system process owned by root
528           if (psinfo_data.pr_uid == 0) {
529             result = true;
530           }
531         }
532       }
533     }
534   }
535 
536   if (fp != NULL) {
537     fclose(fp);
538   }
539 
540   return result;
541 }
542 
543 char* SystemProcessInterface::SystemProcesses::ProcessIterator::allocate_string(const char* str) const {
544   if (str != NULL) {
545     return os::strdup_check_oom(str, mtInternal);
546   }
547   return NULL;
548 }
549 
550 int SystemProcessInterface::SystemProcesses::ProcessIterator::current(SystemProcess* process_info) {
551   if (!is_valid()) {
552     return OS_ERR;
553   }
554 
555   char psinfo_path[PATH_MAX] = {0};
556   jio_snprintf(psinfo_path, PATH_MAX, "/proc/%s/psinfo", _entry->d_name);
557 
558   FILE *fp = NULL;
559   if ((fp = fopen(psinfo_path, "r")) == NULL) {
560     return OS_ERR;
561   }
562 
563   int nread = 0;
564   psinfo_t psinfo_data;
565   if ((nread = fread(&psinfo_data, 1, sizeof(psinfo_t), fp)) == -1) {
566     fclose(fp);
567     return OS_ERR;
568   }
569 
570   char *exe_path = NULL;
571   if ((psinfo_data.pr_fname != NULL) &&
572       (psinfo_data.pr_psargs != NULL)) {
573     char *path_substring = strstr(psinfo_data.pr_psargs, psinfo_data.pr_fname);
574     if (path_substring != NULL) {
575       int len = path_substring - psinfo_data.pr_psargs;
576       exe_path = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);
577       if (exe_path != NULL) {
578         jio_snprintf(exe_path, len, "%s", psinfo_data.pr_psargs);
579         exe_path[len] = '\0';
580       }
581     }
582   }
583 
584   process_info->set_pid(atoi(_entry->d_name));
585   process_info->set_name(allocate_string(psinfo_data.pr_fname));
586   process_info->set_path(allocate_string(exe_path));
587   process_info->set_command_line(allocate_string(psinfo_data.pr_psargs));
588 
589   if (exe_path != NULL) {
590     FREE_C_HEAP_ARRAY(char, exe_path);
591   }
592 
593   if (fp != NULL) {
594     fclose(fp);
595   }
596 
597   return OS_OK;
598 }
599 
600 int SystemProcessInterface::SystemProcesses::ProcessIterator::next_process() {
601   if (!is_valid()) {
602     return OS_ERR;
603   }
604 
605   do {
606     _entry = os::readdir(_dir);
607     if (_entry == NULL) {
608       // Error or reached end.  Could use errno to distinguish those cases.
609       _valid = false;
610       return OS_ERR;
611     }
612   } while(!is_valid_entry(_entry));
613 
614   _valid = true;
615   return OS_OK;
616 }
617 
618 SystemProcessInterface::SystemProcesses::ProcessIterator::ProcessIterator() {
619   _dir = NULL;
620   _entry = NULL;
621   _valid = false;
622 }
623 
624 bool SystemProcessInterface::SystemProcesses::ProcessIterator::initialize() {
625   _dir = os::opendir("/proc");
626   _entry = NULL;
627   _valid = true;
628   next_process();
629 
630   return true;
631 }
632 
633 SystemProcessInterface::SystemProcesses::ProcessIterator::~ProcessIterator() {
634   if (_dir != NULL) {
635     os::closedir(_dir);
636   }
637 }
638 
639 SystemProcessInterface::SystemProcesses::SystemProcesses() {
640   _iterator = NULL;
641 }
642 
643 bool SystemProcessInterface::SystemProcesses::initialize() {
644   _iterator = new SystemProcessInterface::SystemProcesses::ProcessIterator();
645   return _iterator != NULL && _iterator->initialize();
646 }
647 
648 SystemProcessInterface::SystemProcesses::~SystemProcesses() {
649   if (_iterator != NULL) {
650     delete _iterator;
651   }
652 }
653 
654 int SystemProcessInterface::SystemProcesses::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const {
655   assert(system_processes != NULL, "system_processes pointer is NULL!");
656   assert(no_of_sys_processes != NULL, "system_processes counter pointer is NULL!");
657   assert(_iterator != NULL, "iterator is NULL!");
658 
659   // initialize pointers
660   *no_of_sys_processes = 0;
661   *system_processes = NULL;
662 
663   while (_iterator->is_valid()) {
664     SystemProcess* tmp = new SystemProcess();
665     _iterator->current(tmp);
666 
667     //if already existing head
668     if (*system_processes != NULL) {
669       //move "first to second"
670       tmp->set_next(*system_processes);
671     }
672     // new head
673     *system_processes = tmp;
674     // increment
675     (*no_of_sys_processes)++;
676     // step forward
677     _iterator->next_process();
678   }
679   return OS_OK;
680 }
681 
682 int SystemProcessInterface::system_processes(SystemProcess** system_procs, int* no_of_sys_processes) const {
683   return _impl->system_processes(system_procs, no_of_sys_processes);
684 }
685 
686 SystemProcessInterface::SystemProcessInterface() {
687   _impl = NULL;
688 }
689 
690 bool SystemProcessInterface::initialize() {
691   _impl = new SystemProcessInterface::SystemProcesses();
692   return _impl != NULL && _impl->initialize();
693 
694 }
695 
696 SystemProcessInterface::~SystemProcessInterface() {
697   if (_impl != NULL) {
698     delete _impl;
699   }
700 }
701 
702 CPUInformationInterface::CPUInformationInterface() {
703   _cpu_info = NULL;
704 }
705 
706 bool CPUInformationInterface::initialize() {
707   _cpu_info = new CPUInformation();
708   if (_cpu_info == NULL) {
709     return false;
710   }
711   _cpu_info->set_number_of_hardware_threads(VM_Version_Ext::number_of_threads());
712   _cpu_info->set_number_of_cores(VM_Version_Ext::number_of_cores());
713   _cpu_info->set_number_of_sockets(VM_Version_Ext::number_of_sockets());
714   _cpu_info->set_cpu_name(VM_Version_Ext::cpu_name());
715   _cpu_info->set_cpu_description(VM_Version_Ext::cpu_description());
716   return true;
717 }
718 
719 CPUInformationInterface::~CPUInformationInterface() {
720   if (_cpu_info != NULL) {
721     if (_cpu_info->cpu_name() != NULL) {
722       const char* cpu_name = _cpu_info->cpu_name();
723       FREE_C_HEAP_ARRAY(char, cpu_name);
724       _cpu_info->set_cpu_name(NULL);
725     }
726     if (_cpu_info->cpu_description() != NULL) {
727       const char* cpu_desc = _cpu_info->cpu_description();
728       FREE_C_HEAP_ARRAY(char, cpu_desc);
729       _cpu_info->set_cpu_description(NULL);
730     }
731     delete _cpu_info;
732   }
733 }
734 
735 int CPUInformationInterface::cpu_information(CPUInformation& cpu_info) {
736   if (_cpu_info == NULL) {
737     return OS_ERR;
738   }
739 
740   cpu_info = *_cpu_info; // shallow copy assignment
741   return OS_OK;
742 }
743 
744 class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> {
745   friend class NetworkPerformanceInterface;
746  private:
747   NetworkPerformance();
748   NetworkPerformance(const NetworkPerformance& rhs); // no impl
749   NetworkPerformance& operator=(const NetworkPerformance& rhs); // no impl
750   bool initialize();
751   ~NetworkPerformance();
752   int network_utilization(NetworkInterface** network_interfaces) const;
753 };
754 
755 NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() {
756 
757 }
758 
759 bool NetworkPerformanceInterface::NetworkPerformance::initialize() {
760   return true;
761 }
762 
763 NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() {
764 
765 }
766 
767 int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const
768 {
769   kstat_ctl_t* ctl = kstat_open();
770   if (ctl == NULL) {
771     return OS_ERR;
772   }
773 
774   NetworkInterface* ret = NULL;
775   for (kstat_t* k = ctl->kc_chain; k != NULL; k = k->ks_next) {
776     if (strcmp(k->ks_class, "net") != 0) {
777       continue;
778     }
779     if (strcmp(k->ks_module, "link") != 0) {
780       continue;
781     }
782 
783     if (kstat_read(ctl, k, NULL) == -1) {
784       return OS_ERR;
785     }
786 
787     uint64_t bytes_in = UINT64_MAX;
788     uint64_t bytes_out = UINT64_MAX;
789     for (int i = 0; i < k->ks_ndata; ++i) {
790       kstat_named_t* data = &reinterpret_cast<kstat_named_t*>(k->ks_data)[i];
791       if (strcmp(data->name, "rbytes64") == 0) {
792         bytes_in = data->value.ui64;
793       }
794       else if (strcmp(data->name, "obytes64") == 0) {
795         bytes_out = data->value.ui64;
796       }
797     }
798 
799     if ((bytes_in != UINT64_MAX) && (bytes_out != UINT64_MAX)) {
800       NetworkInterface* cur = new NetworkInterface(k->ks_name, bytes_in, bytes_out, ret);
801       ret = cur;
802     }
803   }
804 
805   kstat_close(ctl);
806   *network_interfaces = ret;
807 
808   return OS_OK;
809 }
810 
811 NetworkPerformanceInterface::NetworkPerformanceInterface() {
812   _impl = NULL;
813 }
814 
815 NetworkPerformanceInterface::~NetworkPerformanceInterface() {
816   if (_impl != NULL) {
817     delete _impl;
818   }
819 }
820 
821 bool NetworkPerformanceInterface::initialize() {
822   _impl = new NetworkPerformanceInterface::NetworkPerformance();
823   return _impl != NULL && _impl->initialize();
824 }
825 
826 int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const {
827   return _impl->network_utilization(network_interfaces);
828 }