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