| 1 | /* |
| 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include "signal-helper.hpp" |
| 10 | #include "utils.h" |
| 11 | |
| 12 | #include <arpa/inet.h> |
| 13 | #include <getopt.h> |
| 14 | #include <stdarg.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <sys/mman.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #define TRACEPOINT_DEFINE |
| 24 | #include "tp.h" |
| 25 | |
| 26 | static struct option long_options[] = { |
| 27 | /* These options set a flag. */ |
| 28 | { "iter", required_argument, nullptr, 'i' }, |
| 29 | { "wait", required_argument, nullptr, 'w' }, |
| 30 | { "create-in-main", required_argument, nullptr, 'm' }, |
| 31 | { "wait-before-first-event", required_argument, nullptr, 'b' }, |
| 32 | { nullptr, 0, nullptr, 0 } |
| 33 | }; |
| 34 | |
| 35 | int main(int argc, char **argv) |
| 36 | { |
| 37 | int i, netint, ret = 0, option_index, option; |
| 38 | long values[] = { 1, 2, 3 }; |
| 39 | char text[10] = "test"; |
| 40 | const double dbl = 2.0; |
| 41 | const float flt = 2222.0; |
| 42 | unsigned int nr_iter = 100; |
| 43 | useconds_t nr_usec = 0; |
| 44 | char *wait_before_first_event_file_path = nullptr; |
| 45 | char *create_in_main_file_path = nullptr; |
| 46 | |
| 47 | while ((option = getopt_long(argc, argv, "i:w:b:m:", long_options, &option_index)) != -1) { |
| 48 | switch (option) { |
| 49 | case 'b': |
| 50 | wait_before_first_event_file_path = strdup(optarg); |
| 51 | break; |
| 52 | case 'm': |
| 53 | create_in_main_file_path = strdup(optarg); |
| 54 | break; |
| 55 | case 'i': |
| 56 | nr_iter = atoi(optarg); |
| 57 | break; |
| 58 | case 'w': |
| 59 | nr_usec = atoi(optarg); |
| 60 | break; |
| 61 | case '?': |
| 62 | /* getopt_long already printed an error message. */ |
| 63 | default: |
| 64 | ret = -1; |
| 65 | goto end; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (set_signal_handler()) { |
| 70 | ret = -1; |
| 71 | goto end; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * The two following sync points allow for tests to do work after the |
| 76 | * app has started BUT before it generates any events. |
| 77 | */ |
| 78 | if (create_in_main_file_path) { |
| 79 | ret = create_file(create_in_main_file_path); |
| 80 | if (ret != 0) { |
| 81 | goto end; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (wait_before_first_event_file_path) { |
| 86 | ret = wait_on_file(wait_before_first_event_file_path); |
| 87 | if (ret != 0) { |
| 88 | goto end; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | for (i = 0; i < nr_iter; i++) { |
| 93 | netint = htonl(i); |
| 94 | tracepoint(tp, tptest1, i, netint, values, text, strlen(text), dbl, flt); |
| 95 | tracepoint(tp, tptest2, i, netint, values, text, strlen(text), dbl, flt); |
| 96 | tracepoint(tp, tptest3, i, netint, values, text, strlen(text), dbl, flt); |
| 97 | tracepoint(tp, tptest4, i, netint, values, text, strlen(text), dbl, flt); |
| 98 | tracepoint(tp, tptest5, i, netint, values, text, strlen(text), dbl, flt); |
| 99 | if (nr_usec) { |
| 100 | if (usleep_safe(nr_usec)) { |
| 101 | ret = -1; |
| 102 | goto end; |
| 103 | } |
| 104 | } |
| 105 | if (should_quit) { |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | end: |
| 111 | free(create_in_main_file_path); |
| 112 | free(wait_before_first_event_file_path); |
| 113 | exit(!ret ? EXIT_SUCCESS : EXIT_FAILURE); |
| 114 | } |