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