Import gen-ust-app application
[deliverable/lttng-ivc.git] / lttng_ivc / apps / 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 #include <arpa/inet.h>
19 #include <assert.h>
20 #include <time.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include "tp.h"
37
38 #define NSEC_PER_USEC 1000
39 #define NSEC_PER_SEC 1000000000
40
41 static inline
42 int64_t elapsed_time_ns(struct timespec *t1, struct timespec *t2)
43 {
44 struct timespec delta;
45
46 assert(t1 && t2);
47 delta.tv_sec = t2->tv_sec - t1->tv_sec;
48 delta.tv_nsec = t2->tv_nsec - t1->tv_nsec;
49 return ((int64_t) NSEC_PER_SEC * (int64_t) delta.tv_sec) +
50 (int64_t) delta.tv_nsec;
51 }
52
53 static inline
54 int usleep_safe(useconds_t usec)
55 {
56 int ret = 0;
57 struct timespec t1, t2;
58 int64_t time_remaining_ns = (int64_t) usec * (int64_t) NSEC_PER_USEC;
59
60 ret = clock_gettime(CLOCK_MONOTONIC, &t1);
61 if (ret) {
62 ret = -1;
63 perror("clock_gettime");
64 goto end;
65 }
66
67 while (time_remaining_ns > 0) {
68 ret = usleep(time_remaining_ns / (int64_t) NSEC_PER_USEC);
69 if (ret && errno != EINTR) {
70 perror("usleep");
71 goto end;
72 }
73
74 ret = clock_gettime(CLOCK_MONOTONIC, &t2);
75 if (ret) {
76 perror("clock_gettime");
77 goto end;
78 }
79
80 time_remaining_ns -= elapsed_time_ns(&t1, &t2);
81 }
82 end:
83 return ret;
84 }
85
86 void create_file(const char *path)
87 {
88 static bool file_created = false;
89 int ret;
90
91 if (!path || file_created) {
92 return;
93 }
94
95 ret = creat(path, S_IRWXU);
96 if (ret < 0) {
97 fprintf(stderr, "Failed to create file %s\n", path);
98 return;
99 }
100
101 (void) close(ret);
102 file_created = true;
103 }
104
105 static
106 void wait_on_file(const char *path)
107 {
108 if (!path) {
109 return;
110 }
111 for (;;) {
112 int ret;
113 struct stat buf;
114
115 ret = stat(path, &buf);
116 if (ret == -1 && errno == ENOENT) {
117 (void) poll(NULL, 0, 10); /* 10 ms delay */
118 continue; /* retry */
119 }
120 if (ret) {
121 perror("stat");
122 exit(EXIT_FAILURE);
123 }
124 break; /* found */
125 }
126 }
127
128 int main(int argc, char **argv)
129 {
130 unsigned int i, netint;
131 long values[] = { 1, 2, 3 };
132 char text[10] = "test";
133 double dbl = 2.0;
134 float flt = 2222.0;
135 int nr_iter = 100, ret = 0;
136 useconds_t nr_usec = 0;
137 char *after_first_event_file_path = NULL;
138 char *before_last_event_file_path = NULL;
139
140 if (argc >= 2) {
141 /*
142 * If nr_iter is negative, do an infinite tracing loop.
143 */
144 nr_iter = atoi(argv[1]);
145 }
146
147 if (argc >= 3) {
148 /* By default, don't wait unless user specifies. */
149 nr_usec = atoi(argv[2]);
150 }
151
152 if (argc >= 4) {
153 after_first_event_file_path = argv[3];
154 }
155
156 if (argc >= 5) {
157 before_last_event_file_path = argv[4];
158 }
159
160 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
161 if (nr_iter >= 0 && i == nr_iter - 1) {
162 /*
163 * Wait on synchronization before writing last
164 * event.
165 */
166 wait_on_file(before_last_event_file_path);
167 }
168 netint = htonl(i);
169 tracepoint(tp, tptest, i, netint, values, text,
170 strlen(text), dbl, flt);
171
172 /*
173 * First loop we create the file if asked to indicate
174 * that at least one tracepoint has been hit.
175 */
176 create_file(after_first_event_file_path);
177 if (nr_usec) {
178 if (usleep_safe(nr_usec)) {
179 ret = -1;
180 goto end;
181 }
182 }
183 }
184
185 end:
186 exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE);
187 }
This page took 0.033439 seconds and 5 git commands to generate.