353523b7c2c3ccd18d8f4fcf0b8f908dc35a0065
[lttng-tools.git] / tests / utils / testapp / gen-ust-events / gen-ust-events.c
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
18 #define _LGPL_SOURCE
19 #include <getopt.h>
20 #include <assert.h>
21 #include <arpa/inet.h>
22 #include <fcntl.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <stdbool.h>
32 #include <signal.h>
33 #include <poll.h>
34 #include <errno.h>
35 #include "utils.h"
36 #include "signal-helper.h"
37
38 #define TRACEPOINT_DEFINE
39 #include "tp.h"
40
41 static struct option long_options[] =
42 {
43 /* These options set a flag. */
44 {"iter", required_argument, 0, 'i'},
45 {"wait", required_argument, 0, 'w'},
46 {"sync-after-first-event", required_argument, 0, 'a'},
47 {"sync-before-last-event", required_argument, 0, 'b'},
48 {"sync-before-last-event-touch", required_argument, 0, 'c'},
49 {"sync-before-exit", required_argument, 0, 'd'},
50 {"sync-before-exit-touch", required_argument, 0, 'e'},
51 {0, 0, 0, 0}
52 };
53
54 int main(int argc, char **argv)
55 {
56 unsigned int i, netint;
57 int option_index;
58 int option;
59 long values[] = { 1, 2, 3 };
60 char text[10] = "test";
61 double dbl = 2.0;
62 float flt = 2222.0;
63 int nr_iter = 100, ret = 0, first_event_file_created = 0;
64 useconds_t nr_usec = 0;
65 char *after_first_event_file_path = NULL;
66 char *before_last_event_file_path = NULL;
67 /*
68 * Touch a file to indicate that all events except one were
69 * generated.
70 */
71 char *before_last_event_file_path_touch = NULL;
72 /* Touch file when we are exiting */
73 char *before_exit_file_path_touch = NULL;
74 /* Wait on file before exiting */
75 char *before_exit_file_path = NULL;
76
77 while ((option = getopt_long(argc, argv, "i:w:a:b:c:d:",
78 long_options, &option_index)) != -1) {
79 switch (option) {
80 case 'a':
81 after_first_event_file_path = strdup(optarg);
82 break;
83 case 'b':
84 before_last_event_file_path = strdup(optarg);
85 break;
86 case 'c':
87 before_last_event_file_path_touch = strdup(optarg);
88 break;
89 case 'd':
90 before_exit_file_path = strdup(optarg);
91 break;
92 case 'e':
93 before_exit_file_path_touch = strdup(optarg);
94 break;
95 case 'i':
96 nr_iter = atoi(optarg);
97 break;
98 case 'w':
99 nr_usec = atoi(optarg);
100 break;
101 case '?':
102 /* getopt_long already printed an error message. */
103 break;
104 default:
105 ret = -1;
106 goto end;
107 }
108 }
109
110 if (optind != argc) {
111 fprintf(stderr, "Error: takes long options only.\n");
112 ret = -1;
113 goto end;
114 }
115
116
117 if (set_signal_handler()) {
118 ret = -1;
119 goto end;
120 }
121
122 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
123 if (nr_iter >= 0 && i == nr_iter - 1) {
124 if (before_last_event_file_path_touch) {
125 ret = create_file(before_last_event_file_path_touch);
126 if (ret != 0) {
127 goto end;
128 }
129 }
130
131 /*
132 * Wait on synchronization before writing last
133 * event.
134 */
135 if (before_last_event_file_path) {
136 ret = wait_on_file(before_last_event_file_path);
137 if (ret != 0) {
138 goto end;
139 }
140 }
141 }
142 netint = htonl(i);
143 tracepoint(tp, tptest, i, netint, values, text,
144 strlen(text), dbl, flt);
145
146 /*
147 * First loop we create the file if asked to indicate
148 * that at least one tracepoint has been hit.
149 */
150 if (after_first_event_file_path && first_event_file_created == 0) {
151 ret = create_file(after_first_event_file_path);
152
153 if (ret != 0) {
154 goto end;
155 } else {
156 first_event_file_created = 1;
157 }
158 }
159
160 if (nr_usec) {
161 if (usleep_safe(nr_usec)) {
162 ret = -1;
163 goto end;
164 }
165 }
166 if (should_quit) {
167 break;
168 }
169 }
170
171 if (before_exit_file_path_touch) {
172 ret = create_file(before_exit_file_path_touch);
173 if (ret != 0) {
174 goto end;
175 }
176 }
177 if (before_exit_file_path) {
178 ret = wait_on_file(before_exit_file_path);
179 if (ret != 0) {
180 goto end;
181 }
182 }
183 end:
184 free(after_first_event_file_path);
185 free(before_last_event_file_path);
186 free(before_last_event_file_path_touch);
187 free(before_exit_file_path);
188 free(before_exit_file_path_touch);
189 exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE);
190 }
This page took 0.049507 seconds and 4 git commands to generate.