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;
139 static int verbose_mode
= 0;
141 #define printf_verbose(fmt, args...) \
144 printf(fmt, ##args); \
149 * -t directory Directory name of the trace to write to. Will be created.
150 * -c directory Root directory of the debugfs trace channels.
151 * -d Run in background (daemon).
152 * -a Trace append mode.
153 * -s Send SIGUSR1 to parent when ready for IO.
155 void show_arguments(void)
157 printf("Please use the following arguments :\n");
159 printf("-t directory Directory name of the trace to write to.\n"
160 " It will be created.\n");
161 printf("-c directory Root directory of the debugfs trace channels.\n");
162 printf("-d Run in background (daemon).\n");
163 printf("-a Append to an possibly existing trace.\n");
164 printf("-N Number of threads to start.\n");
165 printf("-f Dump only flight recorder channels.\n");
166 printf("-n Dump only normal channels.\n");
167 printf("-v Verbose mode.\n");
174 * Parses the command line arguments.
176 * Returns 1 if the arguments were correct, but doesn't ask for program
177 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
179 int parse_arguments(int argc
, char **argv
)
185 if(strcmp(argv
[1], "-h") == 0) {
192 switch(argv
[argn
][0]) {
194 switch(argv
[argn
][1]) {
197 trace_name
= argv
[argn
+1];
203 channel_name
= argv
[argn
+1];
215 num_threads
= strtoul(argv
[argn
+1], NULL
, 0);
220 dump_flight_only
= 1;
223 dump_normal_only
= 1;
229 printf("Invalid argument '%s'.\n", argv
[argn
]);
235 printf("Invalid argument '%s'.\n", argv
[argn
]);
242 if(trace_name
== NULL
) {
243 printf("Please specify a trace name.\n");
248 if(channel_name
== NULL
) {
249 printf("Please specify a channel name.\n");
259 printf("Linux Trace Toolkit Trace Daemon " VERSION
"\n");
261 printf("Reading from debugfs directory : %s\n", channel_name
);
262 printf("Writing to trace directory : %s\n", trace_name
);
267 /* signal handling */
269 static void handler(int signo
)
271 printf("Signal %d received : exiting cleanly\n", signo
);
276 int open_buffer_file(char *filename
, char *path_channel
, char *path_trace
,
277 struct channel_trace_fd
*fd_pairs
)
281 struct stat stat_buf
;
283 if(strncmp(filename
, "flight-", sizeof("flight-")-1) != 0) {
284 if(dump_flight_only
) {
285 printf_verbose("Skipping normal channel %s\n",
290 if(dump_normal_only
) {
291 printf_verbose("Skipping flight channel %s\n",
296 printf_verbose("Opening file.\n");
298 fd_pairs
->pair
= realloc(fd_pairs
->pair
,
299 ++fd_pairs
->num_pairs
* sizeof(struct fd_pair
));
301 /* Open the channel in read mode */
302 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
=
303 open(path_channel
, O_RDONLY
| O_NONBLOCK
);
304 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
== -1) {
305 perror(path_channel
);
306 fd_pairs
->num_pairs
--;
307 return 0; /* continue */
309 /* Open the trace in write mode, only append if append_mode */
310 ret
= stat(path_trace
, &stat_buf
);
313 printf_verbose("Appending to file %s as requested\n",
316 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
317 open(path_trace
, O_WRONLY
|O_APPEND
,
318 S_IRWXU
|S_IRWXG
|S_IRWXO
);
320 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
324 printf("File %s exists, cannot open. Try append mode.\n", path_trace
);
329 if(errno
== ENOENT
) {
330 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
331 open(path_trace
, O_WRONLY
|O_CREAT
|O_EXCL
,
332 S_IRWXU
|S_IRWXG
|S_IRWXO
);
333 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
342 int open_channel_trace_pairs(char *subchannel_name
, char *subtrace_name
,
343 struct channel_trace_fd
*fd_pairs
, int *inotify_fd
,
344 struct inotify_watch_array
*iwatch_array
)
346 DIR *channel_dir
= opendir(subchannel_name
);
347 struct dirent
*entry
;
348 struct stat stat_buf
;
350 char path_channel
[PATH_MAX
];
351 int path_channel_len
;
352 char *path_channel_ptr
;
353 char path_trace
[PATH_MAX
];
355 char *path_trace_ptr
;
358 if(channel_dir
== NULL
) {
359 perror(subchannel_name
);
364 printf_verbose("Creating trace subdirectory %s\n", subtrace_name
);
365 ret
= mkdir(subtrace_name
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
367 if(errno
!= EEXIST
) {
368 perror(subtrace_name
);
374 strncpy(path_channel
, subchannel_name
, PATH_MAX
-1);
375 path_channel_len
= strlen(path_channel
);
376 path_channel
[path_channel_len
] = '/';
378 path_channel_ptr
= path_channel
+ path_channel_len
;
380 strncpy(path_trace
, subtrace_name
, PATH_MAX
-1);
381 path_trace_len
= strlen(path_trace
);
382 path_trace
[path_trace_len
] = '/';
384 path_trace_ptr
= path_trace
+ path_trace_len
;
387 iwatch_array
->elem
= realloc(iwatch_array
->elem
,
388 ++iwatch_array
->num
* sizeof(struct inotify_watch
));
390 printf_verbose("Adding inotify for channel %s\n", path_channel
);
391 iwatch_array
->elem
[iwatch_array
->num
-1].wd
= inotify_add_watch(*inotify_fd
, path_channel
, IN_CREATE
);
392 strcpy(iwatch_array
->elem
[iwatch_array
->num
-1].path_channel
, path_channel
);
393 strcpy(iwatch_array
->elem
[iwatch_array
->num
-1].path_trace
, path_trace
);
394 printf_verbose("Added inotify for channel %s, wd %u\n",
395 iwatch_array
->elem
[iwatch_array
->num
-1].path_channel
,
396 iwatch_array
->elem
[iwatch_array
->num
-1].wd
);
399 while((entry
= readdir(channel_dir
)) != NULL
) {
401 if(entry
->d_name
[0] == '.') continue;
403 strncpy(path_channel_ptr
, entry
->d_name
, PATH_MAX
- path_channel_len
);
404 strncpy(path_trace_ptr
, entry
->d_name
, PATH_MAX
- path_trace_len
);
406 ret
= stat(path_channel
, &stat_buf
);
408 perror(path_channel
);
412 printf_verbose("Channel file : %s\n", path_channel
);
414 if(S_ISDIR(stat_buf
.st_mode
)) {
416 printf_verbose("Entering channel subdirectory...\n");
417 ret
= open_channel_trace_pairs(path_channel
, path_trace
, fd_pairs
,
418 inotify_fd
, iwatch_array
);
419 if(ret
< 0) continue;
420 } else if(S_ISREG(stat_buf
.st_mode
)) {
421 open_ret
= open_buffer_file(entry
->d_name
, path_channel
, path_trace
,
429 closedir(channel_dir
);
435 int read_subbuffer(struct fd_pair
*pair
)
437 unsigned int consumed_old
;
440 unsigned long len
, offset
;
443 err
= ioctl(pair
->channel
, RELAY_GET_SUBBUF
, &consumed_old
);
444 printf_verbose("cookie : %u\n", consumed_old
);
447 perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)");
451 err
= TEMP_FAILURE_RETRY(write(pair
->trace
,
453 + (consumed_old
& ((pair
->n_subbufs
* pair
->subbuf_size
)-1)),
458 perror("Error in writing to file");
462 len
= pair
->subbuf_size
;
465 printf_verbose("splice chan to pipe offset %lu\n", offset
);
466 ret
= splice(pair
->channel
, &offset
, thread_pipe
[1], NULL
,
468 printf_verbose("splice chan to pipe ret %ld\n", ret
);
470 perror("Error in relay splice");
473 ret
= splice(thread_pipe
[0], NULL
, pair
->trace
, NULL
,
475 printf_verbose("splice pipe to file %ld\n", ret
);
477 perror("Error in file splice");
484 err
= fsync(pair
->trace
);
487 perror("Error in writing to file");
493 err
= ioctl(pair
->channel
, RELAY_PUT_SUBBUF
, &consumed_old
);
496 if(errno
== EFAULT
) {
497 perror("Error in unreserving sub buffer\n");
498 } else if(errno
== EIO
) {
499 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
500 /* FIXME : we may delete the last written buffer if we wish. */
510 int map_channels(struct channel_trace_fd
*fd_pairs
,
511 int idx_begin
, int idx_end
)
516 if(fd_pairs
->num_pairs
<= 0) {
517 printf("No channel to read\n");
521 /* Get the subbuf sizes and number */
523 for(i
=idx_begin
;i
<idx_end
;i
++) {
524 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
526 ret
= ioctl(pair
->channel
, RELAY_GET_N_SUBBUFS
,
529 perror("Error in getting the number of subbuffers");
532 ret
= ioctl(pair
->channel
, RELAY_GET_SUBBUF_SIZE
,
535 perror("Error in getting the size of the subbuffers");
538 ret
= pthread_mutex_init(&pair
->mutex
, NULL
); /* Fast mutex */
540 perror("Error in mutex init");
547 for(i
=idx_begin
;i
<idx_end
;i
++) {
548 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
550 pair
->mmap
= mmap(0, pair
->subbuf_size
* pair
->n_subbufs
, PROT_READ
,
551 MAP_SHARED
, pair
->channel
, 0);
552 if(pair
->mmap
== MAP_FAILED
) {
553 perror("Mmap error");
558 goto end
; /* success */
561 /* munmap only the successfully mmapped indexes */
564 for(j
=idx_begin
;j
<i
;j
++) {
565 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
568 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
570 perror("Error in munmap");
580 int unmap_channels(struct channel_trace_fd
*fd_pairs
)
586 for(j
=0;j
<fd_pairs
->num_pairs
;j
++) {
587 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
591 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
593 perror("Error in munmap");
597 err_ret
= pthread_mutex_destroy(&pair
->mutex
);
599 perror("Error in mutex destroy");
608 /* Inotify event arrived.
610 * Only support add file for now.
613 int read_inotify(int inotify_fd
,
614 struct channel_trace_fd
*fd_pairs
,
615 struct inotify_watch_array
*iwatch_array
)
617 char buf
[sizeof(struct inotify_event
) + PATH_MAX
];
618 char path_channel
[PATH_MAX
];
619 char path_trace
[PATH_MAX
];
621 struct inotify_event
*ievent
;
628 len
= read(inotify_fd
, buf
, sizeof(struct inotify_event
) + PATH_MAX
);
632 return 0; /* another thread got the data before us */
634 printf("Error in read from inotify FD %s.\n", strerror(len
));
637 while(offset
< len
) {
638 ievent
= (struct inotify_event
*)&(buf
[offset
]);
639 for(i
=0; i
<iwatch_array
->num
; i
++) {
640 if(iwatch_array
->elem
[i
].wd
== ievent
->wd
&&
641 ievent
->mask
== IN_CREATE
) {
643 "inotify wd %u event mask : %u for %s%s\n",
644 ievent
->wd
, ievent
->mask
,
645 iwatch_array
->elem
[i
].path_channel
,
647 old_num
= fd_pairs
->num_pairs
;
648 strcpy(path_channel
, iwatch_array
->elem
[i
].path_channel
);
649 strcat(path_channel
, ievent
->name
);
650 strcpy(path_trace
, iwatch_array
->elem
[i
].path_trace
);
651 strcat(path_trace
, ievent
->name
);
652 if(ret
= open_buffer_file(ievent
->name
, path_channel
,
653 path_trace
, fd_pairs
)) {
654 printf("Error opening buffer file\n");
657 if(ret
= map_channels(fd_pairs
, old_num
, fd_pairs
->num_pairs
)) {
658 printf("Error mapping channel\n");
664 offset
+= sizeof(*ievent
) + ievent
->len
;
673 * Read the debugfs channels and write them in the paired tracefiles.
675 * @fd_pairs : paired channels and trace files.
677 * returns 0 on success, -1 on error.
679 * Note that the high priority polled channels are consumed first. We then poll
680 * again to see if these channels are still in priority. Only when no
681 * high priority channel is left, we start reading low priority channels.
683 * Note that a channel is considered high priority when the buffer is almost
687 int read_channels(unsigned long thread_num
, struct channel_trace_fd
*fd_pairs
,
688 int inotify_fd
, struct inotify_watch_array
*iwatch_array
)
690 struct pollfd
*pollfd
= NULL
;
693 int num_rdy
, num_hup
;
697 unsigned int old_num
;
705 pthread_rwlock_rdlock(&fd_pairs_lock
);
707 /* Start polling the FD. Keep one fd for inotify */
708 pollfd
= malloc((inotify_fds
+ fd_pairs
->num_pairs
) * sizeof(struct pollfd
));
711 pollfd
[0].fd
= inotify_fd
;
712 pollfd
[0].events
= POLLIN
|POLLPRI
;
715 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
716 pollfd
[inotify_fds
+i
].fd
= fd_pairs
->pair
[i
].channel
;
717 pollfd
[inotify_fds
+i
].events
= POLLIN
|POLLPRI
;
719 num_pollfd
= inotify_fds
+ fd_pairs
->num_pairs
;
722 pthread_rwlock_unlock(&fd_pairs_lock
);
728 printf("Press a key for next poll...\n");
730 read(STDIN_FILENO
, &buf
, 1);
731 printf("Next poll (polling %d fd) :\n", num_pollfd
);
734 /* Have we received a signal ? */
735 if(quit_program
) break;
737 num_rdy
= poll(pollfd
, num_pollfd
, -1);
740 perror("Poll error");
744 printf_verbose("Data received\n");
746 switch(pollfd
[0].revents
) {
749 "Error returned in polling inotify fd %d.\n",
754 "Polling inotify fd %d tells it has hung up.\n",
759 "Polling inotify fd %d tells fd is not open.\n",
765 "Polling inotify fd %d : data ready.\n",
768 pthread_rwlock_wrlock(&fd_pairs_lock
);
769 read_inotify(inotify_fd
, fd_pairs
, iwatch_array
);
770 pthread_rwlock_unlock(&fd_pairs_lock
);
776 for(i
=inotify_fds
;i
<num_pollfd
;i
++) {
777 switch(pollfd
[i
].revents
) {
780 "Error returned in polling fd %d.\n",
786 "Polling fd %d tells it has hung up.\n",
792 "Polling fd %d tells fd is not open.\n",
797 pthread_rwlock_rdlock(&fd_pairs_lock
);
798 if(pthread_mutex_trylock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
) == 0) {
800 "Urgent read on fd %d\n",
802 /* Take care of high priority channels first. */
804 /* it's ok to have an unavailable subbuffer */
805 ret
= read_subbuffer(&fd_pairs
->pair
[i
-inotify_fds
]);
806 if(ret
== EAGAIN
) ret
= 0;
808 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
);
810 printf("Error in mutex unlock : %s\n", strerror(ret
));
812 pthread_rwlock_unlock(&fd_pairs_lock
);
816 /* If every buffer FD has hung up, we end the read loop here */
817 if(num_hup
== num_pollfd
- inotify_fds
) break;
820 for(i
=inotify_fds
;i
<num_pollfd
;i
++) {
821 switch(pollfd
[i
].revents
) {
823 pthread_rwlock_rdlock(&fd_pairs_lock
);
824 if(pthread_mutex_trylock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
) == 0) {
825 /* Take care of low priority channels. */
827 "Normal read on fd %d\n",
829 /* it's ok to have an unavailable subbuffer */
830 ret
= read_subbuffer(&fd_pairs
->pair
[i
-inotify_fds
]);
831 if(ret
== EAGAIN
) ret
= 0;
833 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
);
835 printf("Error in mutex unlock : %s\n", strerror(ret
));
837 pthread_rwlock_unlock(&fd_pairs_lock
);
843 /* Update pollfd array if an entry was added to fd_pairs */
844 pthread_rwlock_rdlock(&fd_pairs_lock
);
845 if((inotify_fds
+ fd_pairs
->num_pairs
) != num_pollfd
) {
846 pollfd
= realloc(pollfd
,
847 (inotify_fds
+ fd_pairs
->num_pairs
) * sizeof(struct pollfd
));
848 for(i
=num_pollfd
-inotify_fds
;i
<fd_pairs
->num_pairs
;i
++) {
849 pollfd
[inotify_fds
+i
].fd
= fd_pairs
->pair
[i
].channel
;
850 pollfd
[inotify_fds
+i
].events
= POLLIN
|POLLPRI
;
852 num_pollfd
= fd_pairs
->num_pairs
+ inotify_fds
;
854 pthread_rwlock_unlock(&fd_pairs_lock
);
856 /* NB: If the fd_pairs structure is updated by another thread from this
857 * point forward, the current thread will wait in the poll without
858 * monitoring the new channel. However, this thread will add the
859 * new channel on next poll (and this should not take too much time
860 * on a loaded system).
862 * This event is quite unlikely and can only occur if a CPU is
863 * hot-plugged while multple lttd threads are running.
875 void close_channel_trace_pairs(struct channel_trace_fd
*fd_pairs
, int inotify_fd
,
876 struct inotify_watch_array
*iwatch_array
)
881 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
882 ret
= close(fd_pairs
->pair
[i
].channel
);
883 if(ret
== -1) perror("Close error on channel");
884 ret
= close(fd_pairs
->pair
[i
].trace
);
885 if(ret
== -1) perror("Close error on trace");
887 free(fd_pairs
->pair
);
888 free(iwatch_array
->elem
);
892 void * thread_main(void *arg
)
895 unsigned long thread_num
= (unsigned long)arg
;
897 ret
= pipe(thread_pipe
);
899 perror("Error creating pipe");
902 ret
= read_channels(thread_num
, &fd_pairs
, inotify_fd
, &inotify_watch_array
);
903 close(thread_pipe
[0]); /* close read end */
904 close(thread_pipe
[1]); /* close write end */
913 inotify_fd
= inotify_init();
914 fcntl(inotify_fd
, F_SETFL
, O_NONBLOCK
);
916 if(ret
= open_channel_trace_pairs(channel_name
, trace_name
, &fd_pairs
,
917 &inotify_fd
, &inotify_watch_array
))
920 if(ret
= map_channels(&fd_pairs
, 0, fd_pairs
.num_pairs
))
925 close_channel_trace_pairs(&fd_pairs
, inotify_fd
, &inotify_watch_array
);
932 int main(int argc
, char ** argv
)
935 struct sigaction act
;
940 ret
= parse_arguments(argc
, argv
);
942 if(ret
!= 0) show_arguments();
943 if(ret
< 0) return EINVAL
;
944 if(ret
> 0) return 0;
948 /* Connect the signal handlers */
949 act
.sa_handler
= handler
;
951 sigemptyset(&(act
.sa_mask
));
952 sigaddset(&(act
.sa_mask
), SIGTERM
);
953 sigaddset(&(act
.sa_mask
), SIGQUIT
);
954 sigaddset(&(act
.sa_mask
), SIGINT
);
955 sigaction(SIGTERM
, &act
, NULL
);
956 sigaction(SIGQUIT
, &act
, NULL
);
957 sigaction(SIGINT
, &act
, NULL
);
959 if(ret
= channels_init())
966 perror("An error occured while daemonizing.");
971 tids
= malloc(sizeof(pthread_t
) * num_threads
);
972 for(i
=0; i
<num_threads
; i
++) {
974 ret
= pthread_create(&tids
[i
], NULL
, thread_main
, (void*)i
);
976 perror("Error creating thread");
981 for(i
=0; i
<num_threads
; i
++) {
982 ret
= pthread_join(tids
[i
], &tret
);
984 perror("Error joining thread");
987 if((long)tret
!= 0) {
988 printf("Error %s occured in thread %u\n",
989 strerror((long)tret
), i
);
994 ret
= unmap_channels(&fd_pairs
);
995 close_channel_trace_pairs(&fd_pairs
, inotify_fd
, &inotify_watch_array
);