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>
36 #define TRACEPOINT_DEFINE
37 #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
40 #define NSEC_PER_USEC 1000
41 #define NSEC_PER_SEC 1000000000
44 int64_t elapsed_time_ns(struct timespec
*t1
, struct timespec
*t2
)
46 struct timespec delta
;
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
;
56 int usleep_safe(useconds_t usec
)
59 struct timespec t1
, t2
;
60 int64_t time_remaining_ns
= (int64_t) usec
* (int64_t) NSEC_PER_USEC
;
62 ret
= clock_gettime(CLOCK_MONOTONIC
, &t1
);
65 perror("clock_gettime");
69 while (time_remaining_ns
> 0) {
70 ret
= usleep(time_remaining_ns
/ (int64_t) NSEC_PER_USEC
);
71 if (ret
&& errno
!= EINTR
) {
76 ret
= clock_gettime(CLOCK_MONOTONIC
, &t2
);
78 perror("clock_gettime");
82 time_remaining_ns
-= elapsed_time_ns(&t1
, &t2
);
88 void create_file(const char *path
)
90 static bool file_created
= false;
93 if (!path
|| file_created
) {
97 ret
= creat(path
, S_IRWXU
);
99 fprintf(stderr
, "Failed to create file %s\n", path
);
108 void wait_on_file(const char *path
)
117 ret
= stat(path
, &buf
);
118 if (ret
== -1 && errno
== ENOENT
) {
119 (void) poll(NULL
, 0, 10); /* 10 ms delay */
120 continue; /* retry */
130 int main(int argc
, char **argv
)
132 unsigned int i
, netint
;
133 long values
[] = { 1, 2, 3 };
134 char text
[10] = "test";
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
;
144 * If nr_iter is negative, do an infinite tracing loop.
146 nr_iter
= atoi(argv
[1]);
150 /* By default, don't wait unless user specifies. */
151 nr_usec
= atoi(argv
[2]);
155 after_first_event_file_path
= argv
[3];
159 before_last_event_file_path
= argv
[4];
162 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
163 if (nr_iter
>= 0 && i
== nr_iter
- 1) {
165 * Wait on synchronization before writing last
168 wait_on_file(before_last_event_file_path
);
171 tracepoint(tp
, tptest
, i
, netint
, values
, text
,
172 strlen(text
), dbl
, flt
);
173 tracepoint(tp
, tpenum
, 1);
176 * First loop we create the file if asked to indicate
177 * that at least one tracepoint has been hit.
179 create_file(after_first_event_file_path
);
181 if (usleep_safe(nr_usec
)) {
189 exit(!ret
? EXIT_SUCCESS
: EXIT_FAILURE
);