3 * Linux Trace Toolkit Daemon
5 * This is a simple daemon that reads a few relay+debugfs channels and save
8 * CPU hot-plugging is supported using inotify.
11 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
24 #include <sys/types.h>
34 #include <sys/syscall.h>
36 #include <asm/ioctls.h>
38 #include <linux/version.h>
41 #include <asm/ioctl.h>
42 #include <asm/types.h>
44 /* Get the next sub buffer that can be read. */
45 #define RELAY_GET_SUBBUF _IOR(0xF5, 0x00,__u32)
46 /* Release the oldest reserved (by "get") sub buffer. */
47 #define RELAY_PUT_SUBBUF _IOW(0xF5, 0x01,__u32)
48 /* returns the number of sub buffers in the per cpu channel. */
49 #define RELAY_GET_N_SUBBUFS _IOR(0xF5, 0x02,__u32)
50 /* returns the size of the sub buffers. */
51 #define RELAY_GET_SUBBUF_SIZE _IOR(0xF5, 0x03,__u32)
53 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
54 #include <linux/inotify.h>
55 /* From the inotify-tools 2.6 package */
56 static inline int inotify_init (void)
58 return syscall (__NR_inotify_init
);
61 static inline int inotify_add_watch (int fd
, const char *name
, __u32 mask
)
63 return syscall (__NR_inotify_add_watch
, fd
, name
, mask
);
66 static inline int inotify_rm_watch (int fd
, __u32 wd
)
68 return syscall (__NR_inotify_rm_watch
, fd
, wd
);
72 static inline int inotify_init (void)
77 static inline int inotify_add_watch (int fd
, const char *name
, __u32 mask
)
82 static inline int inotify_rm_watch (int fd
, __u32 wd
)
99 unsigned int n_subbufs
;
100 unsigned int subbuf_size
;
102 pthread_mutex_t mutex
;
105 struct channel_trace_fd
{
106 struct fd_pair
*pair
;
110 struct inotify_watch
{
112 char path_channel
[PATH_MAX
];
113 char path_trace
[PATH_MAX
];
116 struct inotify_watch_array
{
117 struct inotify_watch
*elem
;
121 static __thread
int thread_pipe
[2];
123 struct channel_trace_fd fd_pairs
= { NULL
, 0 };
125 struct inotify_watch_array inotify_watch_array
= { NULL
, 0 };
127 /* protects fd_pairs and inotify_watch_array */
128 pthread_rwlock_t fd_pairs_lock
= PTHREAD_RWLOCK_INITIALIZER
;
131 static char *trace_name
= NULL
;
132 static char *channel_name
= NULL
;
133 static int daemon_mode
= 0;
134 static int append_mode
= 0;
135 static unsigned long num_threads
= 1;
136 volatile static int quit_program
= 0; /* For signal handler */
137 static int dump_flight_only
= 0;
138 static int dump_normal_only
= 0;
142 * -t directory Directory name of the trace to write to. Will be created.
143 * -c directory Root directory of the debugfs trace channels.
144 * -d Run in background (daemon).
145 * -a Trace append mode.
146 * -s Send SIGUSR1 to parent when ready for IO.
148 void show_arguments(void)
150 printf("Please use the following arguments :\n");
152 printf("-t directory Directory name of the trace to write to.\n"
153 " It will be created.\n");
154 printf("-c directory Root directory of the debugfs trace channels.\n");
155 printf("-d Run in background (daemon).\n");
156 printf("-a Append to an possibly existing trace.\n");
157 printf("-N Number of threads to start.\n");
158 printf("-f Dump only flight recorder channels.\n");
159 printf("-n Dump only normal channels.\n");
166 * Parses the command line arguments.
168 * Returns 1 if the arguments were correct, but doesn't ask for program
169 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
171 int parse_arguments(int argc
, char **argv
)
177 if(strcmp(argv
[1], "-h") == 0) {
184 switch(argv
[argn
][0]) {
186 switch(argv
[argn
][1]) {
189 trace_name
= argv
[argn
+1];
195 channel_name
= argv
[argn
+1];
207 num_threads
= strtoul(argv
[argn
+1], NULL
, 0);
212 dump_flight_only
= 1;
215 dump_normal_only
= 1;
218 printf("Invalid argument '%s'.\n", argv
[argn
]);
224 printf("Invalid argument '%s'.\n", argv
[argn
]);
231 if(trace_name
== NULL
) {
232 printf("Please specify a trace name.\n");
237 if(channel_name
== NULL
) {
238 printf("Please specify a channel name.\n");
248 printf("Linux Trace Toolkit Trace Daemon " VERSION
"\n");
250 printf("Reading from debugfs directory : %s\n", channel_name
);
251 printf("Writing to trace directory : %s\n", trace_name
);
256 /* signal handling */
258 static void handler(int signo
)
260 printf("Signal %d received : exiting cleanly\n", signo
);
265 int open_buffer_file(char *filename
, char *path_channel
, char *path_trace
,
266 struct channel_trace_fd
*fd_pairs
)
270 struct stat stat_buf
;
272 if(strncmp(filename
, "flight-", sizeof("flight-")-1) != 0) {
273 if(dump_flight_only
) {
274 printf("Skipping normal channel %s\n", path_channel
);
278 if(dump_normal_only
) {
279 printf("Skipping flight channel %s\n", path_channel
);
283 printf("Opening file.\n");
285 fd_pairs
->pair
= realloc(fd_pairs
->pair
,
286 ++fd_pairs
->num_pairs
* sizeof(struct fd_pair
));
288 /* Open the channel in read mode */
289 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
=
290 open(path_channel
, O_RDONLY
| O_NONBLOCK
);
291 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
== -1) {
292 perror(path_channel
);
293 fd_pairs
->num_pairs
--;
294 return 0; /* continue */
296 /* Open the trace in write mode, only append if append_mode */
297 ret
= stat(path_trace
, &stat_buf
);
300 printf("Appending to file %s as requested\n", path_trace
);
302 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
303 open(path_trace
, O_WRONLY
|O_APPEND
,
304 S_IRWXU
|S_IRWXG
|S_IRWXO
);
306 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
310 printf("File %s exists, cannot open. Try append mode.\n", path_trace
);
315 if(errno
== ENOENT
) {
316 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
317 open(path_trace
, O_WRONLY
|O_CREAT
|O_EXCL
,
318 S_IRWXU
|S_IRWXG
|S_IRWXO
);
319 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
328 int open_channel_trace_pairs(char *subchannel_name
, char *subtrace_name
,
329 struct channel_trace_fd
*fd_pairs
, int *inotify_fd
,
330 struct inotify_watch_array
*iwatch_array
)
332 DIR *channel_dir
= opendir(subchannel_name
);
333 struct dirent
*entry
;
334 struct stat stat_buf
;
336 char path_channel
[PATH_MAX
];
337 int path_channel_len
;
338 char *path_channel_ptr
;
339 char path_trace
[PATH_MAX
];
341 char *path_trace_ptr
;
344 if(channel_dir
== NULL
) {
345 perror(subchannel_name
);
350 printf("Creating trace subdirectory %s\n", subtrace_name
);
351 ret
= mkdir(subtrace_name
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
353 if(errno
!= EEXIST
) {
354 perror(subtrace_name
);
360 strncpy(path_channel
, subchannel_name
, PATH_MAX
-1);
361 path_channel_len
= strlen(path_channel
);
362 path_channel
[path_channel_len
] = '/';
364 path_channel_ptr
= path_channel
+ path_channel_len
;
366 strncpy(path_trace
, subtrace_name
, PATH_MAX
-1);
367 path_trace_len
= strlen(path_trace
);
368 path_trace
[path_trace_len
] = '/';
370 path_trace_ptr
= path_trace
+ path_trace_len
;
373 iwatch_array
->elem
= realloc(iwatch_array
->elem
,
374 ++iwatch_array
->num
* sizeof(struct inotify_watch
));
376 printf("Adding inotify for channel %s\n", path_channel
);
377 iwatch_array
->elem
[iwatch_array
->num
-1].wd
= inotify_add_watch(*inotify_fd
, path_channel
, IN_CREATE
);
378 strcpy(iwatch_array
->elem
[iwatch_array
->num
-1].path_channel
, path_channel
);
379 strcpy(iwatch_array
->elem
[iwatch_array
->num
-1].path_trace
, path_trace
);
380 printf("Added inotify for channel %s, wd %u\n", iwatch_array
->elem
[iwatch_array
->num
-1].path_channel
,
381 iwatch_array
->elem
[iwatch_array
->num
-1].wd
);
384 while((entry
= readdir(channel_dir
)) != NULL
) {
386 if(entry
->d_name
[0] == '.') continue;
388 strncpy(path_channel_ptr
, entry
->d_name
, PATH_MAX
- path_channel_len
);
389 strncpy(path_trace_ptr
, entry
->d_name
, PATH_MAX
- path_trace_len
);
391 ret
= stat(path_channel
, &stat_buf
);
393 perror(path_channel
);
397 printf("Channel file : %s\n", path_channel
);
399 if(S_ISDIR(stat_buf
.st_mode
)) {
401 printf("Entering channel subdirectory...\n");
402 ret
= open_channel_trace_pairs(path_channel
, path_trace
, fd_pairs
,
403 inotify_fd
, iwatch_array
);
404 if(ret
< 0) continue;
405 } else if(S_ISREG(stat_buf
.st_mode
)) {
406 open_ret
= open_buffer_file(entry
->d_name
, path_channel
, path_trace
,
414 closedir(channel_dir
);
420 int read_subbuffer(struct fd_pair
*pair
)
422 unsigned int consumed_old
;
425 unsigned long len
, offset
;
428 err
= ioctl(pair
->channel
, RELAY_GET_SUBBUF
, &consumed_old
);
429 printf("cookie : %u\n", consumed_old
);
432 perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)");
436 err
= TEMP_FAILURE_RETRY(write(pair
->trace
,
438 + (consumed_old
& ((pair
->n_subbufs
* pair
->subbuf_size
)-1)),
443 perror("Error in writing to file");
447 len
= pair
->subbuf_size
;
450 printf("splice chan to pipe offset %lu\n", offset
);
451 ret
= splice(pair
->channel
, &offset
, thread_pipe
[1], NULL
,
453 printf("splice chan to pipe ret %ld\n", ret
);
455 perror("Error in relay splice");
458 ret
= splice(thread_pipe
[0], NULL
, pair
->trace
, NULL
,
460 printf("splice pipe to file %ld\n", ret
);
462 perror("Error in file splice");
469 err
= fsync(pair
->trace
);
472 perror("Error in writing to file");
478 err
= ioctl(pair
->channel
, RELAY_PUT_SUBBUF
, &consumed_old
);
481 if(errno
== EFAULT
) {
482 perror("Error in unreserving sub buffer\n");
483 } else if(errno
== EIO
) {
484 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
485 /* FIXME : we may delete the last written buffer if we wish. */
495 int map_channels(struct channel_trace_fd
*fd_pairs
,
496 int idx_begin
, int idx_end
)
501 if(fd_pairs
->num_pairs
<= 0) {
502 printf("No channel to read\n");
506 /* Get the subbuf sizes and number */
508 for(i
=idx_begin
;i
<idx_end
;i
++) {
509 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
511 ret
= ioctl(pair
->channel
, RELAY_GET_N_SUBBUFS
,
514 perror("Error in getting the number of subbuffers");
517 ret
= ioctl(pair
->channel
, RELAY_GET_SUBBUF_SIZE
,
520 perror("Error in getting the size of the subbuffers");
523 ret
= pthread_mutex_init(&pair
->mutex
, NULL
); /* Fast mutex */
525 perror("Error in mutex init");
532 for(i
=idx_begin
;i
<idx_end
;i
++) {
533 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
535 pair
->mmap
= mmap(0, pair
->subbuf_size
* pair
->n_subbufs
, PROT_READ
,
536 MAP_SHARED
, pair
->channel
, 0);
537 if(pair
->mmap
== MAP_FAILED
) {
538 perror("Mmap error");
543 goto end
; /* success */
546 /* munmap only the successfully mmapped indexes */
549 for(j
=idx_begin
;j
<i
;j
++) {
550 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
553 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
555 perror("Error in munmap");
565 int unmap_channels(struct channel_trace_fd
*fd_pairs
)
571 for(j
=0;j
<fd_pairs
->num_pairs
;j
++) {
572 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
576 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
578 perror("Error in munmap");
582 err_ret
= pthread_mutex_destroy(&pair
->mutex
);
584 perror("Error in mutex destroy");
593 /* Inotify event arrived.
595 * Only support add file for now.
598 int read_inotify(int inotify_fd
,
599 struct channel_trace_fd
*fd_pairs
,
600 struct inotify_watch_array
*iwatch_array
)
602 char buf
[sizeof(struct inotify_event
) + PATH_MAX
];
603 char path_channel
[PATH_MAX
];
604 char path_trace
[PATH_MAX
];
606 struct inotify_event
*ievent
;
613 len
= read(inotify_fd
, buf
, sizeof(struct inotify_event
) + PATH_MAX
);
617 return 0; /* another thread got the data before us */
619 printf("Error in read from inotify FD %s.\n", strerror(len
));
622 while(offset
< len
) {
623 ievent
= (struct inotify_event
*)&(buf
[offset
]);
624 for(i
=0; i
<iwatch_array
->num
; i
++) {
625 if(iwatch_array
->elem
[i
].wd
== ievent
->wd
&&
626 ievent
->mask
== IN_CREATE
) {
627 printf("inotify wd %u event mask : %u for %s%s\n",
628 ievent
->wd
, ievent
->mask
,
629 iwatch_array
->elem
[i
].path_channel
, ievent
->name
);
630 old_num
= fd_pairs
->num_pairs
;
631 strcpy(path_channel
, iwatch_array
->elem
[i
].path_channel
);
632 strcat(path_channel
, ievent
->name
);
633 strcpy(path_trace
, iwatch_array
->elem
[i
].path_trace
);
634 strcat(path_trace
, ievent
->name
);
635 if(ret
= open_buffer_file(ievent
->name
, path_channel
,
636 path_trace
, fd_pairs
)) {
637 printf("Error opening buffer file\n");
640 if(ret
= map_channels(fd_pairs
, old_num
, fd_pairs
->num_pairs
)) {
641 printf("Error mapping channel\n");
647 offset
+= sizeof(*ievent
) + ievent
->len
;
656 * Read the debugfs channels and write them in the paired tracefiles.
658 * @fd_pairs : paired channels and trace files.
660 * returns 0 on success, -1 on error.
662 * Note that the high priority polled channels are consumed first. We then poll
663 * again to see if these channels are still in priority. Only when no
664 * high priority channel is left, we start reading low priority channels.
666 * Note that a channel is considered high priority when the buffer is almost
670 int read_channels(unsigned long thread_num
, struct channel_trace_fd
*fd_pairs
,
671 int inotify_fd
, struct inotify_watch_array
*iwatch_array
)
673 struct pollfd
*pollfd
= NULL
;
676 int num_rdy
, num_hup
;
680 unsigned int old_num
;
688 pthread_rwlock_rdlock(&fd_pairs_lock
);
690 /* Start polling the FD. Keep one fd for inotify */
691 pollfd
= malloc((inotify_fds
+ fd_pairs
->num_pairs
) * sizeof(struct pollfd
));
694 pollfd
[0].fd
= inotify_fd
;
695 pollfd
[0].events
= POLLIN
|POLLPRI
;
698 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
699 pollfd
[inotify_fds
+i
].fd
= fd_pairs
->pair
[i
].channel
;
700 pollfd
[inotify_fds
+i
].events
= POLLIN
|POLLPRI
;
702 num_pollfd
= inotify_fds
+ fd_pairs
->num_pairs
;
705 pthread_rwlock_unlock(&fd_pairs_lock
);
711 printf("Press a key for next poll...\n");
713 read(STDIN_FILENO
, &buf
, 1);
714 printf("Next poll (polling %d fd) :\n", num_pollfd
);
717 /* Have we received a signal ? */
718 if(quit_program
) break;
720 num_rdy
= poll(pollfd
, num_pollfd
, -1);
723 perror("Poll error");
727 printf("Data received\n");
729 switch(pollfd
[0].revents
) {
731 printf("Error returned in polling inotify fd %d.\n", pollfd
[0].fd
);
734 printf("Polling inotify fd %d tells it has hung up.\n", pollfd
[0].fd
);
737 printf("Polling inotify fd %d tells fd is not open.\n", pollfd
[0].fd
);
742 printf("Polling inotify fd %d : data ready.\n", pollfd
[0].fd
);
744 pthread_rwlock_wrlock(&fd_pairs_lock
);
745 read_inotify(inotify_fd
, fd_pairs
, iwatch_array
);
746 pthread_rwlock_unlock(&fd_pairs_lock
);
752 for(i
=inotify_fds
;i
<num_pollfd
;i
++) {
753 switch(pollfd
[i
].revents
) {
755 printf("Error returned in polling fd %d.\n", pollfd
[i
].fd
);
759 printf("Polling fd %d tells it has hung up.\n", pollfd
[i
].fd
);
763 printf("Polling fd %d tells fd is not open.\n", pollfd
[i
].fd
);
767 pthread_rwlock_rdlock(&fd_pairs_lock
);
768 if(pthread_mutex_trylock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
) == 0) {
769 printf("Urgent read on fd %d\n", pollfd
[i
].fd
);
770 /* Take care of high priority channels first. */
772 /* it's ok to have an unavailable subbuffer */
773 ret
= read_subbuffer(&fd_pairs
->pair
[i
-inotify_fds
]);
774 if(ret
== EAGAIN
) ret
= 0;
776 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
);
778 printf("Error in mutex unlock : %s\n", strerror(ret
));
780 pthread_rwlock_unlock(&fd_pairs_lock
);
784 /* If every buffer FD has hung up, we end the read loop here */
785 if(num_hup
== num_pollfd
- inotify_fds
) break;
788 for(i
=inotify_fds
;i
<num_pollfd
;i
++) {
789 switch(pollfd
[i
].revents
) {
791 pthread_rwlock_rdlock(&fd_pairs_lock
);
792 if(pthread_mutex_trylock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
) == 0) {
793 /* Take care of low priority channels. */
794 printf("Normal read on fd %d\n", pollfd
[i
].fd
);
795 /* it's ok to have an unavailable subbuffer */
796 ret
= read_subbuffer(&fd_pairs
->pair
[i
-inotify_fds
]);
797 if(ret
== EAGAIN
) ret
= 0;
799 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
);
801 printf("Error in mutex unlock : %s\n", strerror(ret
));
803 pthread_rwlock_unlock(&fd_pairs_lock
);
809 /* Update pollfd array if an entry was added to fd_pairs */
810 pthread_rwlock_rdlock(&fd_pairs_lock
);
811 if((inotify_fds
+ fd_pairs
->num_pairs
) != num_pollfd
) {
812 pollfd
= realloc(pollfd
,
813 (inotify_fds
+ fd_pairs
->num_pairs
) * sizeof(struct pollfd
));
814 for(i
=num_pollfd
-inotify_fds
;i
<fd_pairs
->num_pairs
;i
++) {
815 pollfd
[inotify_fds
+i
].fd
= fd_pairs
->pair
[i
].channel
;
816 pollfd
[inotify_fds
+i
].events
= POLLIN
|POLLPRI
;
818 num_pollfd
= fd_pairs
->num_pairs
+ inotify_fds
;
820 pthread_rwlock_unlock(&fd_pairs_lock
);
822 /* NB: If the fd_pairs structure is updated by another thread from this
823 * point forward, the current thread will wait in the poll without
824 * monitoring the new channel. However, this thread will add the
825 * new channel on next poll (and this should not take too much time
826 * on a loaded system).
828 * This event is quite unlikely and can only occur if a CPU is
829 * hot-plugged while multple lttd threads are running.
841 void close_channel_trace_pairs(struct channel_trace_fd
*fd_pairs
, int inotify_fd
,
842 struct inotify_watch_array
*iwatch_array
)
847 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
848 ret
= close(fd_pairs
->pair
[i
].channel
);
849 if(ret
== -1) perror("Close error on channel");
850 ret
= close(fd_pairs
->pair
[i
].trace
);
851 if(ret
== -1) perror("Close error on trace");
853 free(fd_pairs
->pair
);
854 free(iwatch_array
->elem
);
858 void * thread_main(void *arg
)
861 unsigned long thread_num
= (unsigned long)arg
;
863 ret
= pipe(thread_pipe
);
865 perror("Error creating pipe");
868 ret
= read_channels(thread_num
, &fd_pairs
, inotify_fd
, &inotify_watch_array
);
869 close(thread_pipe
[0]); /* close read end */
870 close(thread_pipe
[1]); /* close write end */
879 inotify_fd
= inotify_init();
880 fcntl(inotify_fd
, F_SETFL
, O_NONBLOCK
);
882 if(ret
= open_channel_trace_pairs(channel_name
, trace_name
, &fd_pairs
,
883 &inotify_fd
, &inotify_watch_array
))
886 if(ret
= map_channels(&fd_pairs
, 0, fd_pairs
.num_pairs
))
891 close_channel_trace_pairs(&fd_pairs
, inotify_fd
, &inotify_watch_array
);
898 int main(int argc
, char ** argv
)
901 struct sigaction act
;
906 ret
= parse_arguments(argc
, argv
);
908 if(ret
!= 0) show_arguments();
909 if(ret
< 0) return EINVAL
;
910 if(ret
> 0) return 0;
914 /* Connect the signal handlers */
915 act
.sa_handler
= handler
;
917 sigemptyset(&(act
.sa_mask
));
918 sigaddset(&(act
.sa_mask
), SIGTERM
);
919 sigaddset(&(act
.sa_mask
), SIGQUIT
);
920 sigaddset(&(act
.sa_mask
), SIGINT
);
921 sigaction(SIGTERM
, &act
, NULL
);
922 sigaction(SIGQUIT
, &act
, NULL
);
923 sigaction(SIGINT
, &act
, NULL
);
925 if(ret
= channels_init())
932 perror("An error occured while daemonizing.");
937 tids
= malloc(sizeof(pthread_t
) * num_threads
);
938 for(i
=0; i
<num_threads
; i
++) {
940 ret
= pthread_create(&tids
[i
], NULL
, thread_main
, (void*)i
);
942 perror("Error creating thread");
947 for(i
=0; i
<num_threads
; i
++) {
948 ret
= pthread_join(tids
[i
], &tret
);
950 perror("Error joining thread");
953 if((long)tret
!= 0) {
954 printf("Error %s occured in thread %u\n",
955 strerror((long)tret
), i
);
960 ret
= unmap_channels(&fd_pairs
);
961 close_channel_trace_pairs(&fd_pairs
, inotify_fd
, &inotify_watch_array
);