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 <sys/inotify.h>
55 #if 0 /* should now be provided by libc. */
56 /* From the inotify-tools 2.6 package */
57 static inline int inotify_init (void)
59 return syscall (__NR_inotify_init
);
62 static inline int inotify_add_watch (int fd
, const char *name
, __u32 mask
)
64 return syscall (__NR_inotify_add_watch
, fd
, name
, mask
);
67 static inline int inotify_rm_watch (int fd
, __u32 wd
)
69 return syscall (__NR_inotify_rm_watch
, fd
, wd
);
74 static inline int inotify_init (void)
79 static inline int inotify_add_watch (int fd
, const char *name
, __u32 mask
)
84 static inline int inotify_rm_watch (int fd
, __u32 wd
)
101 unsigned int n_subbufs
;
102 unsigned int subbuf_size
;
104 pthread_mutex_t mutex
;
107 struct channel_trace_fd
{
108 struct fd_pair
*pair
;
112 struct inotify_watch
{
114 char path_channel
[PATH_MAX
];
115 char path_trace
[PATH_MAX
];
118 struct inotify_watch_array
{
119 struct inotify_watch
*elem
;
123 static __thread
int thread_pipe
[2];
125 struct channel_trace_fd fd_pairs
= { NULL
, 0 };
127 struct inotify_watch_array inotify_watch_array
= { NULL
, 0 };
129 /* protects fd_pairs and inotify_watch_array */
130 pthread_rwlock_t fd_pairs_lock
= PTHREAD_RWLOCK_INITIALIZER
;
133 static char *trace_name
= NULL
;
134 static char *channel_name
= NULL
;
135 static int daemon_mode
= 0;
136 static int append_mode
= 0;
137 static unsigned long num_threads
= 1;
138 volatile static int quit_program
= 0; /* For signal handler */
139 static int dump_flight_only
= 0;
140 static int dump_normal_only
= 0;
141 static int verbose_mode
= 0;
143 #define printf_verbose(fmt, args...) \
146 printf(fmt, ##args); \
151 * -t directory Directory name of the trace to write to. Will be created.
152 * -c directory Root directory of the debugfs trace channels.
153 * -d Run in background (daemon).
154 * -a Trace append mode.
155 * -s Send SIGUSR1 to parent when ready for IO.
157 void show_arguments(void)
159 printf("Please use the following arguments :\n");
161 printf("-t directory Directory name of the trace to write to.\n"
162 " It will be created.\n");
163 printf("-c directory Root directory of the debugfs trace channels.\n");
164 printf("-d Run in background (daemon).\n");
165 printf("-a Append to an possibly existing trace.\n");
166 printf("-N Number of threads to start.\n");
167 printf("-f Dump only flight recorder channels.\n");
168 printf("-n Dump only normal channels.\n");
169 printf("-v Verbose mode.\n");
176 * Parses the command line arguments.
178 * Returns 1 if the arguments were correct, but doesn't ask for program
179 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
181 int parse_arguments(int argc
, char **argv
)
187 if(strcmp(argv
[1], "-h") == 0) {
194 switch(argv
[argn
][0]) {
196 switch(argv
[argn
][1]) {
199 trace_name
= argv
[argn
+1];
205 channel_name
= argv
[argn
+1];
217 num_threads
= strtoul(argv
[argn
+1], NULL
, 0);
222 dump_flight_only
= 1;
225 dump_normal_only
= 1;
231 printf("Invalid argument '%s'.\n", argv
[argn
]);
237 printf("Invalid argument '%s'.\n", argv
[argn
]);
244 if(trace_name
== NULL
) {
245 printf("Please specify a trace name.\n");
250 if(channel_name
== NULL
) {
251 printf("Please specify a channel name.\n");
261 printf("Linux Trace Toolkit Trace Daemon " VERSION
"\n");
263 printf("Reading from debugfs directory : %s\n", channel_name
);
264 printf("Writing to trace directory : %s\n", trace_name
);
269 /* signal handling */
271 static void handler(int signo
)
273 printf("Signal %d received : exiting cleanly\n", signo
);
278 int open_buffer_file(char *filename
, char *path_channel
, char *path_trace
,
279 struct channel_trace_fd
*fd_pairs
)
283 struct stat stat_buf
;
285 if(strncmp(filename
, "flight-", sizeof("flight-")-1) != 0) {
286 if(dump_flight_only
) {
287 printf_verbose("Skipping normal channel %s\n",
292 if(dump_normal_only
) {
293 printf_verbose("Skipping flight channel %s\n",
298 printf_verbose("Opening file.\n");
300 fd_pairs
->pair
= realloc(fd_pairs
->pair
,
301 ++fd_pairs
->num_pairs
* sizeof(struct fd_pair
));
303 /* Open the channel in read mode */
304 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
=
305 open(path_channel
, O_RDONLY
| O_NONBLOCK
);
306 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].channel
== -1) {
307 perror(path_channel
);
308 fd_pairs
->num_pairs
--;
309 return 0; /* continue */
311 /* Open the trace in write mode, only append if append_mode */
312 ret
= stat(path_trace
, &stat_buf
);
315 printf_verbose("Appending to file %s as requested\n",
318 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
319 open(path_trace
, O_WRONLY
,
320 S_IRWXU
|S_IRWXG
|S_IRWXO
);
321 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
324 ret
= lseek(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
,
330 printf("File %s exists, cannot open. Try append mode.\n", path_trace
);
335 if(errno
== ENOENT
) {
336 fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
=
337 open(path_trace
, O_WRONLY
|O_CREAT
|O_EXCL
,
338 S_IRWXU
|S_IRWXG
|S_IRWXO
);
339 if(fd_pairs
->pair
[fd_pairs
->num_pairs
-1].trace
== -1) {
348 int open_channel_trace_pairs(char *subchannel_name
, char *subtrace_name
,
349 struct channel_trace_fd
*fd_pairs
, int *inotify_fd
,
350 struct inotify_watch_array
*iwatch_array
)
352 DIR *channel_dir
= opendir(subchannel_name
);
353 struct dirent
*entry
;
354 struct stat stat_buf
;
356 char path_channel
[PATH_MAX
];
357 int path_channel_len
;
358 char *path_channel_ptr
;
359 char path_trace
[PATH_MAX
];
361 char *path_trace_ptr
;
364 if(channel_dir
== NULL
) {
365 perror(subchannel_name
);
370 printf_verbose("Creating trace subdirectory %s\n", subtrace_name
);
371 ret
= mkdir(subtrace_name
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
373 if(errno
!= EEXIST
) {
374 perror(subtrace_name
);
380 strncpy(path_channel
, subchannel_name
, PATH_MAX
-1);
381 path_channel_len
= strlen(path_channel
);
382 path_channel
[path_channel_len
] = '/';
384 path_channel_ptr
= path_channel
+ path_channel_len
;
386 strncpy(path_trace
, subtrace_name
, PATH_MAX
-1);
387 path_trace_len
= strlen(path_trace
);
388 path_trace
[path_trace_len
] = '/';
390 path_trace_ptr
= path_trace
+ path_trace_len
;
393 iwatch_array
->elem
= realloc(iwatch_array
->elem
,
394 ++iwatch_array
->num
* sizeof(struct inotify_watch
));
396 printf_verbose("Adding inotify for channel %s\n", path_channel
);
397 iwatch_array
->elem
[iwatch_array
->num
-1].wd
= inotify_add_watch(*inotify_fd
, path_channel
, IN_CREATE
);
398 strcpy(iwatch_array
->elem
[iwatch_array
->num
-1].path_channel
, path_channel
);
399 strcpy(iwatch_array
->elem
[iwatch_array
->num
-1].path_trace
, path_trace
);
400 printf_verbose("Added inotify for channel %s, wd %u\n",
401 iwatch_array
->elem
[iwatch_array
->num
-1].path_channel
,
402 iwatch_array
->elem
[iwatch_array
->num
-1].wd
);
405 while((entry
= readdir(channel_dir
)) != NULL
) {
407 if(entry
->d_name
[0] == '.') continue;
409 strncpy(path_channel_ptr
, entry
->d_name
, PATH_MAX
- path_channel_len
);
410 strncpy(path_trace_ptr
, entry
->d_name
, PATH_MAX
- path_trace_len
);
412 ret
= stat(path_channel
, &stat_buf
);
414 perror(path_channel
);
418 printf_verbose("Channel file : %s\n", path_channel
);
420 if(S_ISDIR(stat_buf
.st_mode
)) {
422 printf_verbose("Entering channel subdirectory...\n");
423 ret
= open_channel_trace_pairs(path_channel
, path_trace
, fd_pairs
,
424 inotify_fd
, iwatch_array
);
425 if(ret
< 0) continue;
426 } else if(S_ISREG(stat_buf
.st_mode
)) {
427 open_ret
= open_buffer_file(entry
->d_name
, path_channel
, path_trace
,
435 closedir(channel_dir
);
441 int read_subbuffer(struct fd_pair
*pair
)
443 unsigned int consumed_old
;
450 err
= ioctl(pair
->channel
, RELAY_GET_SUBBUF
, &consumed_old
);
451 printf_verbose("cookie : %u\n", consumed_old
);
454 perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)");
458 err
= TEMP_FAILURE_RETRY(write(pair
->trace
,
460 + (consumed_old
& ((pair
->n_subbufs
* pair
->subbuf_size
)-1)),
465 perror("Error in writing to file");
469 len
= pair
->subbuf_size
;
472 printf_verbose("splice chan to pipe offset %lu\n",
473 (unsigned long)offset
);
474 ret
= splice(pair
->channel
, &offset
, thread_pipe
[1], NULL
,
475 len
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
476 printf_verbose("splice chan to pipe ret %ld\n", ret
);
478 perror("Error in relay splice");
481 ret
= splice(thread_pipe
[0], NULL
, pair
->trace
, NULL
,
482 ret
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
483 printf_verbose("splice pipe to file %ld\n", ret
);
485 perror("Error in file splice");
492 err
= fsync(pair
->trace
);
495 perror("Error in writing to file");
501 err
= ioctl(pair
->channel
, RELAY_PUT_SUBBUF
, &consumed_old
);
504 if(errno
== EFAULT
) {
505 perror("Error in unreserving sub buffer\n");
506 } else if(errno
== EIO
) {
507 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
508 /* FIXME : we may delete the last written buffer if we wish. */
518 int map_channels(struct channel_trace_fd
*fd_pairs
,
519 int idx_begin
, int idx_end
)
524 if(fd_pairs
->num_pairs
<= 0) {
525 printf("No channel to read\n");
529 /* Get the subbuf sizes and number */
531 for(i
=idx_begin
;i
<idx_end
;i
++) {
532 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
534 ret
= ioctl(pair
->channel
, RELAY_GET_N_SUBBUFS
,
537 perror("Error in getting the number of subbuffers");
540 ret
= ioctl(pair
->channel
, RELAY_GET_SUBBUF_SIZE
,
543 perror("Error in getting the size of the subbuffers");
546 ret
= pthread_mutex_init(&pair
->mutex
, NULL
); /* Fast mutex */
548 perror("Error in mutex init");
555 for(i
=idx_begin
;i
<idx_end
;i
++) {
556 struct fd_pair
*pair
= &fd_pairs
->pair
[i
];
558 pair
->mmap
= mmap(0, pair
->subbuf_size
* pair
->n_subbufs
, PROT_READ
,
559 MAP_SHARED
, pair
->channel
, 0);
560 if(pair
->mmap
== MAP_FAILED
) {
561 perror("Mmap error");
566 goto end
; /* success */
569 /* munmap only the successfully mmapped indexes */
572 for(j
=idx_begin
;j
<i
;j
++) {
573 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");
588 int unmap_channels(struct channel_trace_fd
*fd_pairs
)
594 for(j
=0;j
<fd_pairs
->num_pairs
;j
++) {
595 struct fd_pair
*pair
= &fd_pairs
->pair
[j
];
599 err_ret
= munmap(pair
->mmap
, pair
->subbuf_size
* pair
->n_subbufs
);
601 perror("Error in munmap");
605 err_ret
= pthread_mutex_destroy(&pair
->mutex
);
607 perror("Error in mutex destroy");
616 /* Inotify event arrived.
618 * Only support add file for now.
621 int read_inotify(int inotify_fd
,
622 struct channel_trace_fd
*fd_pairs
,
623 struct inotify_watch_array
*iwatch_array
)
625 char buf
[sizeof(struct inotify_event
) + PATH_MAX
];
626 char path_channel
[PATH_MAX
];
627 char path_trace
[PATH_MAX
];
629 struct inotify_event
*ievent
;
636 len
= read(inotify_fd
, buf
, sizeof(struct inotify_event
) + PATH_MAX
);
640 return 0; /* another thread got the data before us */
642 printf("Error in read from inotify FD %s.\n", strerror(len
));
645 while(offset
< len
) {
646 ievent
= (struct inotify_event
*)&(buf
[offset
]);
647 for(i
=0; i
<iwatch_array
->num
; i
++) {
648 if(iwatch_array
->elem
[i
].wd
== ievent
->wd
&&
649 ievent
->mask
== IN_CREATE
) {
651 "inotify wd %u event mask : %u for %s%s\n",
652 ievent
->wd
, ievent
->mask
,
653 iwatch_array
->elem
[i
].path_channel
,
655 old_num
= fd_pairs
->num_pairs
;
656 strcpy(path_channel
, iwatch_array
->elem
[i
].path_channel
);
657 strcat(path_channel
, ievent
->name
);
658 strcpy(path_trace
, iwatch_array
->elem
[i
].path_trace
);
659 strcat(path_trace
, ievent
->name
);
660 if(ret
= open_buffer_file(ievent
->name
, path_channel
,
661 path_trace
, fd_pairs
)) {
662 printf("Error opening buffer file\n");
665 if(ret
= map_channels(fd_pairs
, old_num
, fd_pairs
->num_pairs
)) {
666 printf("Error mapping channel\n");
672 offset
+= sizeof(*ievent
) + ievent
->len
;
681 * Read the debugfs channels and write them in the paired tracefiles.
683 * @fd_pairs : paired channels and trace files.
685 * returns 0 on success, -1 on error.
687 * Note that the high priority polled channels are consumed first. We then poll
688 * again to see if these channels are still in priority. Only when no
689 * high priority channel is left, we start reading low priority channels.
691 * Note that a channel is considered high priority when the buffer is almost
695 int read_channels(unsigned long thread_num
, struct channel_trace_fd
*fd_pairs
,
696 int inotify_fd
, struct inotify_watch_array
*iwatch_array
)
698 struct pollfd
*pollfd
= NULL
;
701 int num_rdy
, num_hup
;
705 unsigned int old_num
;
713 pthread_rwlock_rdlock(&fd_pairs_lock
);
715 /* Start polling the FD. Keep one fd for inotify */
716 pollfd
= malloc((inotify_fds
+ fd_pairs
->num_pairs
) * sizeof(struct pollfd
));
719 pollfd
[0].fd
= inotify_fd
;
720 pollfd
[0].events
= POLLIN
|POLLPRI
;
723 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
724 pollfd
[inotify_fds
+i
].fd
= fd_pairs
->pair
[i
].channel
;
725 pollfd
[inotify_fds
+i
].events
= POLLIN
|POLLPRI
;
727 num_pollfd
= inotify_fds
+ fd_pairs
->num_pairs
;
730 pthread_rwlock_unlock(&fd_pairs_lock
);
736 printf("Press a key for next poll...\n");
738 read(STDIN_FILENO
, &buf
, 1);
739 printf("Next poll (polling %d fd) :\n", num_pollfd
);
742 /* Have we received a signal ? */
743 if(quit_program
) break;
745 num_rdy
= poll(pollfd
, num_pollfd
, -1);
748 perror("Poll error");
752 printf_verbose("Data received\n");
754 switch(pollfd
[0].revents
) {
757 "Error returned in polling inotify fd %d.\n",
762 "Polling inotify fd %d tells it has hung up.\n",
767 "Polling inotify fd %d tells fd is not open.\n",
773 "Polling inotify fd %d : data ready.\n",
776 pthread_rwlock_wrlock(&fd_pairs_lock
);
777 read_inotify(inotify_fd
, fd_pairs
, iwatch_array
);
778 pthread_rwlock_unlock(&fd_pairs_lock
);
784 for(i
=inotify_fds
;i
<num_pollfd
;i
++) {
785 switch(pollfd
[i
].revents
) {
788 "Error returned in polling fd %d.\n",
794 "Polling fd %d tells it has hung up.\n",
800 "Polling fd %d tells fd is not open.\n",
805 pthread_rwlock_rdlock(&fd_pairs_lock
);
806 if(pthread_mutex_trylock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
) == 0) {
808 "Urgent read on fd %d\n",
810 /* Take care of high priority channels first. */
812 /* it's ok to have an unavailable subbuffer */
813 ret
= read_subbuffer(&fd_pairs
->pair
[i
-inotify_fds
]);
814 if(ret
== EAGAIN
) ret
= 0;
816 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
);
818 printf("Error in mutex unlock : %s\n", strerror(ret
));
820 pthread_rwlock_unlock(&fd_pairs_lock
);
824 /* If every buffer FD has hung up, we end the read loop here */
825 if(num_hup
== num_pollfd
- inotify_fds
) break;
828 for(i
=inotify_fds
;i
<num_pollfd
;i
++) {
829 switch(pollfd
[i
].revents
) {
831 pthread_rwlock_rdlock(&fd_pairs_lock
);
832 if(pthread_mutex_trylock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
) == 0) {
833 /* Take care of low priority channels. */
835 "Normal read on fd %d\n",
837 /* it's ok to have an unavailable subbuffer */
838 ret
= read_subbuffer(&fd_pairs
->pair
[i
-inotify_fds
]);
839 if(ret
== EAGAIN
) ret
= 0;
841 ret
= pthread_mutex_unlock(&fd_pairs
->pair
[i
-inotify_fds
].mutex
);
843 printf("Error in mutex unlock : %s\n", strerror(ret
));
845 pthread_rwlock_unlock(&fd_pairs_lock
);
851 /* Update pollfd array if an entry was added to fd_pairs */
852 pthread_rwlock_rdlock(&fd_pairs_lock
);
853 if((inotify_fds
+ fd_pairs
->num_pairs
) != num_pollfd
) {
854 pollfd
= realloc(pollfd
,
855 (inotify_fds
+ fd_pairs
->num_pairs
) * sizeof(struct pollfd
));
856 for(i
=num_pollfd
-inotify_fds
;i
<fd_pairs
->num_pairs
;i
++) {
857 pollfd
[inotify_fds
+i
].fd
= fd_pairs
->pair
[i
].channel
;
858 pollfd
[inotify_fds
+i
].events
= POLLIN
|POLLPRI
;
860 num_pollfd
= fd_pairs
->num_pairs
+ inotify_fds
;
862 pthread_rwlock_unlock(&fd_pairs_lock
);
864 /* NB: If the fd_pairs structure is updated by another thread from this
865 * point forward, the current thread will wait in the poll without
866 * monitoring the new channel. However, this thread will add the
867 * new channel on next poll (and this should not take too much time
868 * on a loaded system).
870 * This event is quite unlikely and can only occur if a CPU is
871 * hot-plugged while multple lttd threads are running.
883 void close_channel_trace_pairs(struct channel_trace_fd
*fd_pairs
, int inotify_fd
,
884 struct inotify_watch_array
*iwatch_array
)
889 for(i
=0;i
<fd_pairs
->num_pairs
;i
++) {
890 ret
= close(fd_pairs
->pair
[i
].channel
);
891 if(ret
== -1) perror("Close error on channel");
892 ret
= close(fd_pairs
->pair
[i
].trace
);
893 if(ret
== -1) perror("Close error on trace");
895 free(fd_pairs
->pair
);
896 free(iwatch_array
->elem
);
900 void * thread_main(void *arg
)
903 unsigned long thread_num
= (unsigned long)arg
;
905 ret
= pipe(thread_pipe
);
907 perror("Error creating pipe");
910 ret
= read_channels(thread_num
, &fd_pairs
, inotify_fd
, &inotify_watch_array
);
911 close(thread_pipe
[0]); /* close read end */
912 close(thread_pipe
[1]); /* close write end */
921 inotify_fd
= inotify_init();
922 fcntl(inotify_fd
, F_SETFL
, O_NONBLOCK
);
924 if(ret
= open_channel_trace_pairs(channel_name
, trace_name
, &fd_pairs
,
925 &inotify_fd
, &inotify_watch_array
))
927 if (fd_pairs
.num_pairs
== 0) {
928 printf("No channel available for reading, exiting\n");
932 if(ret
= map_channels(&fd_pairs
, 0, fd_pairs
.num_pairs
))
937 close_channel_trace_pairs(&fd_pairs
, inotify_fd
, &inotify_watch_array
);
944 int main(int argc
, char ** argv
)
947 struct sigaction act
;
952 ret
= parse_arguments(argc
, argv
);
954 if(ret
!= 0) show_arguments();
955 if(ret
< 0) return EINVAL
;
956 if(ret
> 0) return 0;
960 /* Connect the signal handlers */
961 act
.sa_handler
= handler
;
963 sigemptyset(&(act
.sa_mask
));
964 sigaddset(&(act
.sa_mask
), SIGTERM
);
965 sigaddset(&(act
.sa_mask
), SIGQUIT
);
966 sigaddset(&(act
.sa_mask
), SIGINT
);
967 sigaction(SIGTERM
, &act
, NULL
);
968 sigaction(SIGQUIT
, &act
, NULL
);
969 sigaction(SIGINT
, &act
, NULL
);
971 if(ret
= channels_init())
978 perror("An error occured while daemonizing.");
983 tids
= malloc(sizeof(pthread_t
) * num_threads
);
984 for(i
=0; i
<num_threads
; i
++) {
986 ret
= pthread_create(&tids
[i
], NULL
, thread_main
, (void*)i
);
988 perror("Error creating thread");
993 for(i
=0; i
<num_threads
; i
++) {
994 ret
= pthread_join(tids
[i
], &tret
);
996 perror("Error joining thread");
999 if((long)tret
!= 0) {
1000 printf("Error %s occured in thread %u\n",
1001 strerror((long)tret
), i
);
1006 ret
= unmap_channels(&fd_pairs
);
1007 close_channel_trace_pairs(&fd_pairs
, inotify_fd
, &inotify_watch_array
);