add ltt-modules into svn
[ltt-control.git] / ltt / trunk / LinuxTraceToolkitViewer-0.6.9 / lttd / lttd.c
1 /* lttd
2 *
3 * Linux Trace Toolkit Daemon
4 *
5 * This is a simple daemon that reads a few relayfs channels and save them in a
6 * trace.
7 *
8 *
9 * Copyright 2005 -
10 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
11 */
12
13 #ifdef HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <stdlib.h>
24 #include <dirent.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <sys/poll.h>
28 #include <sys/mman.h>
29 #include <signal.h>
30
31 /* Relayfs IOCTL */
32 #include <asm/ioctl.h>
33 #include <asm/types.h>
34
35 /* Get the next sub buffer that can be read. */
36 #define RELAYFS_GET_SUBBUF _IOR(0xF4, 0x00,__u32)
37 /* Release the oldest reserved (by "get") sub buffer. */
38 #define RELAYFS_PUT_SUBBUF _IO(0xF4, 0x01)
39 /* returns the number of sub buffers in the per cpu channel. */
40 #define RELAYFS_GET_N_SUBBUFS _IOR(0xF4, 0x02,__u32)
41 /* returns the size of the sub buffers. */
42 #define RELAYFS_GET_SUBBUF_SIZE _IOR(0xF4, 0x03,__u32)
43
44
45
46 enum {
47 GET_SUBBUF,
48 PUT_SUBBUF,
49 GET_N_BUBBUFS,
50 GET_SUBBUF_SIZE
51 };
52
53 struct fd_pair {
54 int channel;
55 int trace;
56 unsigned int n_subbufs;
57 unsigned int subbuf_size;
58 void *mmap;
59 };
60
61 struct channel_trace_fd {
62 struct fd_pair *pair;
63 int num_pairs;
64 };
65
66 static char *trace_name = NULL;
67 static char *channel_name = NULL;
68 static int daemon_mode = 0;
69 static int append_mode = 0;
70 volatile static int quit_program = 0; /* For signal handler */
71
72 /* Args :
73 *
74 * -t directory Directory name of the trace to write to. Will be created.
75 * -c directory Root directory of the relayfs trace channels.
76 * -d Run in background (daemon).
77 * -a Trace append mode.
78 * -s Send SIGUSR1 to parent when ready for IO.
79 */
80 void show_arguments(void)
81 {
82 printf("Please use the following arguments :\n");
83 printf("\n");
84 printf("-t directory Directory name of the trace to write to.\n"
85 " It will be created.\n");
86 printf("-c directory Root directory of the relayfs trace channels.\n");
87 printf("-d Run in background (daemon).\n");
88 printf("-a Append to an possibly existing trace.\n");
89 printf("\n");
90 }
91
92
93 /* parse_arguments
94 *
95 * Parses the command line arguments.
96 *
97 * Returns 1 if the arguments were correct, but doesn't ask for program
98 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
99 */
100 int parse_arguments(int argc, char **argv)
101 {
102 int ret = 0;
103 int argn = 1;
104
105 if(argc == 2) {
106 if(strcmp(argv[1], "-h") == 0) {
107 return 1;
108 }
109 }
110
111 while(argn < argc) {
112
113 switch(argv[argn][0]) {
114 case '-':
115 switch(argv[argn][1]) {
116 case 't':
117 if(argn+1 < argc) {
118 trace_name = argv[argn+1];
119 argn++;
120 }
121 break;
122 case 'c':
123 if(argn+1 < argc) {
124 channel_name = argv[argn+1];
125 argn++;
126 }
127 break;
128 case 'd':
129 daemon_mode = 1;
130 break;
131 case 'a':
132 append_mode = 1;
133 break;
134 default:
135 printf("Invalid argument '%s'.\n", argv[argn]);
136 printf("\n");
137 ret = -1;
138 }
139 break;
140 default:
141 printf("Invalid argument '%s'.\n", argv[argn]);
142 printf("\n");
143 ret = -1;
144 }
145 argn++;
146 }
147
148 if(trace_name == NULL) {
149 printf("Please specify a trace name.\n");
150 printf("\n");
151 ret = -1;
152 }
153
154 if(channel_name == NULL) {
155 printf("Please specify a channel name.\n");
156 printf("\n");
157 ret = -1;
158 }
159
160 return ret;
161 }
162
163 void show_info(void)
164 {
165 printf("Linux Trace Toolkit Trace Daemon\n");
166 printf("\n");
167 printf("Reading from relayfs directory : %s\n", channel_name);
168 printf("Writing to trace directory : %s\n", trace_name);
169 printf("\n");
170 }
171
172
173 /* signal handling */
174
175 static void handler(int signo)
176 {
177 printf("Signal %d received : exiting cleanly\n", signo);
178 quit_program = 1;
179 }
180
181
182
183 int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name,
184 struct channel_trace_fd *fd_pairs)
185 {
186 DIR *channel_dir = opendir(subchannel_name);
187 struct dirent *entry;
188 struct stat stat_buf;
189 int ret;
190 char path_channel[PATH_MAX];
191 int path_channel_len;
192 char *path_channel_ptr;
193 char path_trace[PATH_MAX];
194 int path_trace_len;
195 char *path_trace_ptr;
196 int open_ret = 0;
197
198 if(channel_dir == NULL) {
199 perror(subchannel_name);
200 open_ret = ENOENT;
201 goto end;
202 }
203
204 printf("Creating trace subdirectory %s\n", subtrace_name);
205 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
206 if(ret == -1) {
207 if(errno != EEXIST) {
208 perror(subtrace_name);
209 open_ret = -1;
210 goto end;
211 }
212 }
213
214 strncpy(path_channel, subchannel_name, PATH_MAX-1);
215 path_channel_len = strlen(path_channel);
216 path_channel[path_channel_len] = '/';
217 path_channel_len++;
218 path_channel_ptr = path_channel + path_channel_len;
219
220 strncpy(path_trace, subtrace_name, PATH_MAX-1);
221 path_trace_len = strlen(path_trace);
222 path_trace[path_trace_len] = '/';
223 path_trace_len++;
224 path_trace_ptr = path_trace + path_trace_len;
225
226 while((entry = readdir(channel_dir)) != NULL) {
227
228 if(entry->d_name[0] == '.') continue;
229
230 strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len);
231 strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len);
232
233 ret = stat(path_channel, &stat_buf);
234 if(ret == -1) {
235 perror(path_channel);
236 continue;
237 }
238
239 printf("Channel file : %s\n", path_channel);
240
241 if(S_ISDIR(stat_buf.st_mode)) {
242
243 printf("Entering channel subdirectory...\n");
244 ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs);
245 if(ret < 0) continue;
246 } else if(S_ISREG(stat_buf.st_mode)) {
247 printf("Opening file.\n");
248
249 fd_pairs->pair = realloc(fd_pairs->pair,
250 ++fd_pairs->num_pairs * sizeof(struct fd_pair));
251
252 /* Open the channel in read mode */
253 fd_pairs->pair[fd_pairs->num_pairs-1].channel =
254 open(path_channel, O_RDONLY | O_NONBLOCK);
255 if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) {
256 perror(path_channel);
257 fd_pairs->num_pairs--;
258 continue;
259 }
260 /* Open the trace in write mode, only append if append_mode */
261 ret = stat(path_trace, &stat_buf);
262 if(ret == 0) {
263 if(append_mode) {
264 printf("Appending to file %s as requested\n", path_trace);
265
266 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
267 open(path_trace, O_WRONLY|O_APPEND,
268 S_IRWXU|S_IRWXG|S_IRWXO);
269
270 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
271 perror(path_trace);
272 }
273 } else {
274 printf("File %s exists, cannot open. Try append mode.\n", path_trace);
275 open_ret = -1;
276 goto end;
277 }
278 } else {
279 if(errno == ENOENT) {
280 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
281 open(path_trace, O_WRONLY|O_CREAT|O_EXCL,
282 S_IRWXU|S_IRWXG|S_IRWXO);
283 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
284 perror(path_trace);
285 }
286 }
287 }
288 }
289 }
290
291 end:
292 closedir(channel_dir);
293
294 return open_ret;
295 }
296
297
298 int read_subbuffer(struct fd_pair *pair)
299 {
300 unsigned int subbuf_index;
301 int err, ret;
302
303
304 err = ioctl(pair->channel, RELAYFS_GET_SUBBUF,
305 &subbuf_index);
306 printf("index : %u\n", subbuf_index);
307 if(err != 0) {
308 perror("Error in reserving sub buffer");
309 ret = -EPERM;
310 goto get_error;
311 }
312
313 err = TEMP_FAILURE_RETRY(write(pair->trace,
314 pair->mmap + (subbuf_index * pair->subbuf_size),
315 pair->subbuf_size));
316
317 if(err < 0) {
318 perror("Error in writing to file");
319 ret = err;
320 goto write_error;
321 }
322
323
324 write_error:
325 err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF);
326 if(err != 0) {
327 perror("Error in unreserving sub buffer");
328 ret = -EPERM;
329 goto get_error;
330 }
331
332 get_error:
333 return ret;
334 }
335
336
337 /* read_channels
338 *
339 * Read the realyfs channels and write them in the paired tracefiles.
340 *
341 * @fd_pairs : paired channels and trace files.
342 *
343 * returns 0 on success, -1 on error.
344 *
345 * Note that the high priority polled channels are consumed first. We then poll
346 * again to see if these channels are still in priority. Only when no
347 * high priority channel is left, we start reading low priority channels.
348 *
349 * Note that a channel is considered high priority when the buffer is almost
350 * full.
351 */
352
353 int read_channels(struct channel_trace_fd *fd_pairs)
354 {
355 struct pollfd *pollfd;
356 int i,j;
357 int num_rdy, num_hup;
358 int high_prio;
359 int ret;
360
361 if(fd_pairs->num_pairs <= 0) {
362 printf("No channel to read\n");
363 goto end;
364 }
365
366 /* Get the subbuf sizes and number */
367
368 for(i=0;i<fd_pairs->num_pairs;i++) {
369 struct fd_pair *pair = &fd_pairs->pair[i];
370
371 ret = ioctl(pair->channel, RELAYFS_GET_N_SUBBUFS,
372 &pair->n_subbufs);
373 if(ret != 0) {
374 perror("Error in getting the number of subbuffers");
375 goto end;
376 }
377 ret = ioctl(pair->channel, RELAYFS_GET_SUBBUF_SIZE,
378 &pair->subbuf_size);
379 if(ret != 0) {
380 perror("Error in getting the size of the subbuffers");
381 goto end;
382 }
383 }
384
385 /* Mmap each FD */
386 for(i=0;i<fd_pairs->num_pairs;i++) {
387 struct fd_pair *pair = &fd_pairs->pair[i];
388
389 pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ,
390 MAP_SHARED, pair->channel, 0);
391 if(pair->mmap == MAP_FAILED) {
392 perror("Mmap error");
393 goto munmap;
394 }
395 }
396
397
398 /* Start polling the FD */
399
400 pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd));
401
402 /* Note : index in pollfd is the same index as fd_pair->pair */
403 for(i=0;i<fd_pairs->num_pairs;i++) {
404 pollfd[i].fd = fd_pairs->pair[i].channel;
405 pollfd[i].events = POLLIN|POLLPRI;
406 }
407
408 while(1) {
409 high_prio = 0;
410 num_hup = 0;
411 #ifdef DEBUG
412 printf("Press a key for next poll...\n");
413 char buf[1];
414 read(STDIN_FILENO, &buf, 1);
415 printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs);
416 #endif //DEBUG
417
418 /* Have we received a signal ? */
419 if(quit_program) break;
420
421 num_rdy = poll(pollfd, fd_pairs->num_pairs, -1);
422 if(num_rdy == -1) {
423 perror("Poll error");
424 goto free_fd;
425 }
426
427 printf("Data received\n");
428
429 for(i=0;i<fd_pairs->num_pairs;i++) {
430 switch(pollfd[i].revents) {
431 case POLLERR:
432 printf("Error returned in polling fd %d.\n", pollfd[i].fd);
433 num_hup++;
434 break;
435 case POLLHUP:
436 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
437 num_hup++;
438 break;
439 case POLLNVAL:
440 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
441 num_hup++;
442 break;
443 case POLLPRI:
444 printf("Urgent read on fd %d\n", pollfd[i].fd);
445 /* Take care of high priority channels first. */
446 high_prio = 1;
447 ret |= read_subbuffer(&fd_pairs->pair[i]);
448 break;
449 }
450 }
451 /* If every FD has hung up, we end the read loop here */
452 if(num_hup == fd_pairs->num_pairs) break;
453
454 if(!high_prio) {
455 for(i=0;i<fd_pairs->num_pairs;i++) {
456 switch(pollfd[i].revents) {
457 case POLLIN:
458 /* Take care of low priority channels. */
459 printf("Normal read on fd %d\n", pollfd[i].fd);
460 ret |= read_subbuffer(&fd_pairs->pair[i]);
461 break;
462 }
463 }
464 }
465
466 }
467
468 free_fd:
469 free(pollfd);
470
471 /* munmap only the successfully mmapped indexes */
472 i = fd_pairs->num_pairs;
473 munmap:
474 /* Munmap each FD */
475 for(j=0;j<i;j++) {
476 struct fd_pair *pair = &fd_pairs->pair[j];
477 int err_ret;
478
479 err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
480 if(err_ret != 0) {
481 perror("Error in munmap");
482 }
483 ret |= err_ret;
484 }
485
486 end:
487 return ret;
488 }
489
490
491 void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
492 {
493 int i;
494 int ret;
495
496 for(i=0;i<fd_pairs->num_pairs;i++) {
497 ret = close(fd_pairs->pair[i].channel);
498 if(ret == -1) perror("Close error on channel");
499 ret = close(fd_pairs->pair[i].trace);
500 if(ret == -1) perror("Close error on trace");
501 }
502 free(fd_pairs->pair);
503 }
504
505 int main(int argc, char ** argv)
506 {
507 int ret;
508 struct channel_trace_fd fd_pairs = { NULL, 0 };
509 struct sigaction act;
510
511 ret = parse_arguments(argc, argv);
512
513 if(ret != 0) show_arguments();
514 if(ret < 0) return EINVAL;
515 if(ret > 0) return 0;
516
517 show_info();
518
519 if(daemon_mode) {
520 ret = daemon(0, 0);
521
522 if(ret == -1) {
523 perror("An error occured while daemonizing.");
524 exit(-1);
525 }
526 }
527
528 /* Connect the signal handlers */
529 act.sa_handler = handler;
530 act.sa_flags = 0;
531 sigemptyset(&(act.sa_mask));
532 sigaddset(&(act.sa_mask), SIGTERM);
533 sigaddset(&(act.sa_mask), SIGQUIT);
534 sigaddset(&(act.sa_mask), SIGINT);
535 sigaction(SIGTERM, &act, NULL);
536 sigaction(SIGQUIT, &act, NULL);
537 sigaction(SIGINT, &act, NULL);
538
539 //return 0;
540 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs))
541 goto close_channel;
542
543 ret = read_channels(&fd_pairs);
544
545 close_channel:
546 close_channel_trace_pairs(&fd_pairs);
547
548 return ret;
549 }
This page took 0.040891 seconds and 4 git commands to generate.