Commit | Line | Data |
---|---|---|
7e0cc23b CB |
1 | /* |
2 | * Copyright (C) - 2012 David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU Lesser General Public License as published by the | |
6 | * Free Software Foundation; version 2.1 of the License. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
11 | * for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public License | |
14 | * along with this library; if not, write to the Free Software Foundation, | |
15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
209b934f | 18 | #include <assert.h> |
7e0cc23b | 19 | #include <arpa/inet.h> |
209b934f | 20 | #include <fcntl.h> |
7e0cc23b CB |
21 | #include <stdarg.h> |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/mman.h> | |
26 | #include <sys/stat.h> | |
27 | #include <sys/types.h> | |
28 | #include <unistd.h> | |
7ec0f31f | 29 | #include <stdbool.h> |
61feece8 MD |
30 | #include <signal.h> |
31 | #include <poll.h> | |
32 | #include <errno.h> | |
7e0cc23b CB |
33 | |
34 | #define TRACEPOINT_DEFINE | |
35 | #include "tp.h" | |
36 | ||
209b934f DG |
37 | void create_file(const char *path) |
38 | { | |
61feece8 | 39 | static bool file_created = false; |
209b934f DG |
40 | int ret; |
41 | ||
61feece8 MD |
42 | if (!path || file_created) { |
43 | return; | |
44 | } | |
209b934f DG |
45 | |
46 | ret = creat(path, S_IRWXU); | |
47 | if (ret < 0) { | |
48 | fprintf(stderr, "Failed to create file %s\n", path); | |
49 | return; | |
50 | } | |
51 | ||
52 | (void) close(ret); | |
61feece8 MD |
53 | file_created = true; |
54 | } | |
55 | ||
56 | static | |
57 | void wait_on_file(const char *path) | |
58 | { | |
59 | if (!path) { | |
60 | return; | |
61 | } | |
62 | for (;;) { | |
63 | int ret; | |
64 | struct stat buf; | |
65 | ||
66 | ret = stat(path, &buf); | |
67 | if (ret == -1 && errno == ENOENT) { | |
68 | (void) poll(NULL, 0, 10); /* 10 ms delay */ | |
69 | continue; /* retry */ | |
70 | } | |
71 | if (ret) { | |
72 | perror("stat"); | |
73 | exit(EXIT_FAILURE); | |
74 | } | |
75 | break; /* found */ | |
76 | } | |
209b934f DG |
77 | } |
78 | ||
7e0cc23b CB |
79 | int main(int argc, char **argv) |
80 | { | |
7ec0f31f | 81 | unsigned int i, netint; |
7e0cc23b CB |
82 | long values[] = { 1, 2, 3 }; |
83 | char text[10] = "test"; | |
84 | double dbl = 2.0; | |
85 | float flt = 2222.0; | |
7ec0f31f | 86 | int nr_iter = 100; |
7e0cc23b | 87 | useconds_t nr_usec = 0; |
61feece8 MD |
88 | char *after_first_event_file_path = NULL; |
89 | char *before_last_event_file_path = NULL; | |
7e0cc23b CB |
90 | |
91 | if (argc >= 2) { | |
7ec0f31f MD |
92 | /* |
93 | * If nr_iter is negative, do an infinite tracing loop. | |
94 | */ | |
7e0cc23b CB |
95 | nr_iter = atoi(argv[1]); |
96 | } | |
97 | ||
2c34b645 | 98 | if (argc >= 3) { |
7e0cc23b CB |
99 | /* By default, don't wait unless user specifies. */ |
100 | nr_usec = atoi(argv[2]); | |
101 | } | |
102 | ||
2c34b645 | 103 | if (argc >= 4) { |
61feece8 MD |
104 | after_first_event_file_path = argv[3]; |
105 | } | |
106 | ||
107 | if (argc >= 5) { | |
108 | before_last_event_file_path = argv[4]; | |
209b934f DG |
109 | } |
110 | ||
7ec0f31f | 111 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
61feece8 MD |
112 | if (nr_iter >= 0 && i == nr_iter - 1) { |
113 | /* | |
114 | * Wait on synchronization before writing last | |
115 | * event. | |
116 | */ | |
117 | wait_on_file(before_last_event_file_path); | |
118 | } | |
7e0cc23b | 119 | netint = htonl(i); |
61feece8 MD |
120 | tracepoint(tp, tptest, i, netint, values, text, |
121 | strlen(text), dbl, flt); | |
209b934f DG |
122 | |
123 | /* | |
61feece8 MD |
124 | * First loop we create the file if asked to indicate |
125 | * that at least one tracepoint has been hit. | |
209b934f | 126 | */ |
61feece8 | 127 | create_file(after_first_event_file_path); |
7e0cc23b CB |
128 | usleep(nr_usec); |
129 | } | |
130 | ||
131 | return 0; | |
132 | } |