3 * Linux Trace Toolkit Daemon
5 * This is a simple daemon that reads a few relayfs channels and save them in a
10 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
23 #include <sys/types.h>
35 #include <asm/ioctl.h>
36 #include <asm/types.h>
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)
59 unsigned int n_subbufs
;
60 unsigned int subbuf_size
;
62 pthread_mutex_t mutex
;
65 struct channel_trace_fd
{
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 */
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.
85 void show_arguments(void)
87 printf("Please use the following arguments :\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");
101 * Parses the command line arguments.
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.
106 int parse_arguments(int argc
, char **argv
)
112 if(strcmp(argv
[1], "-h") == 0) {
119 switch(argv
[argn
][0]) {
121 switch(argv
[argn
][1]) {
124 trace_name
= argv
[argn
+1];
130 channel_name
= argv
[argn
+1];
142 num_threads
= strtoul(argv
[argn
+1], NULL
, 0);
147 printf("Invalid argument '%s'.\n", argv
[argn
]);
153 printf("Invalid argument '%s'.\n", argv
[argn
]);
160 if(trace_name
== NULL
) {
161 printf("Please specify a trace name.\n");
166 if(channel_name
== NULL
) {
167 printf("Please specify a channel name.\n");
177 printf("Linux Trace Toolkit Trace Daemon\n");
179 printf("Reading from relayfs directory : %s\n", channel_name
);
180 printf("Writing to trace directory : %s\n", trace_name
);
185 /* signal handling */
187 static void handler(int signo
)
189 printf("Signal %d received : exiting cleanly\n", signo
);
195 int open_channel_trace_pairs(char *subchannel_name
, char *subtrace_name
,
196 struct channel_trace_fd
*fd_pairs
)
198 DIR *channel_dir
= opendir(subchannel_name
);
199 struct dirent
*entry
;
200 struct stat stat_buf
;
202 char path_channel
[PATH_MAX
];
203 int path_channel_len
;
204 char *path_channel_ptr
;
205 char path_trace
[PATH_MAX
];
207 char *path_trace_ptr
;
210 if(channel_dir
== NULL
) {
211 perror(subchannel_name
);
216 printf("Creating trace subdirectory %s\n", subtrace_name
);
217 ret
= mkdir(subtrace_name
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
219 if(errno
!= EEXIST
) {
220 perror(subtrace_name
);
226 strncpy(path_channel
, subchannel_name
, PATH_MAX
-1);
227 path_channel_len
= strlen(path_channel
);
228 path_channel
[path_channel_len
] = '/';
230 path_channel_ptr
= path_channel
+ path_channel_len
;
232 strncpy(path_trace
, subtrace_name
, PATH_MAX
-1);
233 path_trace_len
= strlen(path_trace
);
234 path_trace
[path_trace_len
] = '/';
236 path_trace_ptr
= path_trace
+ path_trace_len
;
238 while((entry
= readdir(channel_dir
)) != NULL
) {
240 if(entry
->d_name
[0] == '.') continue;
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
);
245 ret
= stat(path_channel
, &stat_buf
);
247 perror(path_channel
);
251 printf("Channel file : %s\n", path_channel
);
253 if(S_ISDIR(stat_buf
.st_mode
)) {
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");
261 fd_pairs
->pair
= realloc(fd_pairs
->pair
,
262 ++fd_pairs
->num_pairs
* sizeof(struct fd_pair
));
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
--;
272 /* Open the trace in write mode, only append if append_mode */
273 ret
= stat(path_trace
, &stat_buf
);
276 printf("Appending to file %s as requested\n", path_trace
);
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
);
282 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
286 printf("File %s exists, cannot open. Try append mode.\n", path_trace
);
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) {
304 closedir(channel_dir
);
310 int read_subbuffer(struct fd_pair
*pair
)
312 unsigned int consumed_old
;
316 err
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF
,
318 printf("cookie : %u\n", consumed_old
);
321 perror("Reserving sub buffer failed (everything is normal)");
325 err
= TEMP_FAILURE_RETRY(write(pair
->trace
,
327 + (consumed_old
& ((pair
->n_subbufs
* pair
->subbuf_size
)-1)),
332 perror("Error in writing to file");
338 err
= ioctl(pair
->channel
, RELAYFS_PUT_SUBBUF
, &consumed_old
);
341 if(errno
== -EFAULT
) {
342 perror("Error in unreserving sub buffer\n");
343 } else if(errno
== -EIO
) {
344 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
345 /* FIXME : we may delete the last written buffer if we wish. */
356 int map_channels(struct channel_trace_fd
*fd_pairs
)
361 if(fd_pairs
->num_pairs
<= 0) {
362 printf("No channel to read\n");
366 /* Get the subbuf sizes and number */
368 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
369 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
371 ret
= ioctl(pair
->channel
, RELAYFS_GET_N_SUBBUFS
,
374 perror("Error in getting the number of subbuffers");
377 ret
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF_SIZE
,
380 perror("Error in getting the size of the subbuffers");
383 ret
= pthread_mutex_init(&pair
->mutex
, NULL
); /* Fast mutex */
385 perror("Error in mutex init");
391 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
392 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
394 pair
->mmap
= mmap(0, pair
->subbuf_size
* pair
->n_subbufs
, PROT_READ
,
395 MAP_SHARED
, pair
->channel
, 0);
396 if(pair
->mmap
== MAP_FAILED
) {
397 perror("Mmap error");
402 goto end
; /* success */
405 /* munmap only the successfully mmapped indexes */
409 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
412 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
414 perror("Error in munmap");
426 int unmap_channels(struct channel_trace_fd
*fd_pairs
)
432 for(j
=0;j
<fd_pairs
->num_pairs
;j
++) {
433 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
436 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
438 perror("Error in munmap");
441 err_ret
= pthread_mutex_destroy(&pair
->mutex
);
443 perror("Error in mutex destroy");
456 * Read the relayfs channels and write them in the paired tracefiles.
458 * @fd_pairs : paired channels and trace files.
460 * returns (void*)0 on success, (void*)-1 on error.
462 * Note that the high priority polled channels are consumed first. We then poll
463 * again to see if these channels are still in priority. Only when no
464 * high priority channel is left, we start reading low priority channels.
466 * Note that a channel is considered high priority when the buffer is almost
470 void * read_channels(void *arg
)
472 struct pollfd
*pollfd
;
474 int num_rdy
, num_hup
;
477 struct channel_trace_fd
*fd_pairs
= (struct channel_trace_fd
*)arg
;
479 /* Start polling the FD */
481 pollfd
= malloc(fd_pairs
->num_pairs
* sizeof(struct pollfd
));
483 /* Note : index in pollfd is the same index as fd_pair->pair */
484 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
485 pollfd
[i
].fd
= fd_pairs
->pair
[i
].channel
;
486 pollfd
[i
].events
= POLLIN
|POLLPRI
;
493 printf("Press a key for next poll...\n");
495 read(STDIN_FILENO
, &buf
, 1);
496 printf("Next poll (polling %d fd) :\n", fd_pairs
->num_pairs
);
499 /* Have we received a signal ? */
500 if(quit_program
) break;
502 num_rdy
= poll(pollfd
, fd_pairs
->num_pairs
, -1);
504 perror("Poll error");
508 printf("Data received\n");
510 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
511 switch(pollfd
[i
].revents
) {
513 printf("Error returned in polling fd %d.\n", pollfd
[i
].fd
);
517 printf("Polling fd %d tells it has hung up.\n", pollfd
[i
].fd
);
521 printf("Polling fd %d tells fd is not open.\n", pollfd
[i
].fd
);
525 if(pthread_mutex_trylock(&fd_pairs
->pair
[i
].mutex
) == 0) {
526 printf("Urgent read on fd %d\n", pollfd
[i
].fd
);
527 /* Take care of high priority channels first. */
529 /* it's ok to have an unavailable subbuffer */
530 ret
= read_subbuffer(&fd_pairs
->pair
[i
]);
531 if(ret
== -EAGAIN
) ret
= 0;
533 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
].mutex
);
535 printf("Error in mutex unlock : %s\n", strerror(ret
));
540 /* If every FD has hung up, we end the read loop here */
541 if(num_hup
== fd_pairs
->num_pairs
) break;
544 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
545 switch(pollfd
[i
].revents
) {
547 if(pthread_mutex_trylock(&fd_pairs
->pair
[i
].mutex
) == 0) {
548 /* Take care of low priority channels. */
549 printf("Normal read on fd %d\n", pollfd
[i
].fd
);
550 /* it's ok to have an unavailable subbuffer */
551 ret
= read_subbuffer(&fd_pairs
->pair
[i
]);
552 if(ret
== -EAGAIN
) ret
= 0;
554 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
].mutex
);
556 printf("Error in mutex unlock : %s\n", strerror(ret
));
573 void close_channel_trace_pairs(struct channel_trace_fd
*fd_pairs
)
578 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
579 ret
= close(fd_pairs
->pair
[i
].channel
);
580 if(ret
== -1) perror("Close error on channel");
581 ret
= close(fd_pairs
->pair
[i
].trace
);
582 if(ret
== -1) perror("Close error on trace");
584 free(fd_pairs
->pair
);
587 int main(int argc
, char ** argv
)
590 struct channel_trace_fd fd_pairs
= { NULL
, 0 };
591 struct sigaction act
;
596 ret
= parse_arguments(argc
, argv
);
598 if(ret
!= 0) show_arguments();
599 if(ret
< 0) return EINVAL
;
600 if(ret
> 0) return 0;
608 perror("An error occured while daemonizing.");
613 /* Connect the signal handlers */
614 act
.sa_handler
= handler
;
616 sigemptyset(&(act
.sa_mask
));
617 sigaddset(&(act
.sa_mask
), SIGTERM
);
618 sigaddset(&(act
.sa_mask
), SIGQUIT
);
619 sigaddset(&(act
.sa_mask
), SIGINT
);
620 sigaction(SIGTERM
, &act
, NULL
);
621 sigaction(SIGQUIT
, &act
, NULL
);
622 sigaction(SIGINT
, &act
, NULL
);
625 if(ret
= open_channel_trace_pairs(channel_name
, trace_name
, &fd_pairs
))
628 if(ret
= map_channels(&fd_pairs
))
631 tids
= malloc(sizeof(pthread_t
) * num_threads
);
632 for(i
=0; i
<num_threads
; i
++) {
633 ret
= pthread_create(&tids
[i
], NULL
, read_channels
, &fd_pairs
);
635 perror("Error creating thread");
640 for(i
=0; i
<num_threads
; i
++) {
641 ret
= pthread_join(tids
[i
], &tret
);
643 perror("Error joining thread");
647 printf("Error %s occured in thread %u\n", strerror(-(int)tret
), i
);
653 ret
|= unmap_channels(&fd_pairs
);
656 close_channel_trace_pairs(&fd_pairs
);