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>
21 #include <sys/types.h>
32 #include <asm/ioctl.h>
33 #include <asm/types.h>
35 /* Get the next sub buffer that can be read. */
36 #define RELAYFS_GET_SUBBUF _IOR(0xF4, 0x00,__u32)
37 /* Release the oldest reserved (by "get") sub buffer. */
38 #define RELAYFS_PUT_SUBBUF _IOW(0xF4, 0x01,__u32)
39 /* returns the number of sub buffers in the per cpu channel. */
40 #define RELAYFS_GET_N_SUBBUFS _IOR(0xF4, 0x02,__u32)
41 /* returns the size of the sub buffers. */
42 #define RELAYFS_GET_SUBBUF_SIZE _IOR(0xF4, 0x03,__u32)
56 unsigned int n_subbufs
;
57 unsigned int subbuf_size
;
61 struct channel_trace_fd
{
66 static char *trace_name
= NULL
;
67 static char *channel_name
= NULL
;
68 static int daemon_mode
= 0;
69 static int append_mode
= 0;
70 volatile static int quit_program
= 0; /* For signal handler */
74 * -t directory Directory name of the trace to write to. Will be created.
75 * -c directory Root directory of the relayfs trace channels.
76 * -d Run in background (daemon).
77 * -a Trace append mode.
78 * -s Send SIGUSR1 to parent when ready for IO.
80 void show_arguments(void)
82 printf("Please use the following arguments :\n");
84 printf("-t directory Directory name of the trace to write to.\n"
85 " It will be created.\n");
86 printf("-c directory Root directory of the relayfs trace channels.\n");
87 printf("-d Run in background (daemon).\n");
88 printf("-a Append to an possibly existing trace.\n");
95 * Parses the command line arguments.
97 * Returns 1 if the arguments were correct, but doesn't ask for program
98 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
100 int parse_arguments(int argc
, char **argv
)
106 if(strcmp(argv
[1], "-h") == 0) {
113 switch(argv
[argn
][0]) {
115 switch(argv
[argn
][1]) {
118 trace_name
= argv
[argn
+1];
124 channel_name
= argv
[argn
+1];
135 printf("Invalid argument '%s'.\n", argv
[argn
]);
141 printf("Invalid argument '%s'.\n", argv
[argn
]);
148 if(trace_name
== NULL
) {
149 printf("Please specify a trace name.\n");
154 if(channel_name
== NULL
) {
155 printf("Please specify a channel name.\n");
165 printf("Linux Trace Toolkit Trace Daemon\n");
167 printf("Reading from relayfs directory : %s\n", channel_name
);
168 printf("Writing to trace directory : %s\n", trace_name
);
173 /* signal handling */
175 static void handler(int signo
)
177 printf("Signal %d received : exiting cleanly\n", signo
);
183 int open_channel_trace_pairs(char *subchannel_name
, char *subtrace_name
,
184 struct channel_trace_fd
*fd_pairs
)
186 DIR *channel_dir
= opendir(subchannel_name
);
187 struct dirent
*entry
;
188 struct stat stat_buf
;
190 char path_channel
[PATH_MAX
];
191 int path_channel_len
;
192 char *path_channel_ptr
;
193 char path_trace
[PATH_MAX
];
195 char *path_trace_ptr
;
198 if(channel_dir
== NULL
) {
199 perror(subchannel_name
);
204 printf("Creating trace subdirectory %s\n", subtrace_name
);
205 ret
= mkdir(subtrace_name
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
207 if(errno
!= EEXIST
) {
208 perror(subtrace_name
);
214 strncpy(path_channel
, subchannel_name
, PATH_MAX
-1);
215 path_channel_len
= strlen(path_channel
);
216 path_channel
[path_channel_len
] = '/';
218 path_channel_ptr
= path_channel
+ path_channel_len
;
220 strncpy(path_trace
, subtrace_name
, PATH_MAX
-1);
221 path_trace_len
= strlen(path_trace
);
222 path_trace
[path_trace_len
] = '/';
224 path_trace_ptr
= path_trace
+ path_trace_len
;
226 while((entry
= readdir(channel_dir
)) != NULL
) {
228 if(entry
->d_name
[0] == '.') continue;
230 strncpy(path_channel_ptr
, entry
->d_name
, PATH_MAX
- path_channel_len
);
231 strncpy(path_trace_ptr
, entry
->d_name
, PATH_MAX
- path_trace_len
);
233 ret
= stat(path_channel
, &stat_buf
);
235 perror(path_channel
);
239 printf("Channel file : %s\n", path_channel
);
241 if(S_ISDIR(stat_buf
.st_mode
)) {
243 printf("Entering channel subdirectory...\n");
244 ret
= open_channel_trace_pairs(path_channel
, path_trace
, fd_pairs
);
245 if(ret
< 0) continue;
246 } else if(S_ISREG(stat_buf
.st_mode
)) {
247 printf("Opening file.\n");
249 fd_pairs
->pair
= realloc(fd_pairs
->pair
,
250 ++fd_pairs
->num_pairs
* sizeof(struct fd_pair
));
252 /* Open the channel in read mode */
253 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
=
254 open(path_channel
, O_RDONLY
| O_NONBLOCK
);
255 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
== -1) {
256 perror(path_channel
);
257 fd_pairs
->num_pairs
--;
260 /* Open the trace in write mode, only append if append_mode */
261 ret
= stat(path_trace
, &stat_buf
);
264 printf("Appending to file %s as requested\n", path_trace
);
266 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
267 open(path_trace
, O_WRONLY
|O_APPEND
,
268 S_IRWXU
|S_IRWXG
|S_IRWXO
);
270 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
274 printf("File %s exists, cannot open. Try append mode.\n", path_trace
);
279 if(errno
== ENOENT
) {
280 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
281 open(path_trace
, O_WRONLY
|O_CREAT
|O_EXCL
,
282 S_IRWXU
|S_IRWXG
|S_IRWXO
);
283 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
292 closedir(channel_dir
);
298 int read_subbuffer(struct fd_pair
*pair
)
300 unsigned int consumed_old
;
304 err
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF
,
306 printf("cookie : %u\n", consumed_old
);
308 perror("Error in reserving sub buffer");
313 err
= TEMP_FAILURE_RETRY(write(pair
->trace
,
315 + (consumed_old
& ((pair
->n_subbufs
* pair
->subbuf_size
)-1)),
319 perror("Error in writing to file");
326 err
= ioctl(pair
->channel
, RELAYFS_PUT_SUBBUF
, &consumed_old
);
328 if(errno
== -EFAULT
) {
329 perror("Error in unreserving sub buffer");
331 } else if(errno
== -EIO
) {
332 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
333 /* FIXME : we may delete the last written buffer if we wish. */
346 * Read the realyfs channels and write them in the paired tracefiles.
348 * @fd_pairs : paired channels and trace files.
350 * returns 0 on success, -1 on error.
352 * Note that the high priority polled channels are consumed first. We then poll
353 * again to see if these channels are still in priority. Only when no
354 * high priority channel is left, we start reading low priority channels.
356 * Note that a channel is considered high priority when the buffer is almost
360 int read_channels(struct channel_trace_fd
*fd_pairs
)
362 struct pollfd
*pollfd
;
364 int num_rdy
, num_hup
;
368 if(fd_pairs
->num_pairs
<= 0) {
369 printf("No channel to read\n");
373 /* Get the subbuf sizes and number */
375 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
376 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
378 ret
= ioctl(pair
->channel
, RELAYFS_GET_N_SUBBUFS
,
381 perror("Error in getting the number of subbuffers");
384 ret
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF_SIZE
,
387 perror("Error in getting the size of the subbuffers");
393 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
394 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
396 pair
->mmap
= mmap(0, pair
->subbuf_size
* pair
->n_subbufs
, PROT_READ
,
397 MAP_SHARED
, pair
->channel
, 0);
398 if(pair
->mmap
== MAP_FAILED
) {
399 perror("Mmap error");
405 /* Start polling the FD */
407 pollfd
= malloc(fd_pairs
->num_pairs
* sizeof(struct pollfd
));
409 /* Note : index in pollfd is the same index as fd_pair->pair */
410 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
411 pollfd
[i
].fd
= fd_pairs
->pair
[i
].channel
;
412 pollfd
[i
].events
= POLLIN
|POLLPRI
;
419 printf("Press a key for next poll...\n");
421 read(STDIN_FILENO
, &buf
, 1);
422 printf("Next poll (polling %d fd) :\n", fd_pairs
->num_pairs
);
425 /* Have we received a signal ? */
426 if(quit_program
) break;
428 num_rdy
= poll(pollfd
, fd_pairs
->num_pairs
, -1);
430 perror("Poll error");
434 printf("Data received\n");
436 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
437 switch(pollfd
[i
].revents
) {
439 printf("Error returned in polling fd %d.\n", pollfd
[i
].fd
);
443 printf("Polling fd %d tells it has hung up.\n", pollfd
[i
].fd
);
447 printf("Polling fd %d tells fd is not open.\n", pollfd
[i
].fd
);
451 printf("Urgent read on fd %d\n", pollfd
[i
].fd
);
452 /* Take care of high priority channels first. */
454 ret
|= read_subbuffer(&fd_pairs
->pair
[i
]);
458 /* If every FD has hung up, we end the read loop here */
459 if(num_hup
== fd_pairs
->num_pairs
) break;
462 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
463 switch(pollfd
[i
].revents
) {
465 /* Take care of low priority channels. */
466 printf("Normal read on fd %d\n", pollfd
[i
].fd
);
467 ret
|= read_subbuffer(&fd_pairs
->pair
[i
]);
478 /* munmap only the successfully mmapped indexes */
479 i
= fd_pairs
->num_pairs
;
483 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
486 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
488 perror("Error in munmap");
498 void close_channel_trace_pairs(struct channel_trace_fd
*fd_pairs
)
503 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
504 ret
= close(fd_pairs
->pair
[i
].channel
);
505 if(ret
== -1) perror("Close error on channel");
506 ret
= close(fd_pairs
->pair
[i
].trace
);
507 if(ret
== -1) perror("Close error on trace");
509 free(fd_pairs
->pair
);
512 int main(int argc
, char ** argv
)
515 struct channel_trace_fd fd_pairs
= { NULL
, 0 };
516 struct sigaction act
;
518 ret
= parse_arguments(argc
, argv
);
520 if(ret
!= 0) show_arguments();
521 if(ret
< 0) return EINVAL
;
522 if(ret
> 0) return 0;
530 perror("An error occured while daemonizing.");
535 /* Connect the signal handlers */
536 act
.sa_handler
= handler
;
538 sigemptyset(&(act
.sa_mask
));
539 sigaddset(&(act
.sa_mask
), SIGTERM
);
540 sigaddset(&(act
.sa_mask
), SIGQUIT
);
541 sigaddset(&(act
.sa_mask
), SIGINT
);
542 sigaction(SIGTERM
, &act
, NULL
);
543 sigaction(SIGQUIT
, &act
, NULL
);
544 sigaction(SIGINT
, &act
, NULL
);
547 if(ret
= open_channel_trace_pairs(channel_name
, trace_name
, &fd_pairs
))
550 ret
= read_channels(&fd_pairs
);
553 close_channel_trace_pairs(&fd_pairs
);