2 * Copyright (C) - 2012 David Goulet <dgoulet@efficios.com>
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.
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
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
21 #include <arpa/inet.h>
29 #include <sys/types.h>
36 #include "signal-helper.h"
38 #define TRACEPOINT_DEFINE
42 int create_file(const char *path
)
50 ret
= creat(path
, S_IRWXU
);
66 int wait_on_file(const char *path
)
76 ret
= stat(path
, &buf
);
77 if (ret
== -1 && errno
== ENOENT
) {
78 ret
= poll(NULL
, 0, 10); /* 10 ms delay */
79 /* Should return 0 everytime */
85 "poll return value is larger than zero\n");
101 static struct option long_options
[] =
103 /* These options set a flag. */
104 {"iter", required_argument
, 0, 'i'},
105 {"wait", required_argument
, 0, 'w'},
106 {"sync-after-first-event", required_argument
, 0, 'a'},
107 {"sync-before-last-event", required_argument
, 0, 'b'},
108 {"sync-before-last-event-touch", required_argument
, 0, 'c'},
109 {"sync-before-exit", required_argument
, 0, 'd'},
110 {"sync-before-exit-touch", required_argument
, 0, 'e'},
114 int main(int argc
, char **argv
)
116 unsigned int i
, netint
;
119 long values
[] = { 1, 2, 3 };
120 char text
[10] = "test";
123 int nr_iter
= 100, ret
= 0, first_event_file_created
= 0;
124 useconds_t nr_usec
= 0;
125 char *after_first_event_file_path
= NULL
;
126 char *before_last_event_file_path
= NULL
;
128 * Touch a file to indicate that all events except one were
131 char *before_last_event_file_path_touch
= NULL
;
132 /* Touch file when we are exiting */
133 char *before_exit_file_path_touch
= NULL
;
134 /* Wait on file before exiting */
135 char *before_exit_file_path
= NULL
;
137 while((option_char
= getopt_long(argc
, argv
, "i:w:a:b:c:d:", long_options
, &option_index
)) != -1) {
138 switch (option_char
) {
140 after_first_event_file_path
= strdup(optarg
);
143 before_last_event_file_path
= strdup(optarg
);
146 before_last_event_file_path_touch
= strdup(optarg
);
149 before_exit_file_path
= strdup(optarg
);
152 before_exit_file_path_touch
= strdup(optarg
);
155 nr_iter
= atoi(optarg
);
158 nr_usec
= atoi(optarg
);
161 /* getopt_long already printed an error message. */
169 if (optind
!= argc
) {
170 fprintf(stderr
, "Error: takes long options only.");
176 if (set_signal_handler()) {
181 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
182 if (nr_iter
>= 0 && i
== nr_iter
- 1) {
184 * Wait on synchronization before writing last
187 if (before_last_event_file_path_touch
) {
188 ret
= create_file(before_last_event_file_path_touch
);
193 if (before_last_event_file_path
) {
194 ret
= wait_on_file(before_last_event_file_path
);
201 tracepoint(tp
, tptest
, i
, netint
, values
, text
,
202 strlen(text
), dbl
, flt
);
205 * First loop we create the file if asked to indicate
206 * that at least one tracepoint has been hit.
208 if (after_first_event_file_path
&& first_event_file_created
== 0) {
209 ret
= create_file(after_first_event_file_path
);
214 first_event_file_created
= 1;
219 if (usleep_safe(nr_usec
)) {
229 if (before_exit_file_path_touch
) {
230 ret
= create_file(before_exit_file_path_touch
);
235 if (before_exit_file_path
) {
236 ret
= wait_on_file(before_exit_file_path
);
242 free(after_first_event_file_path
);
243 free(before_last_event_file_path
);
244 free(before_last_event_file_path_touch
);
245 free(before_exit_file_path
);
246 free(before_exit_file_path_touch
);
247 exit(!ret
? EXIT_SUCCESS
: EXIT_FAILURE
);