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>
17 #include <sys/types.h>
28 #include <asm/ioctl.h>
29 #include <asm/types.h>
31 /* Get the next sub buffer that can be read. */
32 #define RELAYFS_GET_SUBBUF _IOR(0xF4, 0x00,__u32)
33 /* Release the oldest reserved (by "get") sub buffer. */
34 #define RELAYFS_PUT_SUBBUF _IO(0xF4, 0x01)
35 /* returns the number of sub buffers in the per cpu channel. */
36 #define RELAYFS_GET_N_SUBBUFS _IOR(0xF4, 0x02,__u32)
37 /* returns the size of the sub buffers. */
38 #define RELAYFS_GET_SUBBUF_SIZE _IOR(0xF4, 0x03,__u32)
52 unsigned int n_subbufs
;
53 unsigned int subbuf_size
;
57 struct channel_trace_fd
{
62 static char *trace_name
= NULL
;
63 static char *channel_name
= NULL
;
64 static int daemon_mode
= 0;
65 static int append_mode
= 0;
66 volatile static int quit_program
= 0; /* For signal handler */
70 * -t directory Directory name of the trace to write to. Will be created.
71 * -c directory Root directory of the relayfs trace channels.
72 * -d Run in background (daemon).
73 * -a Trace append mode.
75 void show_arguments(void)
77 printf("Please use the following arguments :\n");
79 printf("-t directory Directory name of the trace to write to.\n"
80 " It will be created.\n");
81 printf("-c directory Root directory of the relayfs trace channels.\n");
82 printf("-d Run in background (daemon).\n");
83 printf("-a Append to an possibly existing trace.\n");
90 * Parses the command line arguments.
92 * Returns 1 if the arguments were correct, but doesn't ask for program
93 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
95 int parse_arguments(int argc
, char **argv
)
101 if(strcmp(argv
[1], "-h") == 0) {
108 switch(argv
[argn
][0]) {
110 switch(argv
[argn
][1]) {
113 trace_name
= argv
[argn
+1];
119 channel_name
= argv
[argn
+1];
130 printf("Invalid argument '%s'.\n", argv
[argn
]);
136 printf("Invalid argument '%s'.\n", argv
[argn
]);
143 if(trace_name
== NULL
) {
144 printf("Please specify a trace name.\n");
149 if(channel_name
== NULL
) {
150 printf("Please specify a channel name.\n");
160 printf("Linux Trace Toolkit Trace Daemon\n");
162 printf("Reading from relayfs directory : %s\n", channel_name
);
163 printf("Writing to trace directory : %s\n", trace_name
);
168 /* signal handling */
170 static void handler(int signo
)
172 printf("Signal %d received : exiting cleanly\n", signo
);
178 int open_channel_trace_pairs(char *subchannel_name
, char *subtrace_name
,
179 struct channel_trace_fd
*fd_pairs
)
181 DIR *channel_dir
= opendir(subchannel_name
);
182 struct dirent
*entry
;
183 struct stat stat_buf
;
185 char path_channel
[PATH_MAX
];
186 int path_channel_len
;
187 char *path_channel_ptr
;
188 char path_trace
[PATH_MAX
];
190 char *path_trace_ptr
;
192 if(channel_dir
== NULL
) {
193 perror(subchannel_name
);
197 //FIXME : check if the directory already exist, and ask the user if he wants
198 //to append to the traces.
199 printf("Creating trace subdirectory %s\n", subtrace_name
);
200 ret
= mkdir(subtrace_name
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
202 if(errno
== EEXIST
&& append_mode
) {
203 printf("Appending to directory %s as resquested\n", subtrace_name
);
205 perror(subtrace_name
);
210 strncpy(path_channel
, subchannel_name
, PATH_MAX
-1);
211 path_channel_len
= strlen(path_channel
);
212 path_channel
[path_channel_len
] = '/';
214 path_channel_ptr
= path_channel
+ path_channel_len
;
216 strncpy(path_trace
, subtrace_name
, PATH_MAX
-1);
217 path_trace_len
= strlen(path_trace
);
218 path_trace
[path_trace_len
] = '/';
220 path_trace_ptr
= path_trace
+ path_trace_len
;
222 while((entry
= readdir(channel_dir
)) != NULL
) {
224 if(entry
->d_name
[0] == '.') continue;
226 strncpy(path_channel_ptr
, entry
->d_name
, PATH_MAX
- path_channel_len
);
227 strncpy(path_trace_ptr
, entry
->d_name
, PATH_MAX
- path_trace_len
);
229 ret
= stat(path_channel
, &stat_buf
);
231 perror(path_channel
);
235 printf("Channel file : %s\n", path_channel
);
237 if(S_ISDIR(stat_buf
.st_mode
)) {
239 printf("Entering channel subdirectory...\n");
240 ret
= open_channel_trace_pairs(path_channel
, path_trace
, fd_pairs
);
241 if(ret
< 0) continue;
242 } else if(S_ISREG(stat_buf
.st_mode
)) {
243 printf("Opening file.\n");
245 fd_pairs
->pair
= realloc(fd_pairs
->pair
,
246 ++fd_pairs
->num_pairs
* sizeof(struct fd_pair
));
248 /* Open the channel in read mode */
249 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
=
250 open(path_channel
, O_RDONLY
| O_NONBLOCK
);
251 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
== -1) {
252 perror(path_channel
);
254 /* Open the trace in write mode, only append if append_mode */
255 ret
= stat(path_trace
, &stat_buf
);
258 printf("Appending to file %s as resquested\n", path_trace
);
260 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
261 open(path_trace
, O_WRONLY
|O_APPEND
,
262 S_IRWXU
|S_IRWXG
|S_IRWXO
);
264 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
268 printf("File %s exists, cannot open. Try append mode.\n", path_trace
);
272 if(errno
== ENOENT
) {
273 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
274 open(path_trace
, O_WRONLY
|O_CREAT
|O_EXCL
,
275 S_IRWXU
|S_IRWXG
|S_IRWXO
);
276 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
284 closedir(channel_dir
);
290 int read_subbuffer(struct fd_pair
*pair
)
292 unsigned int subbuf_index
;
296 ret
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF
,
299 perror("Error in reserving sub buffer");
303 ret
= TEMP_FAILURE_RETRY(write(pair
->trace
,
304 pair
->mmap
+ (subbuf_index
* pair
->subbuf_size
),
308 perror("Error in writing to file");
313 ret
= ioctl(pair
->channel
, RELAYFS_PUT_SUBBUF
);
315 perror("Error in unreserving sub buffer");
327 * Read the realyfs channels and write them in the paired tracefiles.
329 * @fd_pairs : paired channels and trace files.
331 * returns 0 on success, -1 on error.
333 * Note that the high priority polled channels are consumed first. We then poll
334 * again to see if these channels are still in priority. Only when no
335 * high priority channel is left, we start reading low priority channels.
337 * Note that a channel is considered high priority when the buffer is almost
341 int read_channels(struct channel_trace_fd
*fd_pairs
)
343 struct pollfd
*pollfd
;
345 int num_rdy
, num_hup
;
349 /* Get the subbuf sizes and number */
351 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
352 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
354 ret
= ioctl(pair
->channel
, RELAYFS_GET_N_SUBBUFS
,
357 perror("Error in getting the number of subbuffers");
360 ret
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF_SIZE
,
363 perror("Error in getting the size of the subbuffers");
369 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
370 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
372 pair
->mmap
= mmap(0, pair
->subbuf_size
* pair
->n_subbufs
, PROT_READ
,
373 MAP_SHARED
, pair
->channel
, 0);
374 if(pair
->mmap
== MAP_FAILED
) {
375 perror("Mmap error");
381 /* Start polling the FD */
383 pollfd
= malloc(fd_pairs
->num_pairs
* sizeof(struct pollfd
));
385 /* Note : index in pollfd is the same index as fd_pair->pair */
386 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
387 pollfd
[i
].fd
= fd_pairs
->pair
[i
].channel
;
388 pollfd
[i
].events
= POLLIN
|POLLPRI
;
395 printf("Press a key for next poll...\n");
397 read(STDIN_FILENO
, &buf
, 1);
398 printf("Next poll :\n");
401 /* Have we received a signal ? */
402 if(quit_program
) break;
404 num_rdy
= poll(pollfd
, fd_pairs
->num_pairs
, -1);
406 perror("Poll error");
410 printf("Data received\n");
412 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
413 switch(pollfd
[i
].revents
) {
415 printf("Error returned in polling fd %d.\n", pollfd
[i
].fd
);
419 printf("Polling fd %d tells it has hung up.\n", pollfd
[i
].fd
);
423 printf("Polling fd %d tells fd is not open.\n", pollfd
[i
].fd
);
427 printf("Urgent read on fd %d\n", pollfd
[i
].fd
);
428 /* Take care of high priority channels first. */
430 ret
|= read_subbuffer(&fd_pairs
->pair
[i
]);
434 /* If every FD has hung up, we end the read loop here */
435 if(num_hup
== fd_pairs
->num_pairs
) break;
438 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
439 switch(pollfd
[i
].revents
) {
441 /* Take care of low priority channels. */
442 printf("Normal read on fd %d\n", pollfd
[i
].fd
);
443 ret
|= read_subbuffer(&fd_pairs
->pair
[i
]);
454 /* munmap only the successfully mmapped indexes */
455 i
= fd_pairs
->num_pairs
;
459 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
462 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
464 perror("Error in munmap");
474 void close_channel_trace_pairs(struct channel_trace_fd
*fd_pairs
)
479 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
480 ret
= close(fd_pairs
->pair
[i
].channel
);
481 if(ret
== -1) perror("Close error on channel");
482 ret
= close(fd_pairs
->pair
[i
].trace
);
483 if(ret
== -1) perror("Close error on trace");
485 free(fd_pairs
->pair
);
488 int main(int argc
, char ** argv
)
492 struct channel_trace_fd fd_pairs
= { NULL
, 0 };
493 struct sigaction act
;
495 ret
= parse_arguments(argc
, argv
);
497 if(ret
!= 0) show_arguments();
498 if(ret
< 0) return EINVAL
;
499 if(ret
> 0) return 0;
511 printf("An error occured while forking.\n");
514 /* else, we are the child, continue... */
517 /* Connect the signal handlers */
518 act
.sa_handler
= handler
;
520 sigemptyset(&(act
.sa_mask
));
521 sigaddset(&(act
.sa_mask
), SIGTERM
);
522 sigaddset(&(act
.sa_mask
), SIGQUIT
);
523 sigaddset(&(act
.sa_mask
), SIGINT
);
524 sigaction(SIGTERM
, &act
, NULL
);
525 sigaction(SIGQUIT
, &act
, NULL
);
526 sigaction(SIGINT
, &act
, NULL
);
529 if(ret
= open_channel_trace_pairs(channel_name
, trace_name
, &fd_pairs
))
532 ret
= read_channels(&fd_pairs
);
535 close_channel_trace_pairs(&fd_pairs
);