1 /*
   2  * Copyright (c) 2012, 2018, 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 "os_linux.inline.hpp"
  29 #include "runtime/os.hpp"
  30 #include "runtime/os_perf.hpp"
  31 
  32 #include CPU_HEADER(vm_version_ext)
  33 
  34 #include <stdio.h>
  35 #include <stdarg.h>
  36 #include <unistd.h>
  37 #include <errno.h>
  38 #include <string.h>
  39 #include <sys/resource.h>
  40 #include <sys/types.h>
  41 #include <sys/stat.h>
  42 #include <dirent.h>
  43 #include <stdlib.h>
  44 #include <dlfcn.h>
  45 #include <pthread.h>
  46 #include <limits.h>
  47 #include <ifaddrs.h>
  48 #include <fcntl.h>
  49 
  50 /**
  51    /proc/[number]/stat
  52               Status information about the process.  This is used by ps(1).  It is defined in /usr/src/linux/fs/proc/array.c.
  53 
  54               The fields, in order, with their proper scanf(3) format specifiers, are:
  55 
  56               1. pid %d The process id.
  57 
  58               2. comm %s
  59                      The filename of the executable, in parentheses.  This is visible whether or not the executable is swapped out.
  60 
  61               3. state %c
  62                      One  character  from  the  string "RSDZTW" where R is running, S is sleeping in an interruptible wait, D is waiting in uninterruptible disk
  63                      sleep, Z is zombie, T is traced or stopped (on a signal), and W is paging.
  64 
  65               4. ppid %d
  66                      The PID of the parent.
  67 
  68               5. pgrp %d
  69                      The process group ID of the process.
  70 
  71               6. session %d
  72                      The session ID of the process.
  73 
  74               7. tty_nr %d
  75                      The tty the process uses.
  76 
  77               8. tpgid %d
  78                      The process group ID of the process which currently owns the tty that the process is connected to.
  79 
  80               9. flags %lu
  81                      The flags of the process.  The math bit is decimal 4, and the traced bit is decimal 10.
  82 
  83               10. minflt %lu
  84                      The number of minor faults the process has made which have not required loading a memory page from disk.
  85 
  86               11. cminflt %lu
  87                      The number of minor faults that the process's waited-for children have made.
  88 
  89               12. majflt %lu
  90                      The number of major faults the process has made which have required loading a memory page from disk.
  91 
  92               13. cmajflt %lu
  93                      The number of major faults that the process's waited-for children have made.
  94 
  95               14. utime %lu
  96                      The number of jiffies that this process has been scheduled in user mode.
  97 
  98               15. stime %lu
  99                      The number of jiffies that this process has been scheduled in kernel mode.
 100 
 101               16. cutime %ld
 102                      The number of jiffies that this process's waited-for children have been scheduled in user mode. (See also times(2).)
 103 
 104               17. cstime %ld
 105                      The number of jiffies that this process' waited-for children have been scheduled in kernel mode.
 106 
 107               18. priority %ld
 108                      The standard nice value, plus fifteen.  The value is never negative in the kernel.
 109 
 110               19. nice %ld
 111                      The nice value ranges from 19 (nicest) to -19 (not nice to others).
 112 
 113               20. 0 %ld  This value is hard coded to 0 as a placeholder for a removed field.
 114 
 115               21. itrealvalue %ld
 116                      The time in jiffies before the next SIGALRM is sent to the process due to an interval timer.
 117 
 118               22. starttime %lu
 119                      The time in jiffies the process started after system boot.
 120 
 121               23. vsize %lu
 122                      Virtual memory size in bytes.
 123 
 124               24. rss %ld
 125                      Resident Set Size: number of pages the process has in real memory, minus 3 for administrative purposes. This is just the pages which  count
 126                      towards text, data, or stack space.  This does not include pages which have not been demand-loaded in, or which are swapped out.
 127 
 128               25. rlim %lu
 129                      Current limit in bytes on the rss of the process (usually 4294967295 on i386).
 130 
 131               26. startcode %lu
 132                      The address above which program text can run.
 133 
 134               27. endcode %lu
 135                      The address below which program text can run.
 136 
 137               28. startstack %lu
 138                      The address of the start of the stack.
 139 
 140               29. kstkesp %lu
 141                      The current value of esp (stack pointer), as found in the kernel stack page for the process.
 142 
 143               30. kstkeip %lu
 144                      The current EIP (instruction pointer).
 145 
 146               31. signal %lu
 147                      The bitmap of pending signals (usually 0).
 148 
 149               32. blocked %lu
 150                      The bitmap of blocked signals (usually 0, 2 for shells).
 151 
 152               33. sigignore %lu
 153                      The bitmap of ignored signals.
 154 
 155               34. sigcatch %lu
 156                      The bitmap of catched signals.
 157 
 158               35. wchan %lu
 159                      This  is the "channel" in which the process is waiting.  It is the address of a system call, and can be looked up in a namelist if you need
 160                      a textual name.  (If you have an up-to-date /etc/psdatabase, then try ps -l to see the WCHAN field in action.)
 161 
 162               36. nswap %lu
 163                      Number of pages swapped - not maintained.
 164 
 165               37. cnswap %lu
 166                      Cumulative nswap for child processes.
 167 
 168               38. exit_signal %d
 169                      Signal to be sent to parent when we die.
 170 
 171               39. processor %d
 172                      CPU number last executed on.
 173 
 174 
 175 
 176  ///// SSCANF FORMAT STRING. Copy and use.
 177 
 178 field:        1  2  3  4  5  6  7  8  9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38 39
 179 format:       %d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d
 180 
 181 
 182 */
 183 
 184 /**
 185  * For platforms that have them, when declaring
 186  * a printf-style function,
 187  *   formatSpec is the parameter number (starting at 1)
 188  *       that is the format argument ("%d pid %s")
 189  *   params is the parameter number where the actual args to
 190  *       the format starts. If the args are in a va_list, this
 191  *       should be 0.
 192  */
 193 #ifndef PRINTF_ARGS
 194 #  define PRINTF_ARGS(formatSpec,  params) ATTRIBUTE_PRINTF(formatSpec, params)
 195 #endif
 196 
 197 #ifndef SCANF_ARGS
 198 #  define SCANF_ARGS(formatSpec,   params) ATTRIBUTE_SCANF(formatSpec, params)
 199 #endif
 200 
 201 #ifndef _PRINTFMT_
 202 #  define _PRINTFMT_
 203 #endif
 204 
 205 #ifndef _SCANFMT_
 206 #  define _SCANFMT_
 207 #endif
 208 
 209 
 210 struct CPUPerfTicks {
 211   uint64_t  used;
 212   uint64_t  usedKernel;
 213   uint64_t  total;
 214 };
 215 
 216 typedef enum {
 217   CPU_LOAD_VM_ONLY,
 218   CPU_LOAD_GLOBAL,
 219 } CpuLoadTarget;
 220 
 221 enum {
 222   UNDETECTED,
 223   UNDETECTABLE,
 224   LINUX26_NPTL,
 225   BAREMETAL
 226 };
 227 
 228 struct CPUPerfCounters {
 229   int   nProcs;
 230   CPUPerfTicks jvmTicks;
 231   CPUPerfTicks* cpus;
 232 };
 233 
 234 static double get_cpu_load(int which_logical_cpu, CPUPerfCounters* counters, double* pkernelLoad, CpuLoadTarget target);
 235 
 236 /** reads /proc/<pid>/stat data, with some checks and some skips.
 237  *  Ensure that 'fmt' does _NOT_ contain the first two "%d %s"
 238  */
 239 static int SCANF_ARGS(2, 0) vread_statdata(const char* procfile, _SCANFMT_ const char* fmt, va_list args) {
 240   FILE*f;
 241   int n;
 242   char buf[2048];
 243 
 244   if ((f = fopen(procfile, "r")) == NULL) {
 245     return -1;
 246   }
 247 
 248   if ((n = fread(buf, 1, sizeof(buf), f)) != -1) {
 249     char *tmp;
 250 
 251     buf[n-1] = '\0';
 252     /** skip through pid and exec name. */
 253     if ((tmp = strrchr(buf, ')')) != NULL) {
 254       // skip the ')' and the following space
 255       // but check that buffer is long enough
 256       tmp += 2;
 257       if (tmp < buf + n) {
 258         n = vsscanf(tmp, fmt, args);
 259       }
 260     }
 261   }
 262 
 263   fclose(f);
 264 
 265   return n;
 266 }
 267 
 268 static int SCANF_ARGS(2, 3) read_statdata(const char* procfile, _SCANFMT_ const char* fmt, ...) {
 269   int   n;
 270   va_list args;
 271 
 272   va_start(args, fmt);
 273   n = vread_statdata(procfile, fmt, args);
 274   va_end(args);
 275   return n;
 276 }
 277 
 278 static FILE* open_statfile(void) {
 279   FILE *f;
 280 
 281   if ((f = fopen("/proc/stat", "r")) == NULL) {
 282     static int haveWarned = 0;
 283     if (!haveWarned) {
 284       haveWarned = 1;
 285     }
 286   }
 287   return f;
 288 }
 289 
 290 static void
 291 next_line(FILE *f) {
 292   int c;
 293   do {
 294     c = fgetc(f);
 295   } while (c != '\n' && c != EOF);
 296 }
 297 
 298 /**
 299  * Return the total number of ticks since the system was booted.
 300  * If the usedTicks parameter is not NULL, it will be filled with
 301  * the number of ticks spent on actual processes (user, system or
 302  * nice processes) since system boot. Note that this is the total number
 303  * of "executed" ticks on _all_ CPU:s, that is on a n-way system it is
 304  * n times the number of ticks that has passed in clock time.
 305  *
 306  * Returns a negative value if the reading of the ticks failed.
 307  */
 308 static OSReturn get_total_ticks(int which_logical_cpu, CPUPerfTicks* pticks) {
 309   FILE*         fh;
 310   uint64_t      userTicks, niceTicks, systemTicks, idleTicks;
 311   uint64_t      iowTicks = 0, irqTicks = 0, sirqTicks= 0;
 312   int           logical_cpu = -1;
 313   const int     expected_assign_count = (-1 == which_logical_cpu) ? 4 : 5;
 314   int           n;
 315 
 316   if ((fh = open_statfile()) == NULL) {
 317     return OS_ERR;
 318   }
 319   if (-1 == which_logical_cpu) {
 320     n = fscanf(fh, "cpu " UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " "
 321             UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT,
 322             &userTicks, &niceTicks, &systemTicks, &idleTicks,
 323             &iowTicks, &irqTicks, &sirqTicks);
 324   } else {
 325     // Move to next line
 326     next_line(fh);
 327 
 328     // find the line for requested cpu faster to just iterate linefeeds?
 329     for (int i = 0; i < which_logical_cpu; i++) {
 330       next_line(fh);
 331     }
 332 
 333     n = fscanf(fh, "cpu%u " UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " "
 334                UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT,
 335                &logical_cpu, &userTicks, &niceTicks,
 336                &systemTicks, &idleTicks, &iowTicks, &irqTicks, &sirqTicks);
 337   }
 338 
 339   fclose(fh);
 340   if (n < expected_assign_count || logical_cpu != which_logical_cpu) {
 341 #ifdef DEBUG_LINUX_PROC_STAT
 342     vm_fprintf(stderr, "[stat] read failed");
 343 #endif
 344     return OS_ERR;
 345   }
 346 
 347 #ifdef DEBUG_LINUX_PROC_STAT
 348   vm_fprintf(stderr, "[stat] read "
 349           UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " "
 350           UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " \n",
 351           userTicks, niceTicks, systemTicks, idleTicks,
 352           iowTicks, irqTicks, sirqTicks);
 353 #endif
 354 
 355   pticks->used       = userTicks + niceTicks;
 356   pticks->usedKernel = systemTicks + irqTicks + sirqTicks;
 357   pticks->total      = userTicks + niceTicks + systemTicks + idleTicks +
 358                        iowTicks + irqTicks + sirqTicks;
 359 
 360   return OS_OK;
 361 }
 362 
 363 
 364 static int get_systemtype(void) {
 365   static int procEntriesType = UNDETECTED;
 366   DIR *taskDir;
 367 
 368   if (procEntriesType != UNDETECTED) {
 369     return procEntriesType;
 370   }
 371 
 372   // Check whether we have a task subdirectory
 373   if ((taskDir = opendir("/proc/self/task")) == NULL) {
 374     procEntriesType = UNDETECTABLE;
 375   } else {
 376     // The task subdirectory exists; we're on a Linux >= 2.6 system
 377     closedir(taskDir);
 378     procEntriesType = LINUX26_NPTL;
 379   }
 380 
 381   return procEntriesType;
 382 }
 383 
 384 /** read user and system ticks from a named procfile, assumed to be in 'stat' format then. */
 385 static int read_ticks(const char* procfile, uint64_t* userTicks, uint64_t* systemTicks) {
 386   return read_statdata(procfile, "%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u " UINT64_FORMAT " " UINT64_FORMAT,
 387     userTicks, systemTicks);
 388 }
 389 
 390 /**
 391  * Return the number of ticks spent in any of the processes belonging
 392  * to the JVM on any CPU.
 393  */
 394 static OSReturn get_jvm_ticks(CPUPerfTicks* pticks) {
 395   uint64_t userTicks;
 396   uint64_t systemTicks;
 397 
 398   if (get_systemtype() != LINUX26_NPTL) {
 399     return OS_ERR;
 400   }
 401 
 402   if (read_ticks("/proc/self/stat", &userTicks, &systemTicks) != 2) {
 403     return OS_ERR;
 404   }
 405 
 406   // get the total
 407   if (get_total_ticks(-1, pticks) != OS_OK) {
 408     return OS_ERR;
 409   }
 410 
 411   pticks->used       = userTicks;
 412   pticks->usedKernel = systemTicks;
 413 
 414   return OS_OK;
 415 }
 416 
 417 /**
 418  * Return the load of the CPU as a double. 1.0 means the CPU process uses all
 419  * available time for user or system processes, 0.0 means the CPU uses all time
 420  * being idle.
 421  *
 422  * Returns a negative value if there is a problem in determining the CPU load.
 423  */
 424 static double get_cpu_load(int which_logical_cpu, CPUPerfCounters* counters, double* pkernelLoad, CpuLoadTarget target) {
 425   uint64_t udiff, kdiff, tdiff;
 426   CPUPerfTicks* pticks;
 427   CPUPerfTicks  tmp;
 428   double user_load;
 429 
 430   *pkernelLoad = 0.0;
 431 
 432   if (target == CPU_LOAD_VM_ONLY) {
 433     pticks = &counters->jvmTicks;
 434   } else if (-1 == which_logical_cpu) {
 435     pticks = &counters->cpus[counters->nProcs];
 436   } else {
 437     pticks = &counters->cpus[which_logical_cpu];
 438   }
 439 
 440   tmp = *pticks;
 441 
 442   if (target == CPU_LOAD_VM_ONLY) {
 443     if (get_jvm_ticks(pticks) != OS_OK) {
 444       return -1.0;
 445     }
 446   } else if (get_total_ticks(which_logical_cpu, pticks) != OS_OK) {
 447     return -1.0;
 448   }
 449 
 450   // seems like we sometimes end up with less kernel ticks when
 451   // reading /proc/self/stat a second time, timing issue between cpus?
 452   if (pticks->usedKernel < tmp.usedKernel) {
 453     kdiff = 0;
 454   } else {
 455     kdiff = pticks->usedKernel - tmp.usedKernel;
 456   }
 457   tdiff = pticks->total - tmp.total;
 458   udiff = pticks->used - tmp.used;
 459 
 460   if (tdiff == 0) {
 461     return 0.0;
 462   } else if (tdiff < (udiff + kdiff)) {
 463     tdiff = udiff + kdiff;
 464   }
 465   *pkernelLoad = (kdiff / (double)tdiff);
 466   // BUG9044876, normalize return values to sane values
 467   *pkernelLoad = MAX2<double>(*pkernelLoad, 0.0);
 468   *pkernelLoad = MIN2<double>(*pkernelLoad, 1.0);
 469 
 470   user_load = (udiff / (double)tdiff);
 471   user_load = MAX2<double>(user_load, 0.0);
 472   user_load = MIN2<double>(user_load, 1.0);
 473 
 474   return user_load;
 475 }
 476 
 477 static int SCANF_ARGS(1, 2) parse_stat(_SCANFMT_ const char* fmt, ...) {
 478   FILE *f;
 479   va_list args;
 480 
 481   va_start(args, fmt);
 482 
 483   if ((f = open_statfile()) == NULL) {
 484     va_end(args);
 485     return OS_ERR;
 486   }
 487   for (;;) {
 488     char line[80];
 489     if (fgets(line, sizeof(line), f) != NULL) {
 490       if (vsscanf(line, fmt, args) == 1) {
 491         fclose(f);
 492         va_end(args);
 493         return OS_OK;
 494       }
 495     } else {
 496         fclose(f);
 497         va_end(args);
 498         return OS_ERR;
 499     }
 500   }
 501 }
 502 
 503 static int get_noof_context_switches(uint64_t* switches) {
 504   return parse_stat("ctxt " UINT64_FORMAT "\n", switches);
 505 }
 506 
 507 /** returns boot time in _seconds_ since epoch */
 508 static int get_boot_time(uint64_t* time) {
 509   return parse_stat("btime " UINT64_FORMAT "\n", time);
 510 }
 511 
 512 static int perf_context_switch_rate(double* rate) {
 513   static pthread_mutex_t contextSwitchLock = PTHREAD_MUTEX_INITIALIZER;
 514   static uint64_t      lastTime;
 515   static uint64_t      lastSwitches;
 516   static double        lastRate;
 517 
 518   uint64_t lt = 0;
 519   int res = 0;
 520 
 521   if (lastTime == 0) {
 522     uint64_t tmp;
 523     if (get_boot_time(&tmp) < 0) {
 524       return OS_ERR;
 525     }
 526     lt = tmp * 1000;
 527   }
 528 
 529   res = OS_OK;
 530 
 531   pthread_mutex_lock(&contextSwitchLock);
 532   {
 533 
 534     uint64_t sw;
 535     s8 t, d;
 536 
 537     if (lastTime == 0) {
 538       lastTime = lt;
 539     }
 540 
 541     t = os::javaTimeMillis();
 542     d = t - lastTime;
 543 
 544     if (d == 0) {
 545       *rate = lastRate;
 546     } else if (!get_noof_context_switches(&sw)) {
 547       *rate      = ( (double)(sw - lastSwitches) / d ) * 1000;
 548       lastRate     = *rate;
 549       lastSwitches = sw;
 550       lastTime     = t;
 551     } else {
 552       *rate = 0;
 553       res   = OS_ERR;
 554     }
 555     if (*rate <= 0) {
 556       *rate = 0;
 557       lastRate = 0;
 558     }
 559   }
 560   pthread_mutex_unlock(&contextSwitchLock);
 561 
 562   return res;
 563 }
 564 
 565 class CPUPerformanceInterface::CPUPerformance : public CHeapObj<mtInternal> {
 566   friend class CPUPerformanceInterface;
 567  private:
 568   CPUPerfCounters _counters;
 569 
 570   int cpu_load(int which_logical_cpu, double* cpu_load);
 571   int context_switch_rate(double* rate);
 572   int cpu_load_total_process(double* cpu_load);
 573   int cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad);
 574 
 575  public:
 576   CPUPerformance();
 577   bool initialize();
 578   ~CPUPerformance();
 579 };
 580 
 581 CPUPerformanceInterface::CPUPerformance::CPUPerformance() {
 582   _counters.nProcs = os::active_processor_count();
 583   _counters.cpus = NULL;
 584 }
 585 
 586 bool CPUPerformanceInterface::CPUPerformance::initialize() {
 587   size_t tick_array_size = (_counters.nProcs +1) * sizeof(CPUPerfTicks);
 588   _counters.cpus = (CPUPerfTicks*)NEW_C_HEAP_ARRAY(char, tick_array_size, mtInternal);
 589   if (NULL == _counters.cpus) {
 590     return false;
 591   }
 592   memset(_counters.cpus, 0, tick_array_size);
 593 
 594   // For the CPU load total
 595   get_total_ticks(-1, &_counters.cpus[_counters.nProcs]);
 596 
 597   // For each CPU
 598   for (int i = 0; i < _counters.nProcs; i++) {
 599     get_total_ticks(i, &_counters.cpus[i]);
 600   }
 601   // For JVM load
 602   get_jvm_ticks(&_counters.jvmTicks);
 603 
 604   // initialize context switch system
 605   // the double is only for init
 606   double init_ctx_switch_rate;
 607   perf_context_switch_rate(&init_ctx_switch_rate);
 608 
 609   return true;
 610 }
 611 
 612 CPUPerformanceInterface::CPUPerformance::~CPUPerformance() {
 613   if (_counters.cpus != NULL) {
 614     FREE_C_HEAP_ARRAY(char, _counters.cpus);
 615   }
 616 }
 617 
 618 int CPUPerformanceInterface::CPUPerformance::cpu_load(int which_logical_cpu, double* cpu_load) {
 619   double u, s;
 620   u = get_cpu_load(which_logical_cpu, &_counters, &s, CPU_LOAD_GLOBAL);
 621   if (u < 0) {
 622     *cpu_load = 0.0;
 623     return OS_ERR;
 624   }
 625   // Cap total systemload to 1.0
 626   *cpu_load = MIN2<double>((u + s), 1.0);
 627   return OS_OK;
 628 }
 629 
 630 int CPUPerformanceInterface::CPUPerformance::cpu_load_total_process(double* cpu_load) {
 631   double u, s;
 632   u = get_cpu_load(-1, &_counters, &s, CPU_LOAD_VM_ONLY);
 633   if (u < 0) {
 634     *cpu_load = 0.0;
 635     return OS_ERR;
 636   }
 637   *cpu_load = u + s;
 638   return OS_OK;
 639 }
 640 
 641 int CPUPerformanceInterface::CPUPerformance::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) {
 642   double u, s, t;
 643 
 644   assert(pjvmUserLoad != NULL, "pjvmUserLoad not inited");
 645   assert(pjvmKernelLoad != NULL, "pjvmKernelLoad not inited");
 646   assert(psystemTotalLoad != NULL, "psystemTotalLoad not inited");
 647 
 648   u = get_cpu_load(-1, &_counters, &s, CPU_LOAD_VM_ONLY);
 649   if (u < 0) {
 650     *pjvmUserLoad = 0.0;
 651     *pjvmKernelLoad = 0.0;
 652     *psystemTotalLoad = 0.0;
 653     return OS_ERR;
 654   }
 655 
 656   cpu_load(-1, &t);
 657   // clamp at user+system and 1.0
 658   if (u + s > t) {
 659     t = MIN2<double>(u + s, 1.0);
 660   }
 661 
 662   *pjvmUserLoad = u;
 663   *pjvmKernelLoad = s;
 664   *psystemTotalLoad = t;
 665 
 666   return OS_OK;
 667 }
 668 
 669 int CPUPerformanceInterface::CPUPerformance::context_switch_rate(double* rate) {
 670   return perf_context_switch_rate(rate);
 671 }
 672 
 673 CPUPerformanceInterface::CPUPerformanceInterface() {
 674   _impl = NULL;
 675 }
 676 
 677 bool CPUPerformanceInterface::initialize() {
 678   _impl = new CPUPerformanceInterface::CPUPerformance();
 679   return NULL == _impl ? false : _impl->initialize();
 680 }
 681 
 682 CPUPerformanceInterface::~CPUPerformanceInterface() {
 683   if (_impl != NULL) {
 684     delete _impl;
 685   }
 686 }
 687 
 688 int CPUPerformanceInterface::cpu_load(int which_logical_cpu, double* cpu_load) const {
 689   return _impl->cpu_load(which_logical_cpu, cpu_load);
 690 }
 691 
 692 int CPUPerformanceInterface::cpu_load_total_process(double* cpu_load) const {
 693   return _impl->cpu_load_total_process(cpu_load);
 694 }
 695 
 696 int CPUPerformanceInterface::cpu_loads_process(double* pjvmUserLoad, double* pjvmKernelLoad, double* psystemTotalLoad) const {
 697   return _impl->cpu_loads_process(pjvmUserLoad, pjvmKernelLoad, psystemTotalLoad);
 698 }
 699 
 700 int CPUPerformanceInterface::context_switch_rate(double* rate) const {
 701   return _impl->context_switch_rate(rate);
 702 }
 703 
 704 class SystemProcessInterface::SystemProcesses : public CHeapObj<mtInternal> {
 705   friend class SystemProcessInterface;
 706  private:
 707   class ProcessIterator : public CHeapObj<mtInternal> {
 708     friend class SystemProcessInterface::SystemProcesses;
 709    private:
 710     DIR*           _dir;
 711     struct dirent* _entry;
 712     bool           _valid;
 713     char           _exeName[PATH_MAX];
 714     char           _exePath[PATH_MAX];
 715 
 716     ProcessIterator();
 717     ~ProcessIterator();
 718     bool initialize();
 719 
 720     bool is_valid() const { return _valid; }
 721     bool is_valid_entry(struct dirent* entry) const;
 722     bool is_dir(const char* name) const;
 723     int  fsize(const char* name, uint64_t& size) const;
 724 
 725     char* allocate_string(const char* str) const;
 726     void  get_exe_name();
 727     char* get_exe_path();
 728     char* get_cmdline();
 729 
 730     int current(SystemProcess* process_info);
 731     int next_process();
 732   };
 733 
 734   ProcessIterator* _iterator;
 735   SystemProcesses();
 736   bool initialize();
 737   ~SystemProcesses();
 738 
 739   //information about system processes
 740   int system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const;
 741 };
 742 
 743 bool SystemProcessInterface::SystemProcesses::ProcessIterator::is_dir(const char* name) const {
 744   struct stat mystat;
 745   int ret_val = 0;
 746 
 747   ret_val = stat(name, &mystat);
 748   if (ret_val < 0) {
 749     return false;
 750   }
 751   ret_val = S_ISDIR(mystat.st_mode);
 752   return ret_val > 0;
 753 }
 754 
 755 int SystemProcessInterface::SystemProcesses::ProcessIterator::fsize(const char* name, uint64_t& size) const {
 756   assert(name != NULL, "name pointer is NULL!");
 757   size = 0;
 758   struct stat fbuf;
 759 
 760   if (stat(name, &fbuf) < 0) {
 761     return OS_ERR;
 762   }
 763   size = fbuf.st_size;
 764   return OS_OK;
 765 }
 766 
 767 // if it has a numeric name, is a directory and has a 'stat' file in it
 768 bool SystemProcessInterface::SystemProcesses::ProcessIterator::is_valid_entry(struct dirent* entry) const {
 769   char buffer[PATH_MAX];
 770   uint64_t size = 0;
 771 
 772   if (atoi(entry->d_name) != 0) {
 773     jio_snprintf(buffer, PATH_MAX, "/proc/%s", entry->d_name);
 774     buffer[PATH_MAX - 1] = '\0';
 775 
 776     if (is_dir(buffer)) {
 777       jio_snprintf(buffer, PATH_MAX, "/proc/%s/stat", entry->d_name);
 778       buffer[PATH_MAX - 1] = '\0';
 779       if (fsize(buffer, size) != OS_ERR) {
 780         return true;
 781       }
 782     }
 783   }
 784   return false;
 785 }
 786 
 787 // get exe-name from /proc/<pid>/stat
 788 void SystemProcessInterface::SystemProcesses::ProcessIterator::get_exe_name() {
 789   FILE* fp;
 790   char  buffer[PATH_MAX];
 791 
 792   jio_snprintf(buffer, PATH_MAX, "/proc/%s/stat", _entry->d_name);
 793   buffer[PATH_MAX - 1] = '\0';
 794   if ((fp = fopen(buffer, "r")) != NULL) {
 795     if (fgets(buffer, PATH_MAX, fp) != NULL) {
 796       char* start, *end;
 797       // exe-name is between the first pair of ( and )
 798       start = strchr(buffer, '(');
 799       if (start != NULL && start[1] != '\0') {
 800         start++;
 801         end = strrchr(start, ')');
 802         if (end != NULL) {
 803           size_t len;
 804           len = MIN2<size_t>(end - start, sizeof(_exeName) - 1);
 805           memcpy(_exeName, start, len);
 806           _exeName[len] = '\0';
 807         }
 808       }
 809     }
 810     fclose(fp);
 811   }
 812 }
 813 
 814 // get command line from /proc/<pid>/cmdline
 815 char* SystemProcessInterface::SystemProcesses::ProcessIterator::get_cmdline() {
 816   FILE* fp;
 817   char  buffer[PATH_MAX];
 818   char* cmdline = NULL;
 819 
 820   jio_snprintf(buffer, PATH_MAX, "/proc/%s/cmdline", _entry->d_name);
 821   buffer[PATH_MAX - 1] = '\0';
 822   if ((fp = fopen(buffer, "r")) != NULL) {
 823     size_t size = 0;
 824     char   dummy;
 825 
 826     // find out how long the file is (stat always returns 0)
 827     while (fread(&dummy, 1, 1, fp) == 1) {
 828       size++;
 829     }
 830     if (size > 0) {
 831       cmdline = NEW_C_HEAP_ARRAY(char, size + 1, mtInternal);
 832       if (cmdline != NULL) {
 833         cmdline[0] = '\0';
 834         if (fseek(fp, 0, SEEK_SET) == 0) {
 835           if (fread(cmdline, 1, size, fp) == size) {
 836             // the file has the arguments separated by '\0',
 837             // so we translate '\0' to ' '
 838             for (size_t i = 0; i < size; i++) {
 839               if (cmdline[i] == '\0') {
 840                 cmdline[i] = ' ';
 841               }
 842             }
 843             cmdline[size] = '\0';
 844           }
 845         }
 846       }
 847     }
 848     fclose(fp);
 849   }
 850   return cmdline;
 851 }
 852 
 853 // get full path to exe from /proc/<pid>/exe symlink
 854 char* SystemProcessInterface::SystemProcesses::ProcessIterator::get_exe_path() {
 855   char buffer[PATH_MAX];
 856 
 857   jio_snprintf(buffer, PATH_MAX, "/proc/%s/exe", _entry->d_name);
 858   buffer[PATH_MAX - 1] = '\0';
 859   return realpath(buffer, _exePath);
 860 }
 861 
 862 char* SystemProcessInterface::SystemProcesses::ProcessIterator::allocate_string(const char* str) const {
 863   if (str != NULL) {
 864     size_t len = strlen(str);
 865     char* tmp = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);
 866     strncpy(tmp, str, len);
 867     tmp[len] = '\0';
 868     return tmp;
 869   }
 870   return NULL;
 871 }
 872 
 873 int SystemProcessInterface::SystemProcesses::ProcessIterator::current(SystemProcess* process_info) {
 874   if (!is_valid()) {
 875     return OS_ERR;
 876   }
 877 
 878   process_info->set_pid(atoi(_entry->d_name));
 879 
 880   get_exe_name();
 881   process_info->set_name(allocate_string(_exeName));
 882 
 883   if (get_exe_path() != NULL) {
 884      process_info->set_path(allocate_string(_exePath));
 885   }
 886 
 887   char* cmdline = NULL;
 888   cmdline = get_cmdline();
 889   if (cmdline != NULL) {
 890     process_info->set_command_line(allocate_string(cmdline));
 891     FREE_C_HEAP_ARRAY(char, cmdline);
 892   }
 893 
 894   return OS_OK;
 895 }
 896 
 897 int SystemProcessInterface::SystemProcesses::ProcessIterator::next_process() {
 898   struct dirent* entry;
 899 
 900   if (!is_valid()) {
 901     return OS_ERR;
 902   }
 903 
 904   do {
 905       entry = os::readdir(_dir, _entry);
 906     if (entry == NULL) {
 907       // error
 908       _valid = false;
 909       return OS_ERR;
 910     }
 911     if (_entry == NULL) {
 912       // reached end
 913       _valid = false;
 914       return OS_ERR;
 915     }
 916   } while(!is_valid_entry(_entry));
 917 
 918   _valid = true;
 919   return OS_OK;
 920 }
 921 
 922 SystemProcessInterface::SystemProcesses::ProcessIterator::ProcessIterator() {
 923   _dir = NULL;
 924   _entry = NULL;
 925   _valid = false;
 926 }
 927 
 928 bool SystemProcessInterface::SystemProcesses::ProcessIterator::initialize() {
 929   _dir = opendir("/proc");
 930   _entry = (struct dirent*)NEW_C_HEAP_ARRAY(char, sizeof(struct dirent) + NAME_MAX + 1, mtInternal);
 931   if (NULL == _entry) {
 932     return false;
 933   }
 934   _valid = true;
 935   next_process();
 936 
 937   return true;
 938 }
 939 
 940 SystemProcessInterface::SystemProcesses::ProcessIterator::~ProcessIterator() {
 941   if (_entry != NULL) {
 942     FREE_C_HEAP_ARRAY(char, _entry);
 943   }
 944   if (_dir != NULL) {
 945     closedir(_dir);
 946   }
 947 }
 948 
 949 SystemProcessInterface::SystemProcesses::SystemProcesses() {
 950   _iterator = NULL;
 951 }
 952 
 953 bool SystemProcessInterface::SystemProcesses::initialize() {
 954   _iterator = new SystemProcessInterface::SystemProcesses::ProcessIterator();
 955   return NULL == _iterator ? false : _iterator->initialize();
 956 }
 957 
 958 SystemProcessInterface::SystemProcesses::~SystemProcesses() {
 959   if (_iterator != NULL) {
 960     delete _iterator;
 961   }
 962 }
 963 
 964 int SystemProcessInterface::SystemProcesses::system_processes(SystemProcess** system_processes, int* no_of_sys_processes) const {
 965   assert(system_processes != NULL, "system_processes pointer is NULL!");
 966   assert(no_of_sys_processes != NULL, "system_processes counter pointers is NULL!");
 967   assert(_iterator != NULL, "iterator is NULL!");
 968 
 969   // initialize pointers
 970   *no_of_sys_processes = 0;
 971   *system_processes = NULL;
 972 
 973   while (_iterator->is_valid()) {
 974     SystemProcess* tmp = new SystemProcess();
 975     _iterator->current(tmp);
 976 
 977     //if already existing head
 978     if (*system_processes != NULL) {
 979       //move "first to second"
 980       tmp->set_next(*system_processes);
 981     }
 982     // new head
 983     *system_processes = tmp;
 984     // increment
 985     (*no_of_sys_processes)++;
 986     // step forward
 987     _iterator->next_process();
 988   }
 989   return OS_OK;
 990 }
 991 
 992 int SystemProcessInterface::system_processes(SystemProcess** system_procs, int* no_of_sys_processes) const {
 993   return _impl->system_processes(system_procs, no_of_sys_processes);
 994 }
 995 
 996 SystemProcessInterface::SystemProcessInterface() {
 997   _impl = NULL;
 998 }
 999 
