| 1 | // SPDX-License-Identifier: GPL-3.0-only |
| 2 | /* |
| 3 | * oslat - OS latency detector |
| 4 | * |
| 5 | * Copyright 2020 Red Hat Inc. |
| 6 | * |
| 7 | * Authors: Peter Xu <[email protected]> |
| 8 | * |
| 9 | * Some of the utility code based on sysjitter-1.3: |
| 10 | * Copyright 2010-2015 David Riddoch <[email protected]> |
| 11 | */ |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <inttypes.h> |
| 15 | #include <ctype.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <stdarg.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <getopt.h> |
| 23 | #include <pthread.h> |
| 24 | #include <signal.h> |
| 25 | #include <sched.h> |
| 26 | #include <string.h> |
| 27 | #include <time.h> |
| 28 | #include <errno.h> |
| 29 | #include <numa.h> |
| 30 | #include <math.h> |
| 31 | #include <limits.h> |
| 32 | #include <stdbool.h> |
| 33 | |
| 34 | #include <sys/prctl.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/sysinfo.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/time.h> |
| 39 | #include <sys/resource.h> |
| 40 | #include <sys/utsname.h> |
| 41 | #include <sys/mman.h> |
| 42 | #include <sys/syscall.h> |
| 43 | |
| 44 | #include <linux/unistd.h> |
| 45 | |
| 46 | #include "rt-utils.h" |
| 47 | #include "rt-numa.h" |
| 48 | #include "rt-error.h" |
| 49 | |
| 50 | #ifdef __GNUC__ |
| 51 | # define atomic_inc(ptr) __sync_add_and_fetch((ptr), 1) |
| 52 | # if defined(__x86_64__) |
| 53 | # define relax() __asm__ __volatile__("pause" ::: "memory") |
| 54 | static inline void frc(uint64_t *pval) |
| 55 | { |
| 56 | uint32_t low, high; |
| 57 | /* See rdtsc_ordered() of Linux */ |
| 58 | __asm__ __volatile__("lfence"); |
| 59 | __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high)); |
| 60 | *pval = ((uint64_t) high << 32) | low; |
| 61 | } |
| 62 | # elif defined(__i386__) |
| 63 | # define relax() __asm__ __volatile__("pause" ::: "memory") |
| 64 | static inline void frc(uint64_t *pval) |
| 65 | { |
| 66 | __asm__ __volatile__("rdtsc" : "=A" (*pval)); |
| 67 | } |
| 68 | # elif defined(__PPC64__) |
| 69 | # define relax() do { } while (0) |
| 70 | static inline void frc(uint64_t *pval) |
| 71 | { |
| 72 | __asm__ __volatile__("mfspr %0, 268\n" : "=r" (*pval)); |
| 73 | } |
| 74 | # elif defined(__aarch64__) |
| 75 | # define relax() __asm__ __volatile("yield" : : : "memory") |
| 76 | |
| 77 | #define arch_measure_counter_mhz |
| 78 | static unsigned int measure_counter_mhz(void) |
| 79 | { |
| 80 | unsigned int val; |
| 81 | |
| 82 | __asm__ __volatile__("mrs %0, cntfrq_el0" : "=r" (val)); |
| 83 | |
| 84 | return val / 1e6; |
| 85 | } |
| 86 | |
| 87 | static inline void frc(uint64_t *pval) |
| 88 | { |
| 89 | /* |
| 90 | * This isb() is required to prevent that the counter value |
| 91 | * is speculated. |
| 92 | */ |
| 93 | __asm__ __volatile__("isb" : : : "memory"); |
| 94 | __asm__ __volatile__("mrs %0, cntvct_el0" : "=r" (*pval) :: "memory"); |
| 95 | /* |
| 96 | * This isb() is required to prevent the processor from accessing |
| 97 | * memory appearing in program order after the read of the counter |
| 98 | * before the counter has been read. Which would skew the counter value |
| 99 | * to a later point than intended. |
| 100 | */ |
| 101 | __asm__ __volatile__("isb" : : : "memory"); |
| 102 | |
| 103 | } |
| 104 | # else |
| 105 | # define relax() do { } while (0) |
| 106 | # define frc(x) |
| 107 | # define FRC_MISSING |
| 108 | # endif |
| 109 | #else |
| 110 | # error Need to add support for this compiler. |
| 111 | #endif |
| 112 | |
| 113 | typedef uint64_t stamp_t; /* timestamp */ |
| 114 | typedef uint64_t cycles_t; /* number of cycles */ |
| 115 | |
| 116 | #define true 1 |
| 117 | #define false 0 |
| 118 | |
| 119 | enum command { |
| 120 | WAIT, |
| 121 | GO, |
| 122 | STOP |
| 123 | }; |
| 124 | |
| 125 | enum workload_type { |
| 126 | WORKLOAD_NONE = 0, |
| 127 | WORKLOAD_MEMMOVE, |
| 128 | WORKLOAD_NUM, |
| 129 | }; |
| 130 | |
| 131 | /* This workload needs pre-allocated memory */ |
| 132 | #define WORK_NEED_MEM (1UL << 0) |
| 133 | |
| 134 | typedef void (*workload_fn)(char *src, char *dst, size_t size); |
| 135 | |
| 136 | struct workload { |
| 137 | const char *w_name; |
| 138 | uint64_t w_flags; |
| 139 | workload_fn w_fn; |
| 140 | }; |
| 141 | |
| 142 | /* We'll have buckets 1us, 2us, ..., (BUCKET_SIZE) us. */ |
| 143 | #define BUCKET_SIZE (32) |
| 144 | |
| 145 | /* Default size of the workloads per thread (in bytes, which is 16KB) */ |
| 146 | #define WORKLOAD_MEM_SIZE (16UL << 10) |
| 147 | |
| 148 | /* By default, no workload */ |
| 149 | #define WORKLOAD_DEFAULT WORKLOAD_NONE |
| 150 | |
| 151 | struct thread { |
| 152 | int core_i; |
| 153 | pthread_t thread_id; |
| 154 | |
| 155 | /* NOTE! this is also how many ticks per us */ |
| 156 | unsigned int counter_mhz; |
| 157 | cycles_t int_total; |
| 158 | stamp_t frc_start; |
| 159 | stamp_t frc_stop; |
| 160 | cycles_t runtime; |
| 161 | stamp_t *buckets; |
| 162 | uint64_t minlat; |
| 163 | /* Maximum latency detected */ |
| 164 | uint64_t maxlat; |
| 165 | /* |
| 166 | * The extra part of the interruptions that cannot be put into even the |
| 167 | * biggest bucket. We'll use this to calculate a more accurate average at |
| 168 | * the end of the tests. |
| 169 | */ |
| 170 | uint64_t overflow_sum; |
| 171 | int memory_allocated; |
| 172 | |
| 173 | /* Buffers used for the workloads */ |
| 174 | char *src_buf; |
| 175 | char *dst_buf; |
| 176 | |
| 177 | /* These variables are calculated after the test */ |
| 178 | double average; |
| 179 | }; |
| 180 | |
| 181 | struct global { |
| 182 | /* Configuration. */ |
| 183 | unsigned int runtime_secs; |
| 184 | bool preheat; |
| 185 | /* |
| 186 | * Number of threads running for current test |
| 187 | * (either pre heat or real run) |
| 188 | */ |
| 189 | unsigned int n_threads; |
| 190 | /* Number of threads to test for the real run */ |
| 191 | unsigned int n_threads_total; |
| 192 | struct timeval tv_start; |
| 193 | int rtprio; |
| 194 | int bucket_size; |
| 195 | bool bucket_size_param; |
| 196 | int bucket_width; |
| 197 | int unit_per_us; |
| 198 | int precision; |
| 199 | int trace_threshold; |
| 200 | int runtime; |
| 201 | /* The core that we run the main thread. Default is cpu0 */ |
| 202 | int cpu_main_thread; |
| 203 | char *cpu_list; |
| 204 | char *app_name; |
| 205 | struct workload *workload; |
| 206 | uint64_t workload_mem_size; |
| 207 | int enable_bias; |
| 208 | uint64_t bias; |
| 209 | int quiet; |
| 210 | int single_preheat_thread; |
| 211 | int output_omit_zero_buckets; |
| 212 | char jsonfile[MAX_PATH]; |
| 213 | |
| 214 | /* Mutable state. */ |
| 215 | volatile enum command cmd; |
| 216 | volatile unsigned int n_threads_started; |
| 217 | volatile unsigned int n_threads_running; |
| 218 | volatile unsigned int n_threads_finished; |
| 219 | }; |
| 220 | |
| 221 | static struct global g; |
| 222 | |
| 223 | static void workload_nop(char *dst, char *src, size_t size) |
| 224 | { |
| 225 | /* Nop */ |
| 226 | } |
| 227 | |
| 228 | static void workload_memmove(char *dst, char *src, size_t size) |
| 229 | { |
| 230 | memmove(dst, src, size); |
| 231 | } |
| 232 | |
| 233 | struct workload workload_list[WORKLOAD_NUM] = { |
| 234 | { "no", 0, workload_nop }, |
| 235 | { "memmove", WORK_NEED_MEM, workload_memmove }, |
| 236 | }; |
| 237 | |
| 238 | #define TEST(x) \ |
| 239 | do { \ |
| 240 | if (!(x)) \ |
| 241 | test_fail(#x, __LINE__); \ |
| 242 | } while (0) |
| 243 | |
| 244 | #define TEST0(x) TEST((x) == 0) |
| 245 | |
| 246 | static void test_fail(const char *what, int line) |
| 247 | { |
| 248 | fprintf(stderr, "ERROR:\n"); |
| 249 | fprintf(stderr, "ERROR: TEST(%s)\n", what); |
| 250 | fprintf(stderr, "ERROR: at line %d\n", line); |
| 251 | fprintf(stderr, "ERROR: errno=%d (%s)\n", errno, strerror(errno)); |
| 252 | fprintf(stderr, "ERROR:\n"); |
| 253 | exit(1); |
| 254 | } |
| 255 | |
| 256 | static int move_to_core(int core_i) |
| 257 | { |
| 258 | cpu_set_t cpus; |
| 259 | |
| 260 | CPU_ZERO(&cpus); |
| 261 | CPU_SET(core_i, &cpus); |
| 262 | return sched_setaffinity(0, sizeof(cpus), &cpus); |
| 263 | } |
| 264 | |
| 265 | #ifndef arch_measure_counter_mhz |
| 266 | static cycles_t __measure_counter_hz(void) |
| 267 | { |
| 268 | struct timeval tvs, tve; |
| 269 | stamp_t s, e; |
| 270 | double sec; |
| 271 | |
| 272 | frc(&s); |
| 273 | e = s; |
| 274 | gettimeofday(&tvs, NULL); |
| 275 | while (e - s < 1000000) |
| 276 | frc(&e); |
| 277 | gettimeofday(&tve, NULL); |
| 278 | sec = tve.tv_sec - tvs.tv_sec + (tve.tv_usec - tvs.tv_usec) / 1e6; |
| 279 | return (cycles_t) ((e - s) / sec); |
| 280 | } |
| 281 | |
| 282 | static unsigned int measure_counter_mhz(void) |
| 283 | { |
| 284 | cycles_t m, mprev, d; |
| 285 | |
| 286 | mprev = __measure_counter_hz(); |
| 287 | do { |
| 288 | m = __measure_counter_hz(); |
| 289 | if (m > mprev) |
| 290 | d = m - mprev; |
| 291 | else |
| 292 | d = mprev - m; |
| 293 | mprev = m; |
| 294 | } while (d > m / 1000); |
| 295 | |
| 296 | return (unsigned int) (m / 1000000); |
| 297 | } |
| 298 | #endif |
| 299 | |
| 300 | static void thread_init(struct thread *t) |
| 301 | { |
| 302 | t->counter_mhz = measure_counter_mhz(); |
| 303 | t->maxlat = 0; |
| 304 | t->overflow_sum = 0; |
| 305 | t->minlat = (uint64_t)-1; |
| 306 | |
| 307 | /* NOTE: all the buffers are not freed until the process quits. */ |
| 308 | if (!t->memory_allocated) { |
| 309 | TEST(t->buckets = calloc(1, sizeof(t->buckets[0]) * g.bucket_size)); |
| 310 | if (g.workload->w_flags & WORK_NEED_MEM) { |
| 311 | TEST0(posix_memalign((void **)&t->src_buf, getpagesize(), |
| 312 | g.workload_mem_size)); |
| 313 | memset(t->src_buf, 0, g.workload_mem_size); |
| 314 | TEST0(posix_memalign((void **)&t->dst_buf, getpagesize(), |
| 315 | g.workload_mem_size)); |
| 316 | memset(t->dst_buf, 0, g.workload_mem_size); |
| 317 | } |
| 318 | t->memory_allocated = 1; |
| 319 | } else { |
| 320 | /* Clear the buckets */ |
| 321 | memset(t->buckets, 0, sizeof(t->buckets[0]) * g.bucket_size); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static float cycles_to_sec(const struct thread *t, uint64_t cycles) |
| 326 | { |
| 327 | return cycles / (t->counter_mhz * 1e6); |
| 328 | } |
| 329 | |
| 330 | static void insert_bucket(struct thread *t, stamp_t value) |
| 331 | { |
| 332 | int index; |
| 333 | unsigned int lat; |
| 334 | uint64_t extra; |
| 335 | double us; |
| 336 | |
| 337 | lat = (value * g.unit_per_us) / t->counter_mhz; |
| 338 | us = (double)lat / g.unit_per_us; |
| 339 | if (!g.preheat && g.trace_threshold && us >= g.trace_threshold) { |
| 340 | char *line = "%s: Trace threshold (%d us) triggered on cpu %d with %.*f us!\n"; |
| 341 | tracemark(line, g.app_name, g.trace_threshold, t->core_i, |
| 342 | g.precision, us); |
| 343 | tracing_stop(); |
| 344 | err_quit(line, g.app_name, g.trace_threshold, t->core_i, |
| 345 | g.precision, us); |
| 346 | } |
| 347 | |
| 348 | /* Update max latency */ |
| 349 | if (lat > t->maxlat) |
| 350 | t->maxlat = lat; |
| 351 | |
| 352 | if (lat < t->minlat) |
| 353 | t->minlat = lat; |
| 354 | |
| 355 | if (g.bias) { |
| 356 | /* t->bias will be set after pre-heat if user enabled it */ |
| 357 | lat -= g.bias; |
| 358 | /* |
| 359 | * Negative should hardly happen, but if it happens, we assume we're in |
| 360 | * the smallest bucket. |
| 361 | */ |
| 362 | if (lat <= 0) |
| 363 | lat = 1; |
| 364 | } |
| 365 | |
| 366 | index = lat / g.bucket_width; |
| 367 | assert(index >= 0); |
| 368 | |
| 369 | /* Too big the jitter; put into the last bucket */ |
| 370 | if (index >= g.bucket_size) { |
| 371 | /* Keep the extra bit (in bucket width multiples) */ |
| 372 | extra = index - g.bucket_size; |
| 373 | if (t->overflow_sum + extra < t->overflow_sum) { |
| 374 | /* The uint64_t even overflowed itself; bail out */ |
| 375 | printf("Accumulated overflow too much!\n"); |
| 376 | exit(1); |
| 377 | } |
| 378 | t->overflow_sum += extra; |
| 379 | index = g.bucket_size - 1; |
| 380 | } |
| 381 | |
| 382 | t->buckets[index]++; |
| 383 | if (t->buckets[index] == 0) { |
| 384 | printf("Bucket %d overflowed\n", index); |
| 385 | exit(1); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static void doit(struct thread *t) |
| 390 | { |
| 391 | stamp_t ts1, ts2; |
| 392 | workload_fn workload_fn = g.workload->w_fn; |
| 393 | |
| 394 | frc(&ts2); |
| 395 | do { |
| 396 | workload_fn(t->dst_buf, t->src_buf, g.workload_mem_size); |
| 397 | frc(&ts1); |
| 398 | insert_bucket(t, ts1 - ts2); |
| 399 | ts2 = ts1; |
| 400 | } while (g.cmd == GO); |
| 401 | } |
| 402 | |
| 403 | static int set_fifo_prio(int prio) |
| 404 | { |
| 405 | struct sched_param param; |
| 406 | |
| 407 | memset(¶m, 0, sizeof(param)); |
| 408 | param.sched_priority = prio; |
| 409 | return sched_setscheduler(0, SCHED_FIFO, ¶m); |
| 410 | } |
| 411 | |
| 412 | static void *thread_main(void *arg) |
| 413 | { |
| 414 | /* Important thing to note here is that once we start bashing the CPU, we |
| 415 | * need to keep doing so to prevent the core from changing frequency or |
| 416 | * dropping into a low power state. |
| 417 | */ |
| 418 | struct thread *t = arg; |
| 419 | |
| 420 | /* Alloc memory in the thread itself after setting affinity to get the |
| 421 | * best chance of getting numa-local memory. Doesn't matter so much for |
| 422 | * the "struct thread" since we expect that to stay cache resident. |
| 423 | */ |
| 424 | TEST(move_to_core(t->core_i) == 0); |
| 425 | if (g.rtprio) |
| 426 | TEST(set_fifo_prio(g.rtprio) == 0); |
| 427 | |
| 428 | /* Don't bash the cpu until all threads have got going. */ |
| 429 | atomic_inc(&g.n_threads_started); |
| 430 | while (g.cmd == WAIT) |
| 431 | usleep(1000); |
| 432 | |
| 433 | thread_init(t); |
| 434 | |
| 435 | /* Ensure we all start at the same time. */ |
| 436 | atomic_inc(&g.n_threads_running); |
| 437 | while (g.n_threads_running != g.n_threads) |
| 438 | relax(); |
| 439 | |
| 440 | frc(&t->frc_start); |
| 441 | doit(t); |
| 442 | frc(&t->frc_stop); |
| 443 | |
| 444 | t->runtime = t->frc_stop - t->frc_start; |
| 445 | |
| 446 | /* Wait for everyone to finish so we don't disturb them by exiting and |
| 447 | * waking the main thread. |
| 448 | */ |
| 449 | atomic_inc(&g.n_threads_finished); |
| 450 | while (g.n_threads_finished != g.n_threads) |
| 451 | relax(); |
| 452 | |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | #define putfield(label, val, fmt, end) do { \ |
| 457 | printf("%12s:\t", label); \ |
| 458 | for (i = 0; i < g.n_threads; ++i) \ |
| 459 | printf(" %"fmt, val); \ |
| 460 | printf("%s\n", end); \ |
| 461 | } while (0) |
| 462 | |
| 463 | #define putfieldp(label, val, end) do { \ |
| 464 | printf("%12s:\t", label); \ |
| 465 | for (i = 0; i < g.n_threads; ++i) \ |
| 466 | printf(" %.*f", g.precision, \ |
| 467 | (double)(val) / g.unit_per_us); \ |
| 468 | printf("%s\n", end); \ |
| 469 | } while (0) |
| 470 | |
| 471 | static double bucket_to_lat(int bucket) |
| 472 | { |
| 473 | return (g.bias + (bucket + 1) * (double)g.bucket_width) / g.unit_per_us; |
| 474 | } |
| 475 | |
| 476 | void calculate(struct thread *t) |
| 477 | { |
| 478 | int j; |
| 479 | unsigned int i; |
| 480 | double sum; |
| 481 | uint64_t count; |
| 482 | |
| 483 | for (i = 0; i < g.n_threads; ++i) { |
| 484 | /* Calculate average */ |
| 485 | sum = count = 0; |
| 486 | for (j = 0; j < g.bucket_size; j++) { |
| 487 | sum += t[i].buckets[j] * bucket_to_lat(j); |
| 488 | count += t[i].buckets[j]; |
| 489 | } |
| 490 | /* Add the extra amount of huge spikes in */ |
| 491 | sum += t->overflow_sum * g.bucket_width; |
| 492 | t[i].average = sum / count; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | static void write_summary(struct thread *t) |
| 497 | { |
| 498 | int j, print_dotdotdot = 0; |
| 499 | unsigned long int i, k; |
| 500 | char bucket_name[64]; |
| 501 | |
| 502 | calculate(t); |
| 503 | |
| 504 | putfield("Core", t[i].core_i, "d", ""); |
| 505 | putfield("Counter Freq", t[i].counter_mhz, "u", " (MHz)"); |
| 506 | |
| 507 | for (j = 0; j < g.bucket_size; j++) { |
| 508 | if (j < g.bucket_size-1 && g.output_omit_zero_buckets) { |
| 509 | for (k = 0; k < g.n_threads; k++) { |
| 510 | if (t[k].buckets[j] != 0) |
| 511 | break; |
| 512 | } |
| 513 | if (k == g.n_threads) { |
| 514 | print_dotdotdot = 1; |
| 515 | continue; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if (print_dotdotdot) { |
| 520 | printf(" ...\n"); |
| 521 | print_dotdotdot = 0; |
| 522 | } |
| 523 | |
| 524 | snprintf(bucket_name, sizeof(bucket_name), "%03.*f (us)", |
| 525 | g.precision, bucket_to_lat(j)); |
| 526 | putfield(bucket_name, t[i].buckets[j], PRIu64, |
| 527 | (j == g.bucket_size - 1) ? " (including overflows)" : ""); |
| 528 | } |
| 529 | |
| 530 | putfieldp("Minimum", t[i].minlat, " (us)"); |
| 531 | putfield("Average", t[i].average, ".3lf", " (us)"); |
| 532 | putfieldp("Maximum", t[i].maxlat, " (us)"); |
| 533 | putfieldp("Max-Min", t[i].maxlat - t[i].minlat, " (us)"); |
| 534 | putfield("Duration", cycles_to_sec(&(t[i]), t[i].runtime), |
| 535 | ".3f", " (sec)"); |
| 536 | printf("\n"); |
| 537 | } |
| 538 | |
| 539 | static void write_summary_json(FILE *f, void *data) |
| 540 | { |
| 541 | struct thread *t = data; |
| 542 | int j, comma; |
| 543 | unsigned long int i; |
| 544 | |
| 545 | fprintf(f, " \"num_threads\": %d,\n", g.n_threads); |
| 546 | fprintf(f, " \"thread\": {\n"); |
| 547 | for (i = 0; i < g.n_threads; ++i) { |
| 548 | fprintf(f, " \"%lu\": {\n", i); |
| 549 | fprintf(f, " \"cpu\": %d,\n", t[i].core_i); |
| 550 | fprintf(f, " \"freq\": %d,\n", t[i].counter_mhz); |
| 551 | fprintf(f, " \"min\": %" PRIu64 ",\n", t[i].minlat); |
| 552 | fprintf(f, " \"avg\": %3lf,\n", t[i].average); |
| 553 | fprintf(f, " \"max\": %" PRIu64 ",\n", t[i].maxlat); |
| 554 | fprintf(f, " \"duration\": %.3f,\n", |
| 555 | cycles_to_sec(&(t[i]), t[i].runtime)); |
| 556 | fprintf(f, " \"histogram\": {"); |
| 557 | for (j = 0, comma = 0; j < g.bucket_size; j++) { |
| 558 | if (t[i].buckets[j] == 0) |
| 559 | continue; |
| 560 | fprintf(f, "%s", comma ? ",\n" : "\n"); |
| 561 | fprintf(f, " \"%.*f\": %" PRIu64, |
| 562 | g.precision, bucket_to_lat(j), t[i].buckets[j]); |
| 563 | comma = 1; |
| 564 | } |
| 565 | if (comma) |
| 566 | fprintf(f, "\n"); |
| 567 | fprintf(f, " }\n"); |
| 568 | fprintf(f, " }%s\n", i == g.n_threads - 1 ? "" : ","); |
| 569 | } |
| 570 | fprintf(f, " }\n"); |
| 571 | } |
| 572 | |
| 573 | static void run_expt(struct thread *threads, int runtime_secs, bool preheat) |
| 574 | { |
| 575 | unsigned long int i; |
| 576 | |
| 577 | g.runtime_secs = runtime_secs; |
| 578 | g.preheat = preheat; |
| 579 | g.n_threads_started = 0; |
| 580 | g.n_threads_running = 0; |
| 581 | g.n_threads_finished = 0; |
| 582 | g.cmd = WAIT; |
| 583 | |
| 584 | for (i = 0; i < g.n_threads; ++i) |
| 585 | TEST0(pthread_create(&(threads[i].thread_id), NULL, |
| 586 | thread_main, &(threads[i]))); |
| 587 | while (g.n_threads_started != g.n_threads) |
| 588 | usleep(1000); |
| 589 | |
| 590 | gettimeofday(&g.tv_start, NULL); |
| 591 | g.cmd = GO; |
| 592 | |
| 593 | alarm(runtime_secs); |
| 594 | |
| 595 | /* Go to sleep until the threads have done their stuff. */ |
| 596 | for (i = 0; i < g.n_threads; ++i) |
| 597 | pthread_join(threads[i].thread_id, NULL); |
| 598 | } |
| 599 | |
| 600 | static void handle_alarm(int code) |
| 601 | { |
| 602 | g.cmd = STOP; |
| 603 | } |
| 604 | |
| 605 | static void usage(int error) |
| 606 | { |
| 607 | printf("Usage:\n" |
| 608 | "oslat <options>\n\n" |
| 609 | "This is an OS latency detector by running busy loops on specified cores.\n" |
| 610 | "Please run this tool using root.\n\n" |
| 611 | "Available options:\n\n" |
| 612 | "-b, --bucket-size Specify the number of the buckets (4-1024)\n" |
| 613 | "-B, --bias Add a bias to all the buckets using the estimated mininum\n" |
| 614 | "-c, --cpu-list Specify CPUs to run on, e.g. '1,3,5,7-15'\n" |
| 615 | "-C, --cpu-main-thread Specify which CPU the main thread runs on. Default is cpu0.\n" |
| 616 | "-D, --duration Specify test duration, e.g., 60, 20m, 2H\n" |
| 617 | " (m/M: minutes, h/H: hours, d/D: days)\n" |
| 618 | " --json=FILENAME write final results into FILENAME, JSON formatted\n" |
| 619 | "-f, --rtprio Using SCHED_FIFO priority (1-99)\n" |
| 620 | "-m, --workload-mem Size of the memory to use for the workload (e.g., 4K, 1M).\n" |
| 621 | " Total memory usage will be this value multiplies 2*N,\n" |
| 622 | " because there will be src/dst buffers for each thread, and\n" |
| 623 | " N is the number of processors for testing.\n" |
| 624 | "-q --quiet print a summary only on exit\n" |
| 625 | "-s, --single-preheat Use a single thread when measuring latency at preheat stage\n" |
| 626 | " NOTE: please make sure the CPU frequency on all testing cores\n" |
| 627 | " are locked before using this parmater. If you don't know how\n" |
| 628 | " to lock the freq then please don't use this parameter.\n" |
| 629 | "-T, --trace-threshold Stop the test when threshold triggered (in us),\n" |
| 630 | " print a marker in ftrace and stop ftrace too.\n" |
| 631 | "-v, --version Display the version of the software.\n" |
| 632 | "-w, --workload Specify a kind of workload, default is no workload\n" |
| 633 | " (options: no, memmove)\n" |
| 634 | "-W, --bucket-width Interval between buckets in nanoseconds\n" |
| 635 | " NOTE: Widths not a multiple of 1000 cause ns-precision output\n" |
| 636 | " You are responsible for considering the impact of measurement\n" |
| 637 | " overhead at the nanosecond scale.\n" |
| 638 | "-z, --zero-omit Don't display buckets in the output histogram if all zeros.\n" |
| 639 | ); |
| 640 | exit(error); |
| 641 | } |
| 642 | |
| 643 | static int workload_select(char *name) |
| 644 | { |
| 645 | int i = 0; |
| 646 | |
| 647 | for (i = 0; i < WORKLOAD_NUM; i++) { |
| 648 | if (!strcmp(name, workload_list[i].w_name)) { |
| 649 | g.workload = &workload_list[i]; |
| 650 | return 0; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | return -1; |
| 655 | } |
| 656 | |
| 657 | enum option_value { |
| 658 | OPT_BUCKETSIZE = 1, OPT_BUCKETWIDTH, OPT_CPU_LIST, OPT_CPU_MAIN_THREAD, |
| 659 | OPT_DURATION, OPT_JSON, OPT_RT_PRIO, OPT_HELP, OPT_TRACE_TH, |
| 660 | OPT_WORKLOAD, OPT_WORKLOAD_MEM, OPT_BIAS, |
| 661 | OPT_QUIET, OPT_SINGLE_PREHEAT, OPT_ZERO_OMIT, |
| 662 | OPT_VERSION |
| 663 | }; |
| 664 | |
| 665 | /* Process commandline options */ |
| 666 | static void parse_options(int argc, char *argv[]) |
| 667 | { |
| 668 | while (1) { |
| 669 | int option_index = 0; |
| 670 | static struct option options[] = { |
| 671 | { "bucket-size", required_argument, NULL, OPT_BUCKETSIZE }, |
| 672 | { "bucket-width", required_argument, NULL, OPT_BUCKETWIDTH }, |
| 673 | { "cpu-list", required_argument, NULL, OPT_CPU_LIST }, |
| 674 | { "cpu-main-thread", required_argument, NULL, OPT_CPU_MAIN_THREAD}, |
| 675 | { "duration", required_argument, NULL, OPT_DURATION }, |
| 676 | { "json", required_argument, NULL, OPT_JSON }, |
| 677 | { "rtprio", required_argument, NULL, OPT_RT_PRIO }, |
| 678 | { "help", no_argument, NULL, OPT_HELP }, |
| 679 | { "trace-threshold", required_argument, NULL, OPT_TRACE_TH }, |
| 680 | { "workload", required_argument, NULL, OPT_WORKLOAD }, |
| 681 | { "workload-mem", required_argument, NULL, OPT_WORKLOAD_MEM }, |
| 682 | { "bias", no_argument, NULL, OPT_BIAS }, |
| 683 | { "quiet", no_argument, NULL, OPT_QUIET }, |
| 684 | { "single-preheat", no_argument, NULL, OPT_SINGLE_PREHEAT }, |
| 685 | { "zero-omit", no_argument, NULL, OPT_ZERO_OMIT }, |
| 686 | { "version", no_argument, NULL, OPT_VERSION }, |
| 687 | { NULL, 0, NULL, 0 }, |
| 688 | }; |
| 689 | int i, c = getopt_long(argc, argv, "b:Bc:C:D:f:hm:qsw:W:T:vz", |
| 690 | options, &option_index); |
| 691 | long ncores; |
| 692 | |
| 693 | if (c == -1) |
| 694 | break; |
| 695 | |
| 696 | switch (c) { |
| 697 | case OPT_BUCKETSIZE: |
| 698 | case 'b': |
| 699 | g.bucket_size_param = true; |
| 700 | g.bucket_size = strtol(optarg, NULL, 10); |
| 701 | if (g.bucket_size > 1024 || g.bucket_size <= 4) { |
| 702 | printf("Illegal bucket size: %s (should be: 4-1024)\n", |
| 703 | optarg); |
| 704 | exit(1); |
| 705 | } |
| 706 | break; |
| 707 | case OPT_BUCKETWIDTH: |
| 708 | case 'W': |
| 709 | g.bucket_width = strtol(optarg, NULL, 10); |
| 710 | if (g.bucket_width <= 0) { |
| 711 | printf("Illegal bucket width: %s\n", optarg); |
| 712 | exit(1); |
| 713 | } |
| 714 | if (g.bucket_width % 1000) { |
| 715 | g.unit_per_us = 1000; |
| 716 | g.precision = 3; |
| 717 | } else { |
| 718 | g.bucket_width /= 1000; |
| 719 | } |
| 720 | break; |
| 721 | case OPT_BIAS: |
| 722 | case 'B': |
| 723 | g.enable_bias = 1; |
| 724 | break; |
| 725 | case OPT_CPU_LIST: |
| 726 | case 'c': |
| 727 | g.cpu_list = strdup(optarg); |
| 728 | break; |
| 729 | case OPT_CPU_MAIN_THREAD: |
| 730 | case 'C': |
| 731 | ncores = sysconf(_SC_NPROCESSORS_CONF); |
| 732 | g.cpu_main_thread = strtol(optarg, NULL, 10); |
| 733 | if (g.cpu_main_thread < 0 || g.cpu_main_thread > ncores) { |
| 734 | printf("Illegal core for main thread: %s (should be: 0-%ld)\n", |
| 735 | optarg, ncores); |
| 736 | exit(1); |
| 737 | } |
| 738 | break; |
| 739 | case OPT_DURATION: |
| 740 | case 'D': |
| 741 | g.runtime = parse_time_string(optarg); |
| 742 | if (!g.runtime) { |
| 743 | printf("Illegal runtime: %s\n", optarg); |
| 744 | exit(1); |
| 745 | } |
| 746 | break; |
| 747 | case OPT_RT_PRIO: |
| 748 | case 'f': |
| 749 | g.rtprio = strtol(optarg, NULL, 10); |
| 750 | if (g.rtprio < 1 || g.rtprio > 99) { |
| 751 | printf("Illegal RT priority: %s (should be: 1-99)\n", optarg); |
| 752 | exit(1); |
| 753 | } |
| 754 | break; |
| 755 | case OPT_JSON: |
| 756 | strncpy(g.jsonfile, optarg, strnlen(optarg, MAX_PATH-1)); |
| 757 | break; |
| 758 | case OPT_TRACE_TH: |
| 759 | case 'T': |
| 760 | g.trace_threshold = strtol(optarg, NULL, 10); |
| 761 | if (g.trace_threshold <= 0) { |
| 762 | printf("Parameter --trace-threshold needs to be positive\n"); |
| 763 | exit(1); |
| 764 | } |
| 765 | enable_trace_mark(); |
| 766 | break; |
| 767 | case OPT_WORKLOAD: |
| 768 | case 'w': |
| 769 | if (workload_select(optarg)) { |
| 770 | printf("Unknown workload '%s'. Please choose from: ", optarg); |
| 771 | for (i = 0; i < WORKLOAD_NUM; i++) { |
| 772 | printf("'%s'", workload_list[i].w_name); |
| 773 | if (i != WORKLOAD_NUM - 1) |
| 774 | printf(", "); |
| 775 | } |
| 776 | printf("\n\n"); |
| 777 | exit(1); |
| 778 | } |
| 779 | break; |
| 780 | case OPT_WORKLOAD_MEM: |
| 781 | case 'm': |
| 782 | if (parse_mem_string(optarg, &g.workload_mem_size)) { |
| 783 | printf("Unknown workload memory size '%s'.\n\n", optarg); |
| 784 | exit(1); |
| 785 | } |
| 786 | break; |
| 787 | case OPT_QUIET: |
| 788 | case 'q': |
| 789 | g.quiet = 1; |
| 790 | break; |
| 791 | case OPT_SINGLE_PREHEAT: |
| 792 | case 's': |
| 793 | /* |
| 794 | * Only use one core for pre-heat. Then if --bias is used, the |
| 795 | * bias will be exactly the min value of the pre-heat core. |
| 796 | */ |
| 797 | g.single_preheat_thread = true; |
| 798 | break; |
| 799 | case OPT_VERSION: |
| 800 | case 'v': |
| 801 | /* |
| 802 | * We always print the version before parsing options |
| 803 | * so just exit |
| 804 | */ |
| 805 | exit(0); |
| 806 | break; |
| 807 | case OPT_ZERO_OMIT: |
| 808 | case 'z': |
| 809 | g.output_omit_zero_buckets = 1; |
| 810 | break; |
| 811 | case OPT_HELP: |
| 812 | case 'h': |
| 813 | usage(0); |
| 814 | break; |
| 815 | default: |
| 816 | usage(1); |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | if (!g.bucket_size_param && g.precision == 3 && g.bucket_width < 1000) |
| 822 | g.bucket_size = BUCKET_SIZE * 1000 / g.bucket_width; |
| 823 | } |
| 824 | |
| 825 | void dump_globals(void) |
| 826 | { |
| 827 | printf("Total runtime: \t\t%d seconds\n", g.runtime); |
| 828 | printf("Thread priority: \t"); |
| 829 | if (g.rtprio) |
| 830 | printf("SCHED_FIFO:%d\n", g.rtprio); |
| 831 | else |
| 832 | printf("default\n"); |
| 833 | printf("CPU list: \t\t%s\n", g.cpu_list ?: "(all cores)"); |
| 834 | printf("CPU for main thread: \t%d\n", g.cpu_main_thread); |
| 835 | printf("Workload: \t\t%s\n", g.workload->w_name); |
| 836 | printf("Workload mem: \t\t%"PRIu64" (KiB)\n", |
| 837 | (g.workload->w_flags & WORK_NEED_MEM) ? |
| 838 | (g.workload_mem_size / 1024) : 0); |
| 839 | printf("Preheat cores: \t\t%d\n", g.single_preheat_thread ? |
| 840 | 1 : g.n_threads_total); |
| 841 | printf("\n"); |
| 842 | } |
| 843 | |
| 844 | static void record_bias(struct thread *t) |
| 845 | { |
| 846 | unsigned long int i; |
| 847 | uint64_t bias = (uint64_t)-1; |
| 848 | |
| 849 | if (!g.enable_bias) |
| 850 | return; |
| 851 | |
| 852 | /* Record the min value of minlat on all the threads */ |
| 853 | for (i = 0; i < g.n_threads; ++i) { |
| 854 | if (t[i].minlat < bias) |
| 855 | bias = t[i].minlat; |
| 856 | } |
| 857 | g.bias = bias; |
| 858 | printf("Global bias set to %.*f (us)\n", g.precision, |
| 859 | (double)bias / g.unit_per_us); |
| 860 | } |
| 861 | |
| 862 | int main(int argc, char *argv[]) |
| 863 | { |
| 864 | struct thread *threads; |
| 865 | int n_cores; |
| 866 | unsigned long int i; |
| 867 | struct bitmask *cpu_set = NULL; |
| 868 | |
| 869 | #ifdef FRC_MISSING |
| 870 | printf("This architecture is not yet supported. " |
| 871 | "Please implement frc() function first for %s.\n", argv[0]); |
| 872 | return 0; |
| 873 | #endif |
| 874 | if (numa_available() == -1) { |
| 875 | printf("ERROR: Could not initialize libnuma\n"); |
| 876 | exit(1); |
| 877 | } |
| 878 | |
| 879 | rt_init(argc, argv); |
| 880 | |
| 881 | g.app_name = argv[0]; |
| 882 | g.rtprio = 0; |
| 883 | g.bucket_size = BUCKET_SIZE; |
| 884 | g.bucket_width = 1; |
| 885 | g.unit_per_us = 1; |
| 886 | g.runtime = 1; |
| 887 | g.workload = &workload_list[WORKLOAD_DEFAULT]; |
| 888 | g.workload_mem_size = WORKLOAD_MEM_SIZE; |
| 889 | /* Run the main thread on cpu0 by default */ |
| 890 | g.cpu_main_thread = 0; |
| 891 | printf("oslat V %1.2f\n", VERSION); |
| 892 | parse_options(argc, argv); |
| 893 | |
| 894 | TEST(mlockall(MCL_CURRENT | MCL_FUTURE) == 0); |
| 895 | |
| 896 | if (!g.cpu_list) |
| 897 | g.cpu_list = strdup("all"); |
| 898 | |
| 899 | cpu_set = numa_parse_cpustring_all(g.cpu_list); |
| 900 | if (!cpu_set) |
| 901 | fatal("oslat: numa_parse_cpustring_all failed.\n"); |
| 902 | n_cores = numa_bitmask_weight(cpu_set); |
| 903 | |
| 904 | TEST(threads = calloc(1, n_cores * sizeof(threads[0]))); |
| 905 | for (i = 0; n_cores && i < cpu_set->size; i++) { |
| 906 | if (numa_bitmask_isbitset(cpu_set, i) && move_to_core(i) == 0) { |
| 907 | threads[g.n_threads_total++].core_i = i; |
| 908 | n_cores--; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if (numa_bitmask_isbitset(cpu_set, 0) && g.rtprio) |
| 913 | printf("WARNING: Running SCHED_FIFO workload on CPU 0 may hang the thread\n"); |
| 914 | |
| 915 | numa_bitmask_free(cpu_set); |
| 916 | |
| 917 | TEST(move_to_core(g.cpu_main_thread) == 0); |
| 918 | |
| 919 | signal(SIGALRM, handle_alarm); |
| 920 | signal(SIGINT, handle_alarm); |
| 921 | signal(SIGTERM, handle_alarm); |
| 922 | |
| 923 | if (!g.quiet) |
| 924 | dump_globals(); |
| 925 | |
| 926 | if (!g.quiet) |
| 927 | printf("Pre-heat for 1 seconds...\n"); |
| 928 | if (g.single_preheat_thread) |
| 929 | g.n_threads = 1; |
| 930 | else |
| 931 | g.n_threads = g.n_threads_total; |
| 932 | run_expt(threads, 1, true); |
| 933 | record_bias(threads); |
| 934 | |
| 935 | if (!g.quiet) |
| 936 | printf("Test starts...\n"); |
| 937 | /* Reset n_threads to always run on all the cores */ |
| 938 | g.n_threads = g.n_threads_total; |
| 939 | run_expt(threads, g.runtime, false); |
| 940 | |
| 941 | if (!g.quiet) |
| 942 | printf("Test completed.\n\n"); |
| 943 | |
| 944 | write_summary(threads); |
| 945 | |
| 946 | if (strlen(g.jsonfile) != 0) |
| 947 | rt_write_json(g.jsonfile, 0, write_summary_json, threads); |
| 948 | |
| 949 | if (g.cpu_list) { |
| 950 | free(g.cpu_list); |
| 951 | g.cpu_list = NULL; |
| 952 | } |
| 953 | |
| 954 | disable_trace_mark(); |
| 955 | |
| 956 | return 0; |
| 957 | } |