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");
336 err
= fsync(pair
->trace
);
339 perror("Error in writing to file");
344 err
= ioctl(pair
->channel
, RELAYFS_PUT_SUBBUF
, &consumed_old
);
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. */
362 int map_channels(struct channel_trace_fd
*fd_pairs
)
367 if(fd_pairs
->num_pairs
<= 0) {
368 printf("No channel to read\n");
372 /* Get the subbuf sizes and number */
374 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
375 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
377 ret
= ioctl(pair
->channel
, RELAYFS_GET_N_SUBBUFS
,
380 perror("Error in getting the number of subbuffers");
383 ret
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF_SIZE
,
386 perror("Error in getting the size of the subbuffers");
389 ret
= pthread_mutex_init(&pair
->mutex
, NULL
); /* Fast mutex */
391 perror("Error in mutex init");
397 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
398 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
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");
408 goto end
; /* success */
411 /* munmap only the successfully mmapped indexes */
415 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
418 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
420 perror("Error in munmap");
432 int unmap_channels(struct channel_trace_fd
*fd_pairs
)
438 for(j
=0;j
<fd_pairs
->num_pairs
;j
++) {
439 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
442 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
444 perror("Error in munmap");
447 err_ret
= pthread_mutex_destroy(&pair
->mutex
);
449 perror("Error in mutex destroy");
462 * Read the relayfs channels and write them in the paired tracefiles.
464 * @fd_pairs : paired channels and trace files.
466 * returns (void*)0 on success, (void*)-1 on error.
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.
472 * Note that a channel is considered high priority when the buffer is almost
476 void * read_channels(void *arg
)
478 struct pollfd
*pollfd
;
480 int num_rdy
, num_hup
;
483 struct channel_trace_fd
*fd_pairs
= (struct channel_trace_fd
*)arg
;
485 /* Start polling the FD */
487 pollfd
= malloc(fd_pairs
->num_pairs
* sizeof(struct pollfd
));
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
;
499 printf("Press a key for next poll...\n");
501 read(STDIN_FILENO
, &buf
, 1);
502 printf("Next poll (polling %d fd) :\n", fd_pairs
->num_pairs
);
505 /* Have we received a signal ? */
506 if(quit_program
) break;
508 num_rdy
= poll(pollfd
, fd_pairs
->num_pairs
, -1);
510 perror("Poll error");
514 printf("Data received\n");
516 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
517 switch(pollfd
[i
].revents
) {
519 printf("Error returned in polling fd %d.\n", pollfd
[i
].fd
);
523 printf("Polling fd %d tells it has hung up.\n", pollfd
[i
].fd
);
527 printf("Polling fd %d tells fd is not open.\n", pollfd
[i
].fd
);
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. */
535 /* it's ok to have an unavailable subbuffer */
536 ret
= read_subbuffer(&fd_pairs
->pair
[i
]);
537 if(ret
== -EAGAIN
) ret
= 0;
539 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
].mutex
);
541 printf("Error in mutex unlock : %s\n", strerror(ret
));
546 /* If every FD has hung up, we end the read loop here */
547 if(num_hup
== fd_pairs
->num_pairs
) break;
550 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
551 switch(pollfd
[i
].revents
) {
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;
560 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
].mutex
);
562 printf("Error in mutex unlock : %s\n", strerror(ret
));
579 void close_channel_trace_pairs(struct channel_trace_fd
*fd_pairs
)
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");
590 free(fd_pairs
->pair
);
593 int main(int argc
, char ** argv
)
596 struct channel_trace_fd fd_pairs
= { NULL
, 0 };
597 struct sigaction act
;
602 ret
= parse_arguments(argc
, argv
);
604 if(ret
!= 0) show_arguments();
605 if(ret
< 0) return EINVAL
;
606 if(ret
> 0) return 0;
614 perror("An error occured while daemonizing.");
619 /* Connect the signal handlers */
620 act
.sa_handler
= handler
;
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
);
631 if(ret
= open_channel_trace_pairs(channel_name
, trace_name
, &fd_pairs
))
634 if(ret
= map_channels(&fd_pairs
))
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
);
641 perror("Error creating thread");
646 for(i
=0; i
<num_threads
; i
++) {
647 ret
= pthread_join(tids
[i
], &tret
);
649 perror("Error joining thread");
653 printf("Error %s occured in thread %u\n", strerror((int)tret
), i
);
659 ret
|= unmap_channels(&fd_pairs
);
662 close_channel_trace_pairs(&fd_pairs
);