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