update build
[ltt-control.git] / ltt-control / lttd / lttd.c
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
13 #ifdef HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #define _REENTRANT
18 #define _GNU_SOURCE
19 #include <features.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <stdlib.h>
26 #include <dirent.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <sys/poll.h>
30 #include <sys/mman.h>
31 #include <signal.h>
32 #include <pthread.h>
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. */
41 #define RELAYFS_PUT_SUBBUF _IOW(0xF4, 0x01,__u32)
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
48
49 enum {
50 GET_SUBBUF,
51 PUT_SUBBUF,
52 GET_N_BUBBUFS,
53 GET_SUBBUF_SIZE
54 };
55
56 struct fd_pair {
57 int channel;
58 int trace;
59 unsigned int n_subbufs;
60 unsigned int subbuf_size;
61 void *mmap;
62 pthread_mutex_t mutex;
63 };
64
65 struct channel_trace_fd {
66 struct fd_pair *pair;
67 int num_pairs;
68 };
69
70 static char *trace_name = NULL;
71 static char *channel_name = NULL;
72 static int daemon_mode = 0;
73 static int append_mode = 0;
74 static unsigned long num_threads = 1;
75 volatile static int quit_program = 0; /* For signal handler */
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).
82 * -a Trace append mode.
83 * -s Send SIGUSR1 to parent when ready for IO.
84 */
85 void 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");
93 printf("-a Append to an possibly existing trace.\n");
94 printf("-N Number of threads to start.\n");
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 */
106 int 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
117 while(argn < argc) {
118
119 switch(argv[argn][0]) {
120 case '-':
121 switch(argv[argn][1]) {
122 case 't':
123 if(argn+1 < argc) {
124 trace_name = argv[argn+1];
125 argn++;
126 }
127 break;
128 case 'c':
129 if(argn+1 < argc) {
130 channel_name = argv[argn+1];
131 argn++;
132 }
133 break;
134 case 'd':
135 daemon_mode = 1;
136 break;
137 case 'a':
138 append_mode = 1;
139 break;
140 case 'N':
141 if(argn+1 < argc) {
142 num_threads = strtoul(argv[argn+1], NULL, 0);
143 argn++;
144 }
145 break;
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
175 void 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
185 /* signal handling */
186
187 static void handler(int signo)
188 {
189 printf("Signal %d received : exiting cleanly\n", signo);
190 quit_program = 1;
191 }
192
193
194
195 int 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;
208 int open_ret = 0;
209
210 if(channel_dir == NULL) {
211 perror(subchannel_name);
212 open_ret = ENOENT;
213 goto end;
214 }
215
216 printf("Creating trace subdirectory %s\n", subtrace_name);
217 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
218 if(ret == -1) {
219 if(errno != EEXIST) {
220 perror(subtrace_name);
221 open_ret = -1;
222 goto end;
223 }
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;
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);
269 fd_pairs->num_pairs--;
270 continue;
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) {
276 printf("Appending to file %s as requested\n", path_trace);
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);
287 open_ret = -1;
288 goto end;
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 }
300 }
301 }
302
303 end:
304 closedir(channel_dir);
305
306 return open_ret;
307 }
308
309
310 int read_subbuffer(struct fd_pair *pair)
311 {
312 unsigned int consumed_old;
313 int err, ret=0;
314
315
316 err = ioctl(pair->channel, RELAYFS_GET_SUBBUF,
317 &consumed_old);
318 printf("cookie : %u\n", consumed_old);
319 if(err != 0) {
320 ret = errno;
321 perror("Reserving sub buffer failed (everything is normal)");
322 goto get_error;
323 }
324
325 err = TEMP_FAILURE_RETRY(write(pair->trace,
326 pair->mmap
327 + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)),
328 pair->subbuf_size));
329
330 if(err < 0) {
331 ret = errno;
332 perror("Error in writing to file");
333 goto write_error;
334 }
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
343 write_error:
344 err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF, &consumed_old);
345 if(err != 0) {
346 ret = errno;
347 if(errno == -EFAULT) {
348 perror("Error in unreserving sub buffer\n");
349 } else if(errno == -EIO) {
350 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
351 /* FIXME : we may delete the last written buffer if we wish. */
352 }
353 goto get_error;
354 }
355
356 get_error:
357 return ret;
358 }
359
360
361
362 int map_channels(struct channel_trace_fd *fd_pairs)
363 {
364 int i,j;
365 int ret=0;
366
367 if(fd_pairs->num_pairs <= 0) {
368 printf("No channel to read\n");
369 goto end;
370 }
371
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];
376
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 }
389 ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */
390 if(ret != 0) {
391 perror("Error in mutex init");
392 goto end;
393 }
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
408 goto end; /* success */
409
410 /* Error handling */
411 /* munmap only the successfully mmapped indexes */
412 munmap:
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
425 end:
426 return ret;
427
428
429 }
430
431
432 int 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;
447 err_ret = pthread_mutex_destroy(&pair->mutex);
448 if(err_ret != 0) {
449 perror("Error in mutex destroy");
450 }
451 ret |= err_ret;
452 }
453
454 return ret;
455 }
456
457
458 /* read_channels
459 *
460 * Thread worker.
461 *
462 * Read the relayfs channels and write them in the paired tracefiles.
463 *
464 * @fd_pairs : paired channels and trace files.
465 *
466 * returns (void*)0 on success, (void*)-1 on error.
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
476 void * read_channels(void *arg)
477 {
478 struct pollfd *pollfd;
479 int i,j;
480 int num_rdy, num_hup;
481 int high_prio;
482 int ret = 0;
483 struct channel_trace_fd *fd_pairs = (struct channel_trace_fd *)arg;
484
485 /* Start polling the FD */
486
487 pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd));
488
489 /* Note : index in pollfd is the same index as fd_pair->pair */
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) {
496 high_prio = 0;
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);
502 printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs);
503 #endif //DEBUG
504
505 /* Have we received a signal ? */
506 if(quit_program) break;
507
508 num_rdy = poll(pollfd, fd_pairs->num_pairs, -1);
509 if(num_rdy == -1) {
510 perror("Poll error");
511 goto free_fd;
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);
520 num_hup++;
521 break;
522 case POLLHUP:
523 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
524 num_hup++;
525 break;
526 case POLLNVAL:
527 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
528 num_hup++;
529 break;
530 case POLLPRI:
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]);
537 if(ret == -EAGAIN) ret = 0;
538
539 ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex);
540 if(ret)
541 printf("Error in mutex unlock : %s\n", strerror(ret));
542 }
543 break;
544 }
545 }
546 /* If every FD has hung up, we end the read loop here */
547 if(num_hup == fd_pairs->num_pairs) break;
548
549 if(!high_prio) {
550 for(i=0;i<fd_pairs->num_pairs;i++) {
551 switch(pollfd[i].revents) {
552 case POLLIN:
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]);
558 if(ret == -EAGAIN) ret = 0;
559
560 ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex);
561 if(ret)
562 printf("Error in mutex unlock : %s\n", strerror(ret));
563 }
564 break;
565 }
566 }
567 }
568
569 }
570
571 free_fd:
572 free(pollfd);
573
574 end:
575 return (void*)ret;
576 }
577
578
579 void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
580 {
581 int i;
582 int ret;
583
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);
591 }
592
593 int main(int argc, char ** argv)
594 {
595 int ret = 0;
596 struct channel_trace_fd fd_pairs = { NULL, 0 };
597 struct sigaction act;
598 pthread_t *tids;
599 unsigned int i;
600 void *tret;
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) {
611 ret = daemon(0, 0);
612
613 if(ret == -1) {
614 perror("An error occured while daemonizing.");
615 exit(-1);
616 }
617 }
618
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
630
631 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs))
632 goto close_channel;
633
634 if(ret = map_channels(&fd_pairs))
635 goto close_channel;
636
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 }
645
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) {
653 printf("Error %s occured in thread %u\n", strerror((int)tret), i);
654 }
655 }
656
657 free(tids);
658
659 ret |= unmap_channels(&fd_pairs);
660
661 close_channel:
662 close_channel_trace_pairs(&fd_pairs);
663
664 return ret;
665 }
This page took 0.043432 seconds and 4 git commands to generate.