src/os/linux/vm/os_perf_linux.cpp

Sat, 24 Oct 2020 14:55:37 +0800

author
aoqi
date
Sat, 24 Oct 2020 14:55:37 +0800
changeset 9933
0672fc1cf811
parent 9858
b985cbb00e68
permissions
-rw-r--r--

#15190 MIPS port of 8243823: JFR Backport - vm_version_ext_mips.(h|c)pp support

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

mercurial