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 | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 26 | static void |
| 27 | process_maplog_records (clib_maplog_header_t * h, |
| 28 | test_entry_t * e, u64 records_this_file) |
| 29 | { |
| 30 | static int print_header; |
| 31 | int i = 0; |
| 32 | |
| 33 | if (print_header == 0) |
| 34 | { |
| 35 | print_header = 1; |
| 36 | fformat (stdout, "%U", format_maplog_header, h, 1 /* verbose */ ); |
| 37 | } |
| 38 | |
| 39 | while (records_this_file--) |
| 40 | { |
Dave Barach | 55c79e9 | 2017-12-05 14:48:56 -0500 | [diff] [blame^] | 41 | /* Padding at the end of a damaged log? */ |
| 42 | if (e->serial_number == 0ULL) |
| 43 | break; |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 44 | fformat (stdout, "%4lld ", e->serial_number); |
| 45 | if (++i == 8) |
| 46 | { |
| 47 | fformat (stdout, "\n"); |
| 48 | i = 0; |
| 49 | } |
| 50 | e++; |
| 51 | } |
| 52 | fformat (stdout, "--------------\n"); |
| 53 | } |
| 54 | |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 55 | int |
| 56 | test_maplog_main (unformat_input_t * input) |
| 57 | { |
| 58 | clib_maplog_main_t *mm = &maplog_main; |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 59 | clib_maplog_init_args_t _a, *a = &_a; |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 60 | int rv; |
| 61 | int i; |
| 62 | test_entry_t *t; |
Dave Barach | 55c79e9 | 2017-12-05 14:48:56 -0500 | [diff] [blame^] | 63 | int noclose = 0; |
| 64 | |
| 65 | if (unformat (input, "noclose")) |
| 66 | noclose = 1; |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 67 | |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 68 | memset (a, 0, sizeof (*a)); |
| 69 | a->mm = mm; |
| 70 | a->file_basename = "/tmp/maplog_test"; |
| 71 | a->file_size_in_bytes = 4096; |
| 72 | a->record_size_in_bytes = sizeof (test_entry_t); |
| 73 | a->application_id = 1; |
| 74 | a->application_major_version = 1; |
| 75 | a->application_minor_version = 0; |
| 76 | a->application_patch_version = 0; |
| 77 | |
| 78 | rv = clib_maplog_init (a); |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 79 | |
| 80 | if (rv) |
| 81 | { |
| 82 | clib_warning ("clib_maplog_init returned %d", rv); |
| 83 | exit (1); |
| 84 | } |
| 85 | |
| 86 | for (i = 0; i < 64 * 5; i++) |
| 87 | { |
| 88 | t = clib_maplog_get_entry (mm); |
Dave Barach | 55c79e9 | 2017-12-05 14:48:56 -0500 | [diff] [blame^] | 89 | t->serial_number = i + 1; |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Dave Barach | 55c79e9 | 2017-12-05 14:48:56 -0500 | [diff] [blame^] | 92 | if (noclose) |
| 93 | memset (mm, 0, sizeof (*mm)); |
| 94 | else |
| 95 | clib_maplog_close (mm); |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 96 | |
Dave Barach | 04fee31 | 2017-12-01 16:20:32 -0500 | [diff] [blame] | 97 | clib_maplog_process ("/tmp/maplog_test", process_maplog_records); |
| 98 | |
Dave Barach | e9d9170 | 2017-11-29 16:59:01 -0500 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | #ifdef CLIB_UNIX |
| 103 | int |
| 104 | main (int argc, char *argv[]) |
| 105 | { |
| 106 | unformat_input_t i; |
| 107 | int ret; |
| 108 | |
| 109 | unformat_init_command_line (&i, argv); |
| 110 | ret = test_maplog_main (&i); |
| 111 | unformat_free (&i); |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | #endif /* CLIB_UNIX */ |
| 116 | |
| 117 | |
| 118 | /* |
| 119 | * fd.io coding-style-patch-verification: ON |
| 120 | * |
| 121 | * Local Variables: |
| 122 | * eval: (c-set-style "gnu") |
| 123 | * End: |
| 124 | */ |