update 0.8.61
[lttv.git] / ltt-control / lttd / lttd.c
CommitLineData
6cecb9cf 1/* lttd
2 *
3 * Linux Trace Toolkit Daemon
4 *
5 * This is a simple daemon that reads a few relayfs channels and save them in a
6 * trace.
7 *
8 *
9 * Copyright 2005 -
10 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
11 */
12
4e4d11b3 13#ifdef HAVE_CONFIG_H
13474e75 14#include <config.h>
4e4d11b3 15#endif
16
c3e8c24f 17#define _REENTRANT
4e4d11b3 18#define _GNU_SOURCE
c3e8c24f 19#include <features.h>
6cecb9cf 20#include <stdio.h>
af4061b2 21#include <unistd.h>
6cecb9cf 22#include <errno.h>
23#include <sys/types.h>
24#include <sys/stat.h>
6cecb9cf 25#include <stdlib.h>
26#include <dirent.h>
27#include <string.h>
9e7dbe44 28#include <fcntl.h>
29#include <sys/poll.h>
af4061b2 30#include <sys/mman.h>
31#include <signal.h>
c3e8c24f 32#include <pthread.h>
af4061b2 33
34/* Relayfs IOCTL */
35#include <asm/ioctl.h>
36#include <asm/types.h>
37
38/* Get the next sub buffer that can be read. */
39#define RELAYFS_GET_SUBBUF _IOR(0xF4, 0x00,__u32)
40/* Release the oldest reserved (by "get") sub buffer. */
b2c7cb27 41#define RELAYFS_PUT_SUBBUF _IOW(0xF4, 0x01,__u32)
af4061b2 42/* returns the number of sub buffers in the per cpu channel. */
43#define RELAYFS_GET_N_SUBBUFS _IOR(0xF4, 0x02,__u32)
44/* returns the size of the sub buffers. */
45#define RELAYFS_GET_SUBBUF_SIZE _IOR(0xF4, 0x03,__u32)
46
47
6cecb9cf 48
49enum {
50 GET_SUBBUF,
51 PUT_SUBBUF,
52 GET_N_BUBBUFS,
53 GET_SUBBUF_SIZE
54};
55
56struct fd_pair {
57 int channel;
58 int trace;
af4061b2 59 unsigned int n_subbufs;
60 unsigned int subbuf_size;
61 void *mmap;
22acb617 62 pthread_mutex_t mutex;
6cecb9cf 63};
64
65struct channel_trace_fd {
66 struct fd_pair *pair;
67 int num_pairs;
68};
69
d24c12e7 70static char *trace_name = NULL;
71static char *channel_name = NULL;
72static int daemon_mode = 0;
73static int append_mode = 0;
74static unsigned long num_threads = 1;
af4061b2 75volatile static int quit_program = 0; /* For signal handler */
d24c12e7 76static int dump_flight_only = 0;
77static int dump_normal_only = 0;
6cecb9cf 78
79/* Args :
80 *
81 * -t directory Directory name of the trace to write to. Will be created.
82 * -c directory Root directory of the relayfs trace channels.
83 * -d Run in background (daemon).
9e7dbe44 84 * -a Trace append mode.
86a65fdb 85 * -s Send SIGUSR1 to parent when ready for IO.
6cecb9cf 86 */
87void show_arguments(void)
88{
89 printf("Please use the following arguments :\n");
90 printf("\n");
91 printf("-t directory Directory name of the trace to write to.\n"
92 " It will be created.\n");
93 printf("-c directory Root directory of the relayfs trace channels.\n");
94 printf("-d Run in background (daemon).\n");
9e7dbe44 95 printf("-a Append to an possibly existing trace.\n");
22acb617 96 printf("-N Number of threads to start.\n");
d24c12e7 97 printf("-f Dump only flight recorder channels.\n");
98 printf("-n Dump only normal channels.\n");
6cecb9cf 99 printf("\n");
100}
101
102
103/* parse_arguments
104 *
105 * Parses the command line arguments.
106 *
107 * Returns 1 if the arguments were correct, but doesn't ask for program
108 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
109 */
110int parse_arguments(int argc, char **argv)
111{
112 int ret = 0;
113 int argn = 1;
114
115 if(argc == 2) {
116 if(strcmp(argv[1], "-h") == 0) {
117 return 1;
118 }
119 }
120
9e7dbe44 121 while(argn < argc) {
6cecb9cf 122
123 switch(argv[argn][0]) {
124 case '-':
125 switch(argv[argn][1]) {
126 case 't':
9e7dbe44 127 if(argn+1 < argc) {
128 trace_name = argv[argn+1];
129 argn++;
130 }
6cecb9cf 131 break;
132 case 'c':
9e7dbe44 133 if(argn+1 < argc) {
134 channel_name = argv[argn+1];
135 argn++;
136 }
6cecb9cf 137 break;
138 case 'd':
139 daemon_mode = 1;
140 break;
9e7dbe44 141 case 'a':
142 append_mode = 1;
143 break;
22acb617 144 case 'N':
c3e8c24f 145 if(argn+1 < argc) {
146 num_threads = strtoul(argv[argn+1], NULL, 0);
147 argn++;
148 }
149 break;
d24c12e7 150 case 'f':
151 dump_flight_only = 1;
152 break;
153 case 'n':
154 dump_normal_only = 1;
155 break;
6cecb9cf 156 default:
157 printf("Invalid argument '%s'.\n", argv[argn]);
158 printf("\n");
159 ret = -1;
160 }
161 break;
162 default:
163 printf("Invalid argument '%s'.\n", argv[argn]);
164 printf("\n");
165 ret = -1;
166 }
167 argn++;
168 }
169
170 if(trace_name == NULL) {
171 printf("Please specify a trace name.\n");
172 printf("\n");
173 ret = -1;
174 }
175
176 if(channel_name == NULL) {
177 printf("Please specify a channel name.\n");
178 printf("\n");
179 ret = -1;
180 }
181
182 return ret;
183}
184
185void show_info(void)
186{
187 printf("Linux Trace Toolkit Trace Daemon\n");
188 printf("\n");
189 printf("Reading from relayfs directory : %s\n", channel_name);
190 printf("Writing to trace directory : %s\n", trace_name);
191 printf("\n");
192}
193
194
af4061b2 195/* signal handling */
196
197static void handler(int signo)
198{
199 printf("Signal %d received : exiting cleanly\n", signo);
200 quit_program = 1;
201}
202
203
204
6cecb9cf 205int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name,
206 struct channel_trace_fd *fd_pairs)
207{
208 DIR *channel_dir = opendir(subchannel_name);
209 struct dirent *entry;
210 struct stat stat_buf;
211 int ret;
212 char path_channel[PATH_MAX];
213 int path_channel_len;
214 char *path_channel_ptr;
215 char path_trace[PATH_MAX];
216 int path_trace_len;
217 char *path_trace_ptr;
4d508021 218 int open_ret = 0;
6cecb9cf 219
220 if(channel_dir == NULL) {
221 perror(subchannel_name);
02ba971e 222 open_ret = ENOENT;
4d508021 223 goto end;
6cecb9cf 224 }
225
6cecb9cf 226 printf("Creating trace subdirectory %s\n", subtrace_name);
227 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
228 if(ret == -1) {
3986d00b 229 if(errno != EEXIST) {
9e7dbe44 230 perror(subtrace_name);
4d508021 231 open_ret = -1;
02ba971e 232 goto end;
9e7dbe44 233 }
6cecb9cf 234 }
235
236 strncpy(path_channel, subchannel_name, PATH_MAX-1);
237 path_channel_len = strlen(path_channel);
238 path_channel[path_channel_len] = '/';
239 path_channel_len++;
240 path_channel_ptr = path_channel + path_channel_len;
241
242 strncpy(path_trace, subtrace_name, PATH_MAX-1);
243 path_trace_len = strlen(path_trace);
244 path_trace[path_trace_len] = '/';
245 path_trace_len++;
246 path_trace_ptr = path_trace + path_trace_len;
247
248 while((entry = readdir(channel_dir)) != NULL) {
249
250 if(entry->d_name[0] == '.') continue;
251
252 strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len);
253 strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len);
254
255 ret = stat(path_channel, &stat_buf);
256 if(ret == -1) {
257 perror(path_channel);
258 continue;
259 }
260
261 printf("Channel file : %s\n", path_channel);
262
263 if(S_ISDIR(stat_buf.st_mode)) {
264
265 printf("Entering channel subdirectory...\n");
266 ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs);
267 if(ret < 0) continue;
9e7dbe44 268 } else if(S_ISREG(stat_buf.st_mode)) {
d24c12e7 269 if(strncmp(entry->d_name, "flight-", sizeof("flight-")-1) != 0) {
270 if(dump_flight_only) {
271 printf("Skipping normal channel %s\n", path_channel);
272 continue;
273 }
274 } else {
275 if(dump_normal_only) {
276 printf("Skipping flight channel %s\n", path_channel);
277 continue;
278 }
279 }
9e7dbe44 280 printf("Opening file.\n");
281
282 fd_pairs->pair = realloc(fd_pairs->pair,
283 ++fd_pairs->num_pairs * sizeof(struct fd_pair));
284
285 /* Open the channel in read mode */
286 fd_pairs->pair[fd_pairs->num_pairs-1].channel =
287 open(path_channel, O_RDONLY | O_NONBLOCK);
288 if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) {
289 perror(path_channel);
13474e75 290 fd_pairs->num_pairs--;
291 continue;
9e7dbe44 292 }
293 /* Open the trace in write mode, only append if append_mode */
294 ret = stat(path_trace, &stat_buf);
295 if(ret == 0) {
296 if(append_mode) {
251b5c5e 297 printf("Appending to file %s as requested\n", path_trace);
9e7dbe44 298
299 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
300 open(path_trace, O_WRONLY|O_APPEND,
301 S_IRWXU|S_IRWXG|S_IRWXO);
302
303 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
304 perror(path_trace);
305 }
306 } else {
307 printf("File %s exists, cannot open. Try append mode.\n", path_trace);
02ba971e 308 open_ret = -1;
4d508021 309 goto end;
9e7dbe44 310 }
311 } else {
312 if(errno == ENOENT) {
313 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
314 open(path_trace, O_WRONLY|O_CREAT|O_EXCL,
315 S_IRWXU|S_IRWXG|S_IRWXO);
316 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
317 perror(path_trace);
318 }
319 }
320 }
6cecb9cf 321 }
6cecb9cf 322 }
323
02ba971e 324end:
6cecb9cf 325 closedir(channel_dir);
326
02ba971e 327 return open_ret;
6cecb9cf 328}
329
af4061b2 330
331int read_subbuffer(struct fd_pair *pair)
332{
12bcec97 333 unsigned int consumed_old;
22acb617 334 int err, ret=0;
af4061b2 335
336
13474e75 337 err = ioctl(pair->channel, RELAYFS_GET_SUBBUF,
12bcec97 338 &consumed_old);
339 printf("cookie : %u\n", consumed_old);
13474e75 340 if(err != 0) {
22acb617 341 ret = errno;
63bf9ed1 342 perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)");
13474e75 343 goto get_error;
af4061b2 344 }
13474e75 345
346 err = TEMP_FAILURE_RETRY(write(pair->trace,
2312de30 347 pair->mmap
348 + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)),
af4061b2 349 pair->subbuf_size));
13474e75 350
351 if(err < 0) {
22acb617 352 ret = errno;
af4061b2 353 perror("Error in writing to file");
13474e75 354 goto write_error;
af4061b2 355 }
c5fdfca5 356#if 0
357 err = fsync(pair->trace);
358 if(err < 0) {
359 ret = errno;
360 perror("Error in writing to file");
361 goto write_error;
362 }
363#endif //0
13474e75 364write_error:
12bcec97 365 err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF, &consumed_old);
13474e75 366 if(err != 0) {
22acb617 367 ret = errno;
63bf9ed1 368 if(errno == EFAULT) {
22acb617 369 perror("Error in unreserving sub buffer\n");
63bf9ed1 370 } else if(errno == EIO) {
12bcec97 371 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
db9d75c0 372 /* FIXME : we may delete the last written buffer if we wish. */
12bcec97 373 }
13474e75 374 goto get_error;
af4061b2 375 }
376
13474e75 377get_error:
378 return ret;
af4061b2 379}
380
381
6cecb9cf 382
c3e8c24f 383int map_channels(struct channel_trace_fd *fd_pairs)
6cecb9cf 384{
af4061b2 385 int i,j;
c3e8c24f 386 int ret=0;
af4061b2 387
13474e75 388 if(fd_pairs->num_pairs <= 0) {
389 printf("No channel to read\n");
390 goto end;
391 }
392
af4061b2 393 /* Get the subbuf sizes and number */
394
395 for(i=0;i<fd_pairs->num_pairs;i++) {
396 struct fd_pair *pair = &fd_pairs->pair[i];
9e7dbe44 397
af4061b2 398 ret = ioctl(pair->channel, RELAYFS_GET_N_SUBBUFS,
399 &pair->n_subbufs);
400 if(ret != 0) {
401 perror("Error in getting the number of subbuffers");
402 goto end;
403 }
404 ret = ioctl(pair->channel, RELAYFS_GET_SUBBUF_SIZE,
405 &pair->subbuf_size);
406 if(ret != 0) {
407 perror("Error in getting the size of the subbuffers");
408 goto end;
409 }
22acb617 410 ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */
411 if(ret != 0) {
412 perror("Error in mutex init");
413 goto end;
414 }
af4061b2 415 }
416
417 /* Mmap each FD */
418 for(i=0;i<fd_pairs->num_pairs;i++) {
419 struct fd_pair *pair = &fd_pairs->pair[i];
420
421 pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ,
422 MAP_SHARED, pair->channel, 0);
423 if(pair->mmap == MAP_FAILED) {
424 perror("Mmap error");
425 goto munmap;
426 }
427 }
428
22acb617 429 goto end; /* success */
af4061b2 430
c3e8c24f 431 /* Error handling */
432 /* munmap only the successfully mmapped indexes */
433munmap:
434 /* Munmap each FD */
435 for(j=0;j<i;j++) {
436 struct fd_pair *pair = &fd_pairs->pair[j];
437 int err_ret;
438
439 err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
440 if(err_ret != 0) {
441 perror("Error in munmap");
442 }
443 ret |= err_ret;
444 }
445
446end:
447 return ret;
448
449
450}
451
452
453int unmap_channels(struct channel_trace_fd *fd_pairs)
454{
455 int j;
456 int ret=0;
457
458 /* Munmap each FD */
459 for(j=0;j<fd_pairs->num_pairs;j++) {
460 struct fd_pair *pair = &fd_pairs->pair[j];
461 int err_ret;
462
463 err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
464 if(err_ret != 0) {
465 perror("Error in munmap");
466 }
467 ret |= err_ret;
22acb617 468 err_ret = pthread_mutex_destroy(&pair->mutex);
469 if(err_ret != 0) {
470 perror("Error in mutex destroy");
471 }
472 ret |= err_ret;
c3e8c24f 473 }
474
475 return ret;
476}
477
478
479/* read_channels
22acb617 480 *
481 * Thread worker.
c3e8c24f 482 *
483 * Read the relayfs channels and write them in the paired tracefiles.
484 *
485 * @fd_pairs : paired channels and trace files.
486 *
22acb617 487 * returns (void*)0 on success, (void*)-1 on error.
c3e8c24f 488 *
489 * Note that the high priority polled channels are consumed first. We then poll
490 * again to see if these channels are still in priority. Only when no
491 * high priority channel is left, we start reading low priority channels.
492 *
493 * Note that a channel is considered high priority when the buffer is almost
494 * full.
495 */
496
22acb617 497void * read_channels(void *arg)
c3e8c24f 498{
499 struct pollfd *pollfd;
500 int i,j;
501 int num_rdy, num_hup;
502 int high_prio;
22acb617 503 int ret = 0;
504 struct channel_trace_fd *fd_pairs = (struct channel_trace_fd *)arg;
c3e8c24f 505
af4061b2 506 /* Start polling the FD */
507
9e7dbe44 508 pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd));
509
af4061b2 510 /* Note : index in pollfd is the same index as fd_pair->pair */
9e7dbe44 511 for(i=0;i<fd_pairs->num_pairs;i++) {
512 pollfd[i].fd = fd_pairs->pair[i].channel;
513 pollfd[i].events = POLLIN|POLLPRI;
514 }
515
516 while(1) {
68047576 517 high_prio = 0;
af4061b2 518 num_hup = 0;
519#ifdef DEBUG
520 printf("Press a key for next poll...\n");
521 char buf[1];
522 read(STDIN_FILENO, &buf, 1);
13474e75 523 printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs);
af4061b2 524#endif //DEBUG
525
526 /* Have we received a signal ? */
527 if(quit_program) break;
528
9e7dbe44 529 num_rdy = poll(pollfd, fd_pairs->num_pairs, -1);
530 if(num_rdy == -1) {
531 perror("Poll error");
af4061b2 532 goto free_fd;
9e7dbe44 533 }
534
535 printf("Data received\n");
536
537 for(i=0;i<fd_pairs->num_pairs;i++) {
538 switch(pollfd[i].revents) {
539 case POLLERR:
540 printf("Error returned in polling fd %d.\n", pollfd[i].fd);
af4061b2 541 num_hup++;
9e7dbe44 542 break;
543 case POLLHUP:
544 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
af4061b2 545 num_hup++;
9e7dbe44 546 break;
547 case POLLNVAL:
548 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
af4061b2 549 num_hup++;
9e7dbe44 550 break;
551 case POLLPRI:
22acb617 552 if(pthread_mutex_trylock(&fd_pairs->pair[i].mutex) == 0) {
553 printf("Urgent read on fd %d\n", pollfd[i].fd);
554 /* Take care of high priority channels first. */
555 high_prio = 1;
556 /* it's ok to have an unavailable subbuffer */
557 ret = read_subbuffer(&fd_pairs->pair[i]);
63bf9ed1 558 if(ret == EAGAIN) ret = 0;
b8d28629 559
22acb617 560 ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex);
561 if(ret)
562 printf("Error in mutex unlock : %s\n", strerror(ret));
563 }
9e7dbe44 564 break;
68047576 565 }
9e7dbe44 566 }
af4061b2 567 /* If every FD has hung up, we end the read loop here */
568 if(num_hup == fd_pairs->num_pairs) break;
9e7dbe44 569
68047576 570 if(!high_prio) {
571 for(i=0;i<fd_pairs->num_pairs;i++) {
572 switch(pollfd[i].revents) {
573 case POLLIN:
22acb617 574 if(pthread_mutex_trylock(&fd_pairs->pair[i].mutex) == 0) {
575 /* Take care of low priority channels. */
576 printf("Normal read on fd %d\n", pollfd[i].fd);
577 /* it's ok to have an unavailable subbuffer */
578 ret = read_subbuffer(&fd_pairs->pair[i]);
63bf9ed1 579 if(ret == EAGAIN) ret = 0;
b8d28629 580
22acb617 581 ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex);
582 if(ret)
583 printf("Error in mutex unlock : %s\n", strerror(ret));
584 }
68047576 585 break;
586 }
587 }
9e7dbe44 588 }
589
590 }
591
af4061b2 592free_fd:
9e7dbe44 593 free(pollfd);
594
af4061b2 595end:
22acb617 596 return (void*)ret;
6cecb9cf 597}
598
599
600void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
601{
9e7dbe44 602 int i;
603 int ret;
6cecb9cf 604
9e7dbe44 605 for(i=0;i<fd_pairs->num_pairs;i++) {
606 ret = close(fd_pairs->pair[i].channel);
607 if(ret == -1) perror("Close error on channel");
608 ret = close(fd_pairs->pair[i].trace);
609 if(ret == -1) perror("Close error on trace");
610 }
611 free(fd_pairs->pair);
6cecb9cf 612}
613
614int main(int argc, char ** argv)
615{
c3e8c24f 616 int ret = 0;
6cecb9cf 617 struct channel_trace_fd fd_pairs = { NULL, 0 };
af4061b2 618 struct sigaction act;
22acb617 619 pthread_t *tids;
620 unsigned int i;
621 void *tret;
6cecb9cf 622
623 ret = parse_arguments(argc, argv);
624
625 if(ret != 0) show_arguments();
626 if(ret < 0) return EINVAL;
627 if(ret > 0) return 0;
628
629 show_info();
630
631 if(daemon_mode) {
4d20e474 632 ret = daemon(0, 0);
4d508021 633
634 if(ret == -1) {
635 perror("An error occured while daemonizing.");
636 exit(-1);
637 }
638 }
6cecb9cf 639
af4061b2 640 /* Connect the signal handlers */
641 act.sa_handler = handler;
642 act.sa_flags = 0;
643 sigemptyset(&(act.sa_mask));
644 sigaddset(&(act.sa_mask), SIGTERM);
645 sigaddset(&(act.sa_mask), SIGQUIT);
646 sigaddset(&(act.sa_mask), SIGINT);
647 sigaction(SIGTERM, &act, NULL);
648 sigaction(SIGQUIT, &act, NULL);
649 sigaction(SIGINT, &act, NULL);
650
c3e8c24f 651
6cecb9cf 652 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs))
9e7dbe44 653 goto close_channel;
6cecb9cf 654
c3e8c24f 655 if(ret = map_channels(&fd_pairs))
656 goto close_channel;
657
22acb617 658 tids = malloc(sizeof(pthread_t) * num_threads);
659 for(i=0; i<num_threads; i++) {
660 ret = pthread_create(&tids[i], NULL, read_channels, &fd_pairs);
661 if(ret) {
662 perror("Error creating thread");
663 break;
664 }
665 }
6cecb9cf 666
22acb617 667 for(i=0; i<num_threads; i++) {
668 ret = pthread_join(tids[i], &tret);
669 if(ret) {
670 perror("Error joining thread");
671 break;
672 }
673 if((int)tret != 0) {
c5fdfca5 674 printf("Error %s occured in thread %u\n", strerror((int)tret), i);
22acb617 675 }
676 }
677
678 free(tids);
679
c3e8c24f 680 ret |= unmap_channels(&fd_pairs);
681
9e7dbe44 682close_channel:
6cecb9cf 683 close_channel_trace_pairs(&fd_pairs);
684
6cecb9cf 685 return ret;
686}
This page took 0.06048 seconds and 4 git commands to generate.