Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <vppinfra/maplog.h> |
| 17 | |
| 18 | clib_maplog_main_t maplog_main; |
| 19 | |
| 20 | typedef struct |
| 21 | { |
| 22 | u64 serial_number; |
| 23 | u64 junk[7]; |
| 24 | } test_entry_t; |
| 25 | |
Dave Barach | 6ddede7 | 2018-03-22 10:54:45 -0400 | [diff] [blame] | 26 | typedef enum |
| 27 | { |
| 28 | TEST_NORMAL, |
| 29 | TEST_CIRCULAR, |
| 30 | } test_type_t; |
| 31 | |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 32 | static void |
| 33 | process_maplog_records (clib_maplog_header_t * h, |
| 34 | test_entry_t * e, u64 records_this_file) |
| 35 | { |
| 36 | static int print_header; |
| 37 | int i = 0; |
| 38 | |
| 39 | if (print_header == 0) |
| 40 | { |
| 41 | print_header = 1; |
| 42 | fformat (stdout, "%U", format_maplog_header, h, 1 /* verbose */ ); |
| 43 | } |
| 44 | |
| 45 | while (records_this_file--) |
| 46 | { |
Dave Barach | 55c79e9 | 2017-12-05 14:48:56 -0500 | [diff] [blame] | 47 | /* Padding at the end of a damaged log? */ |
| 48 | if (e->serial_number == 0ULL) |
| 49 | break; |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 50 | fformat (stdout, "%4lld ", e->serial_number); |
| 51 | if (++i == 8) |
| 52 | { |
| 53 | fformat (stdout, "\n"); |
| 54 | i = 0; |
| 55 | } |
| 56 | e++; |
| 57 | } |
Dave Barach | 6ddede7 | 2018-03-22 10:54:45 -0400 | [diff] [blame] | 58 | fformat (stdout, "\n--------------\n"); |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 61 | int |
| 62 | test_maplog_main (unformat_input_t * input) |
| 63 | { |
| 64 | clib_maplog_main_t *mm = &maplog_main; |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 65 | clib_maplog_init_args_t _a, *a = &_a; |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 66 | int rv; |
Dave Barach | 6ddede7 | 2018-03-22 10:54:45 -0400 | [diff] [blame] | 67 | int i, limit; |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 68 | test_entry_t *t; |
Dave Barach | 55c79e9 | 2017-12-05 14:48:56 -0500 | [diff] [blame] | 69 | int noclose = 0; |
Dave Barach | 6ddede7 | 2018-03-22 10:54:45 -0400 | [diff] [blame] | 70 | test_type_t which = TEST_NORMAL; |
Dave Barach | 55c79e9 | 2017-12-05 14:48:56 -0500 | [diff] [blame] | 71 | |
Dave Barach | 6ddede7 | 2018-03-22 10:54:45 -0400 | [diff] [blame] | 72 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 73 | { |
| 74 | if (unformat (input, "noclose")) |
| 75 | noclose = 1; |
| 76 | else if (unformat (input, "circular")) |
| 77 | which = TEST_CIRCULAR; |
| 78 | else |
| 79 | clib_warning ("unknown input '%U'", format_unformat_error, input); |
| 80 | } |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 81 | |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 82 | memset (a, 0, sizeof (*a)); |
| 83 | a->mm = mm; |
| 84 | a->file_basename = "/tmp/maplog_test"; |
| 85 | a->file_size_in_bytes = 4096; |
| 86 | a->record_size_in_bytes = sizeof (test_entry_t); |
| 87 | a->application_id = 1; |
| 88 | a->application_major_version = 1; |
| 89 | a->application_minor_version = 0; |
| 90 | a->application_patch_version = 0; |
Dave Barach | 6ddede7 | 2018-03-22 10:54:45 -0400 | [diff] [blame] | 91 | a->maplog_is_circular = (which == TEST_CIRCULAR) ? 1 : 0; |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 92 | |
| 93 | rv = clib_maplog_init (a); |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 94 | |
| 95 | if (rv) |
| 96 | { |
| 97 | clib_warning ("clib_maplog_init returned %d", rv); |
| 98 | exit (1); |
| 99 | } |
| 100 | |
Dave Barach | 6ddede7 | 2018-03-22 10:54:45 -0400 | [diff] [blame] | 101 | limit = (which == TEST_CIRCULAR) ? (64 + 2) : 64 * 5; |
| 102 | |
| 103 | for (i = 0; i < limit; i++) |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 104 | { |
| 105 | t = clib_maplog_get_entry (mm); |
Dave Barach | 55c79e9 | 2017-12-05 14:48:56 -0500 | [diff] [blame] | 106 | t->serial_number = i + 1; |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Dave Barach | 55c79e9 | 2017-12-05 14:48:56 -0500 | [diff] [blame] | 109 | if (noclose) |
| 110 | memset (mm, 0, sizeof (*mm)); |
| 111 | else |
| 112 | clib_maplog_close (mm); |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 113 | |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 114 | clib_maplog_process ("/tmp/maplog_test", process_maplog_records); |
| 115 | |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | #ifdef CLIB_UNIX |
| 120 | int |
| 121 | main (int argc, char *argv[]) |
| 122 | { |
| 123 | unformat_input_t i; |
| 124 | int ret; |
| 125 | |
| 126 | unformat_init_command_line (&i, argv); |
| 127 | ret = test_maplog_main (&i); |
| 128 | unformat_free (&i); |
| 129 | |
| 130 | return ret; |
| 131 | } |
| 132 | #endif /* CLIB_UNIX */ |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | * fd.io coding-style-patch-verification: ON |
| 137 | * |
| 138 | * Local Variables: |
| 139 | * eval: (c-set-style "gnu") |
| 140 | * End: |
| 141 | */ |