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 printf("Creating trace subdirectory %s\n", subtrace_name
);
198 ret
= mkdir(subtrace_name
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
200 if(errno
== EEXIST
&& append_mode
) {
201 printf("Appending to directory %s as resquested\n", subtrace_name
);
203 perror(subtrace_name
);
208 strncpy(path_channel
, subchannel_name
, PATH_MAX
-1);
209 path_channel_len
= strlen(path_channel
);
210 path_channel
[path_channel_len
] = '/';
212 path_channel_ptr
= path_channel
+ path_channel_len
;
214 strncpy(path_trace
, subtrace_name
, PATH_MAX
-1);
215 path_trace_len
= strlen(path_trace
);
216 path_trace
[path_trace_len
] = '/';
218 path_trace_ptr
= path_trace
+ path_trace_len
;
220 while((entry
= readdir(channel_dir
)) != NULL
) {
222 if(entry
->d_name
[0] == '.') continue;
224 strncpy(path_channel_ptr
, entry
->d_name
, PATH_MAX
- path_channel_len
);
225 strncpy(path_trace_ptr
, entry
->d_name
, PATH_MAX
- path_trace_len
);
227 ret
= stat(path_channel
, &stat_buf
);
229 perror(path_channel
);
233 printf("Channel file : %s\n", path_channel
);
235 if(S_ISDIR(stat_buf
.st_mode
)) {
237 printf("Entering channel subdirectory...\n");
238 ret
= open_channel_trace_pairs(path_channel
, path_trace
, fd_pairs
);
239 if(ret
< 0) continue;
240 } else if(S_ISREG(stat_buf
.st_mode
)) {
241 printf("Opening file.\n");
243 fd_pairs
->pair
= realloc(fd_pairs
->pair
,
244 ++fd_pairs
->num_pairs
* sizeof(struct fd_pair
));
246 /* Open the channel in read mode */
247 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
=
248 open(path_channel
, O_RDONLY
| O_NONBLOCK
);
249 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
== -1) {
250 perror(path_channel
);
252 /* Open the trace in write mode, only append if append_mode */
253 ret
= stat(path_trace
, &stat_buf
);
256 printf("Appending to file %s as requested\n", path_trace
);
258 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
259 open(path_trace
, O_WRONLY
|O_APPEND
,
260 S_IRWXU
|S_IRWXG
|S_IRWXO
);
262 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
266 printf("File %s exists, cannot open. Try append mode.\n", path_trace
);
270 if(errno
== ENOENT
) {
271 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
272 open(path_trace
, O_WRONLY
|O_CREAT
|O_EXCL
,
273 S_IRWXU
|S_IRWXG
|S_IRWXO
);
274 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
282 closedir(channel_dir
);
288 int read_subbuffer(struct fd_pair
*pair
)
290 unsigned int subbuf_index
;
294 ret
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF
,
297 perror("Error in reserving sub buffer");
301 ret
= TEMP_FAILURE_RETRY(write(pair
->trace
,
302 pair
->mmap
+ (subbuf_index
* pair
->subbuf_size
),
306 perror("Error in writing to file");
311 ret
= ioctl(pair
->channel
, RELAYFS_PUT_SUBBUF
);
313 perror("Error in unreserving sub buffer");
325 * Read the realyfs channels and write them in the paired tracefiles.
327 * @fd_pairs : paired channels and trace files.
329 * returns 0 on success, -1 on error.
331 * Note that the high priority polled channels are consumed first. We then poll
332 * again to see if these channels are still in priority. Only when no
333 * high priority channel is left, we start reading low priority channels.
335 * Note that a channel is considered high priority when the buffer is almost
339 int read_channels(struct channel_trace_fd
*fd_pairs
)
341 struct pollfd
*pollfd
;
343 int num_rdy
, num_hup
;
347 /* Get the subbuf sizes and number */
349 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
350 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
352 ret
= ioctl(pair
->channel
, RELAYFS_GET_N_SUBBUFS
,
355 perror("Error in getting the number of subbuffers");
358 ret
= ioctl(pair
->channel
, RELAYFS_GET_SUBBUF_SIZE
,
361 perror("Error in getting the size of the subbuffers");
367 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
368 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
370 pair
->mmap
= mmap(0, pair
->subbuf_size
* pair
->n_subbufs
, PROT_READ
,
371 MAP_SHARED
, pair
->channel
, 0);
372 if(pair
->mmap
== MAP_FAILED
) {
373 perror("Mmap error");
379 /* Start polling the FD */
381 pollfd
= malloc(fd_pairs
->num_pairs
* sizeof(struct pollfd
));
383 /* Note : index in pollfd is the same index as fd_pair->pair */
384 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
385 pollfd
[i
].fd
= fd_pairs
->pair
[i
].channel
;
386 pollfd
[i
].events
= POLLIN
|POLLPRI
;
393 printf("Press a key for next poll...\n");
395 read(STDIN_FILENO
, &buf
, 1);
396 printf("Next poll :\n");
399 /* Have we received a signal ? */
400 if(quit_program
) break;
402 num_rdy
= poll(pollfd
, fd_pairs
->num_pairs
, -1);
404 perror("Poll error");
408 printf("Data received\n");
410 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
411 switch(pollfd
[i
].revents
) {
413 printf("Error returned in polling fd %d.\n", pollfd
[i
].fd
);
417 printf("Polling fd %d tells it has hung up.\n", pollfd
[i
].fd
);
421 printf("Polling fd %d tells fd is not open.\n", pollfd
[i
].fd
);
425 printf("Urgent read on fd %d\n", pollfd
[i
].fd
);
426 /* Take care of high priority channels first. */
428 ret
|= read_subbuffer(&fd_pairs
->pair
[i
]);
432 /* If every FD has hung up, we end the read loop here */
433 if(num_hup
== fd_pairs
->num_pairs
) break;
436 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
437 switch(pollfd
[i
].revents
) {
439 /* Take care of low priority channels. */
440 printf("Normal read on fd %d\n", pollfd
[i
].fd
);
441 ret
|= read_subbuffer(&fd_pairs
->pair
[i
]);
452 /* munmap only the successfully mmapped indexes */
453 i
= fd_pairs
->num_pairs
;
457 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
460 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
462 perror("Error in munmap");
472 void close_channel_trace_pairs(struct channel_trace_fd
*fd_pairs
)
477 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
478 ret
= close(fd_pairs
->pair
[i
].channel
);
479 if(ret
== -1) perror("Close error on channel");
480 ret
= close(fd_pairs
->pair
[i
].trace
);
481 if(ret
== -1) perror("Close error on trace");
483 free(fd_pairs
->pair
);
486 int main(int argc
, char ** argv
)
490 struct channel_trace_fd fd_pairs
= { NULL
, 0 };
491 struct sigaction act
;
493 ret
= parse_arguments(argc
, argv
);
495 if(ret
!= 0) show_arguments();
496 if(ret
< 0) return EINVAL
;
497 if(ret
> 0) return 0;
509 printf("An error occured while forking.\n");
512 /* else, we are the child, continue... */
515 /* Connect the signal handlers */
516 act
.sa_handler
= handler
;
518 sigemptyset(&(act
.sa_mask
));
519 sigaddset(&(act
.sa_mask
), SIGTERM
);
520 sigaddset(&(act
.sa_mask
), SIGQUIT
);
521 sigaddset(&(act
.sa_mask
), SIGINT
);
522 sigaction(SIGTERM
, &act
, NULL
);
523 sigaction(SIGQUIT
, &act
, NULL
);
524 sigaction(SIGINT
, &act
, NULL
);
527 if(ret
= open_channel_trace_pairs(channel_name
, trace_name
, &fd_pairs
))
530 ret
= read_channels(&fd_pairs
);
533 close_channel_trace_pairs(&fd_pairs
);