autotools pthread lib check for lttd
[ltt-control.git] / ltt / branches / poly / 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;
617de8e1 62};
63
64struct channel_trace_fd {
65 struct fd_pair *pair;
66 int num_pairs;
67};
68
69static char *trace_name = NULL;
70static char *channel_name = NULL;
71static int daemon_mode = 0;
90ccaa9a 72static int append_mode = 0;
e54e1d5d 73static unsigned long num_threads = 1;
1d483eea 74volatile static int quit_program = 0; /* For signal handler */
617de8e1 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).
90ccaa9a 81 * -a Trace append mode.
ce2b5176 82 * -s Send SIGUSR1 to parent when ready for IO.
617de8e1 83 */
84void 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");
90ccaa9a 92 printf("-a Append to an possibly existing trace.\n");
e54e1d5d 93 printf("-n Number of threads to start.\n");
617de8e1 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 */
105int 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
90ccaa9a 116 while(argn < argc) {
617de8e1 117
118 switch(argv[argn][0]) {
119 case '-':
120 switch(argv[argn][1]) {
121 case 't':
90ccaa9a 122 if(argn+1 < argc) {
123 trace_name = argv[argn+1];
124 argn++;
125 }
617de8e1 126 break;
127 case 'c':
90ccaa9a 128 if(argn+1 < argc) {
129 channel_name = argv[argn+1];
130 argn++;
131 }
617de8e1 132 break;
133 case 'd':
134 daemon_mode = 1;
135 break;
90ccaa9a 136 case 'a':
137 append_mode = 1;
138 break;
e54e1d5d 139 case 'n':
140 if(argn+1 < argc) {
141 num_threads = strtoul(argv[argn+1], NULL, 0);
142 argn++;
143 }
144 break;
617de8e1 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
174void 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
1d483eea 184/* signal handling */
185
186static void handler(int signo)
187{
188 printf("Signal %d received : exiting cleanly\n", signo);
189 quit_program = 1;
190}
191
192
193
617de8e1 194int 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;
d304b1dd 207 int open_ret = 0;
617de8e1 208
209 if(channel_dir == NULL) {
210 perror(subchannel_name);
d304b1dd 211 open_ret = ENOENT;
212 goto end;
617de8e1 213 }
214
617de8e1 215 printf("Creating trace subdirectory %s\n", subtrace_name);
216 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
217 if(ret == -1) {
b1e3e7c7 218 if(errno != EEXIST) {
90ccaa9a 219 perror(subtrace_name);
d304b1dd 220 open_ret = -1;
221 goto end;
90ccaa9a 222 }
617de8e1 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;
90ccaa9a 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);
469206ed 268 fd_pairs->num_pairs--;
269 continue;
90ccaa9a 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) {
3357d360 275 printf("Appending to file %s as requested\n", path_trace);
90ccaa9a 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);
d304b1dd 286 open_ret = -1;
287 goto end;
90ccaa9a 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 }
617de8e1 299 }
617de8e1 300 }
301
d304b1dd 302end:
617de8e1 303 closedir(channel_dir);
304
d304b1dd 305 return open_ret;
617de8e1 306}
307
1d483eea 308
309int read_subbuffer(struct fd_pair *pair)
310{
4f31148b 311 unsigned int consumed_old;
469206ed 312 int err, ret;
1d483eea 313
314
469206ed 315 err = ioctl(pair->channel, RELAYFS_GET_SUBBUF,
4f31148b 316 &consumed_old);
317 printf("cookie : %u\n", consumed_old);
469206ed 318 if(err != 0) {
1d483eea 319 perror("Error in reserving sub buffer");
469206ed 320 ret = -EPERM;
321 goto get_error;
1d483eea 322 }
469206ed 323
324 err = TEMP_FAILURE_RETRY(write(pair->trace,
e4bed64a 325 pair->mmap
326 + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)),
1d483eea 327 pair->subbuf_size));
469206ed 328
329 if(err < 0) {
1d483eea 330 perror("Error in writing to file");
469206ed 331 ret = err;
332 goto write_error;
1d483eea 333 }
334
335
469206ed 336write_error:
4f31148b 337 err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF, &consumed_old);
469206ed 338 if(err != 0) {
4f31148b 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.");
ec8cce5a 344 /* FIXME : we may delete the last written buffer if we wish. */
4f31148b 345 ret = -EIO;
346 }
469206ed 347 goto get_error;
1d483eea 348 }
349
469206ed 350get_error:
351 return ret;
1d483eea 352}
353
354
617de8e1 355
e54e1d5d 356int map_channels(struct channel_trace_fd *fd_pairs)
617de8e1 357{
1d483eea 358 int i,j;
e54e1d5d 359 int ret=0;
1d483eea 360
469206ed 361 if(fd_pairs->num_pairs <= 0) {
362 printf("No channel to read\n");
363 goto end;
364 }
365
1d483eea 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];
90ccaa9a 370
1d483eea 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
e54e1d5d 398 /* Error handling */
399 /* munmap only the successfully mmapped indexes */
400munmap:
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
413end:
414 return ret;
415
416
417}
418
419
420int 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
457int 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
1d483eea 465 /* Start polling the FD */
466
90ccaa9a 467 pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd));
468
1d483eea 469 /* Note : index in pollfd is the same index as fd_pair->pair */
90ccaa9a 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) {
4f45ea55 476 high_prio = 0;
1d483eea 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);
469206ed 482 printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs);
1d483eea 483#endif //DEBUG
484
485 /* Have we received a signal ? */
486 if(quit_program) break;
487
90ccaa9a 488 num_rdy = poll(pollfd, fd_pairs->num_pairs, -1);
489 if(num_rdy == -1) {
490 perror("Poll error");
1d483eea 491 goto free_fd;
90ccaa9a 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);
1d483eea 500 num_hup++;
90ccaa9a 501 break;
502 case POLLHUP:
503 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
1d483eea 504 num_hup++;
90ccaa9a 505 break;
506 case POLLNVAL:
507 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
1d483eea 508 num_hup++;
90ccaa9a 509 break;
510 case POLLPRI:
1d483eea 511 printf("Urgent read on fd %d\n", pollfd[i].fd);
90ccaa9a 512 /* Take care of high priority channels first. */
4f45ea55 513 high_prio = 1;
1d483eea 514 ret |= read_subbuffer(&fd_pairs->pair[i]);
90ccaa9a 515 break;
4f45ea55 516 }
90ccaa9a 517 }
1d483eea 518 /* If every FD has hung up, we end the read loop here */
519 if(num_hup == fd_pairs->num_pairs) break;
90ccaa9a 520
4f45ea55 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. */
1d483eea 526 printf("Normal read on fd %d\n", pollfd[i].fd);
527 ret |= read_subbuffer(&fd_pairs->pair[i]);
4f45ea55 528 break;
529 }
530 }
90ccaa9a 531 }
532
533 }
534
1d483eea 535free_fd:
90ccaa9a 536 free(pollfd);
537
1d483eea 538end:
539 return ret;
617de8e1 540}
541
542
543void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
544{
90ccaa9a 545 int i;
546 int ret;
617de8e1 547
90ccaa9a 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);
617de8e1 555}
556
557int main(int argc, char ** argv)
558{
e54e1d5d 559 int ret = 0;
617de8e1 560 struct channel_trace_fd fd_pairs = { NULL, 0 };
1d483eea 561 struct sigaction act;
617de8e1 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) {
2253bb8e 572 ret = daemon(0, 0);
573
574 if(ret == -1) {
575 perror("An error occured while daemonizing.");
576 exit(-1);
577 }
578 }
617de8e1 579
1d483eea 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
e54e1d5d 591
617de8e1 592 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs))
90ccaa9a 593 goto close_channel;
617de8e1 594
e54e1d5d 595 if(ret = map_channels(&fd_pairs))
596 goto close_channel;
597
617de8e1 598 ret = read_channels(&fd_pairs);
599
e54e1d5d 600 ret |= unmap_channels(&fd_pairs);
601
90ccaa9a 602close_channel:
617de8e1 603 close_channel_trace_pairs(&fd_pairs);
604
617de8e1 605 return ret;
606}
This page took 0.049234 seconds and 4 git commands to generate.