Commit | Line | Data |
---|---|---|
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 | ||
5fcaccbc | 18 | #define _GNU_SOURCE |
70dac0d7 | 19 | #define _LGPL_SOURCE |
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> | |
7e0cc23b CB |
35 | |
36 | #define TRACEPOINT_DEFINE | |
37 | #include "tp.h" | |
38 | ||
209b934f DG |
39 | void create_file(const char *path) |
40 | { | |
5fcaccbc | 41 | static bool file_created = false; |
209b934f DG |
42 | int ret; |
43 | ||
5fcaccbc MD |
44 | if (!path || file_created) { |
45 | return; | |
46 | } | |
209b934f DG |
47 | |
48 | ret = creat(path, S_IRWXU); | |
49 | if (ret < 0) { | |
50 | fprintf(stderr, "Failed to create file %s\n", path); | |
51 | return; | |
52 | } | |
53 | ||
54 | (void) close(ret); | |
5fcaccbc MD |
55 | file_created = true; |
56 | } | |
57 | ||
58 | static | |
59 | void wait_on_file(const char *path) | |
60 | { | |
61 | if (!path) { | |
62 | return; | |
63 | } | |
64 | for (;;) { | |
65 | int ret; | |
66 | struct stat buf; | |
67 | ||
68 | ret = stat(path, &buf); | |
69 | if (ret == -1 && errno == ENOENT) { | |
70 | (void) poll(NULL, 0, 10); /* 10 ms delay */ | |
71 | continue; /* retry */ | |
72 | } | |
73 | if (ret) { | |
74 | perror("stat"); | |
75 | exit(EXIT_FAILURE); | |
76 | } | |
77 | break; /* found */ | |
78 | } | |
209b934f DG |
79 | } |
80 | ||
7e0cc23b CB |
81 | int main(int argc, char **argv) |
82 | { | |
0fc2834c | 83 | unsigned int i, netint; |
7e0cc23b CB |
84 | long values[] = { 1, 2, 3 }; |
85 | char text[10] = "test"; | |
86 | double dbl = 2.0; | |
87 | float flt = 2222.0; | |
0fc2834c | 88 | int nr_iter = 100; |
7e0cc23b | 89 | useconds_t nr_usec = 0; |
5fcaccbc MD |
90 | char *after_first_event_file_path = NULL; |
91 | char *before_last_event_file_path = NULL; | |
7e0cc23b CB |
92 | |
93 | if (argc >= 2) { | |
0fc2834c MD |
94 | /* |
95 | * If nr_iter is negative, do an infinite tracing loop. | |
96 | */ | |
7e0cc23b CB |
97 | nr_iter = atoi(argv[1]); |
98 | } | |
99 | ||
2c34b645 | 100 | if (argc >= 3) { |
7e0cc23b CB |
101 | /* By default, don't wait unless user specifies. */ |
102 | nr_usec = atoi(argv[2]); | |
103 | } | |
104 | ||
2c34b645 | 105 | if (argc >= 4) { |
5fcaccbc MD |
106 | after_first_event_file_path = argv[3]; |
107 | } | |
108 | ||
109 | if (argc >= 5) { | |
110 | before_last_event_file_path = argv[4]; | |
209b934f DG |
111 | } |
112 | ||
0fc2834c | 113 | for (i = 0; nr_iter < 0 || i < nr_iter; i++) { |
5fcaccbc MD |
114 | if (nr_iter >= 0 && i == nr_iter - 1) { |
115 | /* | |
116 | * Wait on synchronization before writing last | |
117 | * event. | |
118 | */ | |
119 | wait_on_file(before_last_event_file_path); | |
120 | } | |
7e0cc23b | 121 | netint = htonl(i); |
5fcaccbc MD |
122 | tracepoint(tp, tptest, i, netint, values, text, |
123 | strlen(text), dbl, flt); | |
209b934f DG |
124 | |
125 | /* | |
5fcaccbc MD |
126 | * First loop we create the file if asked to indicate |
127 | * that at least one tracepoint has been hit. | |
209b934f | 128 | */ |
5fcaccbc | 129 | create_file(after_first_event_file_path); |
7e0cc23b CB |
130 | usleep(nr_usec); |
131 | } | |
132 | ||
133 | return 0; | |
134 | } |