1000 bool SystemProcessInterface::initialize() {
1001   _impl = new SystemProcessInterface::SystemProcesses();
1002   return NULL == _impl ? false : _impl->initialize();
1003 }
1004 
1005 SystemProcessInterface::~SystemProcessInterface() {
1006   if (_impl != NULL) {
1007     delete _impl;
1008   }
1009 }
1010 
1011 CPUInformationInterface::CPUInformationInterface() {
1012   _cpu_info = NULL;
1013 }
1014 
1015 bool CPUInformationInterface::initialize() {
1016   _cpu_info = new CPUInformation();
1017   if (NULL == _cpu_info) {
1018     return false;
1019   }
1020   _cpu_info->set_number_of_hardware_threads(VM_Version_Ext::number_of_threads());
1021   _cpu_info->set_number_of_cores(VM_Version_Ext::number_of_cores());
1022   _cpu_info->set_number_of_sockets(VM_Version_Ext::number_of_sockets());
1023   _cpu_info->set_cpu_name(VM_Version_Ext::cpu_name());
1024   _cpu_info->set_cpu_description(VM_Version_Ext::cpu_description());
1025 
1026   return true;
1027 }
1028 
1029 CPUInformationInterface::~CPUInformationInterface() {
1030   if (_cpu_info != NULL) {
1031     if (_cpu_info->cpu_name() != NULL) {
1032       const char* cpu_name = _cpu_info->cpu_name();
1033       FREE_C_HEAP_ARRAY(char, cpu_name);
1034       _cpu_info->set_cpu_name(NULL);
1035     }
1036     if (_cpu_info->cpu_description() != NULL) {
1037        const char* cpu_desc = _cpu_info->cpu_description();
1038        FREE_C_HEAP_ARRAY(char, cpu_desc);
1039       _cpu_info->set_cpu_description(NULL);
1040     }
1041     delete _cpu_info;
1042   }
1043 }
1044 
1045 int CPUInformationInterface::cpu_information(CPUInformation& cpu_info) {
1046   if (_cpu_info == NULL) {
1047     return OS_ERR;
1048   }
1049 
1050   cpu_info = *_cpu_info; // shallow copy assignment
1051   return OS_OK;
1052 }
1053 
1054 class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> {
1055   friend class NetworkPerformanceInterface;
1056  private:
1057   NetworkPerformance();
1058   NetworkPerformance(const NetworkPerformance& rhs); // no impl
1059   NetworkPerformance& operator=(const NetworkPerformance& rhs); // no impl
1060   bool initialize();
1061   ~NetworkPerformance();
1062   int64_t read_counter(const char* iface, const char* counter) const;
1063   int network_utilization(NetworkInterface** network_interfaces) const;
1064 };
1065 
1066 NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() {
1067 
1068 }
1069 
1070 bool NetworkPerformanceInterface::NetworkPerformance::initialize() {
1071   return true;
1072 }
1073 
1074 NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() {
1075 }
1076 
1077 int64_t NetworkPerformanceInterface::NetworkPerformance::read_counter(const char* iface, const char* counter) const {
1078   char buf[128];
1079 
1080   snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics/%s", iface, counter);
1081 
1082   int fd = open(buf, O_RDONLY);
1083   if (fd == -1) {
1084     return -1;
1085   }
1086 
1087   ssize_t num_bytes = read(fd, buf, sizeof(buf));
1088   close(fd);
1089   if ((num_bytes == -1) || (num_bytes >= static_cast<ssize_t>(sizeof(buf))) || (num_bytes < 1)) {
1090     return -1;
1091   }
1092 
1093   buf[num_bytes] = '\0';
1094   int64_t value = strtoll(buf, NULL, 10);
1095 
1096   return value;
1097 }
1098 
1099 int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const
1100 {
1101   ifaddrs* addresses;
1102   ifaddrs* cur_address;
1103 
1104   if (getifaddrs(&addresses) != 0) {
1105     return OS_ERR;
1106   }
1107 
1108   NetworkInterface* ret = NULL;
1109   for (cur_address = addresses; cur_address != NULL; cur_address = cur_address->ifa_next) {
1110     if (cur_address->ifa_addr->sa_family != AF_PACKET) {
1111       continue;
1112     }
1113 
1114     int64_t bytes_in = read_counter(cur_address->ifa_name, "rx_bytes");
1115     int64_t bytes_out = read_counter(cur_address->ifa_name, "tx_bytes");
1116 
1117     NetworkInterface* cur = new NetworkInterface(cur_address->ifa_name, bytes_in, bytes_out, ret);
1118     ret = cur;
1119   }
1120 
1121   *network_interfaces = ret;
1122 
1123   return OS_OK;
1124 }
1125 
1126 NetworkPerformanceInterface::NetworkPerformanceInterface() {
1127   _impl = NULL;
1128 }
1129 
1130 NetworkPerformanceInterface::~NetworkPerformanceInterface() {
1131   if (_impl != NULL) {
1132     delete _impl;
1133   }
1134 }
1135 
1136 bool NetworkPerformanceInterface::initialize() {
1137   _impl = new NetworkPerformanceInterface::NetworkPerformance();
1138   return _impl != NULL && _impl->initialize();
1139 }
1140 
1141 int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const {
1142   return _impl->network_utilization(network_interfaces);
1143 }