6cecb9cf |
1 | /* lttd |
2 | * |
3 | * Linux Trace Toolkit Daemon |
4 | * |
bb01fdc6 |
5 | * This is a simple daemon that reads a few relay+debugfs channels and save |
6 | * them in a trace. |
6cecb9cf |
7 | * |
d66cc831 |
8 | * CPU hot-plugging is supported using inotify. |
6cecb9cf |
9 | * |
10 | * Copyright 2005 - |
11 | * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
12 | */ |
13 | |
4e4d11b3 |
14 | #ifdef HAVE_CONFIG_H |
13474e75 |
15 | #include <config.h> |
4e4d11b3 |
16 | #endif |
17 | |
c3e8c24f |
18 | #define _REENTRANT |
4e4d11b3 |
19 | #define _GNU_SOURCE |
c3e8c24f |
20 | #include <features.h> |
6cecb9cf |
21 | #include <stdio.h> |
af4061b2 |
22 | #include <unistd.h> |
6cecb9cf |
23 | #include <errno.h> |
24 | #include <sys/types.h> |
25 | #include <sys/stat.h> |
6cecb9cf |
26 | #include <stdlib.h> |
27 | #include <dirent.h> |
28 | #include <string.h> |
9e7dbe44 |
29 | #include <fcntl.h> |
30 | #include <sys/poll.h> |
af4061b2 |
31 | #include <sys/mman.h> |
32 | #include <signal.h> |
c3e8c24f |
33 | #include <pthread.h> |
47dbc661 |
34 | #include <sys/syscall.h> |
35 | #include <unistd.h> |
36 | #include <asm/ioctls.h> |
37 | |
38 | #include <linux/version.h> |
af4061b2 |
39 | |
40 | /* Relayfs IOCTL */ |
41 | #include <asm/ioctl.h> |
42 | #include <asm/types.h> |
43 | |
44 | /* Get the next sub buffer that can be read. */ |
f2bd6bd2 |
45 | #define RELAY_GET_SUBBUF _IOR(0xF5, 0x00,__u32) |
af4061b2 |
46 | /* Release the oldest reserved (by "get") sub buffer. */ |
f2bd6bd2 |
47 | #define RELAY_PUT_SUBBUF _IOW(0xF5, 0x01,__u32) |
af4061b2 |
48 | /* returns the number of sub buffers in the per cpu channel. */ |
f2bd6bd2 |
49 | #define RELAY_GET_N_SUBBUFS _IOR(0xF5, 0x02,__u32) |
af4061b2 |
50 | /* returns the size of the sub buffers. */ |
f2bd6bd2 |
51 | #define RELAY_GET_SUBBUF_SIZE _IOR(0xF5, 0x03,__u32) |
af4061b2 |
52 | |
47dbc661 |
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) |
57 | { |
58 | return syscall (__NR_inotify_init); |
59 | } |
60 | |
61 | static inline int inotify_add_watch (int fd, const char *name, __u32 mask) |
62 | { |
63 | return syscall (__NR_inotify_add_watch, fd, name, mask); |
64 | } |
65 | |
66 | static inline int inotify_rm_watch (int fd, __u32 wd) |
67 | { |
68 | return syscall (__NR_inotify_rm_watch, fd, wd); |
69 | } |
70 | #define HAS_INOTIFY |
71 | #else |
72 | static inline int inotify_init (void) |
73 | { |
74 | return -1; |
75 | } |
af4061b2 |
76 | |
47dbc661 |
77 | static inline int inotify_add_watch (int fd, const char *name, __u32 mask) |
78 | { |
79 | return 0; |
80 | } |
81 | |
82 | static inline int inotify_rm_watch (int fd, __u32 wd) |
83 | { |
84 | return 0; |
85 | } |
86 | #undef HAS_INOTIFY |
87 | #endif |
6cecb9cf |
88 | |
89 | enum { |
90 | GET_SUBBUF, |
91 | PUT_SUBBUF, |
92 | GET_N_BUBBUFS, |
93 | GET_SUBBUF_SIZE |
94 | }; |
95 | |
96 | struct fd_pair { |
97 | int channel; |
98 | int trace; |
af4061b2 |
99 | unsigned int n_subbufs; |
100 | unsigned int subbuf_size; |
101 | void *mmap; |
22acb617 |
102 | pthread_mutex_t mutex; |
6cecb9cf |
103 | }; |
104 | |
105 | struct channel_trace_fd { |
106 | struct fd_pair *pair; |
107 | int num_pairs; |
108 | }; |
109 | |
47dbc661 |
110 | struct inotify_watch { |
111 | int wd; |
112 | char path_channel[PATH_MAX]; |
113 | char path_trace[PATH_MAX]; |
114 | }; |
115 | |
116 | struct inotify_watch_array { |
117 | struct inotify_watch *elem; |
118 | int num; |
119 | }; |
120 | |
d61d094a |
121 | static __thread int thread_pipe[2]; |
d66cc831 |
122 | |
123 | struct channel_trace_fd fd_pairs = { NULL, 0 }; |
124 | int inotify_fd = -1; |
125 | struct inotify_watch_array inotify_watch_array = { NULL, 0 }; |
126 | |
127 | /* protects fd_pairs and inotify_watch_array */ |
128 | pthread_rwlock_t fd_pairs_lock = PTHREAD_RWLOCK_INITIALIZER; |
129 | |
130 | |
d24c12e7 |
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; |
af4061b2 |
136 | volatile static int quit_program = 0; /* For signal handler */ |
d24c12e7 |
137 | static int dump_flight_only = 0; |
138 | static int dump_normal_only = 0; |
6cecb9cf |
139 | |
140 | /* Args : |
141 | * |
142 | * -t directory Directory name of the trace to write to. Will be created. |
bb01fdc6 |
143 | * -c directory Root directory of the debugfs trace channels. |
6cecb9cf |
144 | * -d Run in background (daemon). |
9e7dbe44 |
145 | * -a Trace append mode. |
86a65fdb |
146 | * -s Send SIGUSR1 to parent when ready for IO. |
6cecb9cf |
147 | */ |
148 | void show_arguments(void) |
149 | { |
150 | printf("Please use the following arguments :\n"); |
151 | printf("\n"); |
152 | printf("-t directory Directory name of the trace to write to.\n" |
153 | " It will be created.\n"); |
bb01fdc6 |
154 | printf("-c directory Root directory of the debugfs trace channels.\n"); |
6cecb9cf |
155 | printf("-d Run in background (daemon).\n"); |
9e7dbe44 |
156 | printf("-a Append to an possibly existing trace.\n"); |
22acb617 |
157 | printf("-N Number of threads to start.\n"); |
d24c12e7 |
158 | printf("-f Dump only flight recorder channels.\n"); |
159 | printf("-n Dump only normal channels.\n"); |
6cecb9cf |
160 | printf("\n"); |
161 | } |
162 | |
163 | |
164 | /* parse_arguments |
165 | * |
166 | * Parses the command line arguments. |
167 | * |
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. |
170 | */ |
171 | int parse_arguments(int argc, char **argv) |
172 | { |
173 | int ret = 0; |
174 | int argn = 1; |
175 | |
176 | if(argc == 2) { |
177 | if(strcmp(argv[1], "-h") == 0) { |
178 | return 1; |
179 | } |
180 | } |
181 | |
9e7dbe44 |
182 | while(argn < argc) { |
6cecb9cf |
183 | |
184 | switch(argv[argn][0]) { |
185 | case '-': |
186 | switch(argv[argn][1]) { |
187 | case 't': |
9e7dbe44 |
188 | if(argn+1 < argc) { |
189 | trace_name = argv[argn+1]; |
190 | argn++; |
191 | } |
6cecb9cf |
192 | break; |
193 | case 'c': |
9e7dbe44 |
194 | if(argn+1 < argc) { |
195 | channel_name = argv[argn+1]; |
196 | argn++; |
197 | } |
6cecb9cf |
198 | break; |
199 | case 'd': |
200 | daemon_mode = 1; |
201 | break; |
9e7dbe44 |
202 | case 'a': |
203 | append_mode = 1; |
204 | break; |
22acb617 |
205 | case 'N': |
c3e8c24f |
206 | if(argn+1 < argc) { |
207 | num_threads = strtoul(argv[argn+1], NULL, 0); |
208 | argn++; |
209 | } |
210 | break; |
d24c12e7 |
211 | case 'f': |
212 | dump_flight_only = 1; |
213 | break; |
214 | case 'n': |
215 | dump_normal_only = 1; |
216 | break; |
6cecb9cf |
217 | default: |
218 | printf("Invalid argument '%s'.\n", argv[argn]); |
219 | printf("\n"); |
220 | ret = -1; |
221 | } |
222 | break; |
223 | default: |
224 | printf("Invalid argument '%s'.\n", argv[argn]); |
225 | printf("\n"); |
226 | ret = -1; |
227 | } |
228 | argn++; |
229 | } |
230 | |
231 | if(trace_name == NULL) { |
232 | printf("Please specify a trace name.\n"); |
233 | printf("\n"); |
234 | ret = -1; |
235 | } |
236 | |
237 | if(channel_name == NULL) { |
238 | printf("Please specify a channel name.\n"); |
239 | printf("\n"); |
240 | ret = -1; |
241 | } |
242 | |
243 | return ret; |
244 | } |
245 | |
246 | void show_info(void) |
247 | { |
e7480f75 |
248 | printf("Linux Trace Toolkit Trace Daemon " VERSION "\n"); |
6cecb9cf |
249 | printf("\n"); |
bb01fdc6 |
250 | printf("Reading from debugfs directory : %s\n", channel_name); |
6cecb9cf |
251 | printf("Writing to trace directory : %s\n", trace_name); |
252 | printf("\n"); |
253 | } |
254 | |
255 | |
af4061b2 |
256 | /* signal handling */ |
257 | |
258 | static void handler(int signo) |
259 | { |
260 | printf("Signal %d received : exiting cleanly\n", signo); |
261 | quit_program = 1; |
262 | } |
263 | |
264 | |
47dbc661 |
265 | int open_buffer_file(char *filename, char *path_channel, char *path_trace, |
266 | struct channel_trace_fd *fd_pairs) |
267 | { |
268 | int open_ret = 0; |
269 | int ret = 0; |
270 | struct stat stat_buf; |
271 | |
272 | if(strncmp(filename, "flight-", sizeof("flight-")-1) != 0) { |
273 | if(dump_flight_only) { |
274 | printf("Skipping normal channel %s\n", path_channel); |
275 | return 0; |
276 | } |
277 | } else { |
278 | if(dump_normal_only) { |
279 | printf("Skipping flight channel %s\n", path_channel); |
280 | return 0; |
281 | } |
282 | } |
283 | printf("Opening file.\n"); |
284 | |
285 | fd_pairs->pair = realloc(fd_pairs->pair, |
286 | ++fd_pairs->num_pairs * sizeof(struct fd_pair)); |
287 | |
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 */ |
295 | } |
296 | /* Open the trace in write mode, only append if append_mode */ |
297 | ret = stat(path_trace, &stat_buf); |
298 | if(ret == 0) { |
299 | if(append_mode) { |
300 | printf("Appending to file %s as requested\n", path_trace); |
301 | |
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); |
305 | |
306 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
307 | perror(path_trace); |
308 | } |
309 | } else { |
310 | printf("File %s exists, cannot open. Try append mode.\n", path_trace); |
311 | open_ret = -1; |
312 | goto end; |
313 | } |
314 | } else { |
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) { |
320 | perror(path_trace); |
321 | } |
322 | } |
323 | } |
324 | end: |
325 | return open_ret; |
326 | } |
af4061b2 |
327 | |
6cecb9cf |
328 | int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, |
47dbc661 |
329 | struct channel_trace_fd *fd_pairs, int *inotify_fd, |
330 | struct inotify_watch_array *iwatch_array) |
6cecb9cf |
331 | { |
332 | DIR *channel_dir = opendir(subchannel_name); |
333 | struct dirent *entry; |
334 | struct stat stat_buf; |
335 | int ret; |
336 | char path_channel[PATH_MAX]; |
337 | int path_channel_len; |
338 | char *path_channel_ptr; |
339 | char path_trace[PATH_MAX]; |
340 | int path_trace_len; |
341 | char *path_trace_ptr; |
4d508021 |
342 | int open_ret = 0; |
6cecb9cf |
343 | |
344 | if(channel_dir == NULL) { |
345 | perror(subchannel_name); |
02ba971e |
346 | open_ret = ENOENT; |
4d508021 |
347 | goto end; |
6cecb9cf |
348 | } |
349 | |
6cecb9cf |
350 | printf("Creating trace subdirectory %s\n", subtrace_name); |
351 | ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO); |
352 | if(ret == -1) { |
3986d00b |
353 | if(errno != EEXIST) { |
9e7dbe44 |
354 | perror(subtrace_name); |
4d508021 |
355 | open_ret = -1; |
02ba971e |
356 | goto end; |
9e7dbe44 |
357 | } |
6cecb9cf |
358 | } |
359 | |
360 | strncpy(path_channel, subchannel_name, PATH_MAX-1); |
361 | path_channel_len = strlen(path_channel); |
362 | path_channel[path_channel_len] = '/'; |
363 | path_channel_len++; |
364 | path_channel_ptr = path_channel + path_channel_len; |
365 | |
366 | strncpy(path_trace, subtrace_name, PATH_MAX-1); |
367 | path_trace_len = strlen(path_trace); |
368 | path_trace[path_trace_len] = '/'; |
369 | path_trace_len++; |
370 | path_trace_ptr = path_trace + path_trace_len; |
371 | |
47dbc661 |
372 | #ifdef HAS_INOTIFY |
373 | iwatch_array->elem = realloc(iwatch_array->elem, |
374 | ++iwatch_array->num * sizeof(struct inotify_watch)); |
375 | |
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); |
382 | #endif |
383 | |
6cecb9cf |
384 | while((entry = readdir(channel_dir)) != NULL) { |
385 | |
386 | if(entry->d_name[0] == '.') continue; |
387 | |
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); |
390 | |
391 | ret = stat(path_channel, &stat_buf); |
392 | if(ret == -1) { |
393 | perror(path_channel); |
394 | continue; |
395 | } |
396 | |
397 | printf("Channel file : %s\n", path_channel); |
398 | |
399 | if(S_ISDIR(stat_buf.st_mode)) { |
400 | |
401 | printf("Entering channel subdirectory...\n"); |
47dbc661 |
402 | ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs, |
403 | inotify_fd, iwatch_array); |
6cecb9cf |
404 | if(ret < 0) continue; |
9e7dbe44 |
405 | } else if(S_ISREG(stat_buf.st_mode)) { |
47dbc661 |
406 | open_ret = open_buffer_file(entry->d_name, path_channel, path_trace, |
407 | fd_pairs); |
408 | if(open_ret) |
409 | goto end; |
6cecb9cf |
410 | } |
6cecb9cf |
411 | } |
412 | |
02ba971e |
413 | end: |
6cecb9cf |
414 | closedir(channel_dir); |
415 | |
02ba971e |
416 | return open_ret; |
6cecb9cf |
417 | } |
418 | |
af4061b2 |
419 | |
420 | int read_subbuffer(struct fd_pair *pair) |
421 | { |
d61d094a |
422 | unsigned int consumed_old; |
423 | int err; |
424 | long ret; |
425 | unsigned long len, offset; |
af4061b2 |
426 | |
427 | |
d61d094a |
428 | err = ioctl(pair->channel, RELAY_GET_SUBBUF, &consumed_old); |
12bcec97 |
429 | printf("cookie : %u\n", consumed_old); |
13474e75 |
430 | if(err != 0) { |
22acb617 |
431 | ret = errno; |
63bf9ed1 |
432 | perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)"); |
13474e75 |
433 | goto get_error; |
af4061b2 |
434 | } |
d61d094a |
435 | #if 0 |
13474e75 |
436 | err = TEMP_FAILURE_RETRY(write(pair->trace, |
2312de30 |
437 | pair->mmap |
438 | + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)), |
af4061b2 |
439 | pair->subbuf_size)); |
13474e75 |
440 | |
441 | if(err < 0) { |
22acb617 |
442 | ret = errno; |
af4061b2 |
443 | perror("Error in writing to file"); |
13474e75 |
444 | goto write_error; |
af4061b2 |
445 | } |
d61d094a |
446 | #endif //0 |
447 | len = pair->subbuf_size; |
448 | offset = 0; |
449 | while (len > 0) { |
450 | printf("splice chan to pipe offset %lu\n", offset); |
451 | ret = splice(pair->channel, &offset, thread_pipe[1], NULL, |
452 | len, SPLICE_F_MOVE); |
453 | printf("splice chan to pipe ret %ld\n", ret); |
454 | if (ret < 0) { |
455 | perror("Error in relay splice"); |
456 | goto write_error; |
457 | } |
458 | ret = splice(thread_pipe[0], NULL, pair->trace, NULL, |
459 | ret, SPLICE_F_MOVE); |
460 | printf("splice pipe to file %ld\n", ret); |
461 | if (ret < 0) { |
462 | perror("Error in file splice"); |
463 | goto write_error; |
464 | } |
465 | len -= ret; |
466 | } |
467 | |
c5fdfca5 |
468 | #if 0 |
469 | err = fsync(pair->trace); |
470 | if(err < 0) { |
471 | ret = errno; |
472 | perror("Error in writing to file"); |
473 | goto write_error; |
474 | } |
475 | #endif //0 |
13474e75 |
476 | write_error: |
d61d094a |
477 | ret = 0; |
bb01fdc6 |
478 | err = ioctl(pair->channel, RELAY_PUT_SUBBUF, &consumed_old); |
13474e75 |
479 | if(err != 0) { |
22acb617 |
480 | ret = errno; |
63bf9ed1 |
481 | if(errno == EFAULT) { |
22acb617 |
482 | perror("Error in unreserving sub buffer\n"); |
63bf9ed1 |
483 | } else if(errno == EIO) { |
12bcec97 |
484 | perror("Reader has been pushed by the writer, last subbuffer corrupted."); |
db9d75c0 |
485 | /* FIXME : we may delete the last written buffer if we wish. */ |
12bcec97 |
486 | } |
13474e75 |
487 | goto get_error; |
af4061b2 |
488 | } |
489 | |
13474e75 |
490 | get_error: |
491 | return ret; |
af4061b2 |
492 | } |
493 | |
494 | |
47dbc661 |
495 | int map_channels(struct channel_trace_fd *fd_pairs, |
496 | int idx_begin, int idx_end) |
6cecb9cf |
497 | { |
af4061b2 |
498 | int i,j; |
c3e8c24f |
499 | int ret=0; |
af4061b2 |
500 | |
13474e75 |
501 | if(fd_pairs->num_pairs <= 0) { |
502 | printf("No channel to read\n"); |
503 | goto end; |
504 | } |
505 | |
af4061b2 |
506 | /* Get the subbuf sizes and number */ |
507 | |
47dbc661 |
508 | for(i=idx_begin;i<idx_end;i++) { |
af4061b2 |
509 | struct fd_pair *pair = &fd_pairs->pair[i]; |
9e7dbe44 |
510 | |
bb01fdc6 |
511 | ret = ioctl(pair->channel, RELAY_GET_N_SUBBUFS, |
af4061b2 |
512 | &pair->n_subbufs); |
513 | if(ret != 0) { |
514 | perror("Error in getting the number of subbuffers"); |
515 | goto end; |
516 | } |
bb01fdc6 |
517 | ret = ioctl(pair->channel, RELAY_GET_SUBBUF_SIZE, |
af4061b2 |
518 | &pair->subbuf_size); |
519 | if(ret != 0) { |
520 | perror("Error in getting the size of the subbuffers"); |
521 | goto end; |
522 | } |
22acb617 |
523 | ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */ |
524 | if(ret != 0) { |
525 | perror("Error in mutex init"); |
526 | goto end; |
527 | } |
af4061b2 |
528 | } |
529 | |
d61d094a |
530 | #if 0 |
af4061b2 |
531 | /* Mmap each FD */ |
47dbc661 |
532 | for(i=idx_begin;i<idx_end;i++) { |
af4061b2 |
533 | struct fd_pair *pair = &fd_pairs->pair[i]; |
534 | |
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"); |
539 | goto munmap; |
540 | } |
541 | } |
542 | |
22acb617 |
543 | goto end; /* success */ |
af4061b2 |
544 | |
c3e8c24f |
545 | /* Error handling */ |
546 | /* munmap only the successfully mmapped indexes */ |
547 | munmap: |
548 | /* Munmap each FD */ |
47dbc661 |
549 | for(j=idx_begin;j<i;j++) { |
c3e8c24f |
550 | struct fd_pair *pair = &fd_pairs->pair[j]; |
551 | int err_ret; |
552 | |
553 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
554 | if(err_ret != 0) { |
555 | perror("Error in munmap"); |
556 | } |
557 | ret |= err_ret; |
558 | } |
559 | |
d61d094a |
560 | #endif //0 |
c3e8c24f |
561 | end: |
562 | return ret; |
c3e8c24f |
563 | } |
564 | |
c3e8c24f |
565 | int unmap_channels(struct channel_trace_fd *fd_pairs) |
566 | { |
567 | int j; |
568 | int ret=0; |
569 | |
570 | /* Munmap each FD */ |
571 | for(j=0;j<fd_pairs->num_pairs;j++) { |
572 | struct fd_pair *pair = &fd_pairs->pair[j]; |
573 | int err_ret; |
574 | |
d61d094a |
575 | #if 0 |
c3e8c24f |
576 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
577 | if(err_ret != 0) { |
578 | perror("Error in munmap"); |
579 | } |
580 | ret |= err_ret; |
d61d094a |
581 | #endif //0 |
22acb617 |
582 | err_ret = pthread_mutex_destroy(&pair->mutex); |
583 | if(err_ret != 0) { |
584 | perror("Error in mutex destroy"); |
585 | } |
586 | ret |= err_ret; |
c3e8c24f |
587 | } |
588 | |
589 | return ret; |
590 | } |
591 | |
47dbc661 |
592 | #ifdef HAS_INOTIFY |
593 | /* Inotify event arrived. |
594 | * |
595 | * Only support add file for now. |
596 | */ |
597 | |
598 | int read_inotify(int inotify_fd, |
599 | struct channel_trace_fd *fd_pairs, |
600 | struct inotify_watch_array *iwatch_array) |
601 | { |
602 | char buf[sizeof(struct inotify_event) + PATH_MAX]; |
603 | char path_channel[PATH_MAX]; |
604 | char path_trace[PATH_MAX]; |
605 | ssize_t len; |
606 | struct inotify_event *ievent; |
607 | size_t offset; |
608 | unsigned int i; |
609 | int ret; |
610 | int old_num; |
611 | |
612 | offset = 0; |
613 | len = read(inotify_fd, buf, sizeof(struct inotify_event) + PATH_MAX); |
614 | if(len < 0) { |
d66cc831 |
615 | |
616 | if(errno == EAGAIN) |
617 | return 0; /* another thread got the data before us */ |
618 | |
47dbc661 |
619 | printf("Error in read from inotify FD %s.\n", strerror(len)); |
620 | return -1; |
621 | } |
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"); |
638 | return -1; |
639 | } |
640 | if(ret = map_channels(fd_pairs, old_num, fd_pairs->num_pairs)) { |
641 | printf("Error mapping channel\n"); |
642 | return -1; |
643 | } |
644 | |
645 | } |
646 | } |
647 | offset += sizeof(*ievent) + ievent->len; |
648 | } |
649 | } |
650 | #endif //HAS_INOTIFY |
c3e8c24f |
651 | |
652 | /* read_channels |
22acb617 |
653 | * |
654 | * Thread worker. |
c3e8c24f |
655 | * |
bb01fdc6 |
656 | * Read the debugfs channels and write them in the paired tracefiles. |
c3e8c24f |
657 | * |
658 | * @fd_pairs : paired channels and trace files. |
659 | * |
47dbc661 |
660 | * returns 0 on success, -1 on error. |
c3e8c24f |
661 | * |
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. |
665 | * |
666 | * Note that a channel is considered high priority when the buffer is almost |
667 | * full. |
668 | */ |
669 | |
d66cc831 |
670 | int read_channels(unsigned long thread_num, struct channel_trace_fd *fd_pairs, |
47dbc661 |
671 | int inotify_fd, struct inotify_watch_array *iwatch_array) |
c3e8c24f |
672 | { |
47dbc661 |
673 | struct pollfd *pollfd = NULL; |
d66cc831 |
674 | int num_pollfd; |
c3e8c24f |
675 | int i,j; |
676 | int num_rdy, num_hup; |
677 | int high_prio; |
22acb617 |
678 | int ret = 0; |
47dbc661 |
679 | int inotify_fds; |
680 | unsigned int old_num; |
c3e8c24f |
681 | |
47dbc661 |
682 | #ifdef HAS_INOTIFY |
683 | inotify_fds = 1; |
684 | #else |
685 | inotify_fds = 0; |
686 | #endif |
687 | |
d66cc831 |
688 | pthread_rwlock_rdlock(&fd_pairs_lock); |
689 | |
47dbc661 |
690 | /* Start polling the FD. Keep one fd for inotify */ |
691 | pollfd = malloc((inotify_fds + fd_pairs->num_pairs) * sizeof(struct pollfd)); |
692 | |
693 | #ifdef HAS_INOTIFY |
694 | pollfd[0].fd = inotify_fd; |
695 | pollfd[0].events = POLLIN|POLLPRI; |
696 | #endif |
9e7dbe44 |
697 | |
698 | for(i=0;i<fd_pairs->num_pairs;i++) { |
47dbc661 |
699 | pollfd[inotify_fds+i].fd = fd_pairs->pair[i].channel; |
700 | pollfd[inotify_fds+i].events = POLLIN|POLLPRI; |
9e7dbe44 |
701 | } |
d66cc831 |
702 | num_pollfd = inotify_fds + fd_pairs->num_pairs; |
703 | |
704 | |
705 | pthread_rwlock_unlock(&fd_pairs_lock); |
706 | |
9e7dbe44 |
707 | while(1) { |
68047576 |
708 | high_prio = 0; |
af4061b2 |
709 | num_hup = 0; |
710 | #ifdef DEBUG |
711 | printf("Press a key for next poll...\n"); |
712 | char buf[1]; |
713 | read(STDIN_FILENO, &buf, 1); |
d66cc831 |
714 | printf("Next poll (polling %d fd) :\n", num_pollfd); |
af4061b2 |
715 | #endif //DEBUG |
47dbc661 |
716 | |
af4061b2 |
717 | /* Have we received a signal ? */ |
718 | if(quit_program) break; |
719 | |
d66cc831 |
720 | num_rdy = poll(pollfd, num_pollfd, -1); |
721 | |
9e7dbe44 |
722 | if(num_rdy == -1) { |
723 | perror("Poll error"); |
af4061b2 |
724 | goto free_fd; |
9e7dbe44 |
725 | } |
726 | |
727 | printf("Data received\n"); |
47dbc661 |
728 | #ifdef HAS_INOTIFY |
729 | switch(pollfd[0].revents) { |
730 | case POLLERR: |
731 | printf("Error returned in polling inotify fd %d.\n", pollfd[0].fd); |
732 | break; |
733 | case POLLHUP: |
734 | printf("Polling inotify fd %d tells it has hung up.\n", pollfd[0].fd); |
735 | break; |
736 | case POLLNVAL: |
737 | printf("Polling inotify fd %d tells fd is not open.\n", pollfd[0].fd); |
738 | break; |
739 | case POLLPRI: |
740 | case POLLIN: |
d66cc831 |
741 | |
47dbc661 |
742 | printf("Polling inotify fd %d : data ready.\n", pollfd[0].fd); |
d66cc831 |
743 | |
744 | pthread_rwlock_wrlock(&fd_pairs_lock); |
47dbc661 |
745 | read_inotify(inotify_fd, fd_pairs, iwatch_array); |
d66cc831 |
746 | pthread_rwlock_unlock(&fd_pairs_lock); |
747 | |
47dbc661 |
748 | break; |
749 | } |
750 | #endif |
9e7dbe44 |
751 | |
d66cc831 |
752 | for(i=inotify_fds;i<num_pollfd;i++) { |
9e7dbe44 |
753 | switch(pollfd[i].revents) { |
754 | case POLLERR: |
755 | printf("Error returned in polling fd %d.\n", pollfd[i].fd); |
af4061b2 |
756 | num_hup++; |
9e7dbe44 |
757 | break; |
758 | case POLLHUP: |
759 | printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd); |
af4061b2 |
760 | num_hup++; |
9e7dbe44 |
761 | break; |
762 | case POLLNVAL: |
763 | printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd); |
af4061b2 |
764 | num_hup++; |
9e7dbe44 |
765 | break; |
766 | case POLLPRI: |
d66cc831 |
767 | pthread_rwlock_rdlock(&fd_pairs_lock); |
47dbc661 |
768 | if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) { |
22acb617 |
769 | printf("Urgent read on fd %d\n", pollfd[i].fd); |
770 | /* Take care of high priority channels first. */ |
771 | high_prio = 1; |
772 | /* it's ok to have an unavailable subbuffer */ |
47dbc661 |
773 | ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]); |
63bf9ed1 |
774 | if(ret == EAGAIN) ret = 0; |
b8d28629 |
775 | |
47dbc661 |
776 | ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex); |
22acb617 |
777 | if(ret) |
778 | printf("Error in mutex unlock : %s\n", strerror(ret)); |
779 | } |
d66cc831 |
780 | pthread_rwlock_unlock(&fd_pairs_lock); |
9e7dbe44 |
781 | break; |
68047576 |
782 | } |
9e7dbe44 |
783 | } |
47dbc661 |
784 | /* If every buffer FD has hung up, we end the read loop here */ |
d66cc831 |
785 | if(num_hup == num_pollfd - inotify_fds) break; |
9e7dbe44 |
786 | |
68047576 |
787 | if(!high_prio) { |
d66cc831 |
788 | for(i=inotify_fds;i<num_pollfd;i++) { |
68047576 |
789 | switch(pollfd[i].revents) { |
790 | case POLLIN: |
d66cc831 |
791 | pthread_rwlock_rdlock(&fd_pairs_lock); |
47dbc661 |
792 | if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) { |
22acb617 |
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 */ |
47dbc661 |
796 | ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]); |
63bf9ed1 |
797 | if(ret == EAGAIN) ret = 0; |
b8d28629 |
798 | |
47dbc661 |
799 | ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex); |
22acb617 |
800 | if(ret) |
801 | printf("Error in mutex unlock : %s\n", strerror(ret)); |
802 | } |
d66cc831 |
803 | pthread_rwlock_unlock(&fd_pairs_lock); |
68047576 |
804 | break; |
805 | } |
806 | } |
9e7dbe44 |
807 | } |
808 | |
d66cc831 |
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; |
817 | } |
818 | num_pollfd = fd_pairs->num_pairs + inotify_fds; |
819 | } |
820 | pthread_rwlock_unlock(&fd_pairs_lock); |
821 | |
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). |
827 | * |
828 | * This event is quite unlikely and can only occur if a CPU is |
829 | * hot-plugged while multple lttd threads are running. |
830 | */ |
9e7dbe44 |
831 | } |
832 | |
af4061b2 |
833 | free_fd: |
9e7dbe44 |
834 | free(pollfd); |
835 | |
af4061b2 |
836 | end: |
47dbc661 |
837 | return ret; |
6cecb9cf |
838 | } |
839 | |
840 | |
47dbc661 |
841 | void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs, int inotify_fd, |
842 | struct inotify_watch_array *iwatch_array) |
6cecb9cf |
843 | { |
9e7dbe44 |
844 | int i; |
845 | int ret; |
6cecb9cf |
846 | |
9e7dbe44 |
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"); |
852 | } |
853 | free(fd_pairs->pair); |
47dbc661 |
854 | free(iwatch_array->elem); |
855 | } |
856 | |
857 | /* Thread worker */ |
858 | void * thread_main(void *arg) |
859 | { |
d66cc831 |
860 | long ret; |
861 | unsigned long thread_num = (unsigned long)arg; |
862 | |
d61d094a |
863 | ret = pipe(thread_pipe); |
864 | if (ret < 0) { |
865 | perror("Error creating pipe"); |
866 | return (void*)ret; |
867 | } |
d66cc831 |
868 | ret = read_channels(thread_num, &fd_pairs, inotify_fd, &inotify_watch_array); |
d61d094a |
869 | close(thread_pipe[0]); /* close read end */ |
870 | close(thread_pipe[1]); /* close write end */ |
d66cc831 |
871 | return (void*)ret; |
872 | } |
873 | |
874 | |
875 | int channels_init() |
876 | { |
47dbc661 |
877 | int ret = 0; |
47dbc661 |
878 | |
879 | inotify_fd = inotify_init(); |
d66cc831 |
880 | fcntl(inotify_fd, F_SETFL, O_NONBLOCK); |
47dbc661 |
881 | |
882 | if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs, |
883 | &inotify_fd, &inotify_watch_array)) |
884 | goto close_channel; |
885 | |
886 | if(ret = map_channels(&fd_pairs, 0, fd_pairs.num_pairs)) |
887 | goto close_channel; |
d66cc831 |
888 | return 0; |
47dbc661 |
889 | |
890 | close_channel: |
891 | close_channel_trace_pairs(&fd_pairs, inotify_fd, &inotify_watch_array); |
892 | if(inotify_fd >= 0) |
893 | close(inotify_fd); |
d66cc831 |
894 | return ret; |
6cecb9cf |
895 | } |
896 | |
d66cc831 |
897 | |
6cecb9cf |
898 | int main(int argc, char ** argv) |
899 | { |
c3e8c24f |
900 | int ret = 0; |
af4061b2 |
901 | struct sigaction act; |
22acb617 |
902 | pthread_t *tids; |
d66cc831 |
903 | unsigned long i; |
22acb617 |
904 | void *tret; |
6cecb9cf |
905 | |
906 | ret = parse_arguments(argc, argv); |
907 | |
908 | if(ret != 0) show_arguments(); |
909 | if(ret < 0) return EINVAL; |
910 | if(ret > 0) return 0; |
911 | |
912 | show_info(); |
913 | |
af4061b2 |
914 | /* Connect the signal handlers */ |
915 | act.sa_handler = handler; |
916 | act.sa_flags = 0; |
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); |
924 | |
d66cc831 |
925 | if(ret = channels_init()) |
926 | return ret; |
927 | |
ebb39a89 |
928 | if(daemon_mode) { |
929 | ret = daemon(0, 0); |
930 | |
931 | if(ret == -1) { |
932 | perror("An error occured while daemonizing."); |
933 | exit(-1); |
934 | } |
935 | } |
936 | |
22acb617 |
937 | tids = malloc(sizeof(pthread_t) * num_threads); |
938 | for(i=0; i<num_threads; i++) { |
d3b6d84f |
939 | |
47dbc661 |
940 | ret = pthread_create(&tids[i], NULL, thread_main, (void*)i); |
22acb617 |
941 | if(ret) { |
942 | perror("Error creating thread"); |
943 | break; |
944 | } |
945 | } |
6cecb9cf |
946 | |
22acb617 |
947 | for(i=0; i<num_threads; i++) { |
948 | ret = pthread_join(tids[i], &tret); |
949 | if(ret) { |
950 | perror("Error joining thread"); |
951 | break; |
952 | } |
d66cc831 |
953 | if((long)tret != 0) { |
954 | printf("Error %s occured in thread %u\n", |
955 | strerror((long)tret), i); |
22acb617 |
956 | } |
957 | } |
958 | |
959 | free(tids); |
d66cc831 |
960 | ret = unmap_channels(&fd_pairs); |
961 | close_channel_trace_pairs(&fd_pairs, inotify_fd, &inotify_watch_array); |
962 | if(inotify_fd >= 0) |
963 | close(inotify_fd); |
22acb617 |
964 | |
6cecb9cf |
965 | return ret; |
966 | } |