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