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
18 #include <arpa/inet.h>
33 #include <sys/types.h>
38 #define NSEC_PER_USEC 1000
39 #define NSEC_PER_SEC 1000000000
42 int64_t elapsed_time_ns(struct timespec
*t1
, struct timespec
*t2
)
44 struct timespec delta
;
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
;
54 int usleep_safe(useconds_t usec
)
57 struct timespec t1
, t2
;
58 int64_t time_remaining_ns
= (int64_t) usec
* (int64_t) NSEC_PER_USEC
;
60 ret
= clock_gettime(CLOCK_MONOTONIC
, &t1
);
63 perror("clock_gettime");
67 while (time_remaining_ns
> 0) {
68 ret
= usleep(time_remaining_ns
/ (int64_t) NSEC_PER_USEC
);
69 if (ret
&& errno
!= EINTR
) {
74 ret
= clock_gettime(CLOCK_MONOTONIC
, &t2
);
76 perror("clock_gettime");
80 time_remaining_ns
-= elapsed_time_ns(&t1
, &t2
);
86 void create_file(const char *path
)
88 static bool file_created
= false;
91 if (!path
|| file_created
) {
95 ret
= creat(path
, S_IRWXU
);
97 fprintf(stderr
, "Failed to create file %s\n", path
);
106 void wait_on_file(const char *path
)
115 ret
= stat(path
, &buf
);
116 if (ret
== -1 && errno
== ENOENT
) {
117 (void) poll(NULL
, 0, 10); /* 10 ms delay */
118 continue; /* retry */
128 int main(int argc
, char **argv
)
130 unsigned int i
, netint
;
131 long values
[] = { 1, 2, 3 };
132 char text
[10] = "test";
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
;
142 * If nr_iter is negative, do an infinite tracing loop.
144 nr_iter
= atoi(argv
[1]);
148 /* By default, don't wait unless user specifies. */
149 nr_usec
= atoi(argv
[2]);
153 after_first_event_file_path
= argv
[3];
157 before_last_event_file_path
= argv
[4];
160 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
161 if (nr_iter
>= 0 && i
== nr_iter
- 1) {
163 * Wait on synchronization before writing last
166 wait_on_file(before_last_event_file_path
);
169 tracepoint(tp
, tptest
, i
, netint
, values
, text
,
170 strlen(text
), dbl
, flt
);
173 * First loop we create the file if asked to indicate
174 * that at least one tracepoint has been hit.
176 create_file(after_first_event_file_path
);
178 if (usleep_safe(nr_usec
)) {
186 exit(!ret
? EXIT_SUCCESS
: EXIT_FAILURE
);