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