Commit | Line | Data |
---|---|---|
617de8e1 | 1 | /* lttd |
2 | * | |
3 | * Linux Trace Toolkit Daemon | |
4 | * | |
c2ffa20f | 5 | * This is a simple daemon that reads a few relay+debugfs channels and save |
6 | * them in a trace. | |
617de8e1 | 7 | * |
31482529 | 8 | * CPU hot-plugging is supported using inotify. |
617de8e1 | 9 | * |
10 | * Copyright 2005 - | |
11 | * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
12 | */ | |
13 | ||
0bb647f5 | 14 | #ifdef HAVE_CONFIG_H |
469206ed | 15 | #include <config.h> |
0bb647f5 | 16 | #endif |
17 | ||
e54e1d5d | 18 | #define _REENTRANT |
0bb647f5 | 19 | #define _GNU_SOURCE |
e54e1d5d | 20 | #include <features.h> |
617de8e1 | 21 | #include <stdio.h> |
1d483eea | 22 | #include <unistd.h> |
617de8e1 | 23 | #include <errno.h> |
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
617de8e1 | 26 | #include <stdlib.h> |
27 | #include <dirent.h> | |
28 | #include <string.h> | |
90ccaa9a | 29 | #include <fcntl.h> |
30 | #include <sys/poll.h> | |
1d483eea | 31 | #include <sys/mman.h> |
32 | #include <signal.h> | |
e54e1d5d | 33 | #include <pthread.h> |
357915bb | 34 | #include <sys/syscall.h> |
35 | #include <unistd.h> | |
36 | #include <asm/ioctls.h> | |
37 | ||
38 | #include <linux/version.h> | |
1d483eea | 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. */ | |
572db7d9 | 45 | #define RELAY_GET_SB _IOR(0xF5, 0x00,__u32) |
1d483eea | 46 | /* Release the oldest reserved (by "get") sub buffer. */ |
572db7d9 | 47 | #define RELAY_PUT_SB _IOW(0xF5, 0x01,__u32) |
1d483eea | 48 | /* returns the number of sub buffers in the per cpu channel. */ |
572db7d9 MD |
49 | #define RELAY_GET_N_SB _IOR(0xF5, 0x02,__u32) |
50 | /* returns the size of the current sub buffer. */ | |
51 | #define RELAY_GET_SB_SIZE _IOR(0xF5, 0x03, __u32) | |
52 | /* returns the size of data to consume in the current sub-buffer. */ | |
53 | #define RELAY_GET_MAX_SB_SIZE _IOR(0xF5, 0x04, __u32) | |
54 | ||
1d483eea | 55 | |
357915bb | 56 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14) |
7967f7c3 | 57 | #include <sys/inotify.h> |
8d91577f | 58 | #if 0 /* should now be provided by libc. */ |
357915bb | 59 | /* From the inotify-tools 2.6 package */ |
60 | static inline int inotify_init (void) | |
61 | { | |
62 | return syscall (__NR_inotify_init); | |
63 | } | |
64 | ||
65 | static inline int inotify_add_watch (int fd, const char *name, __u32 mask) | |
66 | { | |
67 | return syscall (__NR_inotify_add_watch, fd, name, mask); | |
68 | } | |
69 | ||
70 | static inline int inotify_rm_watch (int fd, __u32 wd) | |
71 | { | |
72 | return syscall (__NR_inotify_rm_watch, fd, wd); | |
73 | } | |
8d91577f | 74 | #endif //0 |
357915bb | 75 | #define HAS_INOTIFY |
76 | #else | |
77 | static inline int inotify_init (void) | |
78 | { | |
79 | return -1; | |
80 | } | |
1d483eea | 81 | |
357915bb | 82 | static inline int inotify_add_watch (int fd, const char *name, __u32 mask) |
83 | { | |
84 | return 0; | |
85 | } | |
86 | ||
87 | static inline int inotify_rm_watch (int fd, __u32 wd) | |
88 | { | |
89 | return 0; | |
90 | } | |
91 | #undef HAS_INOTIFY | |
92 | #endif | |
617de8e1 | 93 | |
617de8e1 | 94 | struct fd_pair { |
95 | int channel; | |
96 | int trace; | |
572db7d9 MD |
97 | unsigned int n_sb; |
98 | unsigned int max_sb_size; | |
1d483eea | 99 | void *mmap; |
5ffb77aa | 100 | pthread_mutex_t mutex; |
617de8e1 | 101 | }; |
102 | ||
103 | struct channel_trace_fd { | |
104 | struct fd_pair *pair; | |
105 | int num_pairs; | |
106 | }; | |
107 | ||
357915bb | 108 | struct inotify_watch { |
109 | int wd; | |
110 | char path_channel[PATH_MAX]; | |
111 | char path_trace[PATH_MAX]; | |
112 | }; | |
113 | ||
114 | struct inotify_watch_array { | |
115 | struct inotify_watch *elem; | |
116 | int num; | |
117 | }; | |
118 | ||
f01152ea | 119 | static __thread int thread_pipe[2]; |
31482529 | 120 | |
121 | struct channel_trace_fd fd_pairs = { NULL, 0 }; | |
122 | int inotify_fd = -1; | |
123 | struct inotify_watch_array inotify_watch_array = { NULL, 0 }; | |
124 | ||
125 | /* protects fd_pairs and inotify_watch_array */ | |
126 | pthread_rwlock_t fd_pairs_lock = PTHREAD_RWLOCK_INITIALIZER; | |
127 | ||
128 | ||
89565b43 | 129 | static char *trace_name = NULL; |
130 | static char *channel_name = NULL; | |
131 | static int daemon_mode = 0; | |
132 | static int append_mode = 0; | |
133 | static unsigned long num_threads = 1; | |
1d483eea | 134 | volatile static int quit_program = 0; /* For signal handler */ |
89565b43 | 135 | static int dump_flight_only = 0; |
136 | static int dump_normal_only = 0; | |
083518b7 | 137 | static int verbose_mode = 0; |
138 | ||
139 | #define printf_verbose(fmt, args...) \ | |
140 | do { \ | |
141 | if (verbose_mode) \ | |
142 | printf(fmt, ##args); \ | |
143 | } while (0) | |
617de8e1 | 144 | |
145 | /* Args : | |
146 | * | |
147 | * -t directory Directory name of the trace to write to. Will be created. | |
c2ffa20f | 148 | * -c directory Root directory of the debugfs trace channels. |
617de8e1 | 149 | * -d Run in background (daemon). |
083518b7 | 150 | * -a Trace append mode. |
151 | * -s Send SIGUSR1 to parent when ready for IO. | |
617de8e1 | 152 | */ |
153 | void show_arguments(void) | |
154 | { | |
155 | printf("Please use the following arguments :\n"); | |
156 | printf("\n"); | |
157 | printf("-t directory Directory name of the trace to write to.\n" | |
158 | " It will be created.\n"); | |
c2ffa20f | 159 | printf("-c directory Root directory of the debugfs trace channels.\n"); |
617de8e1 | 160 | printf("-d Run in background (daemon).\n"); |
90ccaa9a | 161 | printf("-a Append to an possibly existing trace.\n"); |
5ffb77aa | 162 | printf("-N Number of threads to start.\n"); |
89565b43 | 163 | printf("-f Dump only flight recorder channels.\n"); |
164 | printf("-n Dump only normal channels.\n"); | |
083518b7 | 165 | printf("-v Verbose mode.\n"); |
617de8e1 | 166 | printf("\n"); |
167 | } | |
168 | ||
169 | ||
170 | /* parse_arguments | |
171 | * | |
172 | * Parses the command line arguments. | |
173 | * | |
174 | * Returns 1 if the arguments were correct, but doesn't ask for program | |
175 | * continuation. Returns -1 if the arguments are incorrect, or 0 if OK. | |
176 | */ | |
177 | int parse_arguments(int argc, char **argv) | |
178 | { | |
179 | int ret = 0; | |
180 | int argn = 1; | |
181 | ||
182 | if(argc == 2) { | |
183 | if(strcmp(argv[1], "-h") == 0) { | |
184 | return 1; | |
185 | } | |
186 | } | |
187 | ||
90ccaa9a | 188 | while(argn < argc) { |
617de8e1 | 189 | |
190 | switch(argv[argn][0]) { | |
191 | case '-': | |
192 | switch(argv[argn][1]) { | |
193 | case 't': | |
90ccaa9a | 194 | if(argn+1 < argc) { |
195 | trace_name = argv[argn+1]; | |
196 | argn++; | |
197 | } | |
617de8e1 | 198 | break; |
199 | case 'c': | |
90ccaa9a | 200 | if(argn+1 < argc) { |
201 | channel_name = argv[argn+1]; | |
202 | argn++; | |
203 | } | |
617de8e1 | 204 | break; |
205 | case 'd': | |
206 | daemon_mode = 1; | |
207 | break; | |
90ccaa9a | 208 | case 'a': |
209 | append_mode = 1; | |
210 | break; | |
5ffb77aa | 211 | case 'N': |
e54e1d5d | 212 | if(argn+1 < argc) { |
213 | num_threads = strtoul(argv[argn+1], NULL, 0); | |
214 | argn++; | |
215 | } | |
216 | break; | |
89565b43 | 217 | case 'f': |
218 | dump_flight_only = 1; | |
219 | break; | |
220 | case 'n': | |
221 | dump_normal_only = 1; | |
222 | break; | |
083518b7 | 223 | case 'v': |
224 | verbose_mode = 1; | |
225 | break; | |
617de8e1 | 226 | default: |
227 | printf("Invalid argument '%s'.\n", argv[argn]); | |
228 | printf("\n"); | |
229 | ret = -1; | |
230 | } | |
231 | break; | |
232 | default: | |
233 | printf("Invalid argument '%s'.\n", argv[argn]); | |
234 | printf("\n"); | |
235 | ret = -1; | |
236 | } | |
237 | argn++; | |
238 | } | |
239 | ||
240 | if(trace_name == NULL) { | |
241 | printf("Please specify a trace name.\n"); | |
242 | printf("\n"); | |
243 | ret = -1; | |
244 | } | |
245 | ||
246 | if(channel_name == NULL) { | |
247 | printf("Please specify a channel name.\n"); | |
248 | printf("\n"); | |
249 | ret = -1; | |
250 | } | |
251 | ||
252 | return ret; | |
253 | } | |
254 | ||
255 | void show_info(void) | |
256 | { | |
15061ecb | 257 | printf("Linux Trace Toolkit Trace Daemon " VERSION "\n"); |
617de8e1 | 258 | printf("\n"); |
c2ffa20f | 259 | printf("Reading from debugfs directory : %s\n", channel_name); |
617de8e1 | 260 | printf("Writing to trace directory : %s\n", trace_name); |
261 | printf("\n"); | |
262 | } | |
263 | ||
264 | ||
1d483eea | 265 | /* signal handling */ |
266 | ||
267 | static void handler(int signo) | |
268 | { | |
269 | printf("Signal %d received : exiting cleanly\n", signo); | |
270 | quit_program = 1; | |
271 | } | |
272 | ||
273 | ||
357915bb | 274 | int open_buffer_file(char *filename, char *path_channel, char *path_trace, |
275 | struct channel_trace_fd *fd_pairs) | |
276 | { | |
277 | int open_ret = 0; | |
278 | int ret = 0; | |
279 | struct stat stat_buf; | |
280 | ||
281 | if(strncmp(filename, "flight-", sizeof("flight-")-1) != 0) { | |
282 | if(dump_flight_only) { | |
083518b7 | 283 | printf_verbose("Skipping normal channel %s\n", |
284 | path_channel); | |
357915bb | 285 | return 0; |
286 | } | |
287 | } else { | |
288 | if(dump_normal_only) { | |
083518b7 | 289 | printf_verbose("Skipping flight channel %s\n", |
290 | path_channel); | |
357915bb | 291 | return 0; |
292 | } | |
293 | } | |
083518b7 | 294 | printf_verbose("Opening file.\n"); |
357915bb | 295 | |
296 | fd_pairs->pair = realloc(fd_pairs->pair, | |
297 | ++fd_pairs->num_pairs * sizeof(struct fd_pair)); | |
298 | ||
299 | /* Open the channel in read mode */ | |
300 | fd_pairs->pair[fd_pairs->num_pairs-1].channel = | |
301 | open(path_channel, O_RDONLY | O_NONBLOCK); | |
302 | if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) { | |
303 | perror(path_channel); | |
304 | fd_pairs->num_pairs--; | |
305 | return 0; /* continue */ | |
306 | } | |
307 | /* Open the trace in write mode, only append if append_mode */ | |
308 | ret = stat(path_trace, &stat_buf); | |
309 | if(ret == 0) { | |
310 | if(append_mode) { | |
083518b7 | 311 | printf_verbose("Appending to file %s as requested\n", |
312 | path_trace); | |
357915bb | 313 | |
314 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = | |
42e99028 | 315 | open(path_trace, O_WRONLY, |
357915bb | 316 | S_IRWXU|S_IRWXG|S_IRWXO); |
357915bb | 317 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
318 | perror(path_trace); | |
1ee8f63e MD |
319 | open_ret = -1; |
320 | close(fd_pairs->pair[fd_pairs->num_pairs-1].channel); | |
321 | fd_pairs->num_pairs--; | |
322 | goto end; | |
357915bb | 323 | } |
42e99028 | 324 | ret = lseek(fd_pairs->pair[fd_pairs->num_pairs-1].trace, |
325 | 0, SEEK_END); | |
326 | if (ret < 0) { | |
327 | perror(path_trace); | |
1ee8f63e MD |
328 | open_ret = -1; |
329 | close(fd_pairs->pair[fd_pairs->num_pairs-1].channel); | |
330 | close(fd_pairs->pair[fd_pairs->num_pairs-1].trace); | |
331 | fd_pairs->num_pairs--; | |
332 | goto end; | |
42e99028 | 333 | } |
357915bb | 334 | } else { |
335 | printf("File %s exists, cannot open. Try append mode.\n", path_trace); | |
336 | open_ret = -1; | |
1ee8f63e MD |
337 | close(fd_pairs->pair[fd_pairs->num_pairs-1].channel); |
338 | fd_pairs->num_pairs--; | |
357915bb | 339 | goto end; |
340 | } | |
341 | } else { | |
342 | if(errno == ENOENT) { | |
343 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = | |
344 | open(path_trace, O_WRONLY|O_CREAT|O_EXCL, | |
345 | S_IRWXU|S_IRWXG|S_IRWXO); | |
346 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { | |
347 | perror(path_trace); | |
1ee8f63e MD |
348 | open_ret = -1; |
349 | close(fd_pairs->pair[fd_pairs->num_pairs-1].channel); | |
350 | fd_pairs->num_pairs--; | |
351 | goto end; | |
357915bb | 352 | } |
353 | } | |
354 | } | |
355 | end: | |
356 | return open_ret; | |
357 | } | |
1d483eea | 358 | |
617de8e1 | 359 | int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, |
357915bb | 360 | struct channel_trace_fd *fd_pairs, int *inotify_fd, |
361 | struct inotify_watch_array *iwatch_array) | |
617de8e1 | 362 | { |
363 | DIR *channel_dir = opendir(subchannel_name); | |
364 | struct dirent *entry; | |
365 | struct stat stat_buf; | |
366 | int ret; | |
367 | char path_channel[PATH_MAX]; | |
368 | int path_channel_len; | |
369 | char *path_channel_ptr; | |
370 | char path_trace[PATH_MAX]; | |
371 | int path_trace_len; | |
372 | char *path_trace_ptr; | |
002f91bb | 373 | int open_ret = 0; |
617de8e1 | 374 | |
375 | if(channel_dir == NULL) { | |
376 | perror(subchannel_name); | |
d304b1dd | 377 | open_ret = ENOENT; |
002f91bb | 378 | goto end; |
617de8e1 | 379 | } |
380 | ||
083518b7 | 381 | printf_verbose("Creating trace subdirectory %s\n", subtrace_name); |
617de8e1 | 382 | ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO); |
383 | if(ret == -1) { | |
b1e3e7c7 | 384 | if(errno != EEXIST) { |
90ccaa9a | 385 | perror(subtrace_name); |
002f91bb | 386 | open_ret = -1; |
d304b1dd | 387 | goto end; |
90ccaa9a | 388 | } |
617de8e1 | 389 | } |
390 | ||
391 | strncpy(path_channel, subchannel_name, PATH_MAX-1); | |
392 | path_channel_len = strlen(path_channel); | |
393 | path_channel[path_channel_len] = '/'; | |
394 | path_channel_len++; | |
395 | path_channel_ptr = path_channel + path_channel_len; | |
396 | ||
397 | strncpy(path_trace, subtrace_name, PATH_MAX-1); | |
398 | path_trace_len = strlen(path_trace); | |
399 | path_trace[path_trace_len] = '/'; | |
400 | path_trace_len++; | |
401 | path_trace_ptr = path_trace + path_trace_len; | |
402 | ||
357915bb | 403 | #ifdef HAS_INOTIFY |
404 | iwatch_array->elem = realloc(iwatch_array->elem, | |
405 | ++iwatch_array->num * sizeof(struct inotify_watch)); | |
406 | ||
083518b7 | 407 | printf_verbose("Adding inotify for channel %s\n", path_channel); |
357915bb | 408 | iwatch_array->elem[iwatch_array->num-1].wd = inotify_add_watch(*inotify_fd, path_channel, IN_CREATE); |
409 | strcpy(iwatch_array->elem[iwatch_array->num-1].path_channel, path_channel); | |
410 | strcpy(iwatch_array->elem[iwatch_array->num-1].path_trace, path_trace); | |
083518b7 | 411 | printf_verbose("Added inotify for channel %s, wd %u\n", |
412 | iwatch_array->elem[iwatch_array->num-1].path_channel, | |
357915bb | 413 | iwatch_array->elem[iwatch_array->num-1].wd); |
414 | #endif | |
415 | ||
617de8e1 | 416 | while((entry = readdir(channel_dir)) != NULL) { |
417 | ||
418 | if(entry->d_name[0] == '.') continue; | |
419 | ||
420 | strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len); | |
421 | strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len); | |
422 | ||
423 | ret = stat(path_channel, &stat_buf); | |
424 | if(ret == -1) { | |
425 | perror(path_channel); | |
426 | continue; | |
427 | } | |
428 | ||
083518b7 | 429 | printf_verbose("Channel file : %s\n", path_channel); |
617de8e1 | 430 | |
431 | if(S_ISDIR(stat_buf.st_mode)) { | |
432 | ||
083518b7 | 433 | printf_verbose("Entering channel subdirectory...\n"); |
357915bb | 434 | ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs, |
435 | inotify_fd, iwatch_array); | |
617de8e1 | 436 | if(ret < 0) continue; |
90ccaa9a | 437 | } else if(S_ISREG(stat_buf.st_mode)) { |
357915bb | 438 | open_ret = open_buffer_file(entry->d_name, path_channel, path_trace, |
439 | fd_pairs); | |
440 | if(open_ret) | |
441 | goto end; | |
617de8e1 | 442 | } |
617de8e1 | 443 | } |
444 | ||
d304b1dd | 445 | end: |
617de8e1 | 446 | closedir(channel_dir); |
447 | ||
d304b1dd | 448 | return open_ret; |
617de8e1 | 449 | } |
450 | ||
1d483eea | 451 | |
452 | int read_subbuffer(struct fd_pair *pair) | |
453 | { | |
572db7d9 | 454 | unsigned int consumed_old, len; |
f01152ea | 455 | int err; |
456 | long ret; | |
4e1c69e6 | 457 | off_t offset; |
1d483eea | 458 | |
459 | ||
572db7d9 | 460 | err = ioctl(pair->channel, RELAY_GET_SB, &consumed_old); |
083518b7 | 461 | printf_verbose("cookie : %u\n", consumed_old); |
469206ed | 462 | if(err != 0) { |
5ffb77aa | 463 | ret = errno; |
30478a4d | 464 | perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)"); |
469206ed | 465 | goto get_error; |
1d483eea | 466 | } |
f01152ea | 467 | #if 0 |
469206ed | 468 | err = TEMP_FAILURE_RETRY(write(pair->trace, |
e4bed64a | 469 | pair->mmap |
470 | + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)), | |
1d483eea | 471 | pair->subbuf_size)); |
469206ed | 472 | |
473 | if(err < 0) { | |
5ffb77aa | 474 | ret = errno; |
1d483eea | 475 | perror("Error in writing to file"); |
469206ed | 476 | goto write_error; |
1d483eea | 477 | } |
f01152ea | 478 | #endif //0 |
572db7d9 MD |
479 | err = ioctl(pair->channel, RELAY_GET_SB_SIZE, &len); |
480 | if(err != 0) { | |
481 | ret = errno; | |
482 | perror("Getting sub-buffer len failed."); | |
483 | goto get_error; | |
484 | } | |
485 | ||
f01152ea | 486 | offset = 0; |
487 | while (len > 0) { | |
4e1c69e6 | 488 | printf_verbose("splice chan to pipe offset %lu\n", |
489 | (unsigned long)offset); | |
f01152ea | 490 | ret = splice(pair->channel, &offset, thread_pipe[1], NULL, |
4b6ff6ef | 491 | len, SPLICE_F_MOVE | SPLICE_F_MORE); |
083518b7 | 492 | printf_verbose("splice chan to pipe ret %ld\n", ret); |
f01152ea | 493 | if (ret < 0) { |
494 | perror("Error in relay splice"); | |
495 | goto write_error; | |
496 | } | |
497 | ret = splice(thread_pipe[0], NULL, pair->trace, NULL, | |
4b6ff6ef | 498 | ret, SPLICE_F_MOVE | SPLICE_F_MORE); |
083518b7 | 499 | printf_verbose("splice pipe to file %ld\n", ret); |
f01152ea | 500 | if (ret < 0) { |
501 | perror("Error in file splice"); | |
502 | goto write_error; | |
503 | } | |
504 | len -= ret; | |
505 | } | |
506 | ||
a7eb8aa2 | 507 | #if 0 |
508 | err = fsync(pair->trace); | |
509 | if(err < 0) { | |
510 | ret = errno; | |
511 | perror("Error in writing to file"); | |
512 | goto write_error; | |
513 | } | |
514 | #endif //0 | |
469206ed | 515 | write_error: |
f01152ea | 516 | ret = 0; |
572db7d9 | 517 | err = ioctl(pair->channel, RELAY_PUT_SB, &consumed_old); |
469206ed | 518 | if(err != 0) { |
5ffb77aa | 519 | ret = errno; |
30478a4d | 520 | if(errno == EFAULT) { |
5ffb77aa | 521 | perror("Error in unreserving sub buffer\n"); |
30478a4d | 522 | } else if(errno == EIO) { |
572db7d9 MD |
523 | /* Should never happen with newer LTTng versions */ |
524 | perror("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
4f31148b | 525 | } |
469206ed | 526 | goto get_error; |
1d483eea | 527 | } |
528 | ||
469206ed | 529 | get_error: |
530 | return ret; | |
1d483eea | 531 | } |
532 | ||
533 | ||
357915bb | 534 | int map_channels(struct channel_trace_fd *fd_pairs, |
535 | int idx_begin, int idx_end) | |
617de8e1 | 536 | { |
1d483eea | 537 | int i,j; |
e54e1d5d | 538 | int ret=0; |
1d483eea | 539 | |
469206ed | 540 | if(fd_pairs->num_pairs <= 0) { |
541 | printf("No channel to read\n"); | |
542 | goto end; | |
543 | } | |
544 | ||
1d483eea | 545 | /* Get the subbuf sizes and number */ |
546 | ||
357915bb | 547 | for(i=idx_begin;i<idx_end;i++) { |
1d483eea | 548 | struct fd_pair *pair = &fd_pairs->pair[i]; |
90ccaa9a | 549 | |
572db7d9 | 550 | ret = ioctl(pair->channel, RELAY_GET_N_SB, &pair->n_sb); |
1d483eea | 551 | if(ret != 0) { |
572db7d9 | 552 | perror("Error in getting the number of sub-buffers"); |
1d483eea | 553 | goto end; |
554 | } | |
572db7d9 MD |
555 | ret = ioctl(pair->channel, RELAY_GET_MAX_SB_SIZE, |
556 | &pair->max_sb_size); | |
1d483eea | 557 | if(ret != 0) { |
572db7d9 | 558 | perror("Error in getting the max sub-buffer size"); |
1d483eea | 559 | goto end; |
560 | } | |
5ffb77aa | 561 | ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */ |
562 | if(ret != 0) { | |
563 | perror("Error in mutex init"); | |
564 | goto end; | |
565 | } | |
1d483eea | 566 | } |
567 | ||
f01152ea | 568 | #if 0 |
1d483eea | 569 | /* Mmap each FD */ |
357915bb | 570 | for(i=idx_begin;i<idx_end;i++) { |
1d483eea | 571 | struct fd_pair *pair = &fd_pairs->pair[i]; |
572 | ||
573 | pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ, | |
574 | MAP_SHARED, pair->channel, 0); | |
575 | if(pair->mmap == MAP_FAILED) { | |
576 | perror("Mmap error"); | |
577 | goto munmap; | |
578 | } | |
579 | } | |
580 | ||
5ffb77aa | 581 | goto end; /* success */ |
1d483eea | 582 | |
e54e1d5d | 583 | /* Error handling */ |
584 | /* munmap only the successfully mmapped indexes */ | |
585 | munmap: | |
586 | /* Munmap each FD */ | |
357915bb | 587 | for(j=idx_begin;j<i;j++) { |
e54e1d5d | 588 | struct fd_pair *pair = &fd_pairs->pair[j]; |
589 | int err_ret; | |
590 | ||
591 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); | |
592 | if(err_ret != 0) { | |
593 | perror("Error in munmap"); | |
594 | } | |
595 | ret |= err_ret; | |
596 | } | |
597 | ||
f01152ea | 598 | #endif //0 |
e54e1d5d | 599 | end: |
600 | return ret; | |
e54e1d5d | 601 | } |
602 | ||
e54e1d5d | 603 | int unmap_channels(struct channel_trace_fd *fd_pairs) |
604 | { | |
605 | int j; | |
606 | int ret=0; | |
607 | ||
608 | /* Munmap each FD */ | |
609 | for(j=0;j<fd_pairs->num_pairs;j++) { | |
610 | struct fd_pair *pair = &fd_pairs->pair[j]; | |
611 | int err_ret; | |
612 | ||
f01152ea | 613 | #if 0 |
e54e1d5d | 614 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
615 | if(err_ret != 0) { | |
616 | perror("Error in munmap"); | |
617 | } | |
618 | ret |= err_ret; | |
f01152ea | 619 | #endif //0 |
5ffb77aa | 620 | err_ret = pthread_mutex_destroy(&pair->mutex); |
621 | if(err_ret != 0) { | |
622 | perror("Error in mutex destroy"); | |
623 | } | |
624 | ret |= err_ret; | |
e54e1d5d | 625 | } |
626 | ||
627 | return ret; | |
628 | } | |
629 | ||
357915bb | 630 | #ifdef HAS_INOTIFY |
631 | /* Inotify event arrived. | |
632 | * | |
633 | * Only support add file for now. | |
634 | */ | |
635 | ||
636 | int read_inotify(int inotify_fd, | |
637 | struct channel_trace_fd *fd_pairs, | |
638 | struct inotify_watch_array *iwatch_array) | |
639 | { | |
640 | char buf[sizeof(struct inotify_event) + PATH_MAX]; | |
641 | char path_channel[PATH_MAX]; | |
642 | char path_trace[PATH_MAX]; | |
643 | ssize_t len; | |
644 | struct inotify_event *ievent; | |
645 | size_t offset; | |
646 | unsigned int i; | |
647 | int ret; | |
648 | int old_num; | |
649 | ||
650 | offset = 0; | |
651 | len = read(inotify_fd, buf, sizeof(struct inotify_event) + PATH_MAX); | |
652 | if(len < 0) { | |
31482529 | 653 | |
654 | if(errno == EAGAIN) | |
655 | return 0; /* another thread got the data before us */ | |
656 | ||
357915bb | 657 | printf("Error in read from inotify FD %s.\n", strerror(len)); |
658 | return -1; | |
659 | } | |
660 | while(offset < len) { | |
661 | ievent = (struct inotify_event *)&(buf[offset]); | |
662 | for(i=0; i<iwatch_array->num; i++) { | |
663 | if(iwatch_array->elem[i].wd == ievent->wd && | |
664 | ievent->mask == IN_CREATE) { | |
083518b7 | 665 | printf_verbose( |
666 | "inotify wd %u event mask : %u for %s%s\n", | |
357915bb | 667 | ievent->wd, ievent->mask, |
083518b7 | 668 | iwatch_array->elem[i].path_channel, |
669 | ievent->name); | |
357915bb | 670 | old_num = fd_pairs->num_pairs; |
671 | strcpy(path_channel, iwatch_array->elem[i].path_channel); | |
672 | strcat(path_channel, ievent->name); | |
673 | strcpy(path_trace, iwatch_array->elem[i].path_trace); | |
674 | strcat(path_trace, ievent->name); | |
675 | if(ret = open_buffer_file(ievent->name, path_channel, | |
676 | path_trace, fd_pairs)) { | |
677 | printf("Error opening buffer file\n"); | |
678 | return -1; | |
679 | } | |
680 | if(ret = map_channels(fd_pairs, old_num, fd_pairs->num_pairs)) { | |
681 | printf("Error mapping channel\n"); | |
682 | return -1; | |
683 | } | |
684 | ||
685 | } | |
686 | } | |
687 | offset += sizeof(*ievent) + ievent->len; | |
688 | } | |
689 | } | |
690 | #endif //HAS_INOTIFY | |
e54e1d5d | 691 | |
692 | /* read_channels | |
5ffb77aa | 693 | * |
694 | * Thread worker. | |
e54e1d5d | 695 | * |
c2ffa20f | 696 | * Read the debugfs channels and write them in the paired tracefiles. |
e54e1d5d | 697 | * |
698 | * @fd_pairs : paired channels and trace files. | |
699 | * | |
357915bb | 700 | * returns 0 on success, -1 on error. |
e54e1d5d | 701 | * |
702 | * Note that the high priority polled channels are consumed first. We then poll | |
703 | * again to see if these channels are still in priority. Only when no | |
704 | * high priority channel is left, we start reading low priority channels. | |
705 | * | |
706 | * Note that a channel is considered high priority when the buffer is almost | |
707 | * full. | |
708 | */ | |
709 | ||
31482529 | 710 | int read_channels(unsigned long thread_num, struct channel_trace_fd *fd_pairs, |
357915bb | 711 | int inotify_fd, struct inotify_watch_array *iwatch_array) |
e54e1d5d | 712 | { |
357915bb | 713 | struct pollfd *pollfd = NULL; |
31482529 | 714 | int num_pollfd; |
e54e1d5d | 715 | int i,j; |
716 | int num_rdy, num_hup; | |
717 | int high_prio; | |
5ffb77aa | 718 | int ret = 0; |
357915bb | 719 | int inotify_fds; |
720 | unsigned int old_num; | |
e54e1d5d | 721 | |
357915bb | 722 | #ifdef HAS_INOTIFY |
723 | inotify_fds = 1; | |
724 | #else | |
725 | inotify_fds = 0; | |
726 | #endif | |
727 | ||
31482529 | 728 | pthread_rwlock_rdlock(&fd_pairs_lock); |
729 | ||
357915bb | 730 | /* Start polling the FD. Keep one fd for inotify */ |
731 | pollfd = malloc((inotify_fds + fd_pairs->num_pairs) * sizeof(struct pollfd)); | |
732 | ||
733 | #ifdef HAS_INOTIFY | |
734 | pollfd[0].fd = inotify_fd; | |
735 | pollfd[0].events = POLLIN|POLLPRI; | |
736 | #endif | |
90ccaa9a | 737 | |
738 | for(i=0;i<fd_pairs->num_pairs;i++) { | |
357915bb | 739 | pollfd[inotify_fds+i].fd = fd_pairs->pair[i].channel; |
740 | pollfd[inotify_fds+i].events = POLLIN|POLLPRI; | |
90ccaa9a | 741 | } |
31482529 | 742 | num_pollfd = inotify_fds + fd_pairs->num_pairs; |
743 | ||
744 | ||
745 | pthread_rwlock_unlock(&fd_pairs_lock); | |
746 | ||
90ccaa9a | 747 | while(1) { |
4f45ea55 | 748 | high_prio = 0; |
1d483eea | 749 | num_hup = 0; |
750 | #ifdef DEBUG | |
751 | printf("Press a key for next poll...\n"); | |
752 | char buf[1]; | |
753 | read(STDIN_FILENO, &buf, 1); | |
31482529 | 754 | printf("Next poll (polling %d fd) :\n", num_pollfd); |
1d483eea | 755 | #endif //DEBUG |
357915bb | 756 | |
1d483eea | 757 | /* Have we received a signal ? */ |
758 | if(quit_program) break; | |
759 | ||
31482529 | 760 | num_rdy = poll(pollfd, num_pollfd, -1); |
761 | ||
90ccaa9a | 762 | if(num_rdy == -1) { |
763 | perror("Poll error"); | |
1d483eea | 764 | goto free_fd; |
90ccaa9a | 765 | } |
766 | ||
083518b7 | 767 | printf_verbose("Data received\n"); |
357915bb | 768 | #ifdef HAS_INOTIFY |
769 | switch(pollfd[0].revents) { | |
770 | case POLLERR: | |
083518b7 | 771 | printf_verbose( |
772 | "Error returned in polling inotify fd %d.\n", | |
773 | pollfd[0].fd); | |
357915bb | 774 | break; |
775 | case POLLHUP: | |
083518b7 | 776 | printf_verbose( |
777 | "Polling inotify fd %d tells it has hung up.\n", | |
778 | pollfd[0].fd); | |
357915bb | 779 | break; |
780 | case POLLNVAL: | |
083518b7 | 781 | printf_verbose( |
782 | "Polling inotify fd %d tells fd is not open.\n", | |
783 | pollfd[0].fd); | |
357915bb | 784 | break; |
785 | case POLLPRI: | |
786 | case POLLIN: | |
083518b7 | 787 | printf_verbose( |
788 | "Polling inotify fd %d : data ready.\n", | |
789 | pollfd[0].fd); | |
31482529 | 790 | |
791 | pthread_rwlock_wrlock(&fd_pairs_lock); | |
357915bb | 792 | read_inotify(inotify_fd, fd_pairs, iwatch_array); |
31482529 | 793 | pthread_rwlock_unlock(&fd_pairs_lock); |
794 | ||
357915bb | 795 | break; |
796 | } | |
797 | #endif | |
90ccaa9a | 798 | |
31482529 | 799 | for(i=inotify_fds;i<num_pollfd;i++) { |
90ccaa9a | 800 | switch(pollfd[i].revents) { |
801 | case POLLERR: | |
083518b7 | 802 | printf_verbose( |
803 | "Error returned in polling fd %d.\n", | |
804 | pollfd[i].fd); | |
1d483eea | 805 | num_hup++; |
90ccaa9a | 806 | break; |
807 | case POLLHUP: | |
083518b7 | 808 | printf_verbose( |
809 | "Polling fd %d tells it has hung up.\n", | |
810 | pollfd[i].fd); | |
1d483eea | 811 | num_hup++; |
90ccaa9a | 812 | break; |
813 | case POLLNVAL: | |
083518b7 | 814 | printf_verbose( |
815 | "Polling fd %d tells fd is not open.\n", | |
816 | pollfd[i].fd); | |
1d483eea | 817 | num_hup++; |
90ccaa9a | 818 | break; |
819 | case POLLPRI: | |
31482529 | 820 | pthread_rwlock_rdlock(&fd_pairs_lock); |
357915bb | 821 | if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) { |
083518b7 | 822 | printf_verbose( |
823 | "Urgent read on fd %d\n", | |
824 | pollfd[i].fd); | |
5ffb77aa | 825 | /* Take care of high priority channels first. */ |
826 | high_prio = 1; | |
572db7d9 | 827 | /* it's ok to have an unavailable sub-buffer */ |
357915bb | 828 | ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]); |
30478a4d | 829 | if(ret == EAGAIN) ret = 0; |
cdad9787 | 830 | |
357915bb | 831 | ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex); |
5ffb77aa | 832 | if(ret) |
833 | printf("Error in mutex unlock : %s\n", strerror(ret)); | |
834 | } | |
31482529 | 835 | pthread_rwlock_unlock(&fd_pairs_lock); |
90ccaa9a | 836 | break; |
4f45ea55 | 837 | } |
90ccaa9a | 838 | } |
357915bb | 839 | /* If every buffer FD has hung up, we end the read loop here */ |
31482529 | 840 | if(num_hup == num_pollfd - inotify_fds) break; |
90ccaa9a | 841 | |
4f45ea55 | 842 | if(!high_prio) { |
31482529 | 843 | for(i=inotify_fds;i<num_pollfd;i++) { |
4f45ea55 | 844 | switch(pollfd[i].revents) { |
845 | case POLLIN: | |
31482529 | 846 | pthread_rwlock_rdlock(&fd_pairs_lock); |
357915bb | 847 | if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) { |
5ffb77aa | 848 | /* Take care of low priority channels. */ |
083518b7 | 849 | printf_verbose( |
850 | "Normal read on fd %d\n", | |
851 | pollfd[i].fd); | |
5ffb77aa | 852 | /* it's ok to have an unavailable subbuffer */ |
357915bb | 853 | ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]); |
30478a4d | 854 | if(ret == EAGAIN) ret = 0; |
cdad9787 | 855 | |
357915bb | 856 | ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex); |
5ffb77aa | 857 | if(ret) |
858 | printf("Error in mutex unlock : %s\n", strerror(ret)); | |
859 | } | |
31482529 | 860 | pthread_rwlock_unlock(&fd_pairs_lock); |
4f45ea55 | 861 | break; |
862 | } | |
863 | } | |
90ccaa9a | 864 | } |
865 | ||
31482529 | 866 | /* Update pollfd array if an entry was added to fd_pairs */ |
867 | pthread_rwlock_rdlock(&fd_pairs_lock); | |
868 | if((inotify_fds + fd_pairs->num_pairs) != num_pollfd) { | |
869 | pollfd = realloc(pollfd, | |
870 | (inotify_fds + fd_pairs->num_pairs) * sizeof(struct pollfd)); | |
871 | for(i=num_pollfd-inotify_fds;i<fd_pairs->num_pairs;i++) { | |
872 | pollfd[inotify_fds+i].fd = fd_pairs->pair[i].channel; | |
873 | pollfd[inotify_fds+i].events = POLLIN|POLLPRI; | |
874 | } | |
875 | num_pollfd = fd_pairs->num_pairs + inotify_fds; | |
876 | } | |
877 | pthread_rwlock_unlock(&fd_pairs_lock); | |
878 | ||
879 | /* NB: If the fd_pairs structure is updated by another thread from this | |
880 | * point forward, the current thread will wait in the poll without | |
881 | * monitoring the new channel. However, this thread will add the | |
882 | * new channel on next poll (and this should not take too much time | |
883 | * on a loaded system). | |
884 | * | |
885 | * This event is quite unlikely and can only occur if a CPU is | |
886 | * hot-plugged while multple lttd threads are running. | |
887 | */ | |
90ccaa9a | 888 | } |
889 | ||
1d483eea | 890 | free_fd: |
90ccaa9a | 891 | free(pollfd); |
892 | ||
1d483eea | 893 | end: |
357915bb | 894 | return ret; |
617de8e1 | 895 | } |
896 | ||
897 | ||
357915bb | 898 | void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs, int inotify_fd, |
899 | struct inotify_watch_array *iwatch_array) | |
617de8e1 | 900 | { |
90ccaa9a | 901 | int i; |
902 | int ret; | |
617de8e1 | 903 | |
90ccaa9a | 904 | for(i=0;i<fd_pairs->num_pairs;i++) { |
905 | ret = close(fd_pairs->pair[i].channel); | |
906 | if(ret == -1) perror("Close error on channel"); | |
907 | ret = close(fd_pairs->pair[i].trace); | |
908 | if(ret == -1) perror("Close error on trace"); | |
909 | } | |
910 | free(fd_pairs->pair); | |
357915bb | 911 | free(iwatch_array->elem); |
912 | } | |
913 | ||
914 | /* Thread worker */ | |
915 | void * thread_main(void *arg) | |
916 | { | |
31482529 | 917 | long ret; |
918 | unsigned long thread_num = (unsigned long)arg; | |
919 | ||
f01152ea | 920 | ret = pipe(thread_pipe); |
921 | if (ret < 0) { | |
922 | perror("Error creating pipe"); | |
923 | return (void*)ret; | |
924 | } | |
31482529 | 925 | ret = read_channels(thread_num, &fd_pairs, inotify_fd, &inotify_watch_array); |
f01152ea | 926 | close(thread_pipe[0]); /* close read end */ |
927 | close(thread_pipe[1]); /* close write end */ | |
31482529 | 928 | return (void*)ret; |
929 | } | |
930 | ||
931 | ||
932 | int channels_init() | |
933 | { | |
357915bb | 934 | int ret = 0; |
357915bb | 935 | |
936 | inotify_fd = inotify_init(); | |
31482529 | 937 | fcntl(inotify_fd, F_SETFL, O_NONBLOCK); |
357915bb | 938 | |
939 | if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs, | |
940 | &inotify_fd, &inotify_watch_array)) | |
941 | goto close_channel; | |
c928825d | 942 | if (fd_pairs.num_pairs == 0) { |
943 | printf("No channel available for reading, exiting\n"); | |
944 | ret = -ENOENT; | |
945 | goto close_channel; | |
946 | } | |
357915bb | 947 | if(ret = map_channels(&fd_pairs, 0, fd_pairs.num_pairs)) |
948 | goto close_channel; | |
31482529 | 949 | return 0; |
357915bb | 950 | |
951 | close_channel: | |
952 | close_channel_trace_pairs(&fd_pairs, inotify_fd, &inotify_watch_array); | |
953 | if(inotify_fd >= 0) | |
954 | close(inotify_fd); | |
31482529 | 955 | return ret; |
617de8e1 | 956 | } |
957 | ||
31482529 | 958 | |
617de8e1 | 959 | int main(int argc, char ** argv) |
960 | { | |
e54e1d5d | 961 | int ret = 0; |
1d483eea | 962 | struct sigaction act; |
5ffb77aa | 963 | pthread_t *tids; |
31482529 | 964 | unsigned long i; |
5ffb77aa | 965 | void *tret; |
617de8e1 | 966 | |
967 | ret = parse_arguments(argc, argv); | |
968 | ||
969 | if(ret != 0) show_arguments(); | |
970 | if(ret < 0) return EINVAL; | |
971 | if(ret > 0) return 0; | |
972 | ||
973 | show_info(); | |
974 | ||
1d483eea | 975 | /* Connect the signal handlers */ |
976 | act.sa_handler = handler; | |
977 | act.sa_flags = 0; | |
978 | sigemptyset(&(act.sa_mask)); | |
979 | sigaddset(&(act.sa_mask), SIGTERM); | |
980 | sigaddset(&(act.sa_mask), SIGQUIT); | |
981 | sigaddset(&(act.sa_mask), SIGINT); | |
982 | sigaction(SIGTERM, &act, NULL); | |
983 | sigaction(SIGQUIT, &act, NULL); | |
984 | sigaction(SIGINT, &act, NULL); | |
985 | ||
31482529 | 986 | if(ret = channels_init()) |
987 | return ret; | |
988 | ||
06cb3ad3 | 989 | if(daemon_mode) { |
990 | ret = daemon(0, 0); | |
991 | ||
992 | if(ret == -1) { | |
993 | perror("An error occured while daemonizing."); | |
994 | exit(-1); | |
995 | } | |
996 | } | |
997 | ||
5ffb77aa | 998 | tids = malloc(sizeof(pthread_t) * num_threads); |
999 | for(i=0; i<num_threads; i++) { | |
ae410d24 | 1000 | |
357915bb | 1001 | ret = pthread_create(&tids[i], NULL, thread_main, (void*)i); |
5ffb77aa | 1002 | if(ret) { |
1003 | perror("Error creating thread"); | |
1004 | break; | |
1005 | } | |
1006 | } | |
617de8e1 | 1007 | |
5ffb77aa | 1008 | for(i=0; i<num_threads; i++) { |
1009 | ret = pthread_join(tids[i], &tret); | |
1010 | if(ret) { | |
1011 | perror("Error joining thread"); | |
1012 | break; | |
1013 | } | |
31482529 | 1014 | if((long)tret != 0) { |
1015 | printf("Error %s occured in thread %u\n", | |
1016 | strerror((long)tret), i); | |
5ffb77aa | 1017 | } |
1018 | } | |
1019 | ||
1020 | free(tids); | |
31482529 | 1021 | ret = unmap_channels(&fd_pairs); |
1022 | close_channel_trace_pairs(&fd_pairs, inotify_fd, &inotify_watch_array); | |
1023 | if(inotify_fd >= 0) | |
1024 | close(inotify_fd); | |
5ffb77aa | 1025 | |
617de8e1 | 1026 | return ret; |
1027 | } |