| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* sendme.c |
| 4 |
* |
| 5 |
* Copyright (C) 2009 Carsten Emde <[email protected]> |
| 6 |
* |
| 7 |
*/ |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <limits.h> |
| 12 |
#include <fcntl.h> |
| 13 |
#include <getopt.h> |
| 14 |
#include <signal.h> |
| 15 |
#include <sched.h> |
| 16 |
#include <string.h> |
| 17 |
#include <time.h> |
| 18 |
#include <errno.h> |
| 19 |
#include "rt-utils.h" |
| 20 |
#include "rt-get_cpu.h" |
| 21 |
|
| 22 |
#include <utmpx.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <sys/stat.h> |
| 25 |
#include <sys/time.h> |
| 26 |
#include <sys/mman.h> |
| 27 |
|
| 28 |
#define SIGTEST SIGHUP |
| 29 |
|
| 30 |
enum { |
| 31 |
AFFINITY_UNSPECIFIED, |
| 32 |
AFFINITY_SPECIFIED, |
| 33 |
AFFINITY_USECURRENT |
| 34 |
}; |
| 35 |
static int setaffinity = AFFINITY_UNSPECIFIED; |
| 36 |
|
| 37 |
static int affinity; |
| 38 |
static int tracelimit; |
| 39 |
static int priority; |
| 40 |
static int shutdown; |
| 41 |
static int max_cycles; |
| 42 |
static volatile struct timeval after; |
| 43 |
static int interval = 1000; |
| 44 |
|
| 45 |
static int kernvar(int mode, const char *name, char *value, size_t sizeofvalue) |
| 46 |
{ |
| 47 |
char filename[128]; |
| 48 |
char *fileprefix = get_tracefs_prefix(); |
| 49 |
int retval = 1; |
| 50 |
int path; |
| 51 |
size_t len_prefix = strlen(fileprefix), len_name = strlen(name); |
| 52 |
|
| 53 |
if (len_prefix + len_name + 1 > sizeof(filename)) { |
| 54 |
errno = ENOMEM; |
| 55 |
return 1; |
| 56 |
} |
| 57 |
|
| 58 |
memcpy(filename, fileprefix, len_prefix); |
| 59 |
memcpy(filename + len_prefix, name, len_name + 1); |
| 60 |
|
| 61 |
path = open(filename, mode); |
| 62 |
if (path >= 0) { |
| 63 |
if (mode == O_RDONLY) { |
| 64 |
int got; |
| 65 |
if ((got = read(path, value, sizeofvalue)) > 0) { |
| 66 |
retval = 0; |
| 67 |
value[got-1] = '\0'; |
| 68 |
} |
| 69 |
} else if (mode == O_WRONLY) { |
| 70 |
if (write(path, value, sizeofvalue) == sizeofvalue) |
| 71 |
retval = 0; |
| 72 |
} |
| 73 |
close(path); |
| 74 |
} |
| 75 |
return retval; |
| 76 |
} |
| 77 |
|
| 78 |
void signalhandler(int signo) |
| 79 |
{ |
| 80 |
struct timeval tv; |
| 81 |
|
| 82 |
gettimeofday(&tv, NULL); |
| 83 |
after = tv; |
| 84 |
if (signo == SIGINT || signo == SIGTERM) |
| 85 |
shutdown = 1; |
| 86 |
} |
| 87 |
|
| 88 |
void stop_tracing(void) |
| 89 |
{ |
| 90 |
kernvar(O_WRONLY, "tracing_enabled", "0", 1); |
| 91 |
} |
| 92 |
|
| 93 |
static void display_help(void) |
| 94 |
{ |
| 95 |
printf("sendme V %1.2f\n", VERSION); |
| 96 |
puts("Usage: sendme <options>"); |
| 97 |
puts("Function: send a signal from driver to userspace"); |
| 98 |
puts( |
| 99 |
"Options:\n" |
| 100 |
"-a [NUM] --affinity pin to current processor\n" |
| 101 |
" with NUM pin to processor NUM\n" |
| 102 |
"-b USEC --breaktrace=USEC send break trace command when latency > USEC\n" |
| 103 |
"-i INTV --interval=INTV base interval of thread in us default=1000\n" |
| 104 |
"-l LOOPS --loops=LOOPS number of loops: default=0(endless)\n" |
| 105 |
"-p PRIO --prio=PRIO priority\n"); |
| 106 |
exit(1); |
| 107 |
} |
| 108 |
|
| 109 |
static void process_options (int argc, char *argv[]) |
| 110 |
{ |
| 111 |
int error = 0; |
| 112 |
int max_cpus = sysconf(_SC_NPROCESSORS_CONF); |
| 113 |
|
| 114 |
for (;;) { |
| 115 |
int option_index = 0; |
| 116 |
/** Options for getopt */ |
| 117 |
static struct option long_options[] = { |
| 118 |
{"affinity", optional_argument, NULL, 'a'}, |
| 119 |
{"breaktrace", required_argument, NULL, 'b'}, |
| 120 |
{"interval", required_argument, NULL, 'i'}, |
| 121 |
{"loops", required_argument, NULL, 'l'}, |
| 122 |
{"priority", required_argument, NULL, 'p'}, |
| 123 |
{"help", no_argument, NULL, '?'}, |
| 124 |
{NULL, 0, NULL, 0} |
| 125 |
}; |
| 126 |
int c = getopt_long (argc, argv, "a::b:i:l:p:", |
| 127 |
long_options, &option_index); |
| 128 |
if (c == -1) |
| 129 |
break; |
| 130 |
switch (c) { |
| 131 |
case 'a': |
| 132 |
if (optarg != NULL) { |
| 133 |
affinity = atoi(optarg); |
| 134 |
setaffinity = AFFINITY_SPECIFIED; |
| 135 |
} else if (optind < argc && atoi(argv[optind])) { |
| 136 |
affinity = atoi(argv[optind]); |
| 137 |
setaffinity = AFFINITY_SPECIFIED; |
| 138 |
} else |
| 139 |
setaffinity = AFFINITY_USECURRENT; |
| 140 |
break; |
| 141 |
case 'b': tracelimit = atoi(optarg); break; |
| 142 |
case 'i': interval = atoi(optarg); break; |
| 143 |
case 'l': max_cycles = atoi(optarg); break; |
| 144 |
case 'p': priority = atoi(optarg); break; |
| 145 |
case '?': error = 1; break; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if (setaffinity == AFFINITY_SPECIFIED) { |
| 150 |
if (affinity < 0) |
| 151 |
error = 1; |
| 152 |
if (affinity >= max_cpus) { |
| 153 |
fprintf(stderr, "ERROR: CPU #%d not found, only %d CPUs available\n", |
| 154 |
affinity, max_cpus); |
| 155 |
error = 1; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if (priority < 0 || priority > 99) |
| 160 |
error = 1; |
| 161 |
|
| 162 |
if (error) |
| 163 |
display_help (); |
| 164 |
} |
| 165 |
|
| 166 |
int main(int argc, char *argv[]) |
| 167 |
{ |
| 168 |
int path; |
| 169 |
cpu_set_t mask; |
| 170 |
int policy = SCHED_FIFO; |
| 171 |
struct sched_param schedp; |
| 172 |
struct flock fl; |
| 173 |
int retval = 0; |
| 174 |
|
| 175 |
process_options(argc, argv); |
| 176 |
|
| 177 |
if (check_privs()) |
| 178 |
return 1; |
| 179 |
|
| 180 |
if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1) { |
| 181 |
perror("mlockall"); |
| 182 |
return 1; |
| 183 |
} |
| 184 |
|
| 185 |
memset(&schedp, 0, sizeof(schedp)); |
| 186 |
schedp.sched_priority = priority; |
| 187 |
sched_setscheduler(0, policy, &schedp); |
| 188 |
|
| 189 |
if (setaffinity != AFFINITY_UNSPECIFIED) { |
| 190 |
CPU_ZERO(&mask); |
| 191 |
if (setaffinity == AFFINITY_USECURRENT) { |
| 192 |
get_cpu_setup(); |
| 193 |
affinity = get_cpu(); |
| 194 |
} |
| 195 |
CPU_SET(affinity, &mask); |
| 196 |
if (sched_setaffinity(0, sizeof(mask), &mask) == -1) |
| 197 |
fprintf(stderr, "WARNING: Could not set CPU affinity " |
| 198 |
"to CPU #%d\n", affinity); |
| 199 |
} |
| 200 |
|
| 201 |
path = open("/dev/backfire", O_RDWR); |
| 202 |
if (path < 0) { |
| 203 |
fprintf(stderr, "ERROR: Could not access backfire device, " |
| 204 |
"try 'modprobe backfire'\n" |
| 205 |
"If the module backfire can't be loaded, " |
| 206 |
"it may need to be built first.\n" |
| 207 |
"Execute 'cd src/backfire; make' in the " |
| 208 |
"rt-tests directory (requires rt-tests\n" |
| 209 |
"sources and kernel-devel package).\n"); |
| 210 |
return 1; |
| 211 |
} |
| 212 |
fl.l_type = F_WRLCK; |
| 213 |
fl.l_whence = SEEK_SET; |
| 214 |
fl.l_start = 0; |
| 215 |
fl.l_len = 1; |
| 216 |
if (fcntl(path, F_SETLK, &fl) == -1) { |
| 217 |
fprintf(stderr, "ERRROR: backfire device locked\n"); |
| 218 |
retval = 1; |
| 219 |
} else { |
| 220 |
char sigtest[8]; |
| 221 |
char timestamp[32]; |
| 222 |
struct timeval before, sendtime, diff; |
| 223 |
unsigned int diffno = 0; |
| 224 |
unsigned int mindiff1 = UINT_MAX, maxdiff1 = 0; |
| 225 |
unsigned int mindiff2 = UINT_MAX, maxdiff2 = 0; |
| 226 |
double sumdiff1 = 0.0, sumdiff2 = 0.0; |
| 227 |
|
| 228 |
if (tracelimit) |
| 229 |
kernvar(O_WRONLY, "tracing_enabled", "1", 1); |
| 230 |
|
| 231 |
sprintf(sigtest, "%d", SIGTEST); |
| 232 |
signal(SIGTEST, signalhandler); |
| 233 |
signal(SIGINT, signalhandler); |
| 234 |
signal(SIGTERM, signalhandler); |
| 235 |
|
| 236 |
while (1) { |
| 237 |
struct timespec ts; |
| 238 |
|
| 239 |
ts.tv_sec = interval / USEC_PER_SEC; |
| 240 |
ts.tv_nsec = (interval % USEC_PER_SEC) * 1000; |
| 241 |
|
| 242 |
gettimeofday(&before, NULL); |
| 243 |
write(path, sigtest, strlen(sigtest)); |
| 244 |
while (after.tv_sec == 0); |
| 245 |
read(path, timestamp, sizeof(timestamp)); |
| 246 |
if (sscanf(timestamp, "%lu,%lu\n", &sendtime.tv_sec, |
| 247 |
&sendtime.tv_usec) != 2) |
| 248 |
break; |
| 249 |
diffno++; |
| 250 |
if(max_cycles && diffno >= max_cycles) |
| 251 |
shutdown = 1; |
| 252 |
|
| 253 |
printf("Samples: %8d\n", diffno); |
| 254 |
timersub(&sendtime, &before, &diff); |
| 255 |
if (diff.tv_usec < mindiff1) |
| 256 |
mindiff1 = diff.tv_usec; |
| 257 |
if (diff.tv_usec > maxdiff1) |
| 258 |
maxdiff1 = diff.tv_usec; |
| 259 |
sumdiff1 += (double) diff.tv_usec; |
| 260 |
printf("To: Min %4d, Cur %4d, Avg %4d, Max %4d\n", |
| 261 |
mindiff1, (int) diff.tv_usec, |
| 262 |
(int) ((sumdiff1 / diffno) + 0.5), |
| 263 |
maxdiff1); |
| 264 |
|
| 265 |
timersub(&after, &sendtime, &diff); |
| 266 |
if (diff.tv_usec < mindiff2) |
| 267 |
mindiff2 = diff.tv_usec; |
| 268 |
if (diff.tv_usec > maxdiff2) |
| 269 |
maxdiff2 = diff.tv_usec; |
| 270 |
sumdiff2 += (double) diff.tv_usec; |
| 271 |
printf("From: Min %4d, Cur %4d, Avg %4d, Max %4d\n", |
| 272 |
mindiff2, (int) diff.tv_usec, |
| 273 |
(int) ((sumdiff2 / diffno) + 0.5), |
| 274 |
maxdiff2); |
| 275 |
after.tv_sec = 0; |
| 276 |
if ((tracelimit && diff.tv_usec > tracelimit) || |
| 277 |
shutdown) { |
| 278 |
if (tracelimit) |
| 279 |
stop_tracing(); |
| 280 |
break; |
| 281 |
} |
| 282 |
nanosleep(&ts, NULL); |
| 283 |
printf("\033[3A"); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
close(path); |
| 288 |
return retval; |
| 289 |
} |