| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * High resolution timer test software |
| 4 | * |
| 5 | * (C) 2013 Clark Williams <[email protected]> |
| 6 | * (C) 2013 John Kacur <[email protected]> |
| 7 | * (C) 2008-2012 Clark Williams <[email protected]> |
| 8 | * (C) 2005-2007 Thomas Gleixner <[email protected]> |
| 9 | * |
| 10 | */ |
| 11 | #ifdef HAVE_LIBCPUPOWER_SUPPORT |
| 12 | #include <cpuidle.h> |
| 13 | #endif /* HAVE_LIBCPUPOWER_SUPPORT */ |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stdarg.h> |
| 18 | #include <unistd.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <getopt.h> |
| 21 | #include <pthread.h> |
| 22 | #include <signal.h> |
| 23 | #include <sched.h> |
| 24 | #include <string.h> |
| 25 | #include <time.h> |
| 26 | #include <errno.h> |
| 27 | #include <limits.h> |
| 28 | |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <sys/resource.h> |
| 33 | #include <sys/utsname.h> |
| 34 | #include <sys/mman.h> |
| 35 | #include <sys/syscall.h> |
| 36 | #include "rt_numa.h" |
| 37 | |
| 38 | #include "rt-utils.h" |
| 39 | #include "rt-numa.h" |
| 40 | #include "rt-error.h" |
| 41 | #include "histogram.h" |
| 42 | |
| 43 | #include <bionic.h> |
| 44 | |
| 45 | #define DEFAULT_INTERVAL 1000 |
| 46 | #define DEFAULT_DISTANCE 500 |
| 47 | |
| 48 | #ifndef SCHED_IDLE |
| 49 | #define SCHED_IDLE 5 |
| 50 | #endif |
| 51 | #ifndef SCHED_NORMAL |
| 52 | #define SCHED_NORMAL SCHED_OTHER |
| 53 | #endif |
| 54 | |
| 55 | #define sigev_notify_thread_id _sigev_un._tid |
| 56 | |
| 57 | #ifdef __UCLIBC__ |
| 58 | #define MAKE_PROCESS_CPUCLOCK(pid, clock) \ |
| 59 | ((~(clockid_t) (pid) << 3) | (clockid_t) (clock)) |
| 60 | #define CPUCLOCK_SCHED 2 |
| 61 | |
| 62 | static int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *req, |
| 63 | struct timespec *rem) |
| 64 | { |
| 65 | if (clock_id == CLOCK_THREAD_CPUTIME_ID) |
| 66 | return -EINVAL; |
| 67 | if (clock_id == CLOCK_PROCESS_CPUTIME_ID) |
| 68 | clock_id = MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED); |
| 69 | |
| 70 | return syscall(__NR_clock_nanosleep, clock_id, flags, req, rem); |
| 71 | } |
| 72 | |
| 73 | int sched_setaffinity(__pid_t __pid, size_t __cpusetsize, |
| 74 | __const cpu_set_t *__cpuset) |
| 75 | { |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
| 79 | #undef CPU_SET |
| 80 | #undef CPU_ZERO |
| 81 | #define CPU_SET(cpu, cpusetp) |
| 82 | #define CPU_ZERO(cpusetp) |
| 83 | |
| 84 | #else |
| 85 | extern int clock_nanosleep(clockid_t __clock_id, int __flags, |
| 86 | __const struct timespec *__req, |
| 87 | struct timespec *__rem); |
| 88 | #endif /* __UCLIBC__ */ |
| 89 | |
| 90 | #define HIST_MAX 1000000 |
| 91 | |
| 92 | #define MODE_CYCLIC 0 |
| 93 | #define MODE_CLOCK_NANOSLEEP 1 |
| 94 | #define MODE_SYS_ITIMER 2 |
| 95 | #define MODE_SYS_NANOSLEEP 3 |
| 96 | #define MODE_SYS_OFFSET 2 |
| 97 | |
| 98 | #define TIMER_RELTIME 0 |
| 99 | |
| 100 | /* Must be power of 2 ! */ |
| 101 | #define VALBUF_SIZE 16384 |
| 102 | |
| 103 | #if (defined(__i386__) || defined(__x86_64__)) |
| 104 | #define ARCH_HAS_SMI_COUNTER |
| 105 | #endif |
| 106 | |
| 107 | #define MSR_SMI_COUNT 0x00000034 |
| 108 | #define MSR_SMI_COUNT_MASK 0xFFFFFFFF |
| 109 | |
| 110 | static char *policyname(int policy); |
| 111 | |
| 112 | /* Struct to transfer parameters to the thread */ |
| 113 | struct thread_param { |
| 114 | int prio; |
| 115 | int policy; |
| 116 | int mode; |
| 117 | int timermode; |
| 118 | int signal; |
| 119 | int clock; |
| 120 | unsigned long max_cycles; |
| 121 | struct thread_stat *stats; |
| 122 | int bufmsk; |
| 123 | unsigned long interval; |
| 124 | int cpu; |
| 125 | int node; |
| 126 | int tnum; |
| 127 | int msr_fd; |
| 128 | }; |
| 129 | |
| 130 | /* Struct for statistics */ |
| 131 | struct thread_stat { |
| 132 | unsigned long cycles; |
| 133 | unsigned long cyclesread; |
| 134 | long min; |
| 135 | long max; |
| 136 | long act; |
| 137 | double avg; |
| 138 | long *values; |
| 139 | long *smis; |
| 140 | struct histogram *hist; |
| 141 | pthread_t thread; |
| 142 | int threadstarted; |
| 143 | int tid; |
| 144 | long reduce; |
| 145 | long redmax; |
| 146 | long cycleofmax; |
| 147 | unsigned long smi_count; |
| 148 | }; |
| 149 | |
| 150 | static pthread_mutex_t trigger_lock = PTHREAD_MUTEX_INITIALIZER; |
| 151 | |
| 152 | static int trigger = 0; /* Record spikes > trigger, 0 means don't record */ |
| 153 | static int trigger_list_size = 1024; /* Number of list nodes */ |
| 154 | |
| 155 | /* Info to store when the diff is greater than the trigger */ |
| 156 | struct thread_trigger { |
| 157 | int cpu; |
| 158 | int tnum; /* thread number */ |
| 159 | int64_t ts; /* time-stamp */ |
| 160 | int diff; |
| 161 | struct thread_trigger *next; |
| 162 | }; |
| 163 | |
| 164 | struct thread_trigger *head = NULL; |
| 165 | struct thread_trigger *tail = NULL; |
| 166 | struct thread_trigger *current = NULL; |
| 167 | static int spikes; /* count of the number of spikes */ |
| 168 | |
| 169 | static int trigger_init(); |
| 170 | static void trigger_print(); |
| 171 | static void trigger_update(struct thread_param *par, int diff, int64_t ts); |
| 172 | |
| 173 | static int shutdown; |
| 174 | static int tracelimit = 0; |
| 175 | static int trace_marker = 0; |
| 176 | static int verbose = 0; |
| 177 | static int oscope_reduction = 1; |
| 178 | static int lockall = 0; |
| 179 | static int histogram = 0; |
| 180 | static int histofall = 0; |
| 181 | static int duration = 0; |
| 182 | static int use_nsecs = 0; |
| 183 | static int refresh_on_max; |
| 184 | static int force_sched_other; |
| 185 | static int priospread = 0; |
| 186 | static int check_clock_resolution; |
| 187 | static int ct_debug; |
| 188 | static int use_fifo = 0; |
| 189 | static pthread_t fifo_threadid; |
| 190 | static int laptop = 0; |
| 191 | static int power_management = 0; |
| 192 | static int use_histfile = 0; |
| 193 | |
| 194 | #ifdef ARCH_HAS_SMI_COUNTER |
| 195 | static int smi = 0; |
| 196 | #else |
| 197 | #define smi 0 |
| 198 | #endif |
| 199 | |
| 200 | static pthread_cond_t refresh_on_max_cond = PTHREAD_COND_INITIALIZER; |
| 201 | static pthread_mutex_t refresh_on_max_lock = PTHREAD_MUTEX_INITIALIZER; |
| 202 | |
| 203 | static pthread_mutex_t break_thread_id_lock = PTHREAD_MUTEX_INITIALIZER; |
| 204 | static pid_t break_thread_id = 0; |
| 205 | static uint64_t break_thread_value = 0; |
| 206 | |
| 207 | static int aligned = 0; |
| 208 | static int secaligned = 0; |
| 209 | static int offset = 0; |
| 210 | static pthread_barrier_t align_barr; |
| 211 | static pthread_barrier_t globalt_barr; |
| 212 | static struct timespec globalt; |
| 213 | |
| 214 | static char fifopath[MAX_PATH]; |
| 215 | static char histfile[MAX_PATH]; |
| 216 | static char jsonfile[MAX_PATH]; |
| 217 | |
| 218 | static struct thread_param **parameters; |
| 219 | static struct thread_stat **statistics; |
| 220 | static struct histoset hset; |
| 221 | |
| 222 | static void print_stat(FILE *fp, struct thread_param *par, int index, int verbose, int quiet); |
| 223 | static void rstat_print_stat(struct thread_param *par, int index, int verbose, int quiet); |
| 224 | static void rstat_setup(void); |
| 225 | |
| 226 | static int latency_target_fd = -1; |
| 227 | static int32_t latency_target_value = 0; |
| 228 | |
| 229 | static int deepest_idle_state = -2; |
| 230 | |
| 231 | static int rstat_ftruncate(int fd, off_t len); |
| 232 | static int rstat_fd = -1; |
| 233 | /* strlen("/cyclictest") + digits in max pid len + '\0' */ |
| 234 | #define SHM_BUF_SIZE 19 |
| 235 | static char shm_name[SHM_BUF_SIZE]; |
| 236 | |
| 237 | /* Latency trick |
| 238 | * if the file /dev/cpu_dma_latency exists, |
| 239 | * open it and write a zero into it. This will tell |
| 240 | * the power management system not to transition to |
| 241 | * a high cstate (in fact, the system acts like idle=poll) |
| 242 | * When the fd to /dev/cpu_dma_latency is closed, the behavior |
| 243 | * goes back to the system default. |
| 244 | * |
| 245 | * Documentation/power/pm_qos_interface.txt |
| 246 | */ |
| 247 | static void set_latency_target(void) |
| 248 | { |
| 249 | struct stat s; |
| 250 | int err; |
| 251 | |
| 252 | if (laptop) { |
| 253 | warn("not setting cpu_dma_latency to save battery power\n"); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (power_management) { |
| 258 | warn("not setting cpu_dma_latency from cyclictest\n"); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | if (deepest_idle_state >= -1) { |
| 263 | warn("not setting cpu_dma_latency, --deepest-idle-state is set instead\n"); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | errno = 0; |
| 268 | err = stat("/dev/cpu_dma_latency", &s); |
| 269 | if (err == -1) { |
| 270 | err_msg_n(errno, "WARN: stat /dev/cpu_dma_latency failed"); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | errno = 0; |
| 275 | latency_target_fd = open("/dev/cpu_dma_latency", O_RDWR); |
| 276 | if (latency_target_fd == -1) { |
| 277 | err_msg_n(errno, "WARN: open /dev/cpu_dma_latency"); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | errno = 0; |
| 282 | err = write(latency_target_fd, &latency_target_value, 4); |
| 283 | if (err < 1) { |
| 284 | err_msg_n(errno, "# error setting cpu_dma_latency to %d!", latency_target_value); |
| 285 | close(latency_target_fd); |
| 286 | return; |
| 287 | } |
| 288 | printf("# /dev/cpu_dma_latency set to %dus\n", latency_target_value); |
| 289 | } |
| 290 | |
| 291 | #ifdef HAVE_LIBCPUPOWER_SUPPORT |
| 292 | static unsigned int **saved_cpu_idle_disable_state; |
| 293 | static size_t saved_cpu_idle_disable_state_alloc_ctr; |
| 294 | |
| 295 | /* |
| 296 | * save_cpu_idle_state_disable - save disable for all idle states of a cpu |
| 297 | * |
| 298 | * Saves the current disable of all idle states of a cpu, to be subsequently |
| 299 | * restored via restore_cpu_idle_disable_state. |
| 300 | * |
| 301 | * Return: idle state count on success, negative on error |
| 302 | */ |
| 303 | static int save_cpu_idle_disable_state(unsigned int cpu) |
| 304 | { |
| 305 | unsigned int nr_states; |
| 306 | unsigned int state; |
| 307 | int disabled; |
| 308 | int nr_cpus; |
| 309 | |
| 310 | nr_states = cpuidle_state_count(cpu); |
| 311 | |
| 312 | if (nr_states == 0) |
| 313 | return 0; |
| 314 | |
| 315 | if (saved_cpu_idle_disable_state == NULL) { |
| 316 | nr_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 317 | saved_cpu_idle_disable_state = calloc(nr_cpus, sizeof(unsigned int *)); |
| 318 | if (!saved_cpu_idle_disable_state) |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | saved_cpu_idle_disable_state[cpu] = calloc(nr_states, sizeof(unsigned int)); |
| 323 | if (!saved_cpu_idle_disable_state[cpu]) |
| 324 | return -1; |
| 325 | saved_cpu_idle_disable_state_alloc_ctr++; |
| 326 | |
| 327 | for (state = 0; state < nr_states; state++) { |
| 328 | disabled = cpuidle_is_state_disabled(cpu, state); |
| 329 | if (disabled < 0) |
| 330 | return disabled; |
| 331 | saved_cpu_idle_disable_state[cpu][state] = disabled; |
| 332 | } |
| 333 | |
| 334 | return nr_states; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * restore_cpu_idle_disable_state - restore disable for all idle states of a cpu |
| 339 | * |
| 340 | * Restores the current disable state of all idle states of a cpu that was |
| 341 | * previously saved by save_cpu_idle_disable_state. |
| 342 | * |
| 343 | * Return: idle state count on success, negative on error |
| 344 | */ |
| 345 | static int restore_cpu_idle_disable_state(unsigned int cpu) |
| 346 | { |
| 347 | unsigned int nr_states; |
| 348 | unsigned int state; |
| 349 | int disabled; |
| 350 | int result; |
| 351 | |
| 352 | nr_states = cpuidle_state_count(cpu); |
| 353 | |
| 354 | if (nr_states == 0) |
| 355 | return 0; |
| 356 | |
| 357 | if (!saved_cpu_idle_disable_state) |
| 358 | return -1; |
| 359 | |
| 360 | for (state = 0; state < nr_states; state++) { |
| 361 | if (!saved_cpu_idle_disable_state[cpu]) |
| 362 | return -1; |
| 363 | disabled = saved_cpu_idle_disable_state[cpu][state]; |
| 364 | result = cpuidle_state_disable(cpu, state, disabled); |
| 365 | if (result < 0) |
| 366 | return result; |
| 367 | } |
| 368 | |
| 369 | free(saved_cpu_idle_disable_state[cpu]); |
| 370 | saved_cpu_idle_disable_state[cpu] = NULL; |
| 371 | saved_cpu_idle_disable_state_alloc_ctr--; |
| 372 | if (saved_cpu_idle_disable_state_alloc_ctr == 0) { |
| 373 | free(saved_cpu_idle_disable_state); |
| 374 | saved_cpu_idle_disable_state = NULL; |
| 375 | } |
| 376 | |
| 377 | return nr_states; |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * free_cpu_idle_disable_states - free saved idle state disable for all cpus |
| 382 | * |
| 383 | * Frees the memory used for storing cpu idle state disable for all cpus |
| 384 | * and states. |
| 385 | * |
| 386 | * Normally, the memory is freed automatically in |
| 387 | * restore_cpu_idle_disable_state; this is mostly for cleaning up after an |
| 388 | * error. |
| 389 | */ |
| 390 | static void free_cpu_idle_disable_states(void) |
| 391 | { |
| 392 | int cpu; |
| 393 | int nr_cpus; |
| 394 | |
| 395 | if (!saved_cpu_idle_disable_state) |
| 396 | return; |
| 397 | |
| 398 | nr_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 399 | |
| 400 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
| 401 | free(saved_cpu_idle_disable_state[cpu]); |
| 402 | saved_cpu_idle_disable_state[cpu] = NULL; |
| 403 | } |
| 404 | |
| 405 | free(saved_cpu_idle_disable_state); |
| 406 | saved_cpu_idle_disable_state = NULL; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * set_deepest_cpu_idle_state - limit idle state of cpu |
| 411 | * |
| 412 | * Disables all idle states deeper than the one given in |
| 413 | * deepest_state (assuming states with higher number are deeper). |
| 414 | * |
| 415 | * This is used to reduce the exit from idle latency. Unlike |
| 416 | * set_cpu_dma_latency, it can disable idle states per cpu. |
| 417 | * |
| 418 | * Return: idle state count on success, negative on error |
| 419 | */ |
| 420 | static int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int deepest_state) |
| 421 | { |
| 422 | unsigned int nr_states; |
| 423 | unsigned int state; |
| 424 | int result; |
| 425 | |
| 426 | nr_states = cpuidle_state_count(cpu); |
| 427 | |
| 428 | for (state = deepest_state + 1; state < nr_states; state++) { |
| 429 | result = cpuidle_state_disable(cpu, state, 1); |
| 430 | if (result < 0) |
| 431 | return result; |
| 432 | } |
| 433 | |
| 434 | return nr_states; |
| 435 | } |
| 436 | |
| 437 | static inline int have_libcpupower_support(void) { return 1; } |
| 438 | #else |
| 439 | static inline int save_cpu_idle_disable_state(unsigned int cpu) { return -1; } |
| 440 | static inline int restore_cpu_idle_disable_state(unsigned int cpu) { return -1; } |
| 441 | static inline void free_cpu_idle_disable_states(void) { } |
| 442 | static inline int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int state) { return -1; } |
| 443 | static inline int have_libcpupower_support(void) { return 0; } |
| 444 | #endif /* HAVE_LIBCPUPOWER_SUPPORT */ |
| 445 | |
| 446 | enum { |
| 447 | ERROR_GENERAL = -1, |
| 448 | ERROR_NOTFOUND = -2, |
| 449 | }; |
| 450 | |
| 451 | /* |
| 452 | * Raise the soft priority limit up to prio, if that is less than or equal |
| 453 | * to the hard limit |
| 454 | * if a call fails, return the error |
| 455 | * if successful return 0 |
| 456 | * if fails, return -1 |
| 457 | */ |
| 458 | static int raise_soft_prio(int policy, const struct sched_param *param) |
| 459 | { |
| 460 | int err; |
| 461 | int policy_max; /* max for scheduling policy such as SCHED_FIFO */ |
| 462 | int soft_max; |
| 463 | int hard_max; |
| 464 | int prio; |
| 465 | struct rlimit rlim; |
| 466 | |
| 467 | prio = param->sched_priority; |
| 468 | |
| 469 | policy_max = sched_get_priority_max(policy); |
| 470 | if (policy_max == -1) { |
| 471 | err = errno; |
| 472 | err_msg("WARN: no such policy\n"); |
| 473 | return err; |
| 474 | } |
| 475 | |
| 476 | err = getrlimit(RLIMIT_RTPRIO, &rlim); |
| 477 | if (err) { |
| 478 | err = errno; |
| 479 | err_msg_n(err, "WARN: getrlimit failed"); |
| 480 | return err; |
| 481 | } |
| 482 | |
| 483 | soft_max = (rlim.rlim_cur == RLIM_INFINITY) ? (unsigned int)policy_max |
| 484 | : rlim.rlim_cur; |
| 485 | hard_max = (rlim.rlim_max == RLIM_INFINITY) ? (unsigned int)policy_max |
| 486 | : rlim.rlim_max; |
| 487 | |
| 488 | if (prio > soft_max && prio <= hard_max) { |
| 489 | rlim.rlim_cur = prio; |
| 490 | err = setrlimit(RLIMIT_RTPRIO, &rlim); |
| 491 | if (err) { |
| 492 | err = errno; |
| 493 | err_msg_n(err, "WARN: setrlimit failed"); |
| 494 | /* return err; */ |
| 495 | } |
| 496 | } else { |
| 497 | err = -1; |
| 498 | } |
| 499 | |
| 500 | return err; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Check the error status of sched_setscheduler |
| 505 | * If an error can be corrected by raising the soft limit priority to |
| 506 | * a priority less than or equal to the hard limit, then do so. |
| 507 | */ |
| 508 | static int setscheduler(pid_t pid, int policy, const struct sched_param *param) |
| 509 | { |
| 510 | int err = 0; |
| 511 | |
| 512 | try_again: |
| 513 | err = sched_setscheduler(pid, policy, param); |
| 514 | if (err) { |
| 515 | err = errno; |
| 516 | if (err == EPERM) { |
| 517 | int err1; |
| 518 | err1 = raise_soft_prio(policy, param); |
| 519 | if (!err1) |
| 520 | goto try_again; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return err; |
| 525 | } |
| 526 | |
| 527 | #ifdef ARCH_HAS_SMI_COUNTER |
| 528 | static int open_msr_file(int cpu) |
| 529 | { |
| 530 | int fd; |
| 531 | char pathname[32]; |
| 532 | |
| 533 | /* SMI needs thread affinity */ |
| 534 | sprintf(pathname, "/dev/cpu/%d/msr", cpu); |
| 535 | fd = open(pathname, O_RDONLY); |
| 536 | if (fd < 0) |
| 537 | warn("%s open failed, try chown or chmod +r " |
| 538 | "/dev/cpu/*/msr, or run as root\n", pathname); |
| 539 | |
| 540 | return fd; |
| 541 | } |
| 542 | |
| 543 | static int get_msr(int fd, off_t offset, unsigned long long *msr) |
| 544 | { |
| 545 | ssize_t retval; |
| 546 | |
| 547 | retval = pread(fd, msr, sizeof *msr, offset); |
| 548 | |
| 549 | if (retval != sizeof *msr) |
| 550 | return 1; |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static int get_smi_counter(int fd, unsigned long *counter) |
| 556 | { |
| 557 | int retval; |
| 558 | unsigned long long msr; |
| 559 | |
| 560 | retval = get_msr(fd, MSR_SMI_COUNT, &msr); |
| 561 | if (retval) |
| 562 | return retval; |
| 563 | |
| 564 | *counter = (unsigned long) (msr & MSR_SMI_COUNT_MASK); |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | #include <cpuid.h> |
| 570 | |
| 571 | /* Guess if the CPU has an SMI counter in the model-specific registers (MSR). |
| 572 | * |
| 573 | * This is true for Intel x86 CPUs after Nehalem (2008). However, it's not |
| 574 | * possible to detect the feature directly, (or at least, turbostat.c doesn't |
| 575 | * know how to do it either) so we assume it's true for all x86 CPUs. |
| 576 | * Previously, this function had an explicit allowlist, which required updates |
| 577 | * every time a new CPU generation was released. |
| 578 | */ |
| 579 | static int has_smi_counter(void) |
| 580 | { |
| 581 | unsigned int ebx, ecx, edx, max_level; |
| 582 | unsigned int fms, family, model; |
| 583 | |
| 584 | fms = family = model = ebx = ecx = edx = 0; |
| 585 | |
| 586 | __get_cpuid(0, &max_level, &ebx, &ecx, &edx); |
| 587 | |
| 588 | /* check genuine intel */ |
| 589 | if (!(ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)) |
| 590 | return 0; |
| 591 | |
| 592 | __get_cpuid(1, &fms, &ebx, &ecx, &edx); |
| 593 | family = (fms >> 8) & 0xf; |
| 594 | |
| 595 | if (family != 6) |
| 596 | return 0; |
| 597 | |
| 598 | /* no MSR */ |
| 599 | if (!(edx & (1 << 5))) |
| 600 | return 0; |
| 601 | |
| 602 | return 1; |
| 603 | } |
| 604 | #else |
| 605 | static int open_msr_file(int cpu) |
| 606 | { |
| 607 | return -1; |
| 608 | } |
| 609 | |
| 610 | static int get_smi_counter(int fd, unsigned long *counter) |
| 611 | { |
| 612 | return 1; |
| 613 | } |
| 614 | static int has_smi_counter(void) |
| 615 | { |
| 616 | return 0; |
| 617 | } |
| 618 | #endif |
| 619 | |
| 620 | /* |
| 621 | * timer thread |
| 622 | * |
| 623 | * Modes: |
| 624 | * - clock_nanosleep based |
| 625 | * - cyclic timer based |
| 626 | * |
| 627 | * Clock: |
| 628 | * - CLOCK_MONOTONIC |
| 629 | * - CLOCK_REALTIME |
| 630 | * |
| 631 | */ |
| 632 | static void *timerthread(void *param) |
| 633 | { |
| 634 | struct thread_param *par = param; |
| 635 | struct sched_param schedp; |
| 636 | struct sigevent sigev; |
| 637 | sigset_t sigset; |
| 638 | timer_t timer; |
| 639 | struct timespec now, next, interval, stop = { 0 }; |
| 640 | struct itimerval itimer; |
| 641 | struct itimerspec tspec; |
| 642 | struct thread_stat *stat = par->stats; |
| 643 | int stopped = 0; |
| 644 | cpu_set_t mask; |
| 645 | pthread_t thread; |
| 646 | unsigned long smi_now, smi_old = 0; |
| 647 | |
| 648 | /* if we're running in numa mode, set our memory node */ |
| 649 | if (par->node != -1) |
| 650 | rt_numa_set_numa_run_on_node(par->node, par->cpu); |
| 651 | |
| 652 | if (par->cpu != -1) { |
| 653 | CPU_ZERO(&mask); |
| 654 | CPU_SET(par->cpu, &mask); |
| 655 | thread = pthread_self(); |
| 656 | if (pthread_setaffinity_np(thread, sizeof(mask), &mask) != 0) |
| 657 | warn("Could not set CPU affinity to CPU #%d\n", |
| 658 | par->cpu); |
| 659 | } |
| 660 | |
| 661 | interval.tv_sec = par->interval / USEC_PER_SEC; |
| 662 | interval.tv_nsec = (par->interval % USEC_PER_SEC) * 1000; |
| 663 | |
| 664 | stat->tid = gettid(); |
| 665 | |
| 666 | sigemptyset(&sigset); |
| 667 | sigaddset(&sigset, par->signal); |
| 668 | sigprocmask(SIG_BLOCK, &sigset, NULL); |
| 669 | |
| 670 | if (par->mode == MODE_CYCLIC) { |
| 671 | sigev.sigev_notify = SIGEV_THREAD_ID | SIGEV_SIGNAL; |
| 672 | sigev.sigev_signo = par->signal; |
| 673 | sigev.sigev_notify_thread_id = stat->tid; |
| 674 | timer_create(par->clock, &sigev, &timer); |
| 675 | tspec.it_interval = interval; |
| 676 | } |
| 677 | |
| 678 | memset(&schedp, 0, sizeof(schedp)); |
| 679 | schedp.sched_priority = par->prio; |
| 680 | if (setscheduler(0, par->policy, &schedp)) |
| 681 | fatal("timerthread%d: failed to set priority to %d\n", |
| 682 | par->cpu, par->prio); |
| 683 | |
| 684 | if (smi) { |
| 685 | par->msr_fd = open_msr_file(par->cpu); |
| 686 | if (par->msr_fd < 0) |
| 687 | fatal("Could not open MSR interface, errno: %d\n", |
| 688 | errno); |
| 689 | /* get current smi count to use as base value */ |
| 690 | if (get_smi_counter(par->msr_fd, &smi_old)) |
| 691 | fatal("Could not read SMI counter, errno: %d\n", |
| 692 | par->cpu, errno); |
| 693 | } |
| 694 | |
| 695 | /* Get current time */ |
| 696 | if (aligned || secaligned) { |
| 697 | pthread_barrier_wait(&globalt_barr); |
| 698 | if (par->tnum == 0) { |
| 699 | clock_gettime(par->clock, &globalt); |
| 700 | if (secaligned) { |
| 701 | /* Ensure that the thread start timestamp is not |
| 702 | * in the past */ |
| 703 | if (globalt.tv_nsec > 900000000) |
| 704 | globalt.tv_sec += 2; |
| 705 | else |
| 706 | globalt.tv_sec++; |
| 707 | globalt.tv_nsec = 0; |
| 708 | } |
| 709 | } |
| 710 | pthread_barrier_wait(&align_barr); |
| 711 | now = globalt; |
| 712 | if (offset) { |
| 713 | if (aligned) |
| 714 | now.tv_nsec += offset * par->tnum; |
| 715 | else |
| 716 | now.tv_nsec += offset; |
| 717 | tsnorm(&now); |
| 718 | } |
| 719 | } else |
| 720 | clock_gettime(par->clock, &now); |
| 721 | |
| 722 | next = now; |
| 723 | next.tv_sec += interval.tv_sec; |
| 724 | next.tv_nsec += interval.tv_nsec; |
| 725 | tsnorm(&next); |
| 726 | |
| 727 | if (duration) { |
| 728 | stop = now; |
| 729 | stop.tv_sec += duration; |
| 730 | } |
| 731 | if (par->mode == MODE_CYCLIC) { |
| 732 | if (par->timermode == TIMER_ABSTIME) |
| 733 | tspec.it_value = next; |
| 734 | else |
| 735 | tspec.it_value = interval; |
| 736 | timer_settime(timer, par->timermode, &tspec, NULL); |
| 737 | } |
| 738 | |
| 739 | if (par->mode == MODE_SYS_ITIMER) { |
| 740 | itimer.it_interval.tv_sec = interval.tv_sec; |
| 741 | itimer.it_interval.tv_usec = interval.tv_nsec / 1000; |
| 742 | itimer.it_value = itimer.it_interval; |
| 743 | setitimer(ITIMER_REAL, &itimer, NULL); |
| 744 | } |
| 745 | |
| 746 | stat->threadstarted++; |
| 747 | |
| 748 | while (!shutdown) { |
| 749 | |
| 750 | uint64_t diff; |
| 751 | unsigned long diff_smi = 0; |
| 752 | int sigs, ret; |
| 753 | |
| 754 | /* Wait for next period */ |
| 755 | switch (par->mode) { |
| 756 | case MODE_CYCLIC: |
| 757 | case MODE_SYS_ITIMER: |
| 758 | if (sigwait(&sigset, &sigs) < 0) |
| 759 | goto out; |
| 760 | break; |
| 761 | |
| 762 | case MODE_CLOCK_NANOSLEEP: |
| 763 | if (par->timermode == TIMER_ABSTIME) { |
| 764 | ret = clock_nanosleep(par->clock, TIMER_ABSTIME, |
| 765 | &next, NULL); |
| 766 | if (ret != 0) { |
| 767 | if (ret != EINTR) |
| 768 | warn("clock_nanosleep failed. errno: %d\n", errno); |
| 769 | goto out; |
| 770 | } |
| 771 | } else { |
| 772 | ret = clock_gettime(par->clock, &now); |
| 773 | if (ret != 0) { |
| 774 | if (ret != EINTR) |
| 775 | warn("clock_gettime() failed: %s", strerror(errno)); |
| 776 | goto out; |
| 777 | } |
| 778 | ret = clock_nanosleep(par->clock, |
| 779 | TIMER_RELTIME, &interval, NULL); |
| 780 | if (ret != 0) { |
| 781 | if (ret != EINTR) |
| 782 | warn("clock_nanosleep() failed. errno: %d\n", errno); |
| 783 | goto out; |
| 784 | } |
| 785 | next.tv_sec = now.tv_sec + interval.tv_sec; |
| 786 | next.tv_nsec = now.tv_nsec + interval.tv_nsec; |
| 787 | tsnorm(&next); |
| 788 | } |
| 789 | break; |
| 790 | |
| 791 | case MODE_SYS_NANOSLEEP: |
| 792 | ret = clock_gettime(par->clock, &now); |
| 793 | if (ret != 0) { |
| 794 | if (ret != EINTR) |
| 795 | warn("clock_gettime() failed: errno %d\n", errno); |
| 796 | goto out; |
| 797 | } |
| 798 | if (nanosleep(&interval, NULL)) { |
| 799 | if (errno != EINTR) |
| 800 | warn("nanosleep failed. errno: %d\n", |
| 801 | errno); |
| 802 | goto out; |
| 803 | } |
| 804 | next.tv_sec = now.tv_sec + interval.tv_sec; |
| 805 | next.tv_nsec = now.tv_nsec + interval.tv_nsec; |
| 806 | tsnorm(&next); |
| 807 | break; |
| 808 | } |
| 809 | ret = clock_gettime(par->clock, &now); |
| 810 | if (ret != 0) { |
| 811 | if (ret != EINTR) |
| 812 | warn("clock_gettime() failed. errno: %d\n", |
| 813 | errno); |
| 814 | goto out; |
| 815 | } |
| 816 | |
| 817 | if (smi) { |
| 818 | if (get_smi_counter(par->msr_fd, &smi_now)) { |
| 819 | warn("Could not read SMI counter, errno: %d\n", |
| 820 | par->cpu, errno); |
| 821 | goto out; |
| 822 | } |
| 823 | diff_smi = smi_now - smi_old; |
| 824 | stat->smi_count += diff_smi; |
| 825 | smi_old = smi_now; |
| 826 | } |
| 827 | |
| 828 | if (use_nsecs) |
| 829 | diff = calcdiff_ns(now, next); |
| 830 | else |
| 831 | diff = calcdiff(now, next); |
| 832 | if (diff < stat->min) |
| 833 | stat->min = diff; |
| 834 | if (diff > stat->max) { |
| 835 | stat->max = diff; |
| 836 | if (refresh_on_max) |
| 837 | pthread_cond_signal(&refresh_on_max_cond); |
| 838 | } |
| 839 | stat->avg += (double) diff; |
| 840 | |
| 841 | if (trigger && (diff > trigger)) |
| 842 | trigger_update(par, diff, calctime(now)); |
| 843 | |
| 844 | if (duration && (calcdiff(now, stop) >= 0)) |
| 845 | shutdown++; |
| 846 | |
| 847 | if (!stopped && tracelimit && (diff > tracelimit)) { |
| 848 | stopped++; |
| 849 | shutdown++; |
| 850 | pthread_mutex_lock(&break_thread_id_lock); |
| 851 | if (break_thread_id == 0) { |
| 852 | break_thread_id = stat->tid; |
| 853 | tracemark("hit latency threshold (%llu > %d)", |
| 854 | (unsigned long long) diff, tracelimit); |
| 855 | tracing_stop(); |
| 856 | break_thread_value = diff; |
| 857 | } |
| 858 | pthread_mutex_unlock(&break_thread_id_lock); |
| 859 | } |
| 860 | stat->act = diff; |
| 861 | |
| 862 | if (par->bufmsk) { |
| 863 | stat->values[stat->cycles & par->bufmsk] = diff; |
| 864 | if (smi) |
| 865 | stat->smis[stat->cycles & par->bufmsk] = diff_smi; |
| 866 | } |
| 867 | |
| 868 | /* Update the histogram */ |
| 869 | if (histogram) |
| 870 | hist_sample(stat->hist, diff); |
| 871 | |
| 872 | stat->cycles++; |
| 873 | |
| 874 | next.tv_sec += interval.tv_sec; |
| 875 | next.tv_nsec += interval.tv_nsec; |
| 876 | if (par->mode == MODE_CYCLIC) { |
| 877 | int overrun_count = timer_getoverrun(timer); |
| 878 | next.tv_sec += overrun_count * interval.tv_sec; |
| 879 | next.tv_nsec += overrun_count * interval.tv_nsec; |
| 880 | } |
| 881 | tsnorm(&next); |
| 882 | |
| 883 | while (tsgreater(&now, &next)) { |
| 884 | next.tv_sec += interval.tv_sec; |
| 885 | next.tv_nsec += interval.tv_nsec; |
| 886 | tsnorm(&next); |
| 887 | } |
| 888 | |
| 889 | if (par->max_cycles && par->max_cycles == stat->cycles) |
| 890 | break; |
| 891 | } |
| 892 | |
| 893 | out: |
| 894 | if (refresh_on_max) { |
| 895 | pthread_mutex_lock(&refresh_on_max_lock); |
| 896 | /* We could reach here with both shutdown and allstopped unset (0). |
| 897 | * Set shutdown with synchronization to notify the main |
| 898 | * thread not to be blocked when it should exit. |
| 899 | */ |
| 900 | shutdown++; |
| 901 | pthread_cond_signal(&refresh_on_max_cond); |
| 902 | pthread_mutex_unlock(&refresh_on_max_lock); |
| 903 | } |
| 904 | |
| 905 | if (par->mode == MODE_CYCLIC) |
| 906 | timer_delete(timer); |
| 907 | |
| 908 | if (par->mode == MODE_SYS_ITIMER) { |
| 909 | itimer.it_value.tv_sec = 0; |
| 910 | itimer.it_value.tv_usec = 0; |
| 911 | itimer.it_interval.tv_sec = 0; |
| 912 | itimer.it_interval.tv_usec = 0; |
| 913 | setitimer(ITIMER_REAL, &itimer, NULL); |
| 914 | } |
| 915 | |
| 916 | /* close msr file */ |
| 917 | if (smi) |
| 918 | close(par->msr_fd); |
| 919 | /* switch to normal */ |
| 920 | schedp.sched_priority = 0; |
| 921 | sched_setscheduler(0, SCHED_OTHER, &schedp); |
| 922 | stat->threadstarted = -1; |
| 923 | |
| 924 | return NULL; |
| 925 | } |
| 926 | |
| 927 | |
| 928 | /* Print usage information */ |
| 929 | static void display_help(int error) |
| 930 | { |
| 931 | printf("cyclictest V %1.2f\n", VERSION); |
| 932 | printf("Usage:\n" |
| 933 | "cyclictest <options>\n\n" |
| 934 | "-a [CPUSET] --affinity Run thread #N on processor #N, if possible,\n" |
| 935 | " or if CPUSET given, pin threads to that set of\n" |
| 936 | " processors in round-robin order.\n" |
| 937 | " E.g. -a 2 pins all threads to CPU 2, but\n" |
| 938 | " -a 3-5,0 -t 5 will run the first and fifth threads\n" |
| 939 | " on CPU 0, the second thread on CPU 3,\n" |
| 940 | " the third thread on CPU 4,\n" |
| 941 | " and the fourth thread on CPU 5.\n" |
| 942 | "-A USEC --aligned=USEC align thread wakeups to a specific offset\n" |
| 943 | "-b USEC --breaktrace=USEC send break trace command when latency > USEC\n" |
| 944 | "-c CLOCK --clock=CLOCK select clock\n" |
| 945 | " 0 = CLOCK_MONOTONIC (default)\n" |
| 946 | " 1 = CLOCK_REALTIME\n" |
| 947 | " --deepest-idle-state=n\n" |
| 948 | " Reduce exit from idle latency by limiting idle state\n" |
| 949 | " up to n on used cpus (-1 disables all idle states).\n" |
| 950 | " Power management is not suppresed on other cpus.\n" |
| 951 | " --default-system Don't attempt to tune the system from cyclictest.\n" |
| 952 | " Power management is not suppressed.\n" |
| 953 | " This might give poorer results, but will allow you\n" |
| 954 | " to discover if you need to tune the system\n" |
| 955 | "-d DIST --distance=DIST distance of thread intervals in us, default=500\n" |
| 956 | "-D --duration=TIME specify a length for the test run.\n" |
| 957 | " Append 'm', 'h', or 'd' to specify minutes, hours or days.\n" |
| 958 | "-F --fifo=<path> create a named pipe at path and write stats to it\n" |
| 959 | "-h --histogram=US dump a latency histogram to stdout after the run\n" |
| 960 | " US is the max latency time to be tracked in microseconds\n" |
| 961 | " This option runs all threads at the same priority.\n" |
| 962 | "-H --histofall=US same as -h except with an additional summary column\n" |
| 963 | " --histfile=<path> dump the latency histogram to <path> instead of stdout\n" |
| 964 | "-i INTV --interval=INTV base interval of thread in us default=1000\n" |
| 965 | " --json=FILENAME write final results into FILENAME, JSON formatted\n" |
| 966 | " --laptop Save battery when running cyclictest\n" |
| 967 | " This will give you poorer realtime results\n" |
| 968 | " but will not drain your battery so quickly\n" |
| 969 | " --latency=PM_QOS power management latency target value\n" |
| 970 | " This value is written to /dev/cpu_dma_latency\n" |
| 971 | " and affects c-states. The default is 0\n" |
| 972 | "-l LOOPS --loops=LOOPS number of loops: default=0(endless)\n" |
| 973 | " --mainaffinity=CPUSET\n" |
| 974 | " Run the main thread on CPU #N. This only affects\n" |
| 975 | " the main thread and not the measurement threads\n" |
| 976 | "-m --mlockall lock current and future memory allocations\n" |
| 977 | "-M --refresh_on_max delay updating the screen until a new max\n" |
| 978 | " latency is hit. Useful for low bandwidth.\n" |
| 979 | "-N --nsecs print results in ns instead of us (default us)\n" |
| 980 | "-o RED --oscope=RED oscilloscope mode, reduce verbose output by RED\n" |
| 981 | "-p PRIO --priority=PRIO priority of highest prio thread\n" |
| 982 | " --policy=NAME policy of measurement thread, where NAME may be one\n" |
| 983 | " of: other, normal, batch, idle, fifo or rr.\n" |
| 984 | " --priospread spread priority levels starting at specified value\n" |
| 985 | "-q --quiet print a summary only on exit\n" |
| 986 | "-r --relative use relative timer instead of absolute\n" |
| 987 | "-R --resolution check clock resolution, calling clock_gettime() many\n" |
| 988 | " times. List of clock_gettime() values will be\n" |
| 989 | " reported with -X\n" |
| 990 | " --secaligned [USEC] align thread wakeups to the next full second\n" |
| 991 | " and apply the optional offset\n" |
| 992 | "-s --system use sys_nanosleep and sys_setitimer\n" |
| 993 | "-S --smp Standard SMP testing: options -a -t and same priority\n" |
| 994 | " of all threads\n" |
| 995 | " --spike=<trigger> record all spikes > trigger\n" |
| 996 | " --spike-nodes=[num of nodes]\n" |
| 997 | " These are the maximum number of spikes we can record.\n" |
| 998 | " The default is 1024 if not specified\n" |
| 999 | #ifdef ARCH_HAS_SMI_COUNTER |
| 1000 | " --smi Enable SMI counting\n" |
| 1001 | #endif |
| 1002 | "-t --threads one thread per available processor\n" |
| 1003 | "-t [NUM] --threads=NUM number of threads:\n" |
| 1004 | " without NUM, threads = max_cpus\n" |
| 1005 | " without -t default = 1\n" |
| 1006 | " --tracemark write a trace mark when -b latency is exceeded\n" |
| 1007 | "-u --unbuffered force unbuffered output for live processing\n" |
| 1008 | "-v --verbose output values on stdout for statistics\n" |
| 1009 | " format: n:c:v n=tasknum c=count v=value in us\n" |
| 1010 | " --dbg_cyclictest print info useful for debugging cyclictest\n" |
| 1011 | "-x --posix_timers use POSIX timers instead of clock_nanosleep.\n" |
| 1012 | ); |
| 1013 | if (error) |
| 1014 | exit(EXIT_FAILURE); |
| 1015 | exit(EXIT_SUCCESS); |
| 1016 | } |
| 1017 | |
| 1018 | static int use_nanosleep = MODE_CLOCK_NANOSLEEP; |
| 1019 | static int timermode = TIMER_ABSTIME; |
| 1020 | static int use_system; |
| 1021 | static int priority; |
| 1022 | static int policy = SCHED_OTHER; /* default policy if not specified */ |
| 1023 | static int num_threads = 1; |
| 1024 | static int max_cycles; |
| 1025 | static int clocksel = 0; |
| 1026 | static int quiet; |
| 1027 | static int interval = DEFAULT_INTERVAL; |
| 1028 | static int distance = -1; |
| 1029 | static struct bitmask *affinity_mask = NULL; |
| 1030 | static struct bitmask *main_affinity_mask = NULL; |
| 1031 | static int smp = 0; |
| 1032 | static int setaffinity = AFFINITY_UNSPECIFIED; |
| 1033 | |
| 1034 | static int clocksources[] = { |
| 1035 | CLOCK_MONOTONIC, |
| 1036 | CLOCK_REALTIME, |
| 1037 | }; |
| 1038 | |
| 1039 | static void handlepolicy(char *polname) |
| 1040 | { |
| 1041 | if (strncasecmp(polname, "other", 5) == 0) |
| 1042 | policy = SCHED_OTHER; |
| 1043 | else if (strncasecmp(polname, "batch", 5) == 0) |
| 1044 | policy = SCHED_BATCH; |
| 1045 | else if (strncasecmp(polname, "idle", 4) == 0) |
| 1046 | policy = SCHED_IDLE; |
| 1047 | else if (strncasecmp(polname, "fifo", 4) == 0) |
| 1048 | policy = SCHED_FIFO; |
| 1049 | else if (strncasecmp(polname, "rr", 2) == 0) |
| 1050 | policy = SCHED_RR; |
| 1051 | else /* default policy if we don't recognize the request */ |
| 1052 | policy = SCHED_OTHER; |
| 1053 | } |
| 1054 | |
| 1055 | static char *policyname(int policy) |
| 1056 | { |
| 1057 | char *policystr = ""; |
| 1058 | |
| 1059 | switch(policy) { |
| 1060 | case SCHED_OTHER: |
| 1061 | policystr = "other"; |
| 1062 | break; |
| 1063 | case SCHED_FIFO: |
| 1064 | policystr = "fifo"; |
| 1065 | break; |
| 1066 | case SCHED_RR: |
| 1067 | policystr = "rr"; |
| 1068 | break; |
| 1069 | case SCHED_BATCH: |
| 1070 | policystr = "batch"; |
| 1071 | break; |
| 1072 | case SCHED_IDLE: |
| 1073 | policystr = "idle"; |
| 1074 | break; |
| 1075 | } |
| 1076 | return policystr; |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | enum option_values { |
| 1081 | OPT_AFFINITY=1, OPT_BREAKTRACE, OPT_CLOCK, |
| 1082 | OPT_DEFAULT_SYSTEM, OPT_DISTANCE, OPT_DURATION, OPT_LATENCY, |
| 1083 | OPT_FIFO, OPT_HISTOGRAM, OPT_HISTOFALL, OPT_HISTFILE, |
| 1084 | OPT_INTERVAL, OPT_JSON, OPT_MAINAFFINITY, OPT_LOOPS, OPT_MLOCKALL, |
| 1085 | OPT_REFRESH, OPT_NANOSLEEP, OPT_NSECS, OPT_OSCOPE, OPT_PRIORITY, |
| 1086 | OPT_QUIET, OPT_PRIOSPREAD, OPT_RELATIVE, OPT_RESOLUTION, |
| 1087 | OPT_SYSTEM, OPT_SMP, OPT_THREADS, OPT_TRIGGER, |
| 1088 | OPT_TRIGGER_NODES, OPT_UNBUFFERED, OPT_NUMA, OPT_VERBOSE, |
| 1089 | OPT_DBGCYCLIC, OPT_POLICY, OPT_HELP, OPT_NUMOPTS, |
| 1090 | OPT_ALIGNED, OPT_SECALIGNED, OPT_LAPTOP, OPT_SMI, |
| 1091 | OPT_TRACEMARK, OPT_POSIX_TIMERS, OPT_DEEPEST_IDLE_STATE, |
| 1092 | }; |
| 1093 | |
| 1094 | /* Process commandline options */ |
| 1095 | static void process_options(int argc, char *argv[], int max_cpus) |
| 1096 | { |
| 1097 | int error = 0; |
| 1098 | int option_affinity = 0; |
| 1099 | |
| 1100 | for (;;) { |
| 1101 | int option_index = 0; |
| 1102 | /* |
| 1103 | * Options for getopt |
| 1104 | * Ordered alphabetically by single letter name |
| 1105 | */ |
| 1106 | static struct option long_options[] = { |
| 1107 | {"affinity", optional_argument, NULL, OPT_AFFINITY}, |
| 1108 | {"aligned", optional_argument, NULL, OPT_ALIGNED }, |
| 1109 | {"breaktrace", required_argument, NULL, OPT_BREAKTRACE }, |
| 1110 | {"clock", required_argument, NULL, OPT_CLOCK }, |
| 1111 | {"default-system", no_argument, NULL, OPT_DEFAULT_SYSTEM }, |
| 1112 | {"distance", required_argument, NULL, OPT_DISTANCE }, |
| 1113 | {"duration", required_argument, NULL, OPT_DURATION }, |
| 1114 | {"latency", required_argument, NULL, OPT_LATENCY }, |
| 1115 | {"fifo", required_argument, NULL, OPT_FIFO }, |
| 1116 | {"histogram", required_argument, NULL, OPT_HISTOGRAM }, |
| 1117 | {"histofall", required_argument, NULL, OPT_HISTOFALL }, |
| 1118 | {"histfile", required_argument, NULL, OPT_HISTFILE }, |
| 1119 | {"interval", required_argument, NULL, OPT_INTERVAL }, |
| 1120 | {"json", required_argument, NULL, OPT_JSON }, |
| 1121 | {"laptop", no_argument, NULL, OPT_LAPTOP }, |
| 1122 | {"loops", required_argument, NULL, OPT_LOOPS }, |
| 1123 | {"mainaffinity", required_argument, NULL, OPT_MAINAFFINITY}, |
| 1124 | {"mlockall", no_argument, NULL, OPT_MLOCKALL }, |
| 1125 | {"refresh_on_max", no_argument, NULL, OPT_REFRESH }, |
| 1126 | {"nsecs", no_argument, NULL, OPT_NSECS }, |
| 1127 | {"oscope", required_argument, NULL, OPT_OSCOPE }, |
| 1128 | {"priority", required_argument, NULL, OPT_PRIORITY }, |
| 1129 | {"quiet", no_argument, NULL, OPT_QUIET }, |
| 1130 | {"priospread", no_argument, NULL, OPT_PRIOSPREAD }, |
| 1131 | {"relative", no_argument, NULL, OPT_RELATIVE }, |
| 1132 | {"resolution", no_argument, NULL, OPT_RESOLUTION }, |
| 1133 | {"secaligned", optional_argument, NULL, OPT_SECALIGNED }, |
| 1134 | {"system", no_argument, NULL, OPT_SYSTEM }, |
| 1135 | {"smi", no_argument, NULL, OPT_SMI }, |
| 1136 | {"smp", no_argument, NULL, OPT_SMP }, |
| 1137 | {"spike", required_argument, NULL, OPT_TRIGGER }, |
| 1138 | {"spike-nodes", required_argument, NULL, OPT_TRIGGER_NODES }, |
| 1139 | {"threads", optional_argument, NULL, OPT_THREADS }, |
| 1140 | {"tracemark", no_argument, NULL, OPT_TRACEMARK }, |
| 1141 | {"unbuffered", no_argument, NULL, OPT_UNBUFFERED }, |
| 1142 | {"verbose", no_argument, NULL, OPT_VERBOSE }, |
| 1143 | {"dbg_cyclictest", no_argument, NULL, OPT_DBGCYCLIC }, |
| 1144 | {"policy", required_argument, NULL, OPT_POLICY }, |
| 1145 | {"help", no_argument, NULL, OPT_HELP }, |
| 1146 | {"posix_timers", no_argument, NULL, OPT_POSIX_TIMERS }, |
| 1147 | {"deepest-idle-state", required_argument, NULL, OPT_DEEPEST_IDLE_STATE }, |
| 1148 | {NULL, 0, NULL, 0 }, |
| 1149 | }; |
| 1150 | int c = getopt_long(argc, argv, "a::A::b:c:d:D:F:h:H:i:l:MNo:p:mqrRsSt::uvx", |
| 1151 | long_options, &option_index); |
| 1152 | if (c == -1) |
| 1153 | break; |
| 1154 | switch (c) { |
| 1155 | case 'a': |
| 1156 | case OPT_AFFINITY: |
| 1157 | option_affinity = 1; |
| 1158 | /* smp sets AFFINITY_USEALL in OPT_SMP */ |
| 1159 | if (smp) |
| 1160 | break; |
| 1161 | numa = numa_initialize(); |
| 1162 | if (optarg) { |
| 1163 | parse_cpumask(optarg, max_cpus, &affinity_mask); |
| 1164 | setaffinity = AFFINITY_SPECIFIED; |
| 1165 | } else if (optind < argc && |
| 1166 | (atoi(argv[optind]) || |
| 1167 | argv[optind][0] == '0' || |
| 1168 | argv[optind][0] == '!' || |
| 1169 | argv[optind][0] == '+' || |
| 1170 | argv[optind][0] == 'a')) { |
| 1171 | parse_cpumask(argv[optind], max_cpus, &affinity_mask); |
| 1172 | setaffinity = AFFINITY_SPECIFIED; |
| 1173 | } else { |
| 1174 | setaffinity = AFFINITY_USEALL; |
| 1175 | } |
| 1176 | |
| 1177 | if (setaffinity == AFFINITY_SPECIFIED && !affinity_mask) |
| 1178 | display_help(1); |
| 1179 | if (verbose && affinity_mask) |
| 1180 | printf("Using %u cpus.\n", |
| 1181 | numa_bitmask_weight(affinity_mask)); |
| 1182 | break; |
| 1183 | case 'A': |
| 1184 | case OPT_ALIGNED: |
| 1185 | aligned = 1; |
| 1186 | if (optarg != NULL) |
| 1187 | offset = atoi(optarg) * 1000; |
| 1188 | else if (optind < argc && atoi(argv[optind])) |
| 1189 | offset = atoi(argv[optind]) * 1000; |
| 1190 | else |
| 1191 | offset = 0; |
| 1192 | break; |
| 1193 | case 'b': |
| 1194 | case OPT_BREAKTRACE: |
| 1195 | tracelimit = atoi(optarg); break; |
| 1196 | case 'c': |
| 1197 | case OPT_CLOCK: |
| 1198 | clocksel = atoi(optarg); break; |
| 1199 | case OPT_DEFAULT_SYSTEM: |
| 1200 | power_management = 1; break; |
| 1201 | case 'd': |
| 1202 | case OPT_DISTANCE: |
| 1203 | distance = atoi(optarg); break; |
| 1204 | case 'D': |
| 1205 | case OPT_DURATION: |
| 1206 | duration = parse_time_string(optarg); break; |
| 1207 | case 'F': |
| 1208 | case OPT_FIFO: |
| 1209 | use_fifo = 1; |
| 1210 | strncpy(fifopath, optarg, strnlen(optarg, MAX_PATH-1)); |
| 1211 | break; |
| 1212 | case 'H': |
| 1213 | case OPT_HISTOFALL: |
| 1214 | histofall = 1; /* fall through */ |
| 1215 | case 'h': |
| 1216 | case OPT_HISTOGRAM: |
| 1217 | histogram = atoi(optarg); |
| 1218 | if (!histogram) |
| 1219 | display_help(1); |
| 1220 | break; |
| 1221 | case OPT_HISTFILE: |
| 1222 | use_histfile = 1; |
| 1223 | strncpy(histfile, optarg, strnlen(optarg, MAX_PATH-1)); |
| 1224 | break; |
| 1225 | case 'i': |
| 1226 | case OPT_INTERVAL: |
| 1227 | interval = atoi(optarg); break; |
| 1228 | case OPT_JSON: |
| 1229 | strncpy(jsonfile, optarg, strnlen(optarg, MAX_PATH-1)); |
| 1230 | break; |
| 1231 | case 'l': |
| 1232 | case OPT_LOOPS: |
| 1233 | max_cycles = atoi(optarg); break; |
| 1234 | case OPT_MAINAFFINITY: |
| 1235 | if (optarg) { |
| 1236 | parse_cpumask(optarg, max_cpus, &main_affinity_mask); |
| 1237 | } else if (optind < argc && |
| 1238 | (atoi(argv[optind]) || |
| 1239 | argv[optind][0] == '0' || |
| 1240 | argv[optind][0] == '!')) { |
| 1241 | parse_cpumask(argv[optind], max_cpus, &main_affinity_mask); |
| 1242 | } |
| 1243 | break; |
| 1244 | case 'm': |
| 1245 | case OPT_MLOCKALL: |
| 1246 | lockall = 1; break; |
| 1247 | case 'M': |
| 1248 | case OPT_REFRESH: |
| 1249 | refresh_on_max = 1; break; |
| 1250 | case 'N': |
| 1251 | case OPT_NSECS: |
| 1252 | use_nsecs = 1; break; |
| 1253 | case 'o': |
| 1254 | case OPT_OSCOPE: |
| 1255 | oscope_reduction = atoi(optarg); break; |
| 1256 | case 'p': |
| 1257 | case OPT_PRIORITY: |
| 1258 | priority = atoi(optarg); |
| 1259 | if (policy != SCHED_FIFO && policy != SCHED_RR) |
| 1260 | policy = SCHED_FIFO; |
| 1261 | break; |
| 1262 | case 'q': |
| 1263 | case OPT_QUIET: |
| 1264 | quiet = 1; break; |
| 1265 | case 'r': |
| 1266 | case OPT_RELATIVE: |
| 1267 | timermode = TIMER_RELTIME; break; |
| 1268 | case 'R': |
| 1269 | case OPT_RESOLUTION: |
| 1270 | check_clock_resolution = 1; break; |
| 1271 | case OPT_SECALIGNED: |
| 1272 | secaligned = 1; |
| 1273 | if (optarg != NULL) |
| 1274 | offset = atoi(optarg) * 1000; |
| 1275 | else if (optind < argc && atoi(argv[optind])) |
| 1276 | offset = atoi(argv[optind]) * 1000; |
| 1277 | else |
| 1278 | offset = 0; |
| 1279 | break; |
| 1280 | case 's': |
| 1281 | case OPT_SYSTEM: |
| 1282 | use_system = MODE_SYS_OFFSET; break; |
| 1283 | case 'S': |
| 1284 | case OPT_SMP: /* SMP testing */ |
| 1285 | if (numa) |
| 1286 | fatal("numa and smp options are mutually exclusive\n"); |
| 1287 | smp = 1; |
| 1288 | num_threads = -1; /* update after parsing */ |
| 1289 | setaffinity = AFFINITY_USEALL; |
| 1290 | break; |
| 1291 | case 't': |
| 1292 | case OPT_THREADS: |
| 1293 | if (smp) { |
| 1294 | warn("-t ignored due to smp mode\n"); |
| 1295 | break; |
| 1296 | } |
| 1297 | if (optarg != NULL) |
| 1298 | num_threads = atoi(optarg); |
| 1299 | else if (optind < argc && atoi(argv[optind])) |
| 1300 | num_threads = atoi(argv[optind]); |
| 1301 | else |
| 1302 | num_threads = -1; /* update after parsing */ |
| 1303 | break; |
| 1304 | case OPT_TRIGGER: |
| 1305 | trigger = atoi(optarg); |
| 1306 | break; |
| 1307 | case OPT_TRIGGER_NODES: |
| 1308 | if (trigger) |
| 1309 | trigger_list_size = atoi(optarg); |
| 1310 | break; |
| 1311 | case 'u': |
| 1312 | case OPT_UNBUFFERED: |
| 1313 | setvbuf(stdout, NULL, _IONBF, 0); break; |
| 1314 | case 'v': |
| 1315 | case OPT_VERBOSE: verbose = 1; break; |
| 1316 | case 'x': |
| 1317 | case OPT_POSIX_TIMERS: |
| 1318 | use_nanosleep = MODE_CYCLIC; break; |
| 1319 | case '?': |
| 1320 | case OPT_HELP: |
| 1321 | display_help(0); break; |
| 1322 | |
| 1323 | /* long only options */ |
| 1324 | case OPT_PRIOSPREAD: |
| 1325 | priospread = 1; break; |
| 1326 | case OPT_LATENCY: |
| 1327 | /* power management latency target value */ |
| 1328 | /* note: default is 0 (zero) */ |
| 1329 | latency_target_value = atoi(optarg); |
| 1330 | if (latency_target_value < 0) |
| 1331 | latency_target_value = 0; |
| 1332 | break; |
| 1333 | case OPT_POLICY: |
| 1334 | handlepolicy(optarg); break; |
| 1335 | case OPT_DBGCYCLIC: |
| 1336 | ct_debug = 1; break; |
| 1337 | case OPT_LAPTOP: |
| 1338 | laptop = 1; break; |
| 1339 | case OPT_SMI: |
| 1340 | #ifdef ARCH_HAS_SMI_COUNTER |
| 1341 | smi = 1; |
| 1342 | #else |
| 1343 | fatal("--smi is not available on your arch\n"); |
| 1344 | #endif |
| 1345 | break; |
| 1346 | case OPT_TRACEMARK: |
| 1347 | trace_marker = 1; break; |
| 1348 | case OPT_DEEPEST_IDLE_STATE: |
| 1349 | deepest_idle_state = atoi(optarg); |
| 1350 | break; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | if ((use_system == MODE_SYS_OFFSET) && (use_nanosleep == MODE_CYCLIC)) { |
| 1355 | warn("The system option requires clock_nanosleep\n"); |
| 1356 | warn("and is not compatible with posix_timers\n"); |
| 1357 | warn("Using clock_nanosleep\n"); |
| 1358 | use_nanosleep = MODE_CLOCK_NANOSLEEP; |
| 1359 | } |
| 1360 | |
| 1361 | /* if smp wasn't requested, test for numa automatically */ |
| 1362 | if (!smp) { |
| 1363 | numa = numa_initialize(); |
| 1364 | } |
| 1365 | |
| 1366 | if (option_affinity) { |
| 1367 | if (smp) |
| 1368 | warn("-a ignored due to smp mode\n"); |
| 1369 | } |
| 1370 | |
| 1371 | if (smi) { |
| 1372 | if (setaffinity == AFFINITY_UNSPECIFIED) |
| 1373 | fatal("SMI counter relies on thread affinity\n"); |
| 1374 | |
| 1375 | if (!has_smi_counter()) |
| 1376 | fatal("SMI counter is not supported " |
| 1377 | "on this processor\n"); |
| 1378 | } |
| 1379 | |
| 1380 | if (clocksel < 0 || clocksel > ARRAY_SIZE(clocksources)) |
| 1381 | error = 1; |
| 1382 | |
| 1383 | if (oscope_reduction < 1) |
| 1384 | error = 1; |
| 1385 | |
| 1386 | if (oscope_reduction > 1 && !verbose) { |
| 1387 | warn("-o option only meaningful, if verbose\n"); |
| 1388 | error = 1; |
| 1389 | } |
| 1390 | |
| 1391 | if (histogram < 0) |
| 1392 | error = 1; |
| 1393 | |
| 1394 | if (histogram > HIST_MAX) |
| 1395 | histogram = HIST_MAX; |
| 1396 | |
| 1397 | if (histogram && distance != -1) |
| 1398 | warn("distance is ignored and set to 0, if histogram enabled\n"); |
| 1399 | if (distance == -1) |
| 1400 | distance = DEFAULT_DISTANCE; |
| 1401 | |
| 1402 | if (priority < 0 || priority > 99) |
| 1403 | error = 1; |
| 1404 | |
| 1405 | if (num_threads == -1) |
| 1406 | num_threads = get_available_cpus(affinity_mask); |
| 1407 | |
| 1408 | if (priospread && priority == 0) { |
| 1409 | fprintf(stderr, "defaulting realtime priority to %d\n", |
| 1410 | num_threads+1); |
| 1411 | priority = num_threads+1; |
| 1412 | } |
| 1413 | |
| 1414 | if (priority && (policy != SCHED_FIFO && policy != SCHED_RR)) { |
| 1415 | fprintf(stderr, "policy and priority don't match: setting policy to SCHED_FIFO\n"); |
| 1416 | policy = SCHED_FIFO; |
| 1417 | } |
| 1418 | |
| 1419 | if ((policy == SCHED_FIFO || policy == SCHED_RR) && priority == 0) { |
| 1420 | fprintf(stderr, "defaulting realtime priority to %d\n", |
| 1421 | num_threads+1); |
| 1422 | priority = num_threads+1; |
| 1423 | } |
| 1424 | |
| 1425 | if (num_threads < 1) |
| 1426 | error = 1; |
| 1427 | |
| 1428 | if (aligned && secaligned) |
| 1429 | error = 1; |
| 1430 | |
| 1431 | if (aligned || secaligned) { |
| 1432 | pthread_barrier_init(&globalt_barr, NULL, num_threads); |
| 1433 | pthread_barrier_init(&align_barr, NULL, num_threads); |
| 1434 | } |
| 1435 | if (error) { |
| 1436 | if (affinity_mask) |
| 1437 | rt_bitmask_free(affinity_mask); |
| 1438 | display_help(1); |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | static int check_timer(void) |
| 1443 | { |
| 1444 | struct timespec ts; |
| 1445 | |
| 1446 | if (clock_getres(CLOCK_MONOTONIC, &ts)) |
| 1447 | return 1; |
| 1448 | |
| 1449 | return (ts.tv_sec != 0 || ts.tv_nsec != 1); |
| 1450 | } |
| 1451 | |
| 1452 | static void sighand(int sig) |
| 1453 | { |
| 1454 | if (sig == SIGUSR1) { |
| 1455 | int i; |
| 1456 | int oldquiet = quiet; |
| 1457 | |
| 1458 | quiet = 0; |
| 1459 | fprintf(stderr, "#---------------------------\n"); |
| 1460 | fprintf(stderr, "# cyclictest current status:\n"); |
| 1461 | for (i = 0; i < num_threads; i++) |
| 1462 | print_stat(stderr, parameters[i], i, 0, 0); |
| 1463 | fprintf(stderr, "#---------------------------\n"); |
| 1464 | quiet = oldquiet; |
| 1465 | return; |
| 1466 | } else if (sig == SIGUSR2) { |
| 1467 | int i; |
| 1468 | int oldquiet = quiet; |
| 1469 | |
| 1470 | if (rstat_fd == -1) { |
| 1471 | fprintf(stderr, "ERROR: rstat_fd not valid\n"); |
| 1472 | return; |
| 1473 | } |
| 1474 | rstat_ftruncate(rstat_fd, 0); |
| 1475 | quiet = 0; |
| 1476 | dprintf(rstat_fd, "#---------------------------\n"); |
| 1477 | dprintf(rstat_fd, "# cyclictest current status:\n"); |
| 1478 | for (i = 0; i < num_threads; i++) |
| 1479 | rstat_print_stat(parameters[i], i, 0, 0); |
| 1480 | dprintf(rstat_fd, "#---------------------------\n"); |
| 1481 | quiet = oldquiet; |
| 1482 | return; |
| 1483 | } |
| 1484 | shutdown = 1; |
| 1485 | if (refresh_on_max) |
| 1486 | pthread_cond_signal(&refresh_on_max_cond); |
| 1487 | } |
| 1488 | |
| 1489 | static void print_tids(struct thread_param *par[], int nthreads) |
| 1490 | { |
| 1491 | int i; |
| 1492 | |
| 1493 | printf("# Thread Ids:"); |
| 1494 | for (i = 0; i < nthreads; i++) |
| 1495 | printf(" %05d", par[i]->stats->tid); |
| 1496 | printf("\n"); |
| 1497 | } |
| 1498 | |
| 1499 | static void print_hist(struct thread_param *par[], int nthreads) |
| 1500 | { |
| 1501 | int i, j; |
| 1502 | unsigned long maxmax, alloverflows; |
| 1503 | FILE *fd; |
| 1504 | |
| 1505 | if (use_histfile) { |
| 1506 | fd = fopen(histfile, "w"); |
| 1507 | if (!fd) { |
| 1508 | perror("opening histogram file:"); |
| 1509 | return; |
| 1510 | } |
| 1511 | } else { |
| 1512 | fd = stdout; |
| 1513 | } |
| 1514 | |
| 1515 | fprintf(fd, "# Histogram\n"); |
| 1516 | for (i = 0; i < histogram; i++) { |
| 1517 | unsigned long flags = 0; |
| 1518 | char buf[64]; |
| 1519 | |
| 1520 | snprintf(buf, sizeof(buf), "%06d ", i); |
| 1521 | |
| 1522 | if (histofall) |
| 1523 | flags |= HSET_PRINT_SUM; |
| 1524 | hset_print_bucket(&hset, fd, buf, i, flags); |
| 1525 | } |
| 1526 | fprintf(fd, "# Min Latencies:"); |
| 1527 | for (j = 0; j < nthreads; j++) |
| 1528 | fprintf(fd, " %05lu", par[j]->stats->min); |
| 1529 | fprintf(fd, "\n"); |
| 1530 | fprintf(fd, "# Avg Latencies:"); |
| 1531 | for (j = 0; j < nthreads; j++) |
| 1532 | fprintf(fd, " %05lu", par[j]->stats->cycles ? |
| 1533 | (long)(par[j]->stats->avg/par[j]->stats->cycles) : 0); |
| 1534 | fprintf(fd, "\n"); |
| 1535 | fprintf(fd, "# Max Latencies:"); |
| 1536 | maxmax = 0; |
| 1537 | for (j = 0; j < nthreads; j++) { |
| 1538 | fprintf(fd, " %05lu", par[j]->stats->max); |
| 1539 | if (par[j]->stats->max > maxmax) |
| 1540 | maxmax = par[j]->stats->max; |
| 1541 | } |
| 1542 | if (histofall && nthreads > 1) |
| 1543 | fprintf(fd, " %05lu", maxmax); |
| 1544 | fprintf(fd, "\n"); |
| 1545 | fprintf(fd, "# Histogram Overflows:"); |
| 1546 | alloverflows = 0; |
| 1547 | for (j = 0; j < nthreads; j++) { |
| 1548 | fprintf(fd, " %05lu", par[j]->stats->hist->oflow_count); |
| 1549 | alloverflows += par[j]->stats->hist->oflow_count; |
| 1550 | } |
| 1551 | if (histofall && nthreads > 1) |
| 1552 | fprintf(fd, " %05lu", alloverflows); |
| 1553 | fprintf(fd, "\n"); |
| 1554 | |
| 1555 | fprintf(fd, "# Histogram Overflow at cycle number:\n"); |
| 1556 | for (i = 0; i < nthreads; i++) { |
| 1557 | fprintf(fd, "# Thread %d: ", i); |
| 1558 | hist_print_oflows(par[i]->stats->hist, fd); |
| 1559 | fprintf(fd, "\n"); |
| 1560 | } |
| 1561 | if (smi) { |
| 1562 | fprintf(fd, "# SMIs:"); |
| 1563 | for (i = 0; i < nthreads; i++) |
| 1564 | fprintf(fd, " %05lu", par[i]->stats->smi_count); |
| 1565 | fprintf(fd, "\n"); |
| 1566 | } |
| 1567 | |
| 1568 | fprintf(fd, "\n"); |
| 1569 | |
| 1570 | if (use_histfile) |
| 1571 | fclose(fd); |
| 1572 | } |
| 1573 | |
| 1574 | static void print_stat(FILE *fp, struct thread_param *par, int index, int verbose, int quiet) |
| 1575 | { |
| 1576 | struct thread_stat *stat = par->stats; |
| 1577 | |
| 1578 | if (!verbose) { |
| 1579 | if (quiet != 1) { |
| 1580 | char *fmt; |
| 1581 | if (use_nsecs) |
| 1582 | fmt = "T:%2d (%5d) P:%2d I:%ld C:%7lu " |
| 1583 | "Min:%7ld Act:%8ld Avg:%8ld Max:%8ld"; |
| 1584 | else |
| 1585 | fmt = "T:%2d (%5d) P:%2d I:%ld C:%7lu " |
| 1586 | "Min:%7ld Act:%5ld Avg:%5ld Max:%8ld"; |
| 1587 | |
| 1588 | fprintf(fp, fmt, index, stat->tid, par->prio, |
| 1589 | par->interval, stat->cycles, stat->min, |
| 1590 | stat->act, stat->cycles ? |
| 1591 | (long)(stat->avg/stat->cycles) : 0, stat->max); |
| 1592 | |
| 1593 | if (smi) |
| 1594 | fprintf(fp, " SMI:%8ld", stat->smi_count); |
| 1595 | |
| 1596 | fprintf(fp, "\n"); |
| 1597 | } |
| 1598 | } else { |
| 1599 | while (stat->cycles != stat->cyclesread) { |
| 1600 | unsigned long diff_smi; |
| 1601 | long diff = stat->values |
| 1602 | [stat->cyclesread & par->bufmsk]; |
| 1603 | |
| 1604 | if (smi) |
| 1605 | diff_smi = stat->smis |
| 1606 | [stat->cyclesread & par->bufmsk]; |
| 1607 | |
| 1608 | if (diff > stat->redmax) { |
| 1609 | stat->redmax = diff; |
| 1610 | stat->cycleofmax = stat->cyclesread; |
| 1611 | } |
| 1612 | if (++stat->reduce == oscope_reduction) { |
| 1613 | if (!smi) |
| 1614 | fprintf(fp, "%8d:%8lu:%8ld\n", index, |
| 1615 | stat->cycleofmax, stat->redmax); |
| 1616 | else |
| 1617 | fprintf(fp, "%8d:%8lu:%8ld%8ld\n", |
| 1618 | index, stat->cycleofmax, |
| 1619 | stat->redmax, diff_smi); |
| 1620 | |
| 1621 | stat->reduce = 0; |
| 1622 | stat->redmax = 0; |
| 1623 | } |
| 1624 | stat->cyclesread++; |
| 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | static void rstat_print_stat(struct thread_param *par, int index, int verbose, int quiet) |
| 1630 | { |
| 1631 | struct thread_stat *stat = par->stats; |
| 1632 | int fd = rstat_fd; |
| 1633 | |
| 1634 | if (!verbose) { |
| 1635 | if (quiet != 1) { |
| 1636 | char *fmt; |
| 1637 | if (use_nsecs) |
| 1638 | fmt = "T:%2d (%5d) P:%2d I:%ld C:%7lu " |
| 1639 | "Min:%7ld Act:%8ld Avg:%8ld Max:%8ld"; |
| 1640 | else |
| 1641 | fmt = "T:%2d (%5d) P:%2d I:%ld C:%7lu " |
| 1642 | "Min:%7ld Act:%5ld Avg:%5ld Max:%8ld"; |
| 1643 | |
| 1644 | dprintf(fd, fmt, index, stat->tid, par->prio, |
| 1645 | par->interval, stat->cycles, stat->min, |
| 1646 | stat->act, stat->cycles ? |
| 1647 | (long)(stat->avg/stat->cycles) : 0, stat->max); |
| 1648 | |
| 1649 | if (smi) |
| 1650 | dprintf(fd, " SMI:%8ld", stat->smi_count); |
| 1651 | |
| 1652 | dprintf(fd, "\n"); |
| 1653 | } |
| 1654 | } else { |
| 1655 | while (stat->cycles != stat->cyclesread) { |
| 1656 | unsigned long diff_smi; |
| 1657 | long diff = stat->values |
| 1658 | [stat->cyclesread & par->bufmsk]; |
| 1659 | |
| 1660 | if (smi) |
| 1661 | diff_smi = stat->smis |
| 1662 | [stat->cyclesread & par->bufmsk]; |
| 1663 | |
| 1664 | if (diff > stat->redmax) { |
| 1665 | stat->redmax = diff; |
| 1666 | stat->cycleofmax = stat->cyclesread; |
| 1667 | } |
| 1668 | if (++stat->reduce == oscope_reduction) { |
| 1669 | if (!smi) |
| 1670 | dprintf(fd, "%8d:%8lu:%8ld\n", index, |
| 1671 | stat->cycleofmax, stat->redmax); |
| 1672 | else |
| 1673 | dprintf(fd, "%8d:%8lu:%8ld%8ld\n", |
| 1674 | index, stat->cycleofmax, |
| 1675 | stat->redmax, diff_smi); |
| 1676 | |
| 1677 | stat->reduce = 0; |
| 1678 | stat->redmax = 0; |
| 1679 | } |
| 1680 | stat->cyclesread++; |
| 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | |
| 1686 | /* |
| 1687 | * thread that creates a named fifo and hands out run stats when someone |
| 1688 | * reads from the fifo. |
| 1689 | */ |
| 1690 | static void *fifothread(void *param) |
| 1691 | { |
| 1692 | int ret; |
| 1693 | int fd; |
| 1694 | FILE *fp; |
| 1695 | int i; |
| 1696 | |
| 1697 | unlink(fifopath); |
| 1698 | ret = mkfifo(fifopath, 0666); |
| 1699 | if (ret) { |
| 1700 | fprintf(stderr, "Error creating fifo %s: %s\n", fifopath, strerror(errno)); |
| 1701 | return NULL; |
| 1702 | } |
| 1703 | while (!shutdown) { |
| 1704 | fd = open(fifopath, O_WRONLY|O_NONBLOCK); |
| 1705 | if (fd < 0) { |
| 1706 | usleep(500000); |
| 1707 | continue; |
| 1708 | } |
| 1709 | fp = fdopen(fd, "w"); |
| 1710 | for (i=0; i < num_threads; i++) |
| 1711 | print_stat(fp, parameters[i], i, 0, 0); |
| 1712 | fclose(fp); |
| 1713 | usleep(250); |
| 1714 | } |
| 1715 | unlink(fifopath); |
| 1716 | return NULL; |
| 1717 | } |
| 1718 | |
| 1719 | static int trigger_init() |
| 1720 | { |
| 1721 | int i; |
| 1722 | int size = trigger_list_size; |
| 1723 | struct thread_trigger *trig = NULL; |
| 1724 | for(i=0; i<size; i++) { |
| 1725 | trig = malloc(sizeof(struct thread_trigger)); |
| 1726 | if (trig != NULL) { |
| 1727 | if (head == NULL) { |
| 1728 | head = trig; |
| 1729 | tail = trig; |
| 1730 | } else { |
| 1731 | tail->next = trig; |
| 1732 | tail = trig; |
| 1733 | } |
| 1734 | trig->tnum = i; |
| 1735 | trig->next = NULL; |
| 1736 | } else { |
| 1737 | return -1; |
| 1738 | } |
| 1739 | } |
| 1740 | current = head; |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | static void trigger_print() |
| 1745 | { |
| 1746 | struct thread_trigger *trig = head; |
| 1747 | char *fmt = "T:%2d Spike:%8ld: TS: %12ld\n"; |
| 1748 | |
| 1749 | if (current == head) |
| 1750 | return; |
| 1751 | printf("\n"); |
| 1752 | while (trig->next != current) { |
| 1753 | fprintf(stdout, fmt, trig->tnum, trig->diff, trig->ts); |
| 1754 | trig = trig->next; |
| 1755 | } |
| 1756 | fprintf(stdout, fmt, trig->tnum, trig->diff, trig->ts); |
| 1757 | printf("spikes = %d\n\n", spikes); |
| 1758 | } |
| 1759 | |
| 1760 | static void trigger_update(struct thread_param *par, int diff, int64_t ts) |
| 1761 | { |
| 1762 | pthread_mutex_lock(&trigger_lock); |
| 1763 | if (current != NULL) { |
| 1764 | current->tnum = par->tnum; |
| 1765 | current->ts = ts; |
| 1766 | current->diff = diff; |
| 1767 | current = current->next; |
| 1768 | } |
| 1769 | spikes++; |
| 1770 | pthread_mutex_unlock(&trigger_lock); |
| 1771 | } |
| 1772 | |
| 1773 | /* Running status shared memory open */ |
| 1774 | static int rstat_shm_open(void) |
| 1775 | { |
| 1776 | int fd; |
| 1777 | pid_t pid; |
| 1778 | |
| 1779 | pid = getpid(); |
| 1780 | |
| 1781 | snprintf(shm_name, SHM_BUF_SIZE, "%s%d", "/cyclictest", pid); |
| 1782 | |
| 1783 | errno = 0; |
| 1784 | fd = shm_unlink(shm_name); |
| 1785 | |
| 1786 | if ((fd == -1) && (errno != ENOENT)) { |
| 1787 | fprintf(stderr, "ERROR: shm_unlink %s\n", strerror(errno)); |
| 1788 | return fd; |
| 1789 | } |
| 1790 | |
| 1791 | errno = 9; |
| 1792 | fd = shm_open(shm_name, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); |
| 1793 | if (fd == -1) |
| 1794 | fprintf(stderr, "ERROR: shm_open %s\n", strerror(errno)); |
| 1795 | |
| 1796 | rstat_fd = fd; |
| 1797 | |
| 1798 | return fd; |
| 1799 | } |
| 1800 | |
| 1801 | static int rstat_ftruncate(int fd, off_t len) |
| 1802 | { |
| 1803 | int err; |
| 1804 | |
| 1805 | errno = 0; |
| 1806 | err = ftruncate(fd, len); |
| 1807 | if (err) |
| 1808 | fprintf(stderr, "ftruncate error %s\n", strerror(errno)); |
| 1809 | |
| 1810 | return err; |
| 1811 | } |
| 1812 | |
| 1813 | static void *rstat_mmap(int fd) |
| 1814 | { |
| 1815 | void *mptr; |
| 1816 | |
| 1817 | errno = 0; |
| 1818 | mptr = mmap(0, _SC_PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 1819 | |
| 1820 | if (mptr == (void*)-1) |
| 1821 | fprintf(stderr, "ERROR: mmap, %s\n", strerror(errno)); |
| 1822 | |
| 1823 | return mptr; |
| 1824 | } |
| 1825 | |
| 1826 | static int rstat_mlock(void *mptr) |
| 1827 | { |
| 1828 | int err; |
| 1829 | |
| 1830 | errno = 0; |
| 1831 | err = mlock(mptr, _SC_PAGE_SIZE); |
| 1832 | if (err == -1) |
| 1833 | fprintf(stderr, "ERROR, mlock %s\n", strerror(errno)); |
| 1834 | |
| 1835 | return err; |
| 1836 | } |
| 1837 | |
| 1838 | static void rstat_setup(void) |
| 1839 | { |
| 1840 | int res; |
| 1841 | void *mptr = NULL; |
| 1842 | |
| 1843 | int sfd = rstat_shm_open(); |
| 1844 | if (sfd < 0) |
| 1845 | goto rstat_err; |
| 1846 | |
| 1847 | res = rstat_ftruncate(sfd, _SC_PAGE_SIZE); |
| 1848 | if (res) |
| 1849 | goto rstat_err1; |
| 1850 | |
| 1851 | mptr = rstat_mmap(sfd); |
| 1852 | if (mptr == MAP_FAILED) |
| 1853 | goto rstat_err1; |
| 1854 | |
| 1855 | res = rstat_mlock(mptr); |
| 1856 | if (res) |
| 1857 | goto rstat_err2; |
| 1858 | |
| 1859 | return; |
| 1860 | |
| 1861 | rstat_err2: |
| 1862 | munmap(mptr, _SC_PAGE_SIZE); |
| 1863 | rstat_err1: |
| 1864 | close(sfd); |
| 1865 | shm_unlink(shm_name); |
| 1866 | rstat_err: |
| 1867 | rstat_fd = -1; |
| 1868 | return; |
| 1869 | } |
| 1870 | |
| 1871 | static void write_stats(FILE *f, void *data) |
| 1872 | { |
| 1873 | struct thread_param **par = parameters; |
| 1874 | int i; |
| 1875 | struct thread_stat *s; |
| 1876 | |
| 1877 | fprintf(f, " \"num_threads\": %d,\n", num_threads); |
| 1878 | fprintf(f, " \"resolution_in_ns\": %u,\n", use_nsecs); |
| 1879 | fprintf(f, " \"thread\": {\n"); |
| 1880 | for (i = 0; i < num_threads; i++) { |
| 1881 | s = par[i]->stats; |
| 1882 | fprintf(f, " \"%u\": {\n", i); |
| 1883 | if (s->hist) { |
| 1884 | fprintf(f, " \"histogram\": {"); |
| 1885 | hist_print_json(s->hist, f); |
| 1886 | fprintf(f, " },\n"); |
| 1887 | } |
| 1888 | fprintf(f, " \"cycles\": %ld,\n", s->cycles); |
| 1889 | fprintf(f, " \"min\": %ld,\n", s->min); |
| 1890 | fprintf(f, " \"max\": %ld,\n", s->max); |
| 1891 | fprintf(f, " \"avg\": %.2f,\n", s->avg/s->cycles); |
| 1892 | fprintf(f, " \"cpu\": %d,\n", par[i]->cpu); |
| 1893 | fprintf(f, " \"node\": %d\n", par[i]->node); |
| 1894 | fprintf(f, " }%s\n", i == num_threads - 1 ? "" : ","); |
| 1895 | } |
| 1896 | fprintf(f, " }\n"); |
| 1897 | } |
| 1898 | |
| 1899 | static void set_main_thread_affinity(struct bitmask *cpumask) |
| 1900 | { |
| 1901 | int res; |
| 1902 | |
| 1903 | errno = 0; |
| 1904 | res = numa_sched_setaffinity(getpid(), cpumask); |
| 1905 | if (res != 0) |
| 1906 | warn("Couldn't setaffinity in main thread: %s\n", |
| 1907 | strerror(errno)); |
| 1908 | } |
| 1909 | |
| 1910 | int main(int argc, char **argv) |
| 1911 | { |
| 1912 | sigset_t sigset; |
| 1913 | int signum = SIGALRM; |
| 1914 | int mode; |
| 1915 | int cpu; |
| 1916 | int max_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 1917 | int online_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 1918 | int i, ret = -1; |
| 1919 | int status; |
| 1920 | |
| 1921 | rt_init(argc, argv); |
| 1922 | process_options(argc, argv, max_cpus); |
| 1923 | |
| 1924 | if (check_privs()) |
| 1925 | exit(EXIT_FAILURE); |
| 1926 | |
| 1927 | if (verbose) { |
| 1928 | printf("Max CPUs = %d\n", max_cpus); |
| 1929 | printf("Online CPUs = %d\n", online_cpus); |
| 1930 | } |
| 1931 | |
| 1932 | if (affinity_mask != NULL) { |
| 1933 | set_main_thread_affinity(affinity_mask); |
| 1934 | if (verbose) |
| 1935 | printf("Using %u cpus.\n", |
| 1936 | numa_bitmask_weight(affinity_mask)); |
| 1937 | } |
| 1938 | |
| 1939 | if (trigger) { |
| 1940 | int retval; |
| 1941 | retval = trigger_init(); |
| 1942 | if (retval != 0) { |
| 1943 | fprintf(stderr, "trigger_init() failed\n"); |
| 1944 | exit(EXIT_FAILURE); |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | /* lock all memory (prevent swapping) */ |
| 1949 | if (lockall) |
| 1950 | if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1) { |
| 1951 | perror("mlockall"); |
| 1952 | goto out; |
| 1953 | } |
| 1954 | |
| 1955 | /* use the /dev/cpu_dma_latency trick if it's there */ |
| 1956 | set_latency_target(); |
| 1957 | |
| 1958 | if (deepest_idle_state >= -1) { |
| 1959 | if (!have_libcpupower_support()) { |
| 1960 | fprintf(stderr, "cyclictest built without libcpupower, --deepest-idle-state is not supported\n"); |
| 1961 | goto out; |
| 1962 | } |
| 1963 | |
| 1964 | for (i = 0; i < max_cpus; i++) { |
| 1965 | if (affinity_mask && !numa_bitmask_isbitset(affinity_mask, i)) |
| 1966 | continue; |
| 1967 | if (save_cpu_idle_disable_state(i) < 0) { |
| 1968 | fprintf(stderr, "Could not save cpu idle state.\n"); |
| 1969 | goto out; |
| 1970 | } |
| 1971 | if (set_deepest_cpu_idle_state(i, deepest_idle_state) < 0) { |
| 1972 | fprintf(stderr, "Could not set deepest cpu idle state.\n"); |
| 1973 | goto out; |
| 1974 | } |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | if (tracelimit && trace_marker) |
| 1979 | enable_trace_mark(); |
| 1980 | |
| 1981 | if (check_timer()) |
| 1982 | warn("High resolution timers not available\n"); |
| 1983 | |
| 1984 | if (check_clock_resolution) { |
| 1985 | int clock; |
| 1986 | uint64_t diff; |
| 1987 | int k; |
| 1988 | uint64_t min_non_zero_diff = UINT64_MAX; |
| 1989 | struct timespec now; |
| 1990 | struct timespec prev; |
| 1991 | uint64_t reported_resolution = UINT64_MAX; |
| 1992 | struct timespec res; |
| 1993 | struct timespec *time; |
| 1994 | int times; |
| 1995 | |
| 1996 | clock = clocksources[clocksel]; |
| 1997 | |
| 1998 | if (clock_getres(clock, &res)) |
| 1999 | warn("clock_getres failed"); |
| 2000 | else |
| 2001 | reported_resolution = (NSEC_PER_SEC * res.tv_sec) + res.tv_nsec; |
| 2002 | |
| 2003 | |
| 2004 | /* |
| 2005 | * Calculate how many calls to clock_gettime are needed. |
| 2006 | * Then call it that many times. |
| 2007 | * Goal is to collect timestamps for ~ 0.001 sec. |
| 2008 | * This will reliably capture resolution <= 500 usec. |
| 2009 | */ |
| 2010 | times = 1000; |
| 2011 | clock_gettime(clock, &prev); |
| 2012 | for (k=0; k < times; k++) |
| 2013 | clock_gettime(clock, &now); |
| 2014 | |
| 2015 | diff = calcdiff_ns(now, prev); |
| 2016 | if (diff == 0) { |
| 2017 | /* |
| 2018 | * No clock rollover occurred. |
| 2019 | * Use the default value for times. |
| 2020 | */ |
| 2021 | times = -1; |
| 2022 | } else { |
| 2023 | int call_time; |
| 2024 | call_time = diff / times; /* duration 1 call */ |
| 2025 | times = NSEC_PER_SEC / call_time; /* calls per second */ |
| 2026 | times /= 1000; /* calls per msec */ |
| 2027 | if (times < 1000) |
| 2028 | times = 1000; |
| 2029 | } |
| 2030 | /* sanity check */ |
| 2031 | if ((times <= 0) || (times > 100000)) |
| 2032 | times = 100000; |
| 2033 | |
| 2034 | time = calloc(times, sizeof(*time)); |
| 2035 | |
| 2036 | for (k=0; k < times; k++) |
| 2037 | clock_gettime(clock, &time[k]); |
| 2038 | |
| 2039 | info(ct_debug, "For %d consecutive calls to clock_gettime():\n", times); |
| 2040 | info(ct_debug, "time, delta time (nsec)\n"); |
| 2041 | |
| 2042 | prev = time[0]; |
| 2043 | for (k=1; k < times; k++) { |
| 2044 | |
| 2045 | diff = calcdiff_ns(time[k], prev); |
| 2046 | prev = time[k]; |
| 2047 | |
| 2048 | if (diff && (diff < min_non_zero_diff)) |
| 2049 | min_non_zero_diff = diff; |
| 2050 | |
| 2051 | info(ct_debug, "%ld.%06ld %5llu\n", |
| 2052 | time[k].tv_sec, time[k].tv_nsec, |
| 2053 | (unsigned long long)diff); |
| 2054 | } |
| 2055 | |
| 2056 | free(time); |
| 2057 | |
| 2058 | |
| 2059 | if (verbose || |
| 2060 | (min_non_zero_diff && (min_non_zero_diff > reported_resolution))) { |
| 2061 | /* |
| 2062 | * Measured clock resolution includes the time to call |
| 2063 | * clock_gettime(), so it will be slightly larger than |
| 2064 | * actual resolution. |
| 2065 | */ |
| 2066 | warn("reported clock resolution: %llu nsec\n", |
| 2067 | (unsigned long long)reported_resolution); |
| 2068 | warn("measured clock resolution approximately: %llu nsec\n", |
| 2069 | (unsigned long long)min_non_zero_diff); |
| 2070 | } |
| 2071 | |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | mode = use_nanosleep + use_system; |
| 2076 | |
| 2077 | sigemptyset(&sigset); |
| 2078 | sigaddset(&sigset, signum); |
| 2079 | sigprocmask(SIG_BLOCK, &sigset, NULL); |
| 2080 | |
| 2081 | signal(SIGINT, sighand); |
| 2082 | signal(SIGTERM, sighand); |
| 2083 | signal(SIGUSR1, sighand); |
| 2084 | signal(SIGUSR2, sighand); |
| 2085 | |
| 2086 | /* Set-up shm */ |
| 2087 | rstat_setup(); |
| 2088 | |
| 2089 | if (histogram && hset_init(&hset, num_threads, 1, histogram, histogram)) |
| 2090 | fatal("failed to allocate histogram of size %d for %d threads\n", |
| 2091 | histogram, num_threads); |
| 2092 | |
| 2093 | parameters = calloc(num_threads, sizeof(struct thread_param *)); |
| 2094 | if (!parameters) |
| 2095 | goto out; |
| 2096 | statistics = calloc(num_threads, sizeof(struct thread_stat *)); |
| 2097 | if (!statistics) |
| 2098 | goto outpar; |
| 2099 | |
| 2100 | for (i = 0; i < num_threads; i++) { |
| 2101 | pthread_attr_t attr; |
| 2102 | int node; |
| 2103 | struct thread_param *par; |
| 2104 | struct thread_stat *stat; |
| 2105 | |
| 2106 | status = pthread_attr_init(&attr); |
| 2107 | if (status != 0) |
| 2108 | fatal("error from pthread_attr_init for thread %d: %s\n", i, strerror(status)); |
| 2109 | |
| 2110 | switch (setaffinity) { |
| 2111 | case AFFINITY_UNSPECIFIED: cpu = -1; break; |
| 2112 | case AFFINITY_SPECIFIED: |
| 2113 | cpu = cpu_for_thread_sp(i, max_cpus, affinity_mask); |
| 2114 | if (verbose) |
| 2115 | printf("Thread %d using cpu %d.\n", i, cpu); |
| 2116 | break; |
| 2117 | case AFFINITY_USEALL: |
| 2118 | cpu = cpu_for_thread_ua(i, max_cpus); |
| 2119 | break; |
| 2120 | default: cpu = -1; |
| 2121 | } |
| 2122 | |
| 2123 | node = -1; |
| 2124 | if (numa) { |
| 2125 | void *stack; |
| 2126 | void *currstk; |
| 2127 | size_t stksize; |
| 2128 | int node_cpu = cpu; |
| 2129 | |
| 2130 | if (node_cpu == -1) |
| 2131 | node_cpu = cpu_for_thread_ua(i, max_cpus); |
| 2132 | |
| 2133 | /* find the memory node associated with the cpu i */ |
| 2134 | node = rt_numa_numa_node_of_cpu(node_cpu); |
| 2135 | |
| 2136 | /* get the stack size set for this thread */ |
| 2137 | if (pthread_attr_getstack(&attr, &currstk, &stksize)) |
| 2138 | fatal("failed to get stack size for thread %d\n", i); |
| 2139 | |
| 2140 | /* if the stack size is zero, set a default */ |
| 2141 | if (stksize == 0) |
| 2142 | stksize = PTHREAD_STACK_MIN * 2; |
| 2143 | |
| 2144 | /* allocate memory for a stack on appropriate node */ |
| 2145 | stack = rt_numa_numa_alloc_onnode(stksize, node, node_cpu); |
| 2146 | |
| 2147 | /* touch the stack pages to pre-fault them in */ |
| 2148 | memset(stack, 0, stksize); |
| 2149 | |
| 2150 | /* set the thread's stack */ |
| 2151 | if (pthread_attr_setstack(&attr, stack, stksize)) |
| 2152 | fatal("failed to set stack addr for thread %d to 0x%x\n", |
| 2153 | i, stack+stksize); |
| 2154 | } |
| 2155 | |
| 2156 | /* allocate the thread's parameter block */ |
| 2157 | parameters[i] = par = threadalloc(sizeof(struct thread_param), node); |
| 2158 | if (par == NULL) |
| 2159 | fatal("error allocating thread_param struct for thread %d\n", i); |
| 2160 | memset(par, 0, sizeof(struct thread_param)); |
| 2161 | |
| 2162 | /* allocate the thread's statistics block */ |
| 2163 | statistics[i] = stat = threadalloc(sizeof(struct thread_stat), node); |
| 2164 | if (stat == NULL) |
| 2165 | fatal("error allocating thread status struct for thread %d\n", i); |
| 2166 | memset(stat, 0, sizeof(struct thread_stat)); |
| 2167 | |
| 2168 | if (histogram) |
| 2169 | stat->hist = &hset.histos[i]; |
| 2170 | |
| 2171 | if (verbose) { |
| 2172 | int bufsize = VALBUF_SIZE * sizeof(long); |
| 2173 | stat->values = threadalloc(bufsize, node); |
| 2174 | if (!stat->values) |
| 2175 | goto outall; |
| 2176 | memset(stat->values, 0, bufsize); |
| 2177 | par->bufmsk = VALBUF_SIZE - 1; |
| 2178 | if (smi) { |
| 2179 | stat->smis = threadalloc(bufsize, node); |
| 2180 | if (!stat->smis) |
| 2181 | goto outall; |
| 2182 | memset(stat->smis, 0, bufsize); |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | par->prio = priority; |
| 2187 | if (priority && (policy == SCHED_FIFO || policy == SCHED_RR)) |
| 2188 | par->policy = policy; |
| 2189 | else { |
| 2190 | par->policy = SCHED_OTHER; |
| 2191 | force_sched_other = 1; |
| 2192 | } |
| 2193 | if (priospread) |
| 2194 | priority--; |
| 2195 | par->clock = clocksources[clocksel]; |
| 2196 | par->mode = mode; |
| 2197 | par->timermode = timermode; |
| 2198 | par->signal = signum; |
| 2199 | par->interval = interval; |
| 2200 | if (!histogram) /* same interval on CPUs */ |
| 2201 | interval += distance; |
| 2202 | if (verbose) |
| 2203 | printf("Thread %d Interval: %d\n", i, interval); |
| 2204 | |
| 2205 | par->max_cycles = max_cycles; |
| 2206 | par->stats = stat; |
| 2207 | par->node = node; |
| 2208 | par->tnum = i; |
| 2209 | par->cpu = cpu; |
| 2210 | |
| 2211 | stat->min = 1000000; |
| 2212 | stat->max = 0; |
| 2213 | stat->avg = 0.0; |
| 2214 | stat->threadstarted = 1; |
| 2215 | stat->smi_count = 0; |
| 2216 | status = pthread_create(&stat->thread, &attr, timerthread, par); |
| 2217 | if (status) |
| 2218 | fatal("failed to create thread %d: %s\n", i, strerror(status)); |
| 2219 | |
| 2220 | } |
| 2221 | /* Restrict the main pid to the affinity specified by the user */ |
| 2222 | if (main_affinity_mask != NULL) |
| 2223 | set_main_thread_affinity(main_affinity_mask); |
| 2224 | |
| 2225 | if (use_fifo) { |
| 2226 | status = pthread_create(&fifo_threadid, NULL, fifothread, NULL); |
| 2227 | if (status) |
| 2228 | fatal("failed to create fifo thread: %s\n", strerror(status)); |
| 2229 | } |
| 2230 | |
| 2231 | while (!shutdown) { |
| 2232 | char lavg[256]; |
| 2233 | int fd, len, allstopped = 0; |
| 2234 | static char *policystr = NULL; |
| 2235 | static char *slash = NULL; |
| 2236 | static char *policystr2; |
| 2237 | |
| 2238 | if (!policystr) |
| 2239 | policystr = policyname(policy); |
| 2240 | |
| 2241 | if (!slash) { |
| 2242 | if (force_sched_other) { |
| 2243 | slash = "/"; |
| 2244 | policystr2 = policyname(SCHED_OTHER); |
| 2245 | } else |
| 2246 | slash = policystr2 = ""; |
| 2247 | } |
| 2248 | if (!verbose && !quiet) { |
| 2249 | fd = open("/proc/loadavg", O_RDONLY, 0666); |
| 2250 | len = read(fd, &lavg, 255); |
| 2251 | close(fd); |
| 2252 | lavg[len-1] = 0x0; |
| 2253 | printf("policy: %s%s%s: loadavg: %s \n\n", |
| 2254 | policystr, slash, policystr2, lavg); |
| 2255 | } |
| 2256 | |
| 2257 | for (i = 0; i < num_threads; i++) { |
| 2258 | |
| 2259 | print_stat(stdout, parameters[i], i, verbose, quiet); |
| 2260 | if (max_cycles && statistics[i]->cycles >= max_cycles) |
| 2261 | allstopped++; |
| 2262 | } |
| 2263 | |
| 2264 | usleep(10000); |
| 2265 | if (shutdown || allstopped) |
| 2266 | break; |
| 2267 | if (!verbose && !quiet) |
| 2268 | printf("\033[%dA", num_threads + 2); |
| 2269 | |
| 2270 | if (refresh_on_max) { |
| 2271 | pthread_mutex_lock(&refresh_on_max_lock); |
| 2272 | if (!shutdown) |
| 2273 | pthread_cond_wait(&refresh_on_max_cond, |
| 2274 | &refresh_on_max_lock); |
| 2275 | pthread_mutex_unlock(&refresh_on_max_lock); |
| 2276 | } |
| 2277 | } |
| 2278 | ret = EXIT_SUCCESS; |
| 2279 | |
| 2280 | outall: |
| 2281 | shutdown = 1; |
| 2282 | usleep(50000); |
| 2283 | |
| 2284 | if (!verbose && !quiet && refresh_on_max) |
| 2285 | printf("\033[%dB", num_threads + 2); |
| 2286 | |
| 2287 | if (strlen(jsonfile) != 0) |
| 2288 | rt_write_json(jsonfile, ret, write_stats, NULL); |
| 2289 | |
| 2290 | if (quiet) |
| 2291 | quiet = 2; |
| 2292 | for (i = 0; i < num_threads; i++) { |
| 2293 | if (statistics[i]->threadstarted > 0) |
| 2294 | pthread_kill(statistics[i]->thread, SIGTERM); |
| 2295 | if (statistics[i]->threadstarted) { |
| 2296 | pthread_join(statistics[i]->thread, NULL); |
| 2297 | if (quiet && !histogram) |
| 2298 | print_stat(stdout, parameters[i], i, 0, 0); |
| 2299 | } |
| 2300 | if (statistics[i]->values) |
| 2301 | threadfree(statistics[i]->values, VALBUF_SIZE*sizeof(long), parameters[i]->node); |
| 2302 | } |
| 2303 | |
| 2304 | if (trigger) |
| 2305 | trigger_print(); |
| 2306 | |
| 2307 | if (histogram) |
| 2308 | print_hist(parameters, num_threads); |
| 2309 | |
| 2310 | if (tracelimit) { |
| 2311 | print_tids(parameters, num_threads); |
| 2312 | if (break_thread_id) { |
| 2313 | printf("# Break thread: %d\n", break_thread_id); |
| 2314 | printf("# Break value: %llu\n", (unsigned long long)break_thread_value); |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | |
| 2319 | for (i=0; i < num_threads; i++) { |
| 2320 | if (!statistics[i]) |
| 2321 | continue; |
| 2322 | threadfree(statistics[i], sizeof(struct thread_stat), parameters[i]->node); |
| 2323 | } |
| 2324 | |
| 2325 | outpar: |
| 2326 | for (i = 0; i < num_threads; i++) { |
| 2327 | if (!parameters[i]) |
| 2328 | continue; |
| 2329 | threadfree(parameters[i], sizeof(struct thread_param), parameters[i]->node); |
| 2330 | } |
| 2331 | out: |
| 2332 | /* close any tracer file descriptors */ |
| 2333 | disable_trace_mark(); |
| 2334 | |
| 2335 | /* unlock everything */ |
| 2336 | if (lockall) |
| 2337 | munlockall(); |
| 2338 | |
| 2339 | /* close the latency_target_fd if it's open */ |
| 2340 | if (latency_target_fd >= 0) |
| 2341 | close(latency_target_fd); |
| 2342 | |
| 2343 | /* restore and free cpu idle disable states */ |
| 2344 | if (deepest_idle_state >= -1) { |
| 2345 | for (i = 0; i < max_cpus; i++) { |
| 2346 | if (affinity_mask && !numa_bitmask_isbitset(affinity_mask, i)) |
| 2347 | continue; |
| 2348 | restore_cpu_idle_disable_state(i); |
| 2349 | } |
| 2350 | } |
| 2351 | free_cpu_idle_disable_states(); |
| 2352 | |
| 2353 | if (affinity_mask) |
| 2354 | rt_bitmask_free(affinity_mask); |
| 2355 | |
| 2356 | /* Remove running status shared memory file if it exists */ |
| 2357 | if (rstat_fd >= 0) |
| 2358 | shm_unlink(shm_name); |
| 2359 | |
| 2360 | hset_destroy(&hset); |
| 2361 | exit(ret); |
| 2362 | } |