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 _IO(0xF4, 0x01)
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 static int sig_parent
= 0;
71 volatile static int quit_program
= 0; /* For signal handler */
75 * -t directory Directory name of the trace to write to. Will be created.
76 * -c directory Root directory of the relayfs trace channels.
77 * -d Run in background (daemon).
78 * -a Trace append mode.
79 * -s Send SIGIO to parent when ready for IO.
81 void show_arguments(void)
83 printf("Please use the following arguments :\n");
85 printf("-t directory Directory name of the trace to write to.\n"
86 " It will be created.\n");
87 printf("-c directory Root directory of the relayfs trace channels.\n");
88 printf("-d Run in background (daemon).\n");
89 printf("-a Append to an possibly existing trace.\n");
90 printf("-s Send SIGIO to parent when ready for IO.\n");
97 * Parses the command line arguments.
99 * Returns 1 if the arguments were correct, but doesn't ask for program
100 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
102 int parse_arguments(int argc
, char **argv
)
108 if(strcmp(argv
[1], "-h") == 0) {
115 switch(argv
[argn
][0]) {
117 switch(argv
[argn
][1]) {
120 trace_name
= argv
[argn
+1];
126 channel_name
= argv
[argn
+1];
140 printf("Invalid argument '%s'.\n", argv
[argn
]);
146 printf("Invalid argument '%s'.\n", argv
[argn
]);
153 if(trace_name
== NULL
) {
154 printf("Please specify a trace name.\n");
159 if(channel_name
== NULL
) {
160 printf("Please specify a channel name.\n");
170 printf("Linux Trace Toolkit Trace Daemon\n");
172 printf("Reading from relayfs directory : %s\n", channel_name
);
173 printf("Writing to trace directory : %s\n", trace_name
);
178 /* signal handling */
180 static void handler(int signo
)
182 printf("Signal %d received : exiting cleanly\n", signo
);
188 int open_channel_trace_pairs(char *subchannel_name
, char *subtrace_name
,
189 struct channel_trace_fd
*fd_pairs
)
191 DIR *channel_dir
= opendir(subchannel_name
);
192 struct dirent
*entry
;
193 struct stat stat_buf
;
195 char path_channel
[PATH_MAX
];
196 int path_channel_len
;
197 char *path_channel_ptr
;
198 char path_trace
[PATH_MAX
];
200 char *path_trace_ptr
;
203 if(channel_dir
== NULL
) {
204 perror(subchannel_name
);
209 printf("Creating trace subdirectory %s\n", subtrace_name
);
210 ret
= mkdir(subtrace_name
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
212 if(errno
== EEXIST
&& append_mode
) {
213 printf("Appending to directory %s as resquested\n", subtrace_name
);
215 perror(subtrace_name
);
221 strncpy(path_channel
, subchannel_name
, PATH_MAX
-1);
222 path_channel_len
= strlen(path_channel
);
223 path_channel
[path_channel_len
] = '/';
225 path_channel_ptr
= path_channel
+ path_channel_len
;
227 strncpy(path_trace
, subtrace_name
, PATH_MAX
-1);
228 path_trace_len
= strlen(path_trace
);
229 path_trace
[path_trace_len
] = '/';
231 path_trace_ptr
= path_trace
+ path_trace_len
;
233 while((entry
= readdir(channel_dir
)) != NULL
) {
235 if(entry
->d_name
[0] == '.') continue;
237 strncpy(path_channel_ptr
, entry
->d_name
, PATH_MAX
- path_channel_len
);
238 strncpy(path_trace_ptr
, entry
->d_name
, PATH_MAX
- path_trace_len
);
240 ret
= stat(path_channel
, &stat_buf
);
242 perror(path_channel
);
246 printf("Channel file : %s\n", path_channel
);
248 if(S_ISDIR(stat_buf
.st_mode
)) {
250 printf("Entering channel subdirectory...\n");
251 ret
= open_channel_trace_pairs(path_channel
, path_trace
, fd_pairs
);
252 if(ret
< 0) continue;
253 } else if(S_ISREG(stat_buf
.st_mode
)) {
254 printf("Opening file.\n");
256 fd_pairs
->pair
= realloc(fd_pairs
->pair
,
257 ++fd_pairs
->num_pairs
* sizeof(struct fd_pair
));
259 /* Open the channel in read mode */
260 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
=
261 open(path_channel
, O_RDONLY
| O_NONBLOCK
);
262 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
== -1) {
263 perror(path_channel
);
264 fd_pairs
->num_pairs
--;
267 /* Open the trace in write mode, only append if append_mode */
268 ret
= stat(path_trace
, &stat_buf
);
271 printf("Appending to file %s as requested\n", path_trace
);
273 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
274 open(path_trace
, O_WRONLY
|O_APPEND
,
275 S_IRWXU
|S_IRWXG
|S_IRWXO
);
277 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
281 printf("File %s exists, cannot open. Try append mode.\n", path_trace
);
286 if(errno
== ENOENT
) {
287 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
288 open(path_trace
, O_WRONLY
|O_CREAT
|O_EXCL
,
289 S_IRWXU
|S_IRWXG
|S_IRWXO
);
290 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
299 closedir(channel_dir
);
305 int read_subbuffer(struct fd_pair
*pair
)
307 unsigned int subbuf_index
;
311 err
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF
,
313 printf("index : %u\n", subbuf_index
);
315 perror("Error in reserving sub buffer");
320 err
= TEMP_FAILURE_RETRY(write(pair
->trace
,
321 pair
->mmap
+ (subbuf_index
* pair
->subbuf_size
),
325 perror("Error in writing to file");
332 err
= ioctl(pair
->channel
, RELAYFS_PUT_SUBBUF
);
334 perror("Error in unreserving sub buffer");
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
;
415 /* Signal the parent that ready for IO */
416 if(sig_parent
) kill(getppid(), SIGIO
);
422 printf("Press a key for next poll...\n");
424 read(STDIN_FILENO
, &buf
, 1);
425 printf("Next poll (polling %d fd) :\n", fd_pairs
->num_pairs
);
428 /* Have we received a signal ? */
429 if(quit_program
) break;
431 num_rdy
= poll(pollfd
, fd_pairs
->num_pairs
, -1);
433 perror("Poll error");
437 printf("Data received\n");
439 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
440 switch(pollfd
[i
].revents
) {
442 printf("Error returned in polling fd %d.\n", pollfd
[i
].fd
);
446 printf("Polling fd %d tells it has hung up.\n", pollfd
[i
].fd
);
450 printf("Polling fd %d tells fd is not open.\n", pollfd
[i
].fd
);
454 printf("Urgent read on fd %d\n", pollfd
[i
].fd
);
455 /* Take care of high priority channels first. */
457 ret
|= read_subbuffer(&fd_pairs
->pair
[i
]);
461 /* If every FD has hung up, we end the read loop here */
462 if(num_hup
== fd_pairs
->num_pairs
) break;
465 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
466 switch(pollfd
[i
].revents
) {
468 /* Take care of low priority channels. */
469 printf("Normal read on fd %d\n", pollfd
[i
].fd
);
470 ret
|= read_subbuffer(&fd_pairs
->pair
[i
]);
481 /* munmap only the successfully mmapped indexes */
482 i
= fd_pairs
->num_pairs
;
486 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
489 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
491 perror("Error in munmap");
501 void close_channel_trace_pairs(struct channel_trace_fd
*fd_pairs
)
506 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
507 ret
= close(fd_pairs
->pair
[i
].channel
);
508 if(ret
== -1) perror("Close error on channel");
509 ret
= close(fd_pairs
->pair
[i
].trace
);
510 if(ret
== -1) perror("Close error on trace");
512 free(fd_pairs
->pair
);
515 int main(int argc
, char ** argv
)
519 struct channel_trace_fd fd_pairs
= { NULL
, 0 };
520 struct sigaction act
;
522 ret
= parse_arguments(argc
, argv
);
524 if(ret
!= 0) show_arguments();
525 if(ret
< 0) return EINVAL
;
526 if(ret
> 0) return 0;
538 printf("An error occured while forking.\n");
541 /* else, we are the child, continue... */
544 /* Connect the signal handlers */
545 act
.sa_handler
= handler
;
547 sigemptyset(&(act
.sa_mask
));
548 sigaddset(&(act
.sa_mask
), SIGTERM
);
549 sigaddset(&(act
.sa_mask
), SIGQUIT
);
550 sigaddset(&(act
.sa_mask
), SIGINT
);
551 sigaction(SIGTERM
, &act
, NULL
);
552 sigaction(SIGQUIT
, &act
, NULL
);
553 sigaction(SIGINT
, &act
, NULL
);
556 if(ret
= open_channel_trace_pairs(channel_name
, trace_name
, &fd_pairs
))
559 ret
= read_channels(&fd_pairs
);
562 close_channel_trace_pairs(&fd_pairs
);