Gen-ust-events: add touch and wait sync points before exit.
[lttng-tools.git] / tests / utils / testapp / gen-ust-events / gen-ust-events.c
CommitLineData
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
70dac0d7 18#define _LGPL_SOURCE
a4c30524 19#include <getopt.h>
209b934f 20#include <assert.h>
7e0cc23b 21#include <arpa/inet.h>
209b934f 22#include <fcntl.h>
7e0cc23b
CB
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>
0fc2834c 31#include <stdbool.h>
5fcaccbc
MD
32#include <signal.h>
33#include <poll.h>
34#include <errno.h>
ae941114 35#include "utils.h"
95983a02 36#include "signal-helper.h"
7e0cc23b
CB
37
38#define TRACEPOINT_DEFINE
39#include "tp.h"
40
a4c30524
JR
41static 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'},
503315ec 48 {"sync-before-last-event-touch", required_argument, 0, 'c'},
66131089
JR
49 {"sync-before-exit", required_argument, 0, 'd'},
50 {"sync-before-exit-touch", required_argument, 0, 'e'},
a4c30524
JR
51 {0, 0, 0, 0}
52};
53
7e0cc23b
CB
54int main(int argc, char **argv)
55{
0fc2834c 56 unsigned int i, netint;
a4c30524
JR
57 int option_index;
58 char option_char;
7e0cc23b
CB
59 long values[] = { 1, 2, 3 };
60 char text[10] = "test";
61 double dbl = 2.0;
62 float flt = 2222.0;
b2047add 63 int nr_iter = 100, ret = 0, first_event_file_created = 0;
7e0cc23b 64 useconds_t nr_usec = 0;
5fcaccbc
MD
65 char *after_first_event_file_path = NULL;
66 char *before_last_event_file_path = NULL;
503315ec
JR
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;
66131089
JR
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;
7e0cc23b 76
a4c30524
JR
77 while((option_char = getopt_long(argc, argv, "i:w:a:b:c:d:", long_options, &option_index)) != -1) {
78 switch (option_char) {
79 case 'a':
80 after_first_event_file_path = strdup(optarg);
81 break;
82 case 'b':
83 before_last_event_file_path = strdup(optarg);
84 break;
503315ec
JR
85 case 'c':
86 before_last_event_file_path_touch = strdup(optarg);
87 break;
66131089
JR
88 case 'd':
89 before_exit_file_path = strdup(optarg);
90 break;
91 case 'e':
92 before_exit_file_path_touch = strdup(optarg);
93 break;
a4c30524
JR
94 case 'i':
95 nr_iter = atoi(optarg);
96 break;
97 case 'w':
98 nr_usec = atoi(optarg);
99 break;
100 case '?':
101 /* getopt_long already printed an error message. */
102 break;
103 default:
104 ret = -1;
105 goto end;
106 }
7e0cc23b
CB
107 }
108
a4c30524
JR
109 if (optind != argc) {
110 fprintf(stderr, "Error: takes long options only.");
111 ret = -1;
112 goto end;
7e0cc23b
CB
113 }
114
5fcaccbc 115
a4c30524
JR
116 if (set_signal_handler()) {
117 ret = -1;
118 goto end;
209b934f
DG
119 }
120
0fc2834c 121 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
5fcaccbc
MD
122 if (nr_iter >= 0 && i == nr_iter - 1) {
123 /*
124 * Wait on synchronization before writing last
125 * event.
126 */
503315ec
JR
127 if (before_last_event_file_path_touch) {
128 ret = create_file(before_last_event_file_path_touch);
129 if (ret != 0) {
130 goto end;
131 }
132 }
b2047add
FD
133 if (before_last_event_file_path) {
134 ret = wait_on_file(before_last_event_file_path);
135 if (ret != 0) {
136 goto end;
137 }
138 }
5fcaccbc 139 }
7e0cc23b 140 netint = htonl(i);
5fcaccbc
MD
141 tracepoint(tp, tptest, i, netint, values, text,
142 strlen(text), dbl, flt);
209b934f
DG
143
144 /*
5fcaccbc
MD
145 * First loop we create the file if asked to indicate
146 * that at least one tracepoint has been hit.
209b934f 147 */
b2047add
FD
148 if (after_first_event_file_path && first_event_file_created == 0) {
149 ret = create_file(after_first_event_file_path);
150
151 if (ret != 0) {
152 goto end;
153 } else {
154 first_event_file_created = 1;
155 }
156 }
157
b734f5f7 158 if (nr_usec) {
ae941114
JG
159 if (usleep_safe(nr_usec)) {
160 ret = -1;
161 goto end;
162 }
b734f5f7 163 }
95983a02
JG
164 if (should_quit) {
165 break;
166 }
7e0cc23b
CB
167 }
168
66131089
JR
169 if (before_exit_file_path_touch) {
170 ret = create_file(before_exit_file_path_touch);
171 if (ret != 0) {
172 goto end;
173 }
174 }
175 if (before_exit_file_path) {
176 ret = wait_on_file(before_exit_file_path);
177 if (ret != 0) {
178 goto end;
179 }
180 }
ae941114 181end:
a4c30524
JR
182 free(after_first_event_file_path);
183 free(before_last_event_file_path);
503315ec 184 free(before_last_event_file_path_touch);
66131089
JR
185 free(before_exit_file_path);
186 free(before_exit_file_path_touch);
ae941114 187 exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE);
7e0cc23b 188}
This page took 0.078339 seconds and 5 git commands to generate.