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