| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2016 Red Hat Inc, Steven Rostedt <[email protected]> |
| 5 | * Copyright (C) 2019 John Kacur <[email protected]> |
| 6 | * Copyright (C) 2019 Clark Williams <[email protected]> |
| 7 | */ |
| 8 | |
| 9 | #include <pthread.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | #include <stdio.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <sched.h> |
| 17 | #include <ctype.h> |
| 18 | #include <errno.h> |
| 19 | #include <signal.h> |
| 20 | #include <getopt.h> |
| 21 | |
| 22 | #include <sys/syscall.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/mount.h> |
| 25 | #include <sys/mman.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/vfs.h> |
| 29 | |
| 30 | #include <linux/unistd.h> |
| 31 | #include <linux/magic.h> |
| 32 | |
| 33 | #include "rt-utils.h" |
| 34 | #include "rt-sched.h" |
| 35 | #include "rt-error.h" |
| 36 | #include "histogram.h" |
| 37 | |
| 38 | #define _STR(x) #x |
| 39 | #define STR(x) _STR(x) |
| 40 | #ifndef MAXPATH |
| 41 | #define MAXPATH 1024 |
| 42 | #endif |
| 43 | |
| 44 | #define HIST_MAX 1000000 |
| 45 | |
| 46 | #define CPUSET_ALL "my_cpuset_all" |
| 47 | #define CPUSET_LOCAL "my_cpuset" |
| 48 | |
| 49 | typedef unsigned long long u64; |
| 50 | typedef unsigned int u32; |
| 51 | typedef int s32; |
| 52 | |
| 53 | /* Struct for statistics */ |
| 54 | struct thread_stat { |
| 55 | unsigned long cycles; |
| 56 | long min; |
| 57 | long max; |
| 58 | long act; |
| 59 | double avg; |
| 60 | long *values; |
| 61 | struct histogram *hist; |
| 62 | pthread_t thread; |
| 63 | int threadstarted; |
| 64 | int tid; |
| 65 | long reduce; |
| 66 | }; |
| 67 | |
| 68 | struct sched_data { |
| 69 | u64 runtime_us; |
| 70 | u64 deadline_us; |
| 71 | |
| 72 | int bufmsk; |
| 73 | |
| 74 | struct thread_stat stat; |
| 75 | }; |
| 76 | |
| 77 | static int shutdown; |
| 78 | static int info_enable; |
| 79 | static int debug_enable; |
| 80 | static int tracelimit; |
| 81 | static int trace_marker; |
| 82 | static int histogram; |
| 83 | static FILE *histfile; |
| 84 | static pthread_mutex_t break_thread_id_lock = PTHREAD_MUTEX_INITIALIZER; |
| 85 | static pid_t break_thread_id; |
| 86 | static uint64_t break_thread_value; |
| 87 | static pthread_barrier_t barrier; |
| 88 | |
| 89 | static int cpu_count; |
| 90 | static int all_cpus; |
| 91 | static int nr_threads; |
| 92 | static int use_nsecs; |
| 93 | static int quiet; |
| 94 | static char jsonfile[MAX_PATH]; |
| 95 | |
| 96 | static struct histoset hset; |
| 97 | |
| 98 | static int find_mount(const char *mount, char *debugfs) |
| 99 | { |
| 100 | char type[100]; |
| 101 | FILE *fp; |
| 102 | |
| 103 | if ((fp = fopen("/proc/mounts", "r")) == NULL) |
| 104 | return 0; |
| 105 | |
| 106 | while (fscanf(fp, "%*s %" |
| 107 | STR(MAXPATH) |
| 108 | "s %99s %*s %*d %*d\n", |
| 109 | debugfs, type) == 2) { |
| 110 | if (strcmp(type, mount) == 0) |
| 111 | break; |
| 112 | } |
| 113 | fclose(fp); |
| 114 | |
| 115 | if (strcmp(type, mount) != 0) |
| 116 | return 0; |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | static const char *find_debugfs(void) |
| 121 | { |
| 122 | static int debugfs_found; |
| 123 | static char debugfs[MAXPATH+1]; |
| 124 | |
| 125 | if (debugfs_found) |
| 126 | return debugfs; |
| 127 | |
| 128 | if (!find_mount("debugfs", debugfs)) |
| 129 | return ""; |
| 130 | |
| 131 | debugfs_found = 1; |
| 132 | |
| 133 | return debugfs; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Return true if file exists |
| 138 | */ |
| 139 | static int check_file_exists(char *path) |
| 140 | { |
| 141 | int ret; |
| 142 | struct stat st; |
| 143 | |
| 144 | ret = !stat(path, &st); |
| 145 | |
| 146 | return ret; |
| 147 | |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Return 0 on success |
| 152 | */ |
| 153 | |
| 154 | static int fill_sched_features(char *path) |
| 155 | { |
| 156 | int ret; |
| 157 | const char *debugfs; |
| 158 | |
| 159 | debugfs = find_debugfs(); |
| 160 | if (strlen(debugfs) == 0) |
| 161 | return -1; |
| 162 | |
| 163 | ret = snprintf(path, MAX_PATH, "%s/sched/features", debugfs); |
| 164 | if (ret >= MAX_PATH) |
| 165 | return -1; |
| 166 | ret = check_file_exists(path); |
| 167 | if (ret) |
| 168 | return 0; |
| 169 | |
| 170 | memset(path, 0, MAX_PATH); |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static int setup_hr_tick(void) |
| 175 | { |
| 176 | char path[MAX_PATH]; |
| 177 | char buf[500]; |
| 178 | static int set = 0; |
| 179 | int hrtick_dl = 0; |
| 180 | char *p; |
| 181 | int ret; |
| 182 | int len; |
| 183 | int fd; |
| 184 | |
| 185 | if (set) |
| 186 | return 1; |
| 187 | set = 1; |
| 188 | |
| 189 | ret = fill_sched_features(path); |
| 190 | if (ret) |
| 191 | return 0; |
| 192 | |
| 193 | fd = open(path, O_RDWR); |
| 194 | if (fd < 0) { |
| 195 | perror(path); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | len = sizeof(buf); |
| 200 | |
| 201 | ret = read(fd, buf, len); |
| 202 | if (ret < 0) { |
| 203 | perror(path); |
| 204 | close(fd); |
| 205 | return 0; |
| 206 | } |
| 207 | if (ret >= len) |
| 208 | ret = len - 1; |
| 209 | buf[ret] = 0; |
| 210 | |
| 211 | ret = 1; |
| 212 | |
| 213 | p = strstr(buf, "HRTICK_DL"); |
| 214 | if (p && p - 3 >= buf) { |
| 215 | hrtick_dl = 1; |
| 216 | p -= 3; |
| 217 | if (strncmp(p, "NO_HRTICK_DL", 12) == 0) { |
| 218 | ret = write(fd, "HRTICK_DL", 9); |
| 219 | if (ret != 9) |
| 220 | ret = 0; |
| 221 | else |
| 222 | ret = 1; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* Backwards compatibility for kernel that only have HRTICK */ |
| 227 | if (!hrtick_dl) { |
| 228 | p = strstr(buf, "HRTICK"); |
| 229 | if (p && p - 3 >= buf) { |
| 230 | p -=3; |
| 231 | if (strncmp(p, "NO_HRTICK", 9) == 0) { |
| 232 | ret = write(fd, "HRTICK", 6); |
| 233 | if (ret != 6) |
| 234 | ret = 0; |
| 235 | else |
| 236 | ret = 1; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | close(fd); |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | static int mounted(const char *path, long magic) |
| 246 | { |
| 247 | struct statfs st_fs; |
| 248 | |
| 249 | if (statfs(path, &st_fs) < 0) |
| 250 | return -1; |
| 251 | if ((long)st_fs.f_type != magic) |
| 252 | return 0; |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | #define CGROUP_PATH "/sys/fs/cgroup" |
| 257 | #define CPUSET_PATH CGROUP_PATH "/cpuset" |
| 258 | |
| 259 | /** |
| 260 | * cgroup_mounted - test if the path /sys/fs/cgroup exists |
| 261 | * and is a supported type |
| 262 | * |
| 263 | * Returns -1 if the path does not exist |
| 264 | * Returns 0 if the path exists but is not a cgroup type |
| 265 | * Returns 1 if the path exists and supports cgroups |
| 266 | */ |
| 267 | static int cgroup_mounted(void) |
| 268 | { |
| 269 | int ret; |
| 270 | |
| 271 | ret = mounted(CGROUP_PATH, TMPFS_MAGIC); |
| 272 | if (ret == -1) |
| 273 | return -1; /* path doesn't exist */ |
| 274 | if (ret == 1) |
| 275 | return 1; /* tmpfs */ |
| 276 | ret = mounted(CGROUP_PATH, CGROUP_SUPER_MAGIC); |
| 277 | if (ret == 1) |
| 278 | return 1; /* cgroup v1 */ |
| 279 | ret = mounted(CGROUP_PATH, CGROUP2_SUPER_MAGIC); |
| 280 | if (ret == 1) |
| 281 | return 1; /* cgroup v2 */ |
| 282 | return 0; /* path exists but type is not recognized */ |
| 283 | } |
| 284 | |
| 285 | static int open_cpuset(const char *path, const char *name) |
| 286 | { |
| 287 | char buf[MAXPATH]; |
| 288 | struct stat st; |
| 289 | int ret; |
| 290 | int fd; |
| 291 | |
| 292 | buf[MAXPATH - 1] = 0; |
| 293 | snprintf(buf, MAXPATH - 1, "%s/%s", path, name); |
| 294 | |
| 295 | ret = stat(buf, &st); |
| 296 | if (ret < 0) |
| 297 | return ret; |
| 298 | |
| 299 | fd = open(buf, O_WRONLY); |
| 300 | return fd; |
| 301 | } |
| 302 | |
| 303 | static int mount_cpuset(void) |
| 304 | { |
| 305 | struct stat st; |
| 306 | int ret; |
| 307 | int fd; |
| 308 | |
| 309 | /* Check if cgroups is already mounted. */ |
| 310 | ret = cgroup_mounted(); |
| 311 | if (ret < 0) /* /sys/fs/cgroup doesn't exist */ |
| 312 | return ret; |
| 313 | |
| 314 | if (!ret) /* /sys/fs/cgroup exists, but we don't recognize the type */ |
| 315 | return -1; |
| 316 | |
| 317 | ret = stat(CPUSET_PATH, &st); |
| 318 | if (ret < 0) { |
| 319 | ret = mkdir(CPUSET_PATH, 0755); |
| 320 | if (ret < 0) |
| 321 | return ret; |
| 322 | } |
| 323 | ret = mounted(CPUSET_PATH, CGROUP_SUPER_MAGIC); |
| 324 | if (ret < 0) |
| 325 | return ret; |
| 326 | if (!ret) { |
| 327 | ret = mount("cpuset", CPUSET_PATH, "cgroup", 0, "cpuset"); |
| 328 | if (ret < 0) |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | fd = open_cpuset(CPUSET_PATH, "cpuset.cpu_exclusive"); |
| 333 | if (fd < 0) |
| 334 | return fd; |
| 335 | ret = write(fd, "1", 2); |
| 336 | close(fd); |
| 337 | |
| 338 | fd = open_cpuset(CPUSET_PATH, "cpuset.sched_load_balance"); |
| 339 | if (fd < 0) |
| 340 | return fd; |
| 341 | ret = write(fd, "0", 2); |
| 342 | if (ret < 0) { |
| 343 | close(fd); |
| 344 | return ret; |
| 345 | } |
| 346 | close(fd); |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | enum { |
| 352 | CPUSET_FL_CPU_EXCLUSIVE = (1 << 0), |
| 353 | CPUSET_FL_MEM_EXCLUSIVE = (1 << 1), |
| 354 | CPUSET_FL_ALL_TASKS = (1 << 2), |
| 355 | CPUSET_FL_TASKS = (1 << 3), |
| 356 | CPUSET_FL_CLEAR_LOADBALANCE = (1 << 4), |
| 357 | CPUSET_FL_SET_LOADBALANCE = (1 << 5), |
| 358 | CPUSET_FL_CLONE_CHILDREN = (1 << 6), |
| 359 | }; |
| 360 | |
| 361 | static void make_cpuset(const char *name, const char *cpus, |
| 362 | const char *mems, unsigned int flags, ...) |
| 363 | { |
| 364 | struct stat st; |
| 365 | char path[MAXPATH]; |
| 366 | char buf[100]; |
| 367 | va_list ap; |
| 368 | int ret; |
| 369 | int fd; |
| 370 | |
| 371 | printf("Creating cpuset '%s'\n", name); |
| 372 | snprintf(path, MAXPATH - 1, "%s/%s", CPUSET_PATH, name); |
| 373 | path[MAXPATH - 1] = 0; |
| 374 | |
| 375 | ret = mount_cpuset(); |
| 376 | if (ret < 0) |
| 377 | fatal("mount_cpuset"); |
| 378 | |
| 379 | ret = stat(path, &st); |
| 380 | if (ret < 0) { |
| 381 | ret = mkdir(path, 0755); |
| 382 | if (ret < 0) |
| 383 | fatal("mkdir"); |
| 384 | } |
| 385 | |
| 386 | fd = open_cpuset(path, "cpuset.cpus"); |
| 387 | if (fd < 0) |
| 388 | fatal("cset"); |
| 389 | ret = write(fd, cpus, strlen(cpus)); |
| 390 | close(fd); |
| 391 | if (ret < 0) |
| 392 | fatal("write cpus"); |
| 393 | |
| 394 | if (mems) { |
| 395 | fd = open_cpuset(path, "cpuset.mems"); |
| 396 | if (fd < 0) |
| 397 | fatal("open mems"); |
| 398 | ret = write(fd, mems, strlen(mems)); |
| 399 | close(fd); |
| 400 | if (ret < 0) |
| 401 | fatal("write mems"); |
| 402 | } |
| 403 | |
| 404 | if (flags & CPUSET_FL_CPU_EXCLUSIVE) { |
| 405 | fd = open_cpuset(path, "cpuset.cpu_exclusive"); |
| 406 | if (fd < 0) |
| 407 | fatal("open cpu_exclusive"); |
| 408 | ret = write(fd, "1", 2); |
| 409 | close(fd); |
| 410 | if (ret < 0) |
| 411 | fatal("write cpu_exclusive"); |
| 412 | } |
| 413 | |
| 414 | if (flags & (CPUSET_FL_CLEAR_LOADBALANCE | CPUSET_FL_SET_LOADBALANCE)) { |
| 415 | fd = open_cpuset(path, "cpuset.sched_load_balance"); |
| 416 | if (fd < 0) |
| 417 | fatal("open sched_load_balance"); |
| 418 | if (flags & CPUSET_FL_SET_LOADBALANCE) |
| 419 | ret = write(fd, "1", 2); |
| 420 | else |
| 421 | ret = write(fd, "0", 2); |
| 422 | close(fd); |
| 423 | if (ret < 0) |
| 424 | fatal("write sched_load_balance"); |
| 425 | } |
| 426 | |
| 427 | if (flags & CPUSET_FL_CLONE_CHILDREN) { |
| 428 | fd = open_cpuset(path, "cgroup.clone_children"); |
| 429 | if (fd < 0) |
| 430 | fatal("open clone_children"); |
| 431 | ret = write(fd, "1", 2); |
| 432 | close(fd); |
| 433 | if (ret < 0) |
| 434 | fatal("write clone_children"); |
| 435 | } |
| 436 | |
| 437 | |
| 438 | if (flags & CPUSET_FL_TASKS) { |
| 439 | int *pids; |
| 440 | int i; |
| 441 | |
| 442 | va_start(ap, flags); |
| 443 | |
| 444 | fd = open_cpuset(path, "tasks"); |
| 445 | if (fd < 0) |
| 446 | fatal("open tasks"); |
| 447 | |
| 448 | ret = 0; |
| 449 | pids = va_arg(ap, int *); |
| 450 | for (i = 0; pids[i]; i++) { |
| 451 | sprintf(buf, "%d ", pids[i]); |
| 452 | ret = write(fd, buf, strlen(buf)); |
| 453 | } |
| 454 | va_end(ap); |
| 455 | close(fd); |
| 456 | if (ret < 0) |
| 457 | fatal("Failed on task %d\n", pids[i]); |
| 458 | } |
| 459 | |
| 460 | if (flags & CPUSET_FL_ALL_TASKS) { |
| 461 | FILE *fp; |
| 462 | int pid; |
| 463 | |
| 464 | fd = open_cpuset(path, "tasks"); |
| 465 | |
| 466 | snprintf(path, MAXPATH - 1, "%s/tasks", CPUSET_PATH); |
| 467 | if ((fp = fopen(path, "r")) == NULL) { |
| 468 | close(fd); |
| 469 | fatal("opening cpuset tasks"); |
| 470 | } |
| 471 | |
| 472 | while (fscanf(fp, "%d", &pid) == 1) { |
| 473 | sprintf(buf, "%d", pid); |
| 474 | ret = write(fd, buf, strlen(buf)); |
| 475 | /* |
| 476 | * Tasks can come and go, the only error we care |
| 477 | * about is ENOSPC, as that means something went |
| 478 | * wrong that we did not expect. |
| 479 | */ |
| 480 | if (ret < 0 && errno == ENOSPC) { |
| 481 | fclose(fp); |
| 482 | close(fd); |
| 483 | fatal("Can not move tasks"); |
| 484 | } |
| 485 | } |
| 486 | fclose(fp); |
| 487 | close(fd); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static void destroy_cpuset(const char *name, int print) |
| 492 | { |
| 493 | struct stat st; |
| 494 | char path[MAXPATH]; |
| 495 | char buf[100]; |
| 496 | FILE *fp; |
| 497 | int pid; |
| 498 | int ret; |
| 499 | int fd; |
| 500 | int retry = 0; |
| 501 | |
| 502 | printf("Removing %s\n", name); |
| 503 | snprintf(path, MAXPATH - 1, "%s/%s", CPUSET_PATH, name); |
| 504 | path[MAXPATH - 1] = 0; |
| 505 | |
| 506 | ret = stat(path, &st); |
| 507 | if (ret < 0) |
| 508 | return; |
| 509 | |
| 510 | again: |
| 511 | strncat(path, "/tasks", MAXPATH - 1); |
| 512 | if ((fp = fopen(path, "r")) == NULL) { |
| 513 | fprintf(stderr, "Failed opening %s\n", path); |
| 514 | perror("fopen"); |
| 515 | return; |
| 516 | } |
| 517 | snprintf(path, MAXPATH - 1, "%s/tasks", CPUSET_PATH); |
| 518 | path[MAXPATH - 1] = 0; |
| 519 | |
| 520 | fd = open(path, O_WRONLY); |
| 521 | if (fd < 0) { |
| 522 | fclose(fp); |
| 523 | fprintf(stderr, "Failed opening %s\n", path); |
| 524 | perror("open"); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | while (fscanf(fp, "%d", &pid) == 1) { |
| 529 | sprintf(buf, "%d", pid); |
| 530 | if (print) |
| 531 | printf("Moving %d out of %s\n", pid, name); |
| 532 | ret = write(fd, buf, strlen(buf)); |
| 533 | if (ret < 0 && errno == ENOSPC) { |
| 534 | fclose(fp); |
| 535 | close(fd); |
| 536 | fatal("Cannot move tasks out of cpuset %s\n", name); |
| 537 | } |
| 538 | |
| 539 | |
| 540 | } |
| 541 | fclose(fp); |
| 542 | close(fd); |
| 543 | |
| 544 | snprintf(path, MAXPATH - 1, "%s/%s", CPUSET_PATH, name); |
| 545 | path[MAXPATH - 1] = 0; |
| 546 | |
| 547 | sleep(1); |
| 548 | ret = rmdir(path); |
| 549 | if (ret < 0) { |
| 550 | if (retry++ < 5) { |
| 551 | err_msg("Trying again\n"); |
| 552 | goto again; |
| 553 | } |
| 554 | err_msg_n(errno, "Failed to remove %s\n", path); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | static void teardown(void) |
| 559 | { |
| 560 | int fd; |
| 561 | int ret; |
| 562 | |
| 563 | if (all_cpus) |
| 564 | return; |
| 565 | |
| 566 | fd = open_cpuset(CPUSET_PATH, "cpuset.cpu_exclusive"); |
| 567 | if (fd >= 0) { |
| 568 | ret = write(fd, "0", 2); |
| 569 | if (ret < 0) |
| 570 | perror("cpuset.cpu_exclusive"); |
| 571 | close(fd); |
| 572 | } |
| 573 | |
| 574 | fd = open_cpuset(CPUSET_PATH, "cpuset.sched_load_balance"); |
| 575 | if (fd >= 0) { |
| 576 | ret = write(fd, "1", 2); |
| 577 | if (ret < 0) |
| 578 | perror("cpuset.sched_load_balance"); |
| 579 | close(fd); |
| 580 | } |
| 581 | |
| 582 | destroy_cpuset(CPUSET_ALL, 0); |
| 583 | destroy_cpuset(CPUSET_LOCAL, 1); |
| 584 | |
| 585 | /* close any tracer file descriptors */ |
| 586 | disable_trace_mark(); |
| 587 | |
| 588 | } |
| 589 | |
| 590 | static void usage(int error) |
| 591 | { |
| 592 | printf("cyclicdeadline V %1.2f\n", VERSION); |
| 593 | printf("Usage:\n" |
| 594 | "cyclicdeadline <options>\n\n" |
| 595 | "-a [CPUSET] --affinity Comma/hyphen separated list of CPUs to run deadline\n" |
| 596 | " tasks on. An empty CPUSET runs on all CPUs a deadline\n" |
| 597 | " task.\n" |
| 598 | "-D TIME --duration Specify a length in seconds for the test run.\n" |
| 599 | " Append 'm', 'h', or 'd' to specify minutes, hours or\n" |
| 600 | " days\n" |
| 601 | "-h --help Show this help menu.\n" |
| 602 | " --histogram=US dump a latency histogram to stdout after the run\n" |
| 603 | " US is the max latency time to be tracked in microseconds\n" |
| 604 | " This option runs all threads at the same priority.\n" |
| 605 | " --histfile=<path> dump the latency histogram to <path> instead of stdout\n" |
| 606 | "-i INTV --interval The shortest deadline for the tasks in us\n" |
| 607 | " (default 1000us).\n" |
| 608 | " --json=FILENAME write final results into FILENAME, JSON formatted\n" |
| 609 | "-s STEP --step The amount to increase the deadline for each task in us\n" |
| 610 | " (default 500us).\n" |
| 611 | "-t NUM --threads The number of threads to run as deadline (default 1).\n" |
| 612 | "-q --quiet print a summary only on exit\n" |
| 613 | "-b USEC --breaktrace=USEC send break trace command when latency > USEC\n" |
| 614 | " --tracemark write a trace mark when -b latency is exceeded\n" |
| 615 | " --debug Print debugging info for cyclicdeadline\n" |
| 616 | " --verbose Print useful information about the test\n" |
| 617 | ); |
| 618 | exit(error); |
| 619 | } |
| 620 | |
| 621 | static u64 get_time_us(void) |
| 622 | { |
| 623 | struct timespec ts; |
| 624 | u64 time; |
| 625 | |
| 626 | clock_gettime(CLOCK_MONOTONIC_RAW, &ts); |
| 627 | time = ts.tv_sec * 1000000; |
| 628 | time += ts.tv_nsec / 1000; |
| 629 | |
| 630 | return time; |
| 631 | } |
| 632 | |
| 633 | static void print_hist(FILE *fp, struct sched_data *sd, int nthreads) |
| 634 | { |
| 635 | int i; |
| 636 | unsigned long maxmax, alloverflows; |
| 637 | |
| 638 | fprintf(fp, "# Histogram\n"); |
| 639 | for (i = 0; i < histogram; i++) { |
| 640 | unsigned long flags = 0; |
| 641 | char buf[64]; |
| 642 | |
| 643 | snprintf(buf, sizeof(buf), "%06d ", i); |
| 644 | hset_print_bucket(&hset, fp, buf, i, flags); |
| 645 | } |
| 646 | fprintf(fp, "# Min Latencies:"); |
| 647 | for (i = 0; i < nthreads; i++) |
| 648 | fprintf(fp, " %05lu", sd[i].stat.min); |
| 649 | fprintf(fp, "\n"); |
| 650 | fprintf(fp, "# Avg Latencies:"); |
| 651 | for (i = 0; i < nthreads; i++) |
| 652 | fprintf(fp, " %05lu", sd[i].stat.cycles ? |
| 653 | (long)(sd[i].stat.avg/sd[i].stat.cycles) : 0); |
| 654 | fprintf(fp, "\n"); |
| 655 | fprintf(fp, "# Max Latencies:"); |
| 656 | maxmax = 0; |
| 657 | for (i = 0; i < nthreads; i++) { |
| 658 | fprintf(fp, " %05lu", sd[i].stat.max); |
| 659 | if (sd[i].stat.max > maxmax) |
| 660 | maxmax = sd[i].stat.max; |
| 661 | } |
| 662 | fprintf(fp, "\n"); |
| 663 | fprintf(fp, "# Histogram Overflows:"); |
| 664 | alloverflows = 0; |
| 665 | for (i = 0; i < nthreads; i++) { |
| 666 | fprintf(fp, " %05lu", sd[i].stat.hist->oflow_count); |
| 667 | alloverflows += sd[i].stat.hist->oflow_count; |
| 668 | } |
| 669 | fprintf(fp, "\n"); |
| 670 | |
| 671 | fprintf(fp, "# Histogram Overflow at cycle number:\n"); |
| 672 | for (i = 0; i < nthreads; i++) { |
| 673 | fprintf(fp, "# Thread %d: ", i); |
| 674 | hist_print_oflows(sd[i].stat.hist, fp); |
| 675 | fprintf(fp, "\n"); |
| 676 | } |
| 677 | |
| 678 | fprintf(fp, "\n"); |
| 679 | } |
| 680 | |
| 681 | static void print_stat(FILE *fp, struct sched_data *sd, int index, int quiet) |
| 682 | { |
| 683 | struct thread_stat *stat = &sd->stat; |
| 684 | char *fmt; |
| 685 | |
| 686 | if (quiet) |
| 687 | return; |
| 688 | |
| 689 | if (use_nsecs) |
| 690 | fmt = "T:%2d (%5d) I:%ld C:%7lu Min:%7ld Act:%8ld Avg:%8ld Max:%8ld\n"; |
| 691 | else |
| 692 | fmt = "T:%2d (%5d) I:%ld C:%7lu Min:%7ld Act:%5ld Avg:%5ld Max:%8ld\n"; |
| 693 | |
| 694 | fprintf(fp, fmt, index, stat->tid, |
| 695 | sd->deadline_us, stat->cycles, stat->min, stat->act, |
| 696 | stat->cycles ? |
| 697 | (long)(stat->avg/stat->cycles) : 0, stat->max); |
| 698 | } |
| 699 | |
| 700 | static u64 do_runtime(struct sched_data *sd, u64 period) |
| 701 | { |
| 702 | struct thread_stat *stat = &sd->stat; |
| 703 | u64 next_period = period + sd->deadline_us; |
| 704 | u64 now = get_time_us(); |
| 705 | u64 diff; |
| 706 | |
| 707 | if (now < period) { |
| 708 | u64 delta = period - now; |
| 709 | /* |
| 710 | * The period could be off due to other deadline tasks |
| 711 | * preempting us when we started. If that's the case then |
| 712 | * adjust the current period. |
| 713 | */ |
| 714 | tracemark("Adjusting period: now: %lld period: %lld delta:%lld%s\n", |
| 715 | now, period, delta, delta > sd->deadline_us / 2 ? |
| 716 | " HUGE ADJUSTMENT" : ""); |
| 717 | period = now; |
| 718 | next_period = period + sd->deadline_us; |
| 719 | } |
| 720 | |
| 721 | tracemark("start at %lld off=%lld (period=%lld next=%lld)\n", |
| 722 | now, now - period, period, next_period); |
| 723 | |
| 724 | |
| 725 | diff = now - period; |
| 726 | if (diff > stat->max) |
| 727 | stat->max = diff; |
| 728 | if (!stat->min || diff < stat->min) |
| 729 | stat->min = diff; |
| 730 | stat->act = diff; |
| 731 | stat->avg += (double) diff; |
| 732 | |
| 733 | if (histogram) |
| 734 | hist_sample(stat->hist, diff); |
| 735 | |
| 736 | stat->cycles++; |
| 737 | |
| 738 | return next_period; |
| 739 | } |
| 740 | |
| 741 | void *run_deadline(void *data) |
| 742 | { |
| 743 | struct sched_data *sd = data; |
| 744 | struct thread_stat *stat = &sd->stat; |
| 745 | struct sched_attr attr; |
| 746 | long tid = gettid(); |
| 747 | u64 period; |
| 748 | int ret; |
| 749 | |
| 750 | debug(debug_enable, "deadline thread %ld\n", tid); |
| 751 | // set up for each measurment thread |
| 752 | stat->tid = tid; |
| 753 | |
| 754 | ret = sched_getattr(0, &attr, sizeof(attr), 0); |
| 755 | if (ret < 0) { |
| 756 | err_msg_n(errno, "[%ld]", tid); |
| 757 | shutdown = 1; |
| 758 | pthread_barrier_wait(&barrier); |
| 759 | pthread_exit("Failed sched_getattr"); |
| 760 | return NULL; |
| 761 | } |
| 762 | |
| 763 | attr.sched_policy = SCHED_DEADLINE; |
| 764 | attr.sched_runtime = sd->runtime_us * 1000; |
| 765 | attr.sched_deadline = sd->deadline_us * 1000; |
| 766 | |
| 767 | debug(debug_enable, "thread[%d] runtime=%lldus deadline=%lldus\n", |
| 768 | gettid(), sd->runtime_us, sd->deadline_us); |
| 769 | |
| 770 | ret = sched_setattr(0, &attr, 0); |
| 771 | if (ret < 0) { |
| 772 | err_msg_n(errno, "[%ld]", tid); |
| 773 | shutdown = 1; |
| 774 | pthread_barrier_wait(&barrier); |
| 775 | pthread_exit("Failed sched_setattr"); |
| 776 | return NULL; |
| 777 | } |
| 778 | |
| 779 | pthread_barrier_wait(&barrier); |
| 780 | |
| 781 | sched_yield(); |
| 782 | period = get_time_us(); |
| 783 | |
| 784 | while (!shutdown) { |
| 785 | period = do_runtime(sd, period); |
| 786 | if (tracelimit && (stat->max > tracelimit)) { |
| 787 | shutdown++; |
| 788 | pthread_mutex_lock(&break_thread_id_lock); |
| 789 | if (break_thread_id == 0) { |
| 790 | break_thread_id = stat->tid; |
| 791 | break_thread_value = stat->max; |
| 792 | tracemark("hit latency threshold (%lld > %d)", |
| 793 | (unsigned long long) stat->max, tracelimit); |
| 794 | tracing_stop(); |
| 795 | } |
| 796 | pthread_mutex_unlock(&break_thread_id_lock); |
| 797 | break; |
| 798 | } |
| 799 | sched_yield(); |
| 800 | } |
| 801 | ret = sched_getattr(0, &attr, sizeof(attr), 0); |
| 802 | if (ret < 0) { |
| 803 | err_msg_n(errno, "sched_getattr"); |
| 804 | pthread_exit("Failed second sched_getattr"); |
| 805 | } |
| 806 | |
| 807 | return NULL; |
| 808 | } |
| 809 | |
| 810 | struct cpu_list { |
| 811 | struct cpu_list *next; |
| 812 | int start_cpu; |
| 813 | int end_cpu; |
| 814 | }; |
| 815 | |
| 816 | static void add_cpus(struct cpu_list **cpu_list, int start_cpu, int end_cpu) |
| 817 | { |
| 818 | struct cpu_list *list; |
| 819 | |
| 820 | while (*cpu_list && (*cpu_list)->end_cpu + 1 < start_cpu) |
| 821 | cpu_list = &(*cpu_list)->next; |
| 822 | |
| 823 | if (!*cpu_list) { |
| 824 | *cpu_list = malloc(sizeof(struct cpu_list)); |
| 825 | (*cpu_list)->start_cpu = start_cpu; |
| 826 | (*cpu_list)->end_cpu = end_cpu; |
| 827 | (*cpu_list)->next = NULL; |
| 828 | return; |
| 829 | } |
| 830 | |
| 831 | /* Look to concatinate */ |
| 832 | if (end_cpu > (*cpu_list)->start_cpu && |
| 833 | start_cpu <= (*cpu_list)->end_cpu + 1) { |
| 834 | if (start_cpu < (*cpu_list)->start_cpu) |
| 835 | (*cpu_list)->start_cpu = start_cpu; |
| 836 | list = (*cpu_list)->next; |
| 837 | while (list && list->start_cpu <= end_cpu + 1) { |
| 838 | (*cpu_list)->end_cpu = list->end_cpu; |
| 839 | (*cpu_list)->next = list->next; |
| 840 | free(list); |
| 841 | list = (*cpu_list)->next; |
| 842 | } |
| 843 | if ((*cpu_list)->end_cpu < end_cpu) |
| 844 | (*cpu_list)->end_cpu = end_cpu; |
| 845 | return; |
| 846 | } |
| 847 | |
| 848 | /* Check for overlaps */ |
| 849 | if (end_cpu >= (*cpu_list)->start_cpu - 1) { |
| 850 | (*cpu_list)->start_cpu = start_cpu; |
| 851 | return; |
| 852 | } |
| 853 | |
| 854 | list = malloc(sizeof(struct cpu_list)); |
| 855 | list->start_cpu = start_cpu; |
| 856 | list->end_cpu = end_cpu; |
| 857 | list->next = (*cpu_list)->next; |
| 858 | (*cpu_list)->next = list; |
| 859 | } |
| 860 | |
| 861 | static int count_cpus(struct cpu_list *cpu_list) |
| 862 | { |
| 863 | struct cpu_list *list; |
| 864 | int cpus = 0; |
| 865 | int fail = 0; |
| 866 | |
| 867 | while (cpu_list) { |
| 868 | list = cpu_list; |
| 869 | cpus += (list->end_cpu - list->start_cpu) + 1; |
| 870 | if (list->end_cpu >= cpu_count) |
| 871 | fail = 1; |
| 872 | cpu_list = list->next; |
| 873 | free(list); |
| 874 | } |
| 875 | return fail ? -1 : cpus; |
| 876 | } |
| 877 | |
| 878 | static char *append_cpus(char *buf, int start, int end, |
| 879 | const char *comma, int *total) |
| 880 | { |
| 881 | int len; |
| 882 | |
| 883 | if (start == end) { |
| 884 | len = snprintf(NULL, 0, "%s%d", comma, start); |
| 885 | buf = realloc(buf, *total + len + 1); |
| 886 | buf[*total] = 0; |
| 887 | snprintf(buf + *total, len + 1, "%s%d", comma, start); |
| 888 | } else { |
| 889 | len = snprintf(NULL, 0, "%s%d-%d", comma, start, end); |
| 890 | buf = realloc(buf, *total + len + 1); |
| 891 | buf[*total] = 0; |
| 892 | snprintf(buf + *total, len + 1, "%s%d-%d", comma, |
| 893 | start, end); |
| 894 | } |
| 895 | *total += len; |
| 896 | return buf; |
| 897 | } |
| 898 | |
| 899 | static void make_new_list(struct cpu_list *cpu_list, char **buf) |
| 900 | { |
| 901 | char *comma = ""; |
| 902 | int total = 0; |
| 903 | |
| 904 | while (cpu_list) { |
| 905 | *buf = append_cpus(*buf, cpu_list->start_cpu, cpu_list->end_cpu, |
| 906 | comma, &total); |
| 907 | comma = ","; |
| 908 | cpu_list = cpu_list->next; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | static void make_other_cpu_list(const char *setcpu, char **cpus) |
| 913 | { |
| 914 | const char *p = setcpu; |
| 915 | const char *comma = ""; |
| 916 | int curr_cpu = 0; |
| 917 | int cpu; |
| 918 | int total = 0; |
| 919 | |
| 920 | while (*p && curr_cpu < cpu_count) { |
| 921 | cpu = atoi(p); |
| 922 | if (cpu > curr_cpu) { |
| 923 | *cpus = append_cpus(*cpus, curr_cpu, cpu - 1, |
| 924 | comma, &total); |
| 925 | comma = ","; |
| 926 | } |
| 927 | while (isdigit(*p)) |
| 928 | p++; |
| 929 | if (*p == '-') { |
| 930 | p++; |
| 931 | cpu = atoi(p); |
| 932 | while (isdigit(*p)) |
| 933 | p++; |
| 934 | } |
| 935 | curr_cpu = cpu + 1; |
| 936 | if (*p) |
| 937 | p++; |
| 938 | } |
| 939 | |
| 940 | if (curr_cpu < cpu_count) { |
| 941 | *cpus = append_cpus(*cpus, curr_cpu, cpu_count - 1, |
| 942 | comma, &total); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | static int calc_nr_cpus(const char *setcpu, char **buf) |
| 947 | { |
| 948 | struct cpu_list *cpu_list = NULL; |
| 949 | const char *p; |
| 950 | int end_cpu; |
| 951 | int cpu; |
| 952 | |
| 953 | for (p = setcpu; *p; ) { |
| 954 | cpu = atoi(p); |
| 955 | if (cpu < 0 || (!cpu && *p != '0')) |
| 956 | goto err; |
| 957 | |
| 958 | while (isdigit(*p)) |
| 959 | p++; |
| 960 | if (*p == '-') { |
| 961 | p++; |
| 962 | end_cpu = atoi(p); |
| 963 | if (end_cpu < cpu || (!end_cpu && *p != '0')) |
| 964 | goto err; |
| 965 | while (isdigit(*p)) |
| 966 | p++; |
| 967 | } else |
| 968 | end_cpu = cpu; |
| 969 | |
| 970 | add_cpus(&cpu_list, cpu, end_cpu); |
| 971 | if (*p == ',') |
| 972 | p++; |
| 973 | } |
| 974 | |
| 975 | make_new_list(cpu_list, buf); |
| 976 | return count_cpus(cpu_list); |
| 977 | err: |
| 978 | /* Frees the list */ |
| 979 | count_cpus(cpu_list); |
| 980 | return -1; |
| 981 | } |
| 982 | |
| 983 | static void sighand(int sig) |
| 984 | { |
| 985 | shutdown = 1; |
| 986 | } |
| 987 | |
| 988 | static const char *join_thread(pthread_t *thread) |
| 989 | { |
| 990 | void *result; |
| 991 | |
| 992 | pthread_join(*thread, &result); |
| 993 | return result; |
| 994 | } |
| 995 | |
| 996 | static void loop(struct sched_data *sched_data, int nr_threads) |
| 997 | { |
| 998 | int i; |
| 999 | |
| 1000 | while (!shutdown) { |
| 1001 | for (i = 0; i < nr_threads; i++) |
| 1002 | print_stat(stdout, &sched_data[i], i, quiet); |
| 1003 | usleep(10000); |
| 1004 | if (!quiet) |
| 1005 | printf("\033[%dA", nr_threads); |
| 1006 | } |
| 1007 | usleep(10000); |
| 1008 | if (!quiet) { |
| 1009 | printf("\033[%dB", nr_threads + 2); |
| 1010 | } else if (!histogram) { |
| 1011 | for (i = 0; i < nr_threads; ++i) |
| 1012 | print_stat(stdout, &sched_data[i], i, 0); |
| 1013 | } |
| 1014 | |
| 1015 | if (histogram) { |
| 1016 | FILE *out = histfile ? histfile : stdout; |
| 1017 | |
| 1018 | print_hist(out, sched_data, nr_threads); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | static void write_stats(FILE *f, void *data) |
| 1023 | { |
| 1024 | struct sched_data *sd = data; |
| 1025 | struct thread_stat *s; |
| 1026 | int i; |
| 1027 | |
| 1028 | fprintf(f, " \"num_threads\": %d,\n", nr_threads); |
| 1029 | fprintf(f, " \"resolution_in_ns\": %u,\n", use_nsecs); |
| 1030 | fprintf(f, " \"thread\": {\n"); |
| 1031 | for (i = 0; i < nr_threads; i++) { |
| 1032 | s = &sd[i].stat; |
| 1033 | fprintf(f, " \"%u\": {\n", i); |
| 1034 | if (s->hist) { |
| 1035 | fprintf(f, " \"histogram\": {"); |
| 1036 | hist_print_json(s->hist, f); |
| 1037 | fprintf(f, " },\n"); |
| 1038 | } |
| 1039 | fprintf(f, " \"cycles\": %ld,\n", s->cycles); |
| 1040 | fprintf(f, " \"min\": %ld,\n", s->min); |
| 1041 | fprintf(f, " \"max\": %ld,\n", s->max); |
| 1042 | fprintf(f, " \"avg\": %.2f\n", s->avg/s->cycles); |
| 1043 | fprintf(f, " }%s\n", i == nr_threads - 1 ? "" : ","); |
| 1044 | } |
| 1045 | fprintf(f, " }\n"); |
| 1046 | } |
| 1047 | |
| 1048 | enum options_values { |
| 1049 | OPT_AFFINITY=1, OPT_DURATION, OPT_HELP, OPT_INTERVAL, |
| 1050 | OPT_JSON, OPT_STEP, OPT_THREADS, OPT_QUIET, |
| 1051 | OPT_BREAKTRACE, OPT_TRACEMARK, OPT_INFO, OPT_DEBUG, |
| 1052 | OPT_HISTOGRAM, OPT_HISTFILE |
| 1053 | }; |
| 1054 | |
| 1055 | int main(int argc, char **argv) |
| 1056 | { |
| 1057 | struct sched_data *sched_data; |
| 1058 | struct sched_data *sd; |
| 1059 | const char *res; |
| 1060 | const char *setcpu = NULL; |
| 1061 | char *setcpu_buf = NULL; |
| 1062 | char *allcpu_buf = NULL; |
| 1063 | pthread_t *thread; |
| 1064 | unsigned int interval = 1000; |
| 1065 | unsigned int step = 500; |
| 1066 | int percent = 60; |
| 1067 | int duration = 0; |
| 1068 | u64 runtime; |
| 1069 | u64 start_period; |
| 1070 | u64 end_period; |
| 1071 | int nr_cpus; |
| 1072 | int i; |
| 1073 | int c; |
| 1074 | int ret; |
| 1075 | |
| 1076 | rt_init(argc, argv); |
| 1077 | |
| 1078 | cpu_count = sysconf(_SC_NPROCESSORS_CONF); |
| 1079 | if (cpu_count < 1) |
| 1080 | err_quit("Can not calculate number of CPUS\n"); |
| 1081 | |
| 1082 | for (;;) { |
| 1083 | static struct option options[] = { |
| 1084 | { "affinity", optional_argument, NULL, OPT_AFFINITY }, |
| 1085 | { "duration", required_argument, NULL, OPT_DURATION }, |
| 1086 | { "help", no_argument, NULL, OPT_HELP }, |
| 1087 | { "interval", required_argument, NULL, OPT_INTERVAL }, |
| 1088 | { "json", required_argument, NULL, OPT_JSON }, |
| 1089 | { "step", required_argument, NULL, OPT_STEP }, |
| 1090 | { "threads", required_argument, NULL, OPT_THREADS }, |
| 1091 | { "quiet", no_argument, NULL, OPT_QUIET }, |
| 1092 | { "breaktrace", required_argument, NULL, OPT_BREAKTRACE }, |
| 1093 | { "tracemark", no_argument, NULL, OPT_TRACEMARK }, |
| 1094 | { "verbose", no_argument, NULL, OPT_INFO}, |
| 1095 | { "debug", no_argument, NULL, OPT_DEBUG}, |
| 1096 | { "histogram", required_argument, NULL, OPT_HISTOGRAM }, |
| 1097 | { "histfile", required_argument, NULL, OPT_HISTFILE }, |
| 1098 | { NULL, 0, NULL, 0 }, |
| 1099 | }; |
| 1100 | c = getopt_long(argc, argv, "a::c:D:hi:s:t:b:q", options, NULL); |
| 1101 | if (c == -1) |
| 1102 | break; |
| 1103 | switch (c) { |
| 1104 | case OPT_AFFINITY: |
| 1105 | case 'a': |
| 1106 | case 'c': |
| 1107 | if (!nr_threads) |
| 1108 | nr_threads = cpu_count; |
| 1109 | if (optarg) |
| 1110 | setcpu = optarg; |
| 1111 | else if (optind < argc) |
| 1112 | setcpu = argv[optind]; |
| 1113 | else |
| 1114 | all_cpus = 1; |
| 1115 | break; |
| 1116 | case OPT_INTERVAL: |
| 1117 | case 'i': |
| 1118 | interval = atoi(optarg); |
| 1119 | break; |
| 1120 | case OPT_JSON: |
| 1121 | strncpy(jsonfile, optarg, strnlen(optarg, MAX_PATH-1)); |
| 1122 | break; |
| 1123 | case OPT_STEP: |
| 1124 | case 's': |
| 1125 | step = atoi(optarg); |
| 1126 | break; |
| 1127 | case OPT_THREADS: |
| 1128 | case 't': |
| 1129 | nr_threads = atoi(optarg); |
| 1130 | break; |
| 1131 | case OPT_DURATION: |
| 1132 | case 'D': |
| 1133 | duration = parse_time_string(optarg); |
| 1134 | break; |
| 1135 | case 'b': |
| 1136 | case OPT_BREAKTRACE: |
| 1137 | tracelimit = atoi(optarg); |
| 1138 | break; |
| 1139 | case OPT_QUIET: |
| 1140 | case 'q': |
| 1141 | quiet = 1; |
| 1142 | break; |
| 1143 | case OPT_HELP: |
| 1144 | case 'h': |
| 1145 | usage(0); |
| 1146 | break; |
| 1147 | case OPT_TRACEMARK: |
| 1148 | trace_marker = 1; |
| 1149 | break; |
| 1150 | case OPT_INFO: |
| 1151 | info_enable = 1; |
| 1152 | break; |
| 1153 | case OPT_DEBUG: |
| 1154 | debug_enable = 1; |
| 1155 | break; |
| 1156 | case OPT_HISTOGRAM: |
| 1157 | histogram = atoi(optarg); |
| 1158 | if (histogram <= 0 || histogram > HIST_MAX) |
| 1159 | usage(1); |
| 1160 | break; |
| 1161 | case OPT_HISTFILE: |
| 1162 | histfile = fopen(optarg, "w"); |
| 1163 | if (!histfile) |
| 1164 | fatal("Couldn\'t open histfile %s: %s\n", |
| 1165 | optarg, strerror(errno)); |
| 1166 | break; |
| 1167 | default: |
| 1168 | usage(1); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | if (!nr_threads) |
| 1173 | nr_threads = 1; |
| 1174 | |
| 1175 | if (setcpu) { |
| 1176 | nr_cpus = calc_nr_cpus(setcpu, &setcpu_buf); |
| 1177 | if (nr_cpus < 0 || nr_cpus > cpu_count) |
| 1178 | fatal("Invalid cpu input '%s'\n", setcpu); |
| 1179 | } else |
| 1180 | nr_cpus = cpu_count; |
| 1181 | |
| 1182 | if (!all_cpus && cpu_count == nr_cpus) { |
| 1183 | printf("Using all CPUS\n"); |
| 1184 | all_cpus = 1; |
| 1185 | } |
| 1186 | |
| 1187 | /* Default cpu to use is the last one */ |
| 1188 | if (!all_cpus && !setcpu) { |
| 1189 | setcpu_buf = malloc(12); |
| 1190 | if (!setcpu_buf) |
| 1191 | fatal("malloc"); |
| 1192 | sprintf(setcpu_buf, "%d", cpu_count - 1); |
| 1193 | } |
| 1194 | |
| 1195 | setcpu = setcpu_buf; |
| 1196 | |
| 1197 | if (setcpu) |
| 1198 | make_other_cpu_list(setcpu, &allcpu_buf); |
| 1199 | |
| 1200 | if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1) |
| 1201 | warn("mlockall"); |
| 1202 | |
| 1203 | if (tracelimit && trace_marker) |
| 1204 | enable_trace_mark(); |
| 1205 | |
| 1206 | thread = calloc(nr_threads, sizeof(*thread)); |
| 1207 | sched_data = calloc(nr_threads, sizeof(*sched_data)); |
| 1208 | if (!thread || !sched_data) |
| 1209 | fatal("allocating threads"); |
| 1210 | |
| 1211 | if (histogram && hset_init(&hset, nr_threads, 1, histogram, histogram)) |
| 1212 | fatal("failed to allocate histogram of size %d for %d threads\n", |
| 1213 | histogram, nr_threads); |
| 1214 | |
| 1215 | if (nr_threads > nr_cpus) { |
| 1216 | /* |
| 1217 | * More threads than CPUs, then have the total be |
| 1218 | * no more than 80 percent. |
| 1219 | */ |
| 1220 | percent = nr_cpus * 80 / nr_threads; |
| 1221 | } |
| 1222 | |
| 1223 | /* Set up the data while still in SCHED_FIFO */ |
| 1224 | for (i = 0; i < nr_threads; i++) { |
| 1225 | sd = &sched_data[i]; |
| 1226 | /* |
| 1227 | * Interval is the deadline/period |
| 1228 | * The runtime is the percentage of that period. |
| 1229 | */ |
| 1230 | runtime = interval * percent / 100; |
| 1231 | |
| 1232 | if (runtime < 2000) { |
| 1233 | /* |
| 1234 | * If the runtime is less than 2ms, then we better |
| 1235 | * have HRTICK enabled. |
| 1236 | */ |
| 1237 | if (!setup_hr_tick()) |
| 1238 | fatal("For less than 2ms run times, you need to\n" |
| 1239 | "have HRTICK enabled in debugfs/sched_features\n"); |
| 1240 | } |
| 1241 | sd->runtime_us = runtime; |
| 1242 | sd->deadline_us = interval; |
| 1243 | |
| 1244 | if (histogram) |
| 1245 | sd->stat.hist = &hset.histos[i]; |
| 1246 | |
| 1247 | info(info_enable, "interval: %lld:%lld\n", sd->runtime_us, sd->deadline_us); |
| 1248 | |
| 1249 | /* Make sure that we can make our deadlines */ |
| 1250 | start_period = get_time_us(); |
| 1251 | do_runtime(sd, start_period); |
| 1252 | end_period = get_time_us(); |
| 1253 | if (end_period - start_period > sd->runtime_us) |
| 1254 | fatal("Failed to perform task within runtime: Missed by %lld us\n", |
| 1255 | end_period - start_period - sd->runtime_us); |
| 1256 | |
| 1257 | info(info_enable, " Tested at %lldus of %lldus\n", |
| 1258 | end_period - start_period, sd->runtime_us); |
| 1259 | |
| 1260 | interval += step; |
| 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | pthread_barrier_init(&barrier, NULL, nr_threads + 1); |
| 1265 | |
| 1266 | for (i = 0; i < nr_threads; i++) { |
| 1267 | sd = &sched_data[i]; |
| 1268 | pthread_create(&thread[i], NULL, run_deadline, sd); |
| 1269 | } |
| 1270 | |
| 1271 | atexit(teardown); |
| 1272 | |
| 1273 | if (shutdown) |
| 1274 | fatal("failed to setup child threads at step 1\n"); |
| 1275 | |
| 1276 | if (!all_cpus) { |
| 1277 | int *pids; |
| 1278 | |
| 1279 | make_cpuset(CPUSET_ALL, allcpu_buf, "0", |
| 1280 | CPUSET_FL_SET_LOADBALANCE | |
| 1281 | CPUSET_FL_CLONE_CHILDREN | |
| 1282 | CPUSET_FL_ALL_TASKS); |
| 1283 | |
| 1284 | pids = calloc(nr_threads + 1, sizeof(int)); |
| 1285 | if (!pids) |
| 1286 | fatal("Allocating pids"); |
| 1287 | |
| 1288 | for (i = 0; i < nr_threads; i++) |
| 1289 | pids[i] = sched_data[i].stat.tid; |
| 1290 | |
| 1291 | make_cpuset(CPUSET_LOCAL, setcpu, "0", |
| 1292 | CPUSET_FL_CPU_EXCLUSIVE | |
| 1293 | CPUSET_FL_SET_LOADBALANCE | |
| 1294 | CPUSET_FL_CLONE_CHILDREN | |
| 1295 | CPUSET_FL_TASKS, pids); |
| 1296 | |
| 1297 | ret = system("cat /sys/fs/cgroup/cpuset/my_cpuset/tasks"); |
| 1298 | if (ret < 0) |
| 1299 | perror("system call failed"); |
| 1300 | } |
| 1301 | |
| 1302 | debug(debug_enable, "main thread %d\n", gettid()); |
| 1303 | |
| 1304 | if (shutdown) |
| 1305 | fatal("failed to setup child threads at step 2"); |
| 1306 | |
| 1307 | pthread_barrier_wait(&barrier); |
| 1308 | |
| 1309 | signal(SIGINT, sighand); |
| 1310 | signal(SIGTERM, sighand); |
| 1311 | signal(SIGALRM, sighand); |
| 1312 | |
| 1313 | if (duration) |
| 1314 | alarm(duration); |
| 1315 | |
| 1316 | loop(sched_data, nr_threads); |
| 1317 | |
| 1318 | if (tracelimit) { |
| 1319 | if (break_thread_id) { |
| 1320 | printf("# Break thread: %d\n", break_thread_id); |
| 1321 | printf("# Break value: %llu\n", (unsigned long long)break_thread_value); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | for (i = 0; i < nr_threads; i++) { |
| 1326 | |
| 1327 | sd = &sched_data[i]; |
| 1328 | |
| 1329 | res = join_thread(&thread[i]); |
| 1330 | if (res) { |
| 1331 | warn("Thread %d failed: %s\n", i, res); |
| 1332 | continue; |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | if (strlen(jsonfile) != 0) |
| 1337 | rt_write_json(jsonfile, 0, write_stats, sched_data); |
| 1338 | |
| 1339 | if (setcpu_buf) |
| 1340 | free(setcpu_buf); |
| 1341 | free(thread); |
| 1342 | free(sched_data); |
| 1343 | if (histfile) |
| 1344 | fclose(histfile); |
| 1345 | hset_destroy(&hset); |
| 1346 | |
| 1347 | return 0; |
| 1348 | } |