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