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