| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Copyright (C) 2009 Carsten Emde <[email protected]> |
| 4 |
* Copyright (C) 2010 Clark Williams <[email protected]> |
| 5 |
* Copyright (C) 2015 John Kacur <[email protected]> |
| 6 |
* |
| 7 |
* based on functions from cyclictest that has |
| 8 |
* (C) 2008-2009 Clark Williams <[email protected]> |
| 9 |
* (C) 2005-2007 Thomas Gleixner <[email protected]> |
| 10 |
*/ |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
#include <sched.h> |
| 15 |
#include <stdarg.h> |
| 16 |
#include <errno.h> |
| 17 |
#include <ctype.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#include <sys/types.h> |
| 20 |
#include <sys/stat.h> |
| 21 |
#include <unistd.h> |
| 22 |
#include <sys/syscall.h> /* For SYS_gettid definitions */ |
| 23 |
#include <sys/utsname.h> |
| 24 |
#include <time.h> |
| 25 |
#include <sys/time.h> |
| 26 |
|
| 27 |
#include "rt-utils.h" |
| 28 |
#include "rt-sched.h" |
| 29 |
#include "rt-error.h" |
| 30 |
|
| 31 |
#define TRACEBUFSIZ 1024 |
| 32 |
#define MAX_COMMAND_LINE 4096 |
| 33 |
#define MAX_TS_SIZE 64 |
| 34 |
|
| 35 |
static char tracefs_prefix[MAX_PATH]; |
| 36 |
static int trace_fd = -1; |
| 37 |
static int tracemark_fd = -1; |
| 38 |
static __thread char tracebuf[TRACEBUFSIZ]; |
| 39 |
static char test_cmdline[MAX_COMMAND_LINE]; |
| 40 |
static char ts_start[MAX_TS_SIZE]; |
| 41 |
|
| 42 |
/* |
| 43 |
* Finds the tracing directory |
| 44 |
*/ |
| 45 |
char *get_tracefs_prefix(void) |
| 46 |
{ |
| 47 |
char type[100]; |
| 48 |
FILE *fp; |
| 49 |
int found = 0; |
| 50 |
struct stat s; |
| 51 |
|
| 52 |
if (tracefs_prefix[0] != '\0') |
| 53 |
goto out; |
| 54 |
|
| 55 |
/* look in the "standard" mount point first */ |
| 56 |
if ((stat("/sys/kernel/tracing", &s) == 0) && S_ISDIR(s.st_mode)) { |
| 57 |
strcpy(tracefs_prefix, "/sys/kernel/tracing/"); |
| 58 |
goto out; |
| 59 |
} |
| 60 |
|
| 61 |
/* oh well, parse /proc/mounts and see if it's there */ |
| 62 |
if ((fp = fopen("/proc/mounts", "r")) == NULL) |
| 63 |
goto out; |
| 64 |
|
| 65 |
while (fscanf(fp, "%*s %" |
| 66 |
STR(MAX_PATH) |
| 67 |
"s %99s %*s %*d %*d\n", |
| 68 |
tracefs_prefix, type) == 2) { |
| 69 |
if (strcmp(type, "tracefs") == 0) { |
| 70 |
found = 1; |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
fclose(fp); |
| 75 |
|
| 76 |
if (!found) { |
| 77 |
tracefs_prefix[0] = '\0'; |
| 78 |
goto out; |
| 79 |
} |
| 80 |
out: |
| 81 |
return tracefs_prefix; |
| 82 |
} |
| 83 |
|
| 84 |
static char **tracer_list; |
| 85 |
static char *tracer_buffer; |
| 86 |
static int num_tracers; |
| 87 |
#define CHUNKSZ 1024 |
| 88 |
|
| 89 |
/* |
| 90 |
* return a list of the tracers configured into the running kernel |
| 91 |
*/ |
| 92 |
|
| 93 |
int get_tracers(char ***list) |
| 94 |
{ |
| 95 |
int ret; |
| 96 |
FILE *fp; |
| 97 |
char buffer[CHUNKSZ]; |
| 98 |
char *prefix = get_tracefs_prefix(); |
| 99 |
char *tmpbuf = NULL; |
| 100 |
char *ptr; |
| 101 |
int tmpsz = 0; |
| 102 |
|
| 103 |
/* if we've already parse it, return what we have */ |
| 104 |
if (tracer_list) { |
| 105 |
*list = tracer_list; |
| 106 |
return num_tracers; |
| 107 |
} |
| 108 |
|
| 109 |
/* open the tracing file available_tracers */ |
| 110 |
sprintf(buffer, "%savailable_tracers", prefix); |
| 111 |
if ((fp = fopen(buffer, "r")) == NULL) |
| 112 |
fatal("Can't open %s for reading\n", buffer); |
| 113 |
|
| 114 |
/* allocate initial buffer */ |
| 115 |
ptr = tmpbuf = malloc(CHUNKSZ); |
| 116 |
if (ptr == NULL) |
| 117 |
fatal("error allocating initial space for tracer list\n"); |
| 118 |
|
| 119 |
/* read in the list of available tracers */ |
| 120 |
while ((ret = fread(buffer, sizeof(char), CHUNKSZ, fp))) { |
| 121 |
if ((ptr+ret+1) > (tmpbuf+tmpsz)) { |
| 122 |
tmpbuf = realloc(tmpbuf, tmpsz + CHUNKSZ); |
| 123 |
if (tmpbuf == NULL) |
| 124 |
fatal("error allocating space for list of valid tracers\n"); |
| 125 |
tmpsz += CHUNKSZ; |
| 126 |
} |
| 127 |
strncpy(ptr, buffer, ret); |
| 128 |
ptr += ret; |
| 129 |
} |
| 130 |
fclose(fp); |
| 131 |
if (tmpsz == 0) |
| 132 |
fatal("error reading available tracers\n"); |
| 133 |
|
| 134 |
tracer_buffer = tmpbuf; |
| 135 |
|
| 136 |
/* get a buffer for the pointers to tracers */ |
| 137 |
if (!(tracer_list = malloc(sizeof(char *)))) |
| 138 |
fatal("error allocatinging tracer list buffer\n"); |
| 139 |
|
| 140 |
/* parse the buffer */ |
| 141 |
ptr = strtok(tmpbuf, " \t\n\r"); |
| 142 |
do { |
| 143 |
tracer_list[num_tracers++] = ptr; |
| 144 |
tracer_list = realloc(tracer_list, sizeof(char*)*(num_tracers+1)); |
| 145 |
tracer_list[num_tracers] = NULL; |
| 146 |
} while ((ptr = strtok(NULL, " \t\n\r")) != NULL); |
| 147 |
|
| 148 |
/* return the list and number of tracers */ |
| 149 |
*list = tracer_list; |
| 150 |
return num_tracers; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
/* |
| 155 |
* return zero if tracername is not a valid tracer, non-zero if it is |
| 156 |
*/ |
| 157 |
|
| 158 |
int valid_tracer(char *tracername) |
| 159 |
{ |
| 160 |
char **list; |
| 161 |
int ntracers; |
| 162 |
int i; |
| 163 |
|
| 164 |
ntracers = get_tracers(&list); |
| 165 |
if (ntracers == 0 || tracername == NULL) |
| 166 |
return 0; |
| 167 |
for (i = 0; i < ntracers; i++) |
| 168 |
if (strncmp(list[i], tracername, strlen(list[i])) == 0) |
| 169 |
return 1; |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
/* |
| 174 |
* enable event tracepoint |
| 175 |
*/ |
| 176 |
int setevent(char *event, char *val) |
| 177 |
{ |
| 178 |
char *prefix = get_tracefs_prefix(); |
| 179 |
char buffer[MAX_PATH]; |
| 180 |
int fd; |
| 181 |
int ret; |
| 182 |
|
| 183 |
sprintf(buffer, "%s%s", prefix, event); |
| 184 |
if ((fd = open(buffer, O_WRONLY)) < 0) { |
| 185 |
warn("unable to open %s\n", buffer); |
| 186 |
return -1; |
| 187 |
} |
| 188 |
if ((ret = write(fd, val, strlen(val))) < 0) { |
| 189 |
warn("unable to write %s to %s\n", val, buffer); |
| 190 |
close(fd); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
close(fd); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
int event_enable_all(void) |
| 198 |
{ |
| 199 |
return setevent("events/enable", "1"); |
| 200 |
} |
| 201 |
|
| 202 |
int event_disable_all(void) |
| 203 |
{ |
| 204 |
return setevent("events/enable", "0"); |
| 205 |
} |
| 206 |
|
| 207 |
int event_enable(char *event) |
| 208 |
{ |
| 209 |
char path[MAX_PATH]; |
| 210 |
|
| 211 |
sprintf(path, "events/%s/enable", event); |
| 212 |
return setevent(path, "1"); |
| 213 |
} |
| 214 |
|
| 215 |
int event_disable(char *event) |
| 216 |
{ |
| 217 |
char path[MAX_PATH]; |
| 218 |
|
| 219 |
sprintf(path, "events/%s/enable", event); |
| 220 |
return setevent(path, "0"); |
| 221 |
} |
| 222 |
|
| 223 |
int check_privs(void) |
| 224 |
{ |
| 225 |
int policy = sched_getscheduler(0); |
| 226 |
struct sched_param param, old_param; |
| 227 |
|
| 228 |
/* if we're already running a realtime scheduler |
| 229 |
* then we *should* be able to change things later |
| 230 |
*/ |
| 231 |
if (policy == SCHED_FIFO || policy == SCHED_RR) |
| 232 |
return 0; |
| 233 |
|
| 234 |
/* first get the current parameters */ |
| 235 |
if (sched_getparam(0, &old_param)) { |
| 236 |
fprintf(stderr, "unable to get scheduler parameters\n"); |
| 237 |
return 1; |
| 238 |
} |
| 239 |
param = old_param; |
| 240 |
|
| 241 |
/* try to change to SCHED_FIFO */ |
| 242 |
param.sched_priority = 1; |
| 243 |
if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 244 |
fprintf(stderr, "Unable to change scheduling policy!\n"); |
| 245 |
fprintf(stderr, "either run as root or join realtime group\n"); |
| 246 |
return 1; |
| 247 |
} |
| 248 |
|
| 249 |
/* we're good; change back and return success */ |
| 250 |
return sched_setscheduler(0, policy, &old_param); |
| 251 |
} |
| 252 |
|
| 253 |
const char *policy_to_string(int policy) |
| 254 |
{ |
| 255 |
switch (policy) { |
| 256 |
case SCHED_OTHER: |
| 257 |
return "SCHED_OTHER"; |
| 258 |
case SCHED_FIFO: |
| 259 |
return "SCHED_FIFO"; |
| 260 |
case SCHED_RR: |
| 261 |
return "SCHED_RR"; |
| 262 |
case SCHED_BATCH: |
| 263 |
return "SCHED_BATCH"; |
| 264 |
case SCHED_IDLE: |
| 265 |
return "SCHED_IDLE"; |
| 266 |
case SCHED_DEADLINE: |
| 267 |
return "SCHED_DEADLINE"; |
| 268 |
} |
| 269 |
|
| 270 |
return "unknown"; |
| 271 |
} |
| 272 |
|
| 273 |
uint32_t string_to_policy(const char *str) |
| 274 |
{ |
| 275 |
if (!strcmp(str, "other")) |
| 276 |
return SCHED_OTHER; |
| 277 |
else if (!strcmp(str, "fifo")) |
| 278 |
return SCHED_FIFO; |
| 279 |
else if (!strcmp(str, "rr")) |
| 280 |
return SCHED_RR; |
| 281 |
else if (!strcmp(str, "batch")) |
| 282 |
return SCHED_BATCH; |
| 283 |
else if (!strcmp(str, "idle")) |
| 284 |
return SCHED_IDLE; |
| 285 |
else if (!strcmp(str, "deadline")) |
| 286 |
return SCHED_DEADLINE; |
| 287 |
|
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
pid_t gettid(void) |
| 292 |
{ |
| 293 |
return syscall(SYS_gettid); |
| 294 |
} |
| 295 |
|
| 296 |
/* |
| 297 |
* parse an input value as a base10 value followed by an optional |
| 298 |
* suffix. The input value is presumed to be in seconds, unless |
| 299 |
* followed by a modifier suffix: m=minutes, h=hours, d=days |
| 300 |
* |
| 301 |
* the return value is a value in seconds |
| 302 |
*/ |
| 303 |
int parse_time_string(char *val) |
| 304 |
{ |
| 305 |
char *end; |
| 306 |
int t = strtol(val, &end, 10); |
| 307 |
if (end) { |
| 308 |
switch (*end) { |
| 309 |
case 'm': |
| 310 |
case 'M': |
| 311 |
t *= 60; |
| 312 |
break; |
| 313 |
|
| 314 |
case 'h': |
| 315 |
case 'H': |
| 316 |
t *= 60*60; |
| 317 |
break; |
| 318 |
|
| 319 |
case 'd': |
| 320 |
case 'D': |
| 321 |
t *= 24*60*60; |
| 322 |
break; |
| 323 |
|
| 324 |
} |
| 325 |
} |
| 326 |
return t; |
| 327 |
} |
| 328 |
|
| 329 |
int parse_mem_string(char *str, uint64_t *val) |
| 330 |
{ |
| 331 |
char *endptr; |
| 332 |
int v = strtol(str, &endptr, 10); |
| 333 |
|
| 334 |
if (!*endptr) |
| 335 |
return v; |
| 336 |
|
| 337 |
switch (*endptr) { |
| 338 |
case 'g': |
| 339 |
case 'G': |
| 340 |
v *= 1024; |
| 341 |
__attribute__ ((fallthrough)); |
| 342 |
case 'm': |
| 343 |
case 'M': |
| 344 |
v *= 1024; |
| 345 |
__attribute__ ((fallthrough)); |
| 346 |
case 'k': |
| 347 |
case 'K': |
| 348 |
v *= 1024; |
| 349 |
__attribute__ ((fallthrough)); |
| 350 |
case 'b': |
| 351 |
case 'B': |
| 352 |
break; |
| 353 |
default: |
| 354 |
return -1; |
| 355 |
} |
| 356 |
|
| 357 |
*val = v; |
| 358 |
|
| 359 |
return 0; |
| 360 |
} |
| 361 |
|
| 362 |
static void open_tracemark_fd(void) |
| 363 |
{ |
| 364 |
char path[MAX_PATH]; |
| 365 |
int n; |
| 366 |
|
| 367 |
/* |
| 368 |
* open the tracemark file if it's not already open |
| 369 |
*/ |
| 370 |
if (tracemark_fd < 0) { |
| 371 |
n = snprintf(path, sizeof(path), "%s/%s", tracefs_prefix, "trace_marker"); |
| 372 |
if (n >= sizeof(path)) { |
| 373 |
warn("tracefs path too long\n"); |
| 374 |
return; |
| 375 |
} |
| 376 |
tracemark_fd = open(path, O_WRONLY); |
| 377 |
if (tracemark_fd < 0) { |
| 378 |
warn("unable to open trace_marker file: %s\n", path); |
| 379 |
return; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/* |
| 384 |
* if we're not tracing and the tracing_on fd is not open, |
| 385 |
* open the tracing_on file so that we can stop the trace |
| 386 |
* if we hit a breaktrace threshold |
| 387 |
*/ |
| 388 |
if (trace_fd < 0) { |
| 389 |
n = snprintf(path, sizeof(path), "%s/%s", tracefs_prefix, "tracing_on"); |
| 390 |
if (n >= sizeof(path)) { |
| 391 |
warn("tracefs path too long\n"); |
| 392 |
return; |
| 393 |
} |
| 394 |
if ((trace_fd = open(path, O_WRONLY)) < 0) |
| 395 |
warn("unable to open tracing_on file: %s\n", path); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
static void close_tracemark_fd(void) |
| 400 |
{ |
| 401 |
if (tracemark_fd > 0) |
| 402 |
close(tracemark_fd); |
| 403 |
|
| 404 |
if (trace_fd > 0) |
| 405 |
close(trace_fd); |
| 406 |
} |
| 407 |
|
| 408 |
static int trace_file_exists(char *name) |
| 409 |
{ |
| 410 |
struct stat sbuf; |
| 411 |
return stat(tracefs_prefix, &sbuf) ? 0 : 1; |
| 412 |
} |
| 413 |
|
| 414 |
static void tracefs_prepare(void) |
| 415 |
{ |
| 416 |
get_tracefs_prefix(); |
| 417 |
if (!trace_file_exists("tracing_enabled") && |
| 418 |
!trace_file_exists("tracing_on")) |
| 419 |
warn("tracing_enabled or tracing_on not found\n" |
| 420 |
"tracefs not found"); |
| 421 |
} |
| 422 |
|
| 423 |
void tracemark(char *fmt, ...) |
| 424 |
{ |
| 425 |
va_list ap; |
| 426 |
int len; |
| 427 |
int ret; |
| 428 |
|
| 429 |
/* bail out if we're not tracing */ |
| 430 |
/* or if the kernel doesn't support trace_mark */ |
| 431 |
if (tracemark_fd < 0) |
| 432 |
return; |
| 433 |
|
| 434 |
va_start(ap, fmt); |
| 435 |
len = vsnprintf(tracebuf, TRACEBUFSIZ, fmt, ap); |
| 436 |
va_end(ap); |
| 437 |
|
| 438 |
/* write the tracemark message */ |
| 439 |
ret = write(tracemark_fd, tracebuf, len); |
| 440 |
if (ret != len) |
| 441 |
warn("trace mark write failed\n"); |
| 442 |
} |
| 443 |
|
| 444 |
void tracing_stop(void) |
| 445 |
{ |
| 446 |
int ret; |
| 447 |
|
| 448 |
if (trace_fd < 0) |
| 449 |
return; |
| 450 |
ret = write(trace_fd, "0\n", 2); |
| 451 |
if (ret != 2) |
| 452 |
warn("trace stop write failed\n"); |
| 453 |
} |
| 454 |
|
| 455 |
void enable_trace_mark(void) |
| 456 |
{ |
| 457 |
tracefs_prepare(); |
| 458 |
open_tracemark_fd(); |
| 459 |
} |
| 460 |
|
| 461 |
void disable_trace_mark(void) |
| 462 |
{ |
| 463 |
close_tracemark_fd(); |
| 464 |
} |
| 465 |
|
| 466 |
static void get_timestamp(char *tsbuf) |
| 467 |
{ |
| 468 |
struct timeval tv; |
| 469 |
struct tm *tm; |
| 470 |
time_t t; |
| 471 |
|
| 472 |
gettimeofday(&tv, NULL); |
| 473 |
t = tv.tv_sec; |
| 474 |
tm = localtime(&t); |
| 475 |
/* RFC 2822-compliant date format */ |
| 476 |
strftime(tsbuf, MAX_TS_SIZE, "%a, %d %b %Y %T %z", tm); |
| 477 |
} |
| 478 |
|
| 479 |
void rt_init(int argc, char *argv[]) |
| 480 |
{ |
| 481 |
int offset = 0; |
| 482 |
int len, i; |
| 483 |
|
| 484 |
test_cmdline[0] = '\0'; |
| 485 |
|
| 486 |
/* |
| 487 |
* getopt_long() permutes the contents of argv as it scans, so |
| 488 |
* that eventually all the nonoptions are at the end. Make a |
| 489 |
* copy before calling getopt_long(). |
| 490 |
*/ |
| 491 |
for (i = 0; i < argc;) { |
| 492 |
len = strlen(argv[i]); |
| 493 |
if (offset + len + 1 >= MAX_COMMAND_LINE) |
| 494 |
break; |
| 495 |
|
| 496 |
strcat(test_cmdline, argv[i]); |
| 497 |
i++; |
| 498 |
if (i < argc) |
| 499 |
strcat(test_cmdline, " "); |
| 500 |
|
| 501 |
offset += len + 1; |
| 502 |
} |
| 503 |
|
| 504 |
get_timestamp(ts_start); |
| 505 |
} |
| 506 |
|
| 507 |
void rt_write_json(const char *filename, int return_code, |
| 508 |
void (*cb)(FILE *, void *), |
| 509 |
void *data) |
| 510 |
{ |
| 511 |
unsigned char buf[1]; |
| 512 |
struct utsname uts; |
| 513 |
char ts_end[MAX_TS_SIZE]; |
| 514 |
FILE *f, *s; |
| 515 |
size_t n; |
| 516 |
int rt = 0; |
| 517 |
|
| 518 |
if (!filename || !strcmp("-", filename)) { |
| 519 |
f = stdout; |
| 520 |
} else { |
| 521 |
f = fopen(filename, "w"); |
| 522 |
if (!f) |
| 523 |
err_exit(errno, "Failed to open '%s'\n", filename); |
| 524 |
} |
| 525 |
|
| 526 |
get_timestamp(ts_end); |
| 527 |
|
| 528 |
s = fopen("/sys/kernel/realtime", "r"); |
| 529 |
if (s) { |
| 530 |
n = fread(buf, 1, 1, s); |
| 531 |
if (n == 1 && buf[0] == '1') |
| 532 |
rt = 1; |
| 533 |
fclose(s); |
| 534 |
} |
| 535 |
|
| 536 |
if (uname(&uts)) |
| 537 |
err_exit(errno, "Could not retrieve system information"); |
| 538 |
|
| 539 |
fprintf(f, "{\n"); |
| 540 |
fprintf(f, " \"file_version\": 1,\n"); |
| 541 |
fprintf(f, " \"cmdline:\": \"%s\",\n", test_cmdline); |
| 542 |
fprintf(f, " \"rt_test_version:\": \"%1.2f\",\n", VERSION); |
| 543 |
fprintf(f, " \"start_time\": \"%s\",\n", ts_start); |
| 544 |
fprintf(f, " \"end_time\": \"%s\",\n", ts_end); |
| 545 |
fprintf(f, " \"return_code\": %d,\n", return_code); |
| 546 |
fprintf(f, " \"sysinfo\": {\n"); |
| 547 |
fprintf(f, " \"sysname\": \"%s\",\n", uts.sysname); |
| 548 |
fprintf(f, " \"nodename\": \"%s\",\n", uts.nodename); |
| 549 |
fprintf(f, " \"release\": \"%s\",\n", uts.release); |
| 550 |
fprintf(f, " \"version\": \"%s\",\n", uts.version); |
| 551 |
fprintf(f, " \"machine\": \"%s\",\n", uts.machine); |
| 552 |
fprintf(f, " \"realtime\": %d\n", rt); |
| 553 |
|
| 554 |
if (cb) { |
| 555 |
fprintf(f, " },\n"); |
| 556 |
(cb)(f, data); |
| 557 |
} else { |
| 558 |
fprintf(f, " }\n"); |
| 559 |
} |
| 560 |
|
| 561 |
fprintf(f, "}\n"); |
| 562 |
|
| 563 |
if (!filename || strcmp("-", filename)) |
| 564 |
fclose(f); |
| 565 |
} |