| 1 |
/* -*- linux-c -*- |
| 2 |
* |
| 3 |
* relay.c - staprun relayfs functions |
| 4 |
* |
| 5 |
* This file is part of systemtap, and is free software. You can |
| 6 |
* redistribute it and/or modify it under the terms of the GNU General |
| 7 |
* Public License (GPL); either version 2, or (at your option) any |
| 8 |
* later version. |
| 9 |
* |
| 10 |
* Copyright (C) 2007-2023 Red Hat Inc. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "staprun.h" |
| 14 |
#include <string.h> |
| 15 |
#ifdef HAVE_STDATOMIC_H |
| 16 |
#include <stdatomic.h> |
| 17 |
#endif |
| 18 |
#define NDEBUG |
| 19 |
#include "gheap.h" |
| 20 |
|
| 21 |
|
| 22 |
int out_fd[MAX_NR_CPUS]; |
| 23 |
int monitor_end = 0; |
| 24 |
static pthread_t reader[MAX_NR_CPUS]; |
| 25 |
static int relay_fd[MAX_NR_CPUS]; // fd to kernel per-cpu relayfs |
| 26 |
static int avail_cpus[MAX_NR_CPUS]; |
| 27 |
static volatile sig_atomic_t sigusr2_count; // number of SIGUSR2's received by process |
| 28 |
static int sigusr2_processed[MAX_NR_CPUS]; // each thread's count of processed SIGUSR2's |
| 29 |
static int bulkmode = 0; |
| 30 |
static volatile int stop_threads = 0; // set during relayfs_close to signal threads to die |
| 31 |
static time_t *time_backlog[MAX_NR_CPUS]; |
| 32 |
static int backlog_order=0; |
| 33 |
#define BACKLOG_MASK ((1 << backlog_order) - 1) |
| 34 |
#define MONITORLINELENGTH 4096 |
| 35 |
|
| 36 |
#ifdef NEED_PPOLL |
| 37 |
int ppoll(struct pollfd *fds, nfds_t nfds, |
| 38 |
const struct timespec *timeout, const sigset_t *sigmask) |
| 39 |
{ |
| 40 |
sigset_t origmask; |
| 41 |
int ready; |
| 42 |
int tim; |
| 43 |
if (timeout == NULL) |
| 44 |
tim = -1; |
| 45 |
else |
| 46 |
tim = timeout->tv_sec * 1000 + timeout->tv_nsec / 1000000; |
| 47 |
|
| 48 |
sigprocmask(SIG_SETMASK, sigmask, &origmask); |
| 49 |
ready = poll(fds, nfds, tim); |
| 50 |
sigprocmask(SIG_SETMASK, &origmask, NULL); |
| 51 |
return ready; |
| 52 |
} |
| 53 |
#endif |
| 54 |
|
| 55 |
int init_backlog(int cpu) |
| 56 |
{ |
| 57 |
int order = 0; |
| 58 |
if (!fnum_max) |
| 59 |
return 0; |
| 60 |
while (fnum_max > 1<<order) order++; |
| 61 |
time_backlog[cpu] = (time_t *)calloc(1<<order, sizeof(time_t)); |
| 62 |
if (time_backlog[cpu] == NULL) { |
| 63 |
_err("Memory allocation failed\n"); |
| 64 |
return -1; |
| 65 |
} |
| 66 |
backlog_order = order; |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
void write_backlog(int cpu, int fnum, time_t t) |
| 71 |
{ |
| 72 |
time_backlog[cpu][fnum & BACKLOG_MASK] = t; |
| 73 |
} |
| 74 |
|
| 75 |
time_t read_backlog(int cpu, int fnum) |
| 76 |
{ |
| 77 |
return time_backlog[cpu][fnum & BACKLOG_MASK]; |
| 78 |
} |
| 79 |
|
| 80 |
static int open_outfile(int fnum, int cpu, int remove_file) |
| 81 |
{ |
| 82 |
char buf[PATH_MAX]; |
| 83 |
time_t t; |
| 84 |
if (!outfile_name) { |
| 85 |
_err("-S is set without -o. Please file a bug report.\n"); |
| 86 |
return -1; |
| 87 |
} |
| 88 |
|
| 89 |
time(&t); |
| 90 |
if (fnum_max) { |
| 91 |
if (remove_file) { |
| 92 |
/* remove oldest file */ |
| 93 |
if (make_outfile_name(buf, PATH_MAX, fnum - fnum_max, |
| 94 |
cpu, read_backlog(cpu, fnum - fnum_max), |
| 95 |
bulkmode) < 0) |
| 96 |
return -1; |
| 97 |
remove(buf); /* don't care */ |
| 98 |
} |
| 99 |
write_backlog(cpu, fnum, t); |
| 100 |
} |
| 101 |
|
| 102 |
if (make_outfile_name(buf, PATH_MAX, fnum, cpu, t, bulkmode) < 0) |
| 103 |
return -1; |
| 104 |
out_fd[cpu] = open_cloexec (buf, O_CREAT|O_TRUNC|O_WRONLY, 0666); |
| 105 |
if (out_fd[cpu] < 0) { |
| 106 |
perr("Couldn't open output file %s", buf); |
| 107 |
return -1; |
| 108 |
} |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
static int switch_outfile(int cpu, int *fnum) |
| 113 |
{ |
| 114 |
int remove_file = 0; |
| 115 |
|
| 116 |
dbug(3, "thread %d switching file\n", cpu); |
| 117 |
close(out_fd[cpu]); |
| 118 |
*fnum += 1; |
| 119 |
if (fnum_max && *fnum >= fnum_max) |
| 120 |
remove_file = 1; |
| 121 |
if (open_outfile(*fnum, cpu, remove_file) < 0) { |
| 122 |
perr("Couldn't open file for cpu %d, exiting.", cpu); |
| 123 |
return -1; |
| 124 |
} |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
/* In serialized (non-bulk) output mode, ndividual messages that have |
| 131 |
been received from the kernel per-cpu relays are stored in an central |
| 132 |
serializing data structure - in this case, a heap. They are ordered |
| 133 |
by message sequence number. An additional thread (serializer_thread) |
| 134 |
scans & sequences the output. */ |
| 135 |
struct serialized_message { |
| 136 |
union { |
| 137 |
struct _stp_trace bufhdr; |
| 138 |
char bufhdr_raw[sizeof(struct _stp_trace)]; |
| 139 |
}; |
| 140 |
time_t received; // timestamp when this message was enqueued |
| 141 |
char *buf; // malloc()'d size >= rounded(bufhdr.pdu_len) |
| 142 |
}; |
| 143 |
static struct serialized_message* buffer_heap = NULL; // the heap |
| 144 |
|
| 145 |
// NB: we control memory via realloc(), gheap just manipulates entries in place |
| 146 |
static unsigned buffer_heap_size = 0; // used number of entries |
| 147 |
static unsigned buffer_heap_alloc = 0; // allocation length, always >= buffer_heap_size |
| 148 |
static unsigned last_sequence_number = 0; // last processed sequential message number |
| 149 |
|
| 150 |
#ifdef HAVE_STDATOMIC_H |
| 151 |
static atomic_ulong lost_message_count = 0; // how many sequence numbers we know we missed |
| 152 |
static atomic_ulong lost_byte_count = 0; // how many bytes were skipped during resync |
| 153 |
#else |
| 154 |
static unsigned long lost_message_count = 0; // how many sequence numbers we know we missed |
| 155 |
static unsigned long lost_byte_count = 0; // how many bytes were skipped during resync |
| 156 |
#endif |
| 157 |
|
| 158 |
// concurrency control for the buffer_heap |
| 159 |
static pthread_cond_t buffer_heap_cond = PTHREAD_COND_INITIALIZER; |
| 160 |
static pthread_mutex_t buffer_heap_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 161 |
static pthread_t serializer_thread; // ! bulkmode only |
| 162 |
|
| 163 |
|
| 164 |
static void buffer_heap_mover (void *const dest, const void *const src) |
| 165 |
{ |
| 166 |
memmove (dest, src, sizeof(struct serialized_message)); |
| 167 |
} |
| 168 |
|
| 169 |
// NB: since we want to sort messages into an INCREASING heap sequence, |
| 170 |
// we reverse the normal comparison operator. gheap_pop_heap() should |
| 171 |
// therefore return the SMALLEST element. |
| 172 |
static int buffer_heap_comparer (const void *const ctx, const void *a, const void *b) |
| 173 |
{ |
| 174 |
(void) ctx; |
| 175 |
uint32_t aa = ((struct serialized_message *)a)->bufhdr.sequence; |
| 176 |
uint32_t bb = ((struct serialized_message *)b)->bufhdr.sequence; |
| 177 |
return (aa > bb); |
| 178 |
} |
| 179 |
|
| 180 |
static const struct gheap_ctx buffer_heap_ctx = { |
| 181 |
.item_size = sizeof(struct serialized_message), |
| 182 |
.less_comparer = buffer_heap_comparer, |
| 183 |
.item_mover = buffer_heap_mover, |
| 184 |
.page_chunks = 16, // arbitrary |
| 185 |
.fanout = 2 // standard binary heap |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
#define MAX_MESSAGE_LENGTH (128*1024) /* maximum likely length of a single pdu */ |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
/* Thread that reads per-cpu messages, and stuffs complete ones into |
| 194 |
dynamically allocated serialized_message nodes in a binary tree. */ |
| 195 |
static void* reader_thread_serialmode (void *data) |
| 196 |
{ |
| 197 |
int rc, cpu = (int)(long)data; |
| 198 |
struct pollfd pollfd; |
| 199 |
sigset_t sigs; |
| 200 |
cpu_set_t cpu_mask; |
| 201 |
|
| 202 |
sigemptyset(&sigs); |
| 203 |
sigaddset(&sigs,SIGUSR2); |
| 204 |
pthread_sigmask(SIG_BLOCK, &sigs, NULL); |
| 205 |
|
| 206 |
sigfillset(&sigs); |
| 207 |
sigdelset(&sigs,SIGUSR2); |
| 208 |
|
| 209 |
CPU_ZERO(&cpu_mask); |
| 210 |
CPU_SET(cpu, &cpu_mask); |
| 211 |
if( sched_setaffinity( 0, sizeof(cpu_mask), &cpu_mask ) < 0 ) |
| 212 |
_perr("sched_setaffinity"); |
| 213 |
|
| 214 |
pollfd.fd = relay_fd[cpu]; |
| 215 |
pollfd.events = POLLIN; |
| 216 |
|
| 217 |
while (! stop_threads) { |
| 218 |
// read a message header |
| 219 |
struct serialized_message message; |
| 220 |
|
| 221 |
/* 200ms, close to human level of "instant" */ |
| 222 |
struct timespec tim, *timeout = &tim; |
| 223 |
timeout->tv_sec = reader_timeout_ms / 1000; |
| 224 |
timeout->tv_nsec = (reader_timeout_ms - timeout->tv_sec * 1000) * 1000000; |
| 225 |
|
| 226 |
rc = ppoll(&pollfd, 1, timeout, &sigs); |
| 227 |
if (rc < 0) { |
| 228 |
dbug(3, "cpu=%d poll=%d errno=%d\n", cpu, rc, errno); |
| 229 |
if (errno == EINTR) { |
| 230 |
if (stop_threads) |
| 231 |
break; |
| 232 |
} else { |
| 233 |
_perr("poll error"); |
| 234 |
goto error_out; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// set the timestamp |
| 239 |
message.received = time(NULL); |
| 240 |
|
| 241 |
/* Read the header. */ |
| 242 |
rc = read(relay_fd[cpu], &message.bufhdr, sizeof(message.bufhdr)); |
| 243 |
if (rc <= 0) /* seen during normal shutdown or error */ |
| 244 |
continue; |
| 245 |
if (rc != sizeof(message.bufhdr)) { |
| 246 |
lost_byte_count += rc; |
| 247 |
continue; |
| 248 |
} |
| 249 |
|
| 250 |
/* Validate the magic value. In case of mismatch, |
| 251 |
keep on reading & shifting the header, one byte at |
| 252 |
a time, until we get a match. */ |
| 253 |
while (! stop_threads && memcmp(message.bufhdr.magic, STAP_TRACE_MAGIC, 4)) { |
| 254 |
/* Do not count padding bytes */ |
| 255 |
if ((* (&message.bufhdr_raw[0])) != '\0') |
| 256 |
lost_byte_count ++; |
| 257 |
memmove(& message.bufhdr_raw[0], |
| 258 |
& message.bufhdr_raw[1], |
| 259 |
sizeof(message.bufhdr_raw)-1); |
| 260 |
rc = read(relay_fd[cpu], |
| 261 |
&message.bufhdr_raw[sizeof(message.bufhdr_raw)-1], |
| 262 |
1); |
| 263 |
if (rc <= 0) /* seen during normal shutdown or error */ |
| 264 |
break; |
| 265 |
} |
| 266 |
|
| 267 |
/* Validate it slightly. Because of lost messages, we might be getting |
| 268 |
not a proper _stp_trace struct but the interior of some piece of |
| 269 |
trace text message. XXX: validate bufhdr.sequence a little bit too? */ |
| 270 |
if (message.bufhdr.pdu_len == 0 || |
| 271 |
message.bufhdr.pdu_len > MAX_MESSAGE_LENGTH) { |
| 272 |
lost_byte_count += sizeof(message.bufhdr); |
| 273 |
continue; |
| 274 |
} |
| 275 |
|
| 276 |
// Allocate the pdu body |
| 277 |
message.buf = malloc(message.bufhdr.pdu_len); |
| 278 |
if (message.buf == NULL) |
| 279 |
{ |
| 280 |
lost_byte_count += message.bufhdr.pdu_len; |
| 281 |
continue; |
| 282 |
} |
| 283 |
|
| 284 |
/* Read the message, perhaps in pieces (such as if crossing |
| 285 |
* relayfs subbuf boundaries). */ |
| 286 |
size_t bufread = 0; |
| 287 |
while (bufread < message.bufhdr.pdu_len) { |
| 288 |
rc = read(relay_fd[cpu], message.buf+bufread, message.bufhdr.pdu_len-bufread); |
| 289 |
if (rc <= 0) { |
| 290 |
lost_byte_count += message.bufhdr.pdu_len-bufread; |
| 291 |
break; /* still process it; hope we can resync at next packet. */ |
| 292 |
} |
| 293 |
bufread += rc; |
| 294 |
} |
| 295 |
|
| 296 |
// plop the message into the buffer_heap |
| 297 |
pthread_mutex_lock(& buffer_heap_mutex); |
| 298 |
if (message.bufhdr.sequence < last_sequence_number) { |
| 299 |
// whoa! is this some old message that we've assumed lost? |
| 300 |
// or are we wrapping around the uint_32 sequence numbers? |
| 301 |
_perr("unexpected sequence=%u", message.bufhdr.sequence); |
| 302 |
} |
| 303 |
|
| 304 |
// is it large enough? if not, realloc |
| 305 |
if (buffer_heap_alloc - buffer_heap_size == 0) { // full |
| 306 |
unsigned new_buffer_heap_alloc = (buffer_heap_alloc + 1) * 1.5; |
| 307 |
struct serialized_message *new_buffer_heap = |
| 308 |
realloc(buffer_heap, |
| 309 |
new_buffer_heap_alloc * sizeof(struct serialized_message)); |
| 310 |
if (new_buffer_heap == NULL) { |
| 311 |
_perr("out of memory while enlarging buffer heap"); |
| 312 |
free (message.buf); |
| 313 |
lost_message_count ++; |
| 314 |
pthread_mutex_unlock(& buffer_heap_mutex); |
| 315 |
continue; |
| 316 |
} |
| 317 |
buffer_heap = new_buffer_heap; |
| 318 |
buffer_heap_alloc = new_buffer_heap_alloc; |
| 319 |
} |
| 320 |
// plop copy of message struct into slot at end of heap |
| 321 |
buffer_heap[buffer_heap_size++] = message; |
| 322 |
// push it into heap |
| 323 |
gheap_push_heap(&buffer_heap_ctx, |
| 324 |
buffer_heap, |
| 325 |
buffer_heap_size); |
| 326 |
// and c'est tout |
| 327 |
pthread_mutex_unlock(& buffer_heap_mutex); |
| 328 |
pthread_cond_broadcast (& buffer_heap_cond); |
| 329 |
dbug(3, "thread %d received seq=%u\n", cpu, message.bufhdr.sequence); |
| 330 |
} |
| 331 |
|
| 332 |
dbug(3, "exiting thread for cpu %d\n", cpu); |
| 333 |
return NULL; |
| 334 |
|
| 335 |
error_out: |
| 336 |
/* Signal the main thread that we need to quit */ |
| 337 |
kill(getpid(), SIGTERM); |
| 338 |
dbug(2, "exiting thread for cpu %d after error\n", cpu); |
| 339 |
|
| 340 |
return NULL; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
// Print and free buffer of given serialized message. |
| 345 |
static void print_serialized_message (struct serialized_message *msg) |
| 346 |
{ |
| 347 |
// check if file switching is necessary, as per staprun -S |
| 348 |
|
| 349 |
// NB: unlike reader_thread_bulkmode(), we don't need to use |
| 350 |
// mutexes to protect switch_file[] or such, because we're the |
| 351 |
// ONLY thread doing output. |
| 352 |
unsigned cpu = 0; // arbitrary |
| 353 |
static ssize_t wsize = 0; // how many bytes we've written into the serialized file so far |
| 354 |
static int fnum = 0; // which file number we're using |
| 355 |
|
| 356 |
if ((fsize_max && (wsize > fsize_max)) || |
| 357 |
(sigusr2_count > sigusr2_processed[cpu])) { |
| 358 |
dbug(2, "switching output file wsize=%ld fsize_max=%ld sigusr2 %d > %d\n", |
| 359 |
wsize, fsize_max, sigusr2_count, sigusr2_processed[cpu]); |
| 360 |
sigusr2_processed[cpu] = sigusr2_count; |
| 361 |
if (switch_outfile(cpu, &fnum) < 0) { |
| 362 |
perr("unable to switch output file"); |
| 363 |
// but continue |
| 364 |
} |
| 365 |
wsize = 0; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
// write loop ... could block if e.g. the output disk is slow |
| 370 |
// or the user hits a ^S (XOFF) on the tty |
| 371 |
ssize_t sent = 0; |
| 372 |
do { |
| 373 |
ssize_t ret = write (out_fd[avail_cpus[0]], |
| 374 |
msg->buf+sent, msg->bufhdr.pdu_len-sent); |
| 375 |
if (ret <= 0) { |
| 376 |
perr("error writing output"); |
| 377 |
break; |
| 378 |
} |
| 379 |
sent += ret; |
| 380 |
} while ((unsigned)sent < msg->bufhdr.pdu_len); |
| 381 |
wsize += sent; |
| 382 |
|
| 383 |
// free the associated buffer |
| 384 |
free (msg->buf); |
| 385 |
msg->buf = NULL; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
/* Thread that checks on the heap of messages, and pumps them out to |
| 390 |
the designated output fd in sequence. It waits, but only a little |
| 391 |
while, if it has only fresher messages than it's expecting. It |
| 392 |
exits upon a global stop_threads indication. |
| 393 |
*/ |
| 394 |
static void* reader_thread_serializer (void *data) { |
| 395 |
(void) data; |
| 396 |
while (! stop_threads) { |
| 397 |
/* timeout 0-1 seconds; this is the maximum extra time that |
| 398 |
stapio will be waiting after a ^C */ |
| 399 |
struct timespec ts = {.tv_sec=time(NULL)+1, .tv_nsec=0}; |
| 400 |
int rc; |
| 401 |
pthread_mutex_lock(& buffer_heap_mutex); |
| 402 |
rc = pthread_cond_timedwait (& buffer_heap_cond, |
| 403 |
& buffer_heap_mutex, |
| 404 |
& ts); |
| 405 |
|
| 406 |
dbug(3, "serializer cond wait rc=%d heapsize=%u\n", rc, buffer_heap_size); |
| 407 |
time_t now = time(NULL); |
| 408 |
unsigned processed = 0; |
| 409 |
while (buffer_heap_size > 0) { // consume as much as possible |
| 410 |
// check out the sequence# of the first element |
| 411 |
uint32_t buf_min_seq = buffer_heap[0].bufhdr.sequence; |
| 412 |
|
| 413 |
dbug(3, "serializer last=%u seq=%u\n", last_sequence_number, buf_min_seq); |
| 414 |
|
| 415 |
if ((buf_min_seq == last_sequence_number + 1) || // expected seq# |
| 416 |
(buffer_heap[0].received + 2 <= now)) { // message too old |
| 417 |
// "we've got one!" -- or waited too long for one |
| 418 |
// get it off the head of the heap |
| 419 |
gheap_pop_heap(&buffer_heap_ctx, |
| 420 |
buffer_heap, |
| 421 |
buffer_heap_size); |
| 422 |
buffer_heap_size --; // becomes index where the head was moved |
| 423 |
processed ++; |
| 424 |
|
| 425 |
// take a copy of the whole message |
| 426 |
struct serialized_message msg = buffer_heap[buffer_heap_size]; |
| 427 |
|
| 428 |
// paranoid clear this field of the now-unused slot |
| 429 |
buffer_heap[buffer_heap_size].buf = NULL; |
| 430 |
// update statistics |
| 431 |
if (attach_mod == 1 && last_sequence_number == 0) // first message after staprun -A |
| 432 |
; // do not penalize it with lost messages |
| 433 |
else |
| 434 |
lost_message_count += (buf_min_seq - last_sequence_number - 1); |
| 435 |
last_sequence_number = buf_min_seq; |
| 436 |
|
| 437 |
// unlock the mutex, permitting |
| 438 |
// reader_thread_serialmode threads to |
| 439 |
// resume piling messages into the |
| 440 |
// heap while we print stuff |
| 441 |
pthread_mutex_unlock(& buffer_heap_mutex); |
| 442 |
|
| 443 |
print_serialized_message (& msg); |
| 444 |
|
| 445 |
// must re-take lock for next iteration of the while loop |
| 446 |
pthread_mutex_lock(& buffer_heap_mutex); |
| 447 |
} else { |
| 448 |
// processed as much of the heap as we |
| 449 |
// could this time; wait for the |
| 450 |
// condition again |
| 451 |
break; |
| 452 |
} |
| 453 |
} |
| 454 |
pthread_mutex_unlock(& buffer_heap_mutex); |
| 455 |
if (processed > 0) |
| 456 |
dbug(2, "serializer processed n=%u\n", processed); |
| 457 |
} |
| 458 |
return NULL; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
// At the end of the program main loop, flush out any the remaining |
| 464 |
// messages and free up all that heapy data. |
| 465 |
static void reader_serialized_flush() |
| 466 |
{ |
| 467 |
dbug(3, "serializer flushing messages=%u\n", buffer_heap_size); |
| 468 |
while (buffer_heap_size > 0) { // consume it all |
| 469 |
// check out the sequence# of the first element |
| 470 |
uint32_t buf_min_seq = buffer_heap[0].bufhdr.sequence; |
| 471 |
dbug(3, "serializer seq=%u\n", buf_min_seq); |
| 472 |
gheap_pop_heap(&buffer_heap_ctx, |
| 473 |
buffer_heap, |
| 474 |
buffer_heap_size); |
| 475 |
buffer_heap_size --; // also index where the head was moved |
| 476 |
|
| 477 |
// NB: no need for mutex manipulations, this is super single threaded |
| 478 |
print_serialized_message (& buffer_heap[buffer_heap_size]); |
| 479 |
|
| 480 |
lost_message_count += (buf_min_seq - last_sequence_number - 1); |
| 481 |
last_sequence_number = buf_min_seq; |
| 482 |
} |
| 483 |
free (buffer_heap); |
| 484 |
buffer_heap = NULL; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
/** |
| 490 |
* reader_thread - per-cpu channel buffer reader, bulkmode (one output file per cpu input file) |
| 491 |
*/ |
| 492 |
static void *reader_thread_bulkmode (void *data) |
| 493 |
{ |
| 494 |
char buf[MAX_MESSAGE_LENGTH]; |
| 495 |
struct _stp_trace bufhdr; |
| 496 |
|
| 497 |
int rc, cpu = (int)(long)data; |
| 498 |
struct pollfd pollfd; |
| 499 |
sigset_t sigs; |
| 500 |
off_t wsize = 0; |
| 501 |
int fnum = 0; |
| 502 |
cpu_set_t cpu_mask; |
| 503 |
|
| 504 |
sigemptyset(&sigs); |
| 505 |
sigaddset(&sigs,SIGUSR2); |
| 506 |
pthread_sigmask(SIG_BLOCK, &sigs, NULL); |
| 507 |
|
| 508 |
sigfillset(&sigs); |
| 509 |
sigdelset(&sigs,SIGUSR2); |
| 510 |
|
| 511 |
CPU_ZERO(&cpu_mask); |
| 512 |
CPU_SET(cpu, &cpu_mask); |
| 513 |
if( sched_setaffinity( 0, sizeof(cpu_mask), &cpu_mask ) < 0 ) |
| 514 |
_perr("sched_setaffinity"); |
| 515 |
|
| 516 |
pollfd.fd = relay_fd[cpu]; |
| 517 |
pollfd.events = POLLIN; |
| 518 |
|
| 519 |
do { |
| 520 |
/* 200ms, close to human level of "instant" */ |
| 521 |
struct timespec tim, *timeout = &tim; |
| 522 |
timeout->tv_sec = reader_timeout_ms / 1000; |
| 523 |
timeout->tv_nsec = (reader_timeout_ms - timeout->tv_sec * 1000) * 1000000; |
| 524 |
|
| 525 |
rc = ppoll(&pollfd, 1, timeout, &sigs); |
| 526 |
if (rc < 0) { |
| 527 |
dbug(3, "cpu=%d poll=%d errno=%d\n", cpu, rc, errno); |
| 528 |
if (errno == EINTR) { |
| 529 |
if (stop_threads) |
| 530 |
break; |
| 531 |
|
| 532 |
if (sigusr2_count > sigusr2_processed[cpu]) { |
| 533 |
sigusr2_processed[cpu] = sigusr2_count; |
| 534 |
if (switch_outfile(cpu, &fnum) < 0) { |
| 535 |
goto error_out; |
| 536 |
} |
| 537 |
wsize = 0; |
| 538 |
} |
| 539 |
} else { |
| 540 |
_perr("poll error"); |
| 541 |
goto error_out; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
/* Read the header. */ |
| 546 |
rc = read(relay_fd[cpu], &bufhdr, sizeof(bufhdr)); |
| 547 |
if (rc <= 0) /* seen during normal shutdown */ |
| 548 |
continue; |
| 549 |
if (rc != sizeof(bufhdr)) { |
| 550 |
_perr("bufhdr read error, attempting resync"); |
| 551 |
rc = read(relay_fd[cpu], buf, sizeof(buf)); /* drain the buffers */ |
| 552 |
(void) rc; |
| 553 |
continue; |
| 554 |
} |
| 555 |
|
| 556 |
/* Validate it slightly. Because of lost messages, we might be getting |
| 557 |
not a proper _stp_trace struct but the interior of some piece of |
| 558 |
trace text message. XXX: validate bufhdr.sequence a little bit too? */ |
| 559 |
if (bufhdr.pdu_len == 0 || bufhdr.pdu_len > sizeof(buf)) { |
| 560 |
/* _perr("bufhdr corrupt, attempting resync"); */ |
| 561 |
rc = read(relay_fd[cpu], buf, sizeof(buf)); /* drain the buffers */ |
| 562 |
(void) rc; |
| 563 |
continue; /* may resync at next subbuf boundary so don't give up */ |
| 564 |
} |
| 565 |
|
| 566 |
/* Read the message, perhaps in pieces (such as if crossing |
| 567 |
* relayfs subbuf boundaries). */ |
| 568 |
size_t bufread = 0; |
| 569 |
while (bufread < bufhdr.pdu_len) { |
| 570 |
rc = read(relay_fd[cpu], buf+bufread, bufhdr.pdu_len-bufread); |
| 571 |
if (rc <= 0) { |
| 572 |
/* _perr("partial message received"); */ |
| 573 |
break; /* still process it; hope we can resync next time. */ |
| 574 |
} |
| 575 |
bufread += rc; |
| 576 |
} |
| 577 |
|
| 578 |
int wbytes = rc; |
| 579 |
char *wbuf = buf; |
| 580 |
|
| 581 |
dbug(3, "cpu %d: read %d bytes of data\n", cpu, rc); |
| 582 |
|
| 583 |
/* Switching file */ |
| 584 |
if ((fsize_max && ((wsize + rc) > fsize_max)) || |
| 585 |
(sigusr2_count > sigusr2_processed[cpu])) { |
| 586 |
sigusr2_processed[cpu] = sigusr2_count; |
| 587 |
if (switch_outfile(cpu, &fnum) < 0) { |
| 588 |
goto error_out; |
| 589 |
} |
| 590 |
wsize = 0; |
| 591 |
} |
| 592 |
|
| 593 |
/* Copy loop. Must repeat write(2) in case of a pipe overflow |
| 594 |
or other transient fullness. */ |
| 595 |
while (wbytes > 0) { |
| 596 |
if (monitor) { |
| 597 |
ssize_t bytes = wbytes > MONITORLINELENGTH ? MONITORLINELENGTH : wbytes; |
| 598 |
/* Start scanning the wbuf[] for lines - \n. |
| 599 |
Plop each one found into the h_queue.lines[] ring. */ |
| 600 |
char *p = wbuf; /* scan position */ |
| 601 |
char *p_end = wbuf + bytes; /* one past last byte */ |
| 602 |
char *line = p; |
| 603 |
while (p < p_end) { |
| 604 |
if (*p == '\n') { /* got a line */ |
| 605 |
monitor_remember_output_line(line, (p-line)+1); /* strlen, including \n */ |
| 606 |
line = p+1; |
| 607 |
} |
| 608 |
p++; |
| 609 |
} |
| 610 |
/* Flush remaining output */ |
| 611 |
if (line != p_end) |
| 612 |
monitor_remember_output_line(line, (p_end - line)); |
| 613 |
wbytes -= bytes; |
| 614 |
wbuf += bytes; |
| 615 |
wsize += bytes; |
| 616 |
} else { |
| 617 |
int fd; |
| 618 |
/* Only bulkmode and fsize_max use per-cpu output files. Otherwise, |
| 619 |
there's just a single output fd stored at out_fd[avail_cpus[0]]. */ |
| 620 |
fd = out_fd[cpu]; |
| 621 |
rc = write(fd, &bufhdr, sizeof(bufhdr)); // write header |
| 622 |
rc |= write(fd, wbuf, wbytes); // write payload |
| 623 |
if (rc <= 0) { |
| 624 |
perr("Couldn't write to output %d for cpu %d, exiting.", |
| 625 |
fd, cpu); |
| 626 |
goto error_out; |
| 627 |
} |
| 628 |
wbytes -= rc; |
| 629 |
wbuf += rc; |
| 630 |
wsize += rc; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
} while (!stop_threads); |
| 635 |
dbug(3, "exiting thread for cpu %d\n", cpu); |
| 636 |
return(NULL); |
| 637 |
|
| 638 |
error_out: |
| 639 |
/* Signal the main thread that we need to quit */ |
| 640 |
kill(getpid(), SIGTERM); |
| 641 |
dbug(2, "exiting thread for cpu %d after error\n", cpu); |
| 642 |
return(NULL); |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
static void switchfile_handler(int sig) |
| 647 |
{ |
| 648 |
(void) sig; |
| 649 |
if (stop_threads || !outfile_name) |
| 650 |
return; |
| 651 |
sigusr2_count ++; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
/** |
| 656 |
* init_relayfs - create files and threads for relayfs processing |
| 657 |
* |
| 658 |
* Returns 0 if successful, negative otherwise |
| 659 |
*/ |
| 660 |
int init_relayfs(void) |
| 661 |
{ |
| 662 |
int i, len; |
| 663 |
int cpui = 0; |
| 664 |
char rqbuf[128]; |
| 665 |
char buf[PATH_MAX]; |
| 666 |
struct sigaction sa; |
| 667 |
|
| 668 |
dbug(2, "initializing relayfs\n"); |
| 669 |
|
| 670 |
reader[0] = (pthread_t)0; |
| 671 |
relay_fd[0] = 0; |
| 672 |
out_fd[0] = 0; |
| 673 |
|
| 674 |
/* Find out whether probe module was compiled with STP_BULKMODE. */ |
| 675 |
if (send_request(STP_BULK, rqbuf, sizeof(rqbuf)) == 0) |
| 676 |
bulkmode = 1; |
| 677 |
|
| 678 |
/* Try to open a slew of per-cpu trace%d files. Per PR19241, |
| 679 |
we need to go through all potentially present CPUs up to |
| 680 |
get_nprocs_conf(), up to MAX_NR_CPUS (used for array |
| 681 |
allocations). For !bulknode, "trace0" will be typically |
| 682 |
used, prior to systemtap 4.5; after, all are used. */ |
| 683 |
|
| 684 |
int nprocs = get_nprocs_conf(); |
| 685 |
if (nprocs > MAX_NR_CPUS) { |
| 686 |
err("Too many CPUs: get_nprocs_conf()=%d vs MAX_NR_CPUS=%d\n", nprocs, MAX_NR_CPUS); |
| 687 |
return -1; |
| 688 |
} |
| 689 |
|
| 690 |
for (i = 0; i < nprocs; i++) { |
| 691 |
relay_fd[i] = -1; |
| 692 |
|
| 693 |
#ifdef HAVE_OPENAT |
| 694 |
if (relay_basedir_fd >= 0) { |
| 695 |
if (sprintf_chk(buf, "trace%d", i)) |
| 696 |
return -1; |
| 697 |
dbug(2, "attempting to openat %s\n", buf); |
| 698 |
relay_fd[i] = openat_cloexec(relay_basedir_fd, buf, O_RDONLY | O_NONBLOCK, 0); |
| 699 |
} |
| 700 |
#endif |
| 701 |
if (relay_fd[i] < 0) { |
| 702 |
if (sprintf_chk(buf, "/sys/kernel/debug/systemtap/%s/trace%d", |
| 703 |
modname, i)) |
| 704 |
return -1; |
| 705 |
dbug(2, "attempting to open %s\n", buf); |
| 706 |
relay_fd[i] = open_cloexec(buf, O_RDONLY | O_NONBLOCK, 0); |
| 707 |
} |
| 708 |
if (relay_fd[i] < 0) { |
| 709 |
if (sprintf_chk(buf, "/proc/systemtap/%s/trace%d", |
| 710 |
modname, i)) |
| 711 |
return -1; |
| 712 |
dbug(2, "attempting to open %s\n", buf); |
| 713 |
relay_fd[i] = open_cloexec(buf, O_RDONLY | O_NONBLOCK, 0); |
| 714 |
} |
| 715 |
if (relay_fd[i] >= 0) { |
| 716 |
avail_cpus[cpui++] = i; |
| 717 |
} |
| 718 |
} |
| 719 |
ncpus = cpui; |
| 720 |
/* ncpus could be smaller than nprocs if some cpus are offline */ |
| 721 |
dbug(2, "ncpus=%d, nprocs=%d, bulkmode=%d\n", ncpus, nprocs, bulkmode); |
| 722 |
for (i = 0; i < ncpus; i++) |
| 723 |
dbug(2, "cpui=%d, relayfd=%d\n", i, avail_cpus[i]); |
| 724 |
|
| 725 |
if (ncpus == 0) { |
| 726 |
_err("couldn't open %s: %s\n", buf, strerror(errno)); |
| 727 |
return -1; |
| 728 |
} |
| 729 |
|
| 730 |
/* PR7097 */ |
| 731 |
if (load_only) |
| 732 |
return 0; |
| 733 |
|
| 734 |
if (fsize_max) { |
| 735 |
/* switch file mode */ |
| 736 |
for (i = 0; i < ncpus; i++) { |
| 737 |
if (init_backlog(avail_cpus[i]) < 0) |
| 738 |
return -1; |
| 739 |
if (open_outfile(0, avail_cpus[i], 0) < 0) |
| 740 |
return -1; |
| 741 |
} |
| 742 |
} else if (bulkmode) { |
| 743 |
for (i = 0; i < ncpus; i++) { |
| 744 |
if (outfile_name) { |
| 745 |
/* special case: for testing we sometimes want to write to /dev/null */ |
| 746 |
if (strcmp(outfile_name, "/dev/null") == 0) { |
| 747 |
/* This strcpy() is OK, since |
| 748 |
* we know buf is PATH_MAX |
| 749 |
* bytes long. */ |
| 750 |
strcpy(buf, "/dev/null"); |
| 751 |
} else { |
| 752 |
len = stap_strfloctime(buf, PATH_MAX, |
| 753 |
outfile_name, time(NULL)); |
| 754 |
if (len < 0) { |
| 755 |
err("Invalid FILE name format\n"); |
| 756 |
return -1; |
| 757 |
} |
| 758 |
if (snprintf_chk(&buf[len], |
| 759 |
PATH_MAX - len, "_%d", avail_cpus[i])) |
| 760 |
return -1; |
| 761 |
} |
| 762 |
} else { |
| 763 |
if (sprintf_chk(buf, "stpd_cpu%d", avail_cpus[i])) |
| 764 |
return -1; |
| 765 |
} |
| 766 |
|
| 767 |
out_fd[avail_cpus[i]] = open_cloexec (buf, O_CREAT|O_TRUNC|O_WRONLY, 0666); |
| 768 |
if (out_fd[avail_cpus[i]] < 0) { |
| 769 |
perr("Couldn't open output file %s", buf); |
| 770 |
return -1; |
| 771 |
} |
| 772 |
} |
| 773 |
} else { |
| 774 |
/* stream mode */ |
| 775 |
if (outfile_name) { |
| 776 |
len = stap_strfloctime(buf, PATH_MAX, |
| 777 |
outfile_name, time(NULL)); |
| 778 |
if (len < 0) { |
| 779 |
err("Invalid FILE name format\n"); |
| 780 |
return -1; |
| 781 |
} |
| 782 |
out_fd[avail_cpus[0]] = open_cloexec (buf, O_CREAT|O_TRUNC|O_WRONLY, 0666); |
| 783 |
if (out_fd[avail_cpus[0]] < 0) { |
| 784 |
perr("Couldn't open output file %s", buf); |
| 785 |
return -1; |
| 786 |
} |
| 787 |
} else |
| 788 |
out_fd[avail_cpus[0]] = STDOUT_FILENO; |
| 789 |
} |
| 790 |
|
| 791 |
memset(&sa, 0, sizeof(sa)); |
| 792 |
sa.sa_handler = switchfile_handler; |
| 793 |
sa.sa_flags = 0; |
| 794 |
sigemptyset(&sa.sa_mask); |
| 795 |
sigaction(SIGUSR2, &sa, NULL); |
| 796 |
|
| 797 |
dbug(2, "starting threads\n"); |
| 798 |
for (i = 0; i < ncpus; i++) { |
| 799 |
if (pthread_create(&reader[avail_cpus[i]], NULL, |
| 800 |
bulkmode ? reader_thread_bulkmode : reader_thread_serialmode, |
| 801 |
(void *)(long)avail_cpus[i]) < 0) { |
| 802 |
_perr("failed to create thread"); |
| 803 |
return -1; |
| 804 |
} |
| 805 |
} |
| 806 |
if (! bulkmode) |
| 807 |
if (pthread_create(&serializer_thread, NULL, |
| 808 |
reader_thread_serializer, NULL) < 0) { |
| 809 |
_perr("failed to create thread"); |
| 810 |
return -1; |
| 811 |
} |
| 812 |
|
| 813 |
return 0; |
| 814 |
} |
| 815 |
|
| 816 |
void close_relayfs(void) |
| 817 |
{ |
| 818 |
int i; |
| 819 |
usleep(reader_timeout_ms*2*1000); /* PR31597, delay to drain buffers */ |
| 820 |
stop_threads = 1; |
| 821 |
dbug(2, "closing\n"); |
| 822 |
|
| 823 |
for (i = 0; i < ncpus; i++) { |
| 824 |
if (reader[avail_cpus[i]]) |
| 825 |
pthread_join(reader[avail_cpus[i]], NULL); |
| 826 |
else |
| 827 |
break; |
| 828 |
} |
| 829 |
if (! bulkmode) { |
| 830 |
if (serializer_thread) // =0 on load_only! |
| 831 |
pthread_join(serializer_thread, NULL); |
| 832 |
// at this point, we know all reader and writer |
| 833 |
// threads for the buffer_heap are dead. |
| 834 |
reader_serialized_flush(); |
| 835 |
|
| 836 |
if (lost_message_count > 0 || lost_byte_count > 0) |
| 837 |
eprintf("WARNING: There were %u lost messages and %u lost bytes.\n", |
| 838 |
lost_message_count, lost_byte_count); |
| 839 |
} |
| 840 |
|
| 841 |
for (i = 0; i < ncpus; i++) { |
| 842 |
if (relay_fd[avail_cpus[i]] >= 0) |
| 843 |
close(relay_fd[avail_cpus[i]]); |
| 844 |
else |
| 845 |
break; |
| 846 |
} |
| 847 |
dbug(2, "done\n"); |
| 848 |
} |
| 849 |
|
| 850 |
void kill_relayfs(void) |
| 851 |
{ |
| 852 |
int i; |
| 853 |
stop_threads = 1; |
| 854 |
dbug(2, "killing\n"); |
| 855 |
for (i = 0; i < ncpus; i++) { |
| 856 |
if (reader[avail_cpus[i]]) |
| 857 |
pthread_cancel(reader[avail_cpus[i]]); /* no wait */ |
| 858 |
else |
| 859 |
break; |
| 860 |
} |
| 861 |
for (i = 0; i < ncpus; i++) { |
| 862 |
if (relay_fd[avail_cpus[i]] >= 0) |
| 863 |
close(relay_fd[avail_cpus[i]]); |
| 864 |
else |
| 865 |
break; |
| 866 |
} |
| 867 |
dbug(2, "done\n"); |
| 868 |
} |