| 1 | |
| 2 | |
| 3 | * Copyright (C) 2023 Red Hat Inc, Daniel Bristot de Oliveira <[email protected]>
|
| 4 |
|
| 5 |
|
| 6 | #define _GNU_SOURCE
|
| 7 | #include <sched.h>
|
| 8 | #include <fcntl.h>
|
| 9 | #include <stdlib.h>
|
| 10 | #include <unistd.h>
|
| 11 | #include <stdio.h>
|
| 12 | #include <errno.h>
|
| 13 | #include <string.h>
|
| 14 | #include <tracefs.h>
|
| 15 | #include <pthread.h>
|
| 16 | #include <sys/wait.h>
|
| 17 | #include <sys/prctl.h>
|
| 18 |
|
| 19 | #include "common.h"
|
| 20 | #include "timerlat_u.h"
|
| 21 |
|
| 22 | |
| 23 | * This is the user-space main for the tool timerlatu/ threads.
|
| 24 | *
|
| 25 | * It is as simple as this:
|
| 26 | * - set affinity
|
| 27 | * - set priority
|
| 28 | * - open tracer fd
|
| 29 | * - spin
|
| 30 | * - close
|
| 31 |
|
| 32 | static int timerlat_u_main(int cpu, struct timerlat_u_params *params)
|
| 33 | {
|
| 34 | struct sched_param sp = { .sched_priority = 95 };
|
| 35 | char buffer[MAX_PATH];
|
| 36 | int timerlat_fd;
|
| 37 | cpu_set_t set;
|
| 38 | int retval;
|
| 39 |
|
| 40 | |
| 41 | * This all is only setting up the tool.
|
| 42 |
|
| 43 | CPU_ZERO(&set);
|
| 44 | CPU_SET(cpu, &set);
|
| 45 |
|
| 46 | retval = sched_setaffinity(gettid(), sizeof(set), &set);
|
| 47 | if (retval == -1) {
|
| 48 | debug_msg("Error setting user thread affinity %d, is the CPU online?\n", cpu);
|
| 49 | exit(1);
|
| 50 | }
|
| 51 |
|
| 52 | if (!params->sched_param) {
|
| 53 | retval = sched_setscheduler(0, SCHED_FIFO, &sp);
|
| 54 | if (retval < 0)
|
| 55 | fatal("Error setting timerlat u default priority: %s", strerror(errno));
|
| 56 | } else {
|
| 57 | retval = __set_sched_attr(getpid(), params->sched_param);
|
| 58 | if (retval) {
|
| 59 |
|
| 60 | exit(0);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | if (params->cgroup_name) {
|
| 65 | retval = set_pid_cgroup(gettid(), params->cgroup_name);
|
| 66 | if (!retval) {
|
| 67 | err_msg("Error setting timerlat u cgroup pid\n");
|
| 68 | pthread_exit(&retval);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | |
| 73 | * This is the tool's loop. If you want to use as base for your own tool...
|
| 74 | * go ahead.
|
| 75 |
|
| 76 | snprintf(buffer, sizeof(buffer), "osnoise/per_cpu/cpu%d/timerlat_fd", cpu);
|
| 77 |
|
| 78 | timerlat_fd = tracefs_instance_file_open(NULL, buffer, O_RDONLY);
|
| 79 | if (timerlat_fd < 0)
|
| 80 | fatal("Error opening %s:%s", buffer, strerror(errno));
|
| 81 |
|
| 82 | debug_msg("User-space timerlat pid %d on cpu %d\n", gettid(), cpu);
|
| 83 |
|
| 84 |
|
| 85 | while (true) {
|
| 86 | retval = read(timerlat_fd, buffer, ARRAY_SIZE(buffer));
|
| 87 | if (retval < 0)
|
| 88 | break;
|
| 89 | }
|
| 90 |
|
| 91 | close(timerlat_fd);
|
| 92 |
|
| 93 | debug_msg("Leaving timerlat pid %d on cpu %d\n", gettid(), cpu);
|
| 94 | exit(0);
|
| 95 | }
|
| 96 |
|
| 97 | |
| 98 | * timerlat_u_send_kill - send a kill signal for all processes
|
| 99 | *
|
| 100 | * Return the number of processes that received the kill.
|
| 101 |
|
| 102 | static int timerlat_u_send_kill(pid_t *procs)
|
| 103 | {
|
| 104 | int killed = 0;
|
| 105 | int i, retval;
|
| 106 |
|
| 107 | for (i = 0; i < nr_cpus; i++) {
|
| 108 | if (!procs[i])
|
| 109 | continue;
|
| 110 | retval = kill(procs[i], SIGKILL);
|
| 111 | if (!retval)
|
| 112 | killed++;
|
| 113 | else
|
| 114 | err_msg("Error killing child process %d\n", procs[i]);
|
| 115 | }
|
| 116 |
|
| 117 | return killed;
|
| 118 | }
|
| 119 |
|
| 120 | |
| 121 | * timerlat_u_dispatcher - dispatch one timerlatu/ process per monitored CPU
|
| 122 | *
|
| 123 | * This is a thread main that will fork one new process for each monitored
|
| 124 | * CPU. It will wait for:
|
| 125 | *
|
| 126 | * - rtla to tell to kill the child processes
|
| 127 | * - some child process to die, and the cleanup all the processes
|
| 128 | *
|
| 129 | * whichever comes first.
|
| 130 | *
|
| 131 |
|
| 132 | void *timerlat_u_dispatcher(void *data)
|
| 133 | {
|
| 134 | struct timerlat_u_params *params = data;
|
| 135 | char proc_name[128];
|
| 136 | int procs_count = 0;
|
| 137 | int retval = 1;
|
| 138 | pid_t *procs;
|
| 139 | int wstatus;
|
| 140 | pid_t pid;
|
| 141 | int i;
|
| 142 |
|
| 143 | debug_msg("Dispatching timerlat u procs\n");
|
| 144 |
|
| 145 | procs = calloc(nr_cpus, sizeof(pid_t));
|
| 146 | if (!procs)
|
| 147 | pthread_exit(&retval);
|
| 148 |
|
| 149 | for (i = 0; i < nr_cpus; i++) {
|
| 150 | if (params->set && !CPU_ISSET(i, params->set))
|
| 151 | continue;
|
| 152 |
|
| 153 | pid = fork();
|
| 154 |
|
| 155 |
|
| 156 | if (!pid) {
|
| 157 |
|
| 158 | |
| 159 | * rename the process
|
| 160 |
|
| 161 | snprintf(proc_name, sizeof(proc_name), "timerlatu/%d", i);
|
| 162 | pthread_setname_np(pthread_self(), proc_name);
|
| 163 | prctl(PR_SET_NAME, (unsigned long)proc_name, 0, 0, 0);
|
| 164 |
|
| 165 | timerlat_u_main(i, params);
|
| 166 |
|
| 167 | pthread_exit(&retval);
|
| 168 | }
|
| 169 |
|
| 170 |
|
| 171 | if (pid == -1) {
|
| 172 | timerlat_u_send_kill(procs);
|
| 173 | debug_msg("Failed to create child processes");
|
| 174 | pthread_exit(&retval);
|
| 175 | }
|
| 176 |
|
| 177 | procs_count++;
|
| 178 | procs[i] = pid;
|
| 179 | }
|
| 180 |
|
| 181 | while (params->should_run) {
|
| 182 |
|
| 183 | pid = waitpid(-1, &wstatus, WNOHANG);
|
| 184 | if (pid != 0) {
|
| 185 | for (i = 0; i < nr_cpus; i++) {
|
| 186 | if (procs[i] == pid) {
|
| 187 | procs[i] = 0;
|
| 188 | procs_count--;
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | if (!procs_count)
|
| 193 | break;
|
| 194 | }
|
| 195 |
|
| 196 | sleep(1);
|
| 197 | }
|
| 198 |
|
| 199 | timerlat_u_send_kill(procs);
|
| 200 |
|
| 201 | while (procs_count) {
|
| 202 | pid = waitpid(-1, &wstatus, 0);
|
| 203 | if (pid == -1) {
|
| 204 | err_msg("Failed to monitor child processes");
|
| 205 | pthread_exit(&retval);
|
| 206 | }
|
| 207 | for (i = 0; i < nr_cpus; i++) {
|
| 208 | if (procs[i] == pid) {
|
| 209 | procs[i] = 0;
|
| 210 | procs_count--;
|
| 211 | }
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | params->stopped_running = 1;
|
| 216 |
|
| 217 | free(procs);
|
| 218 | retval = 0;
|
| 219 | pthread_exit(&retval);
|
| 220 |
|
| 221 | }
|