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