blob: b51f7f51b25ee982de436b223b521a6f1d990db2 [file] [log] [blame]
Dave Barache9d91702017-11-29 16:59:01 -05001/*
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
18clib_maplog_main_t maplog_main;
19
20typedef struct
21{
22 u64 serial_number;
23 u64 junk[7];
24} test_entry_t;
25
Dave Barach6ddede72018-03-22 10:54:45 -040026typedef enum
27{
28 TEST_NORMAL,
29 TEST_CIRCULAR,
30} test_type_t;
31
Dave Barach04fee312017-12-01 16:20:32 -050032static void
33process_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 Barach55c79e92017-12-05 14:48:56 -050047 /* Padding at the end of a damaged log? */
48 if (e->serial_number == 0ULL)
49 break;
Dave Barach04fee312017-12-01 16:20:32 -050050 fformat (stdout, "%4lld ", e->serial_number);
51 if (++i == 8)
52 {
53 fformat (stdout, "\n");
54 i = 0;
55 }
56 e++;
57 }
Dave Barach6ddede72018-03-22 10:54:45 -040058 fformat (stdout, "\n--------------\n");
Dave Barach04fee312017-12-01 16:20:32 -050059}
60
Dave Barache9d91702017-11-29 16:59:01 -050061int
62test_maplog_main (unformat_input_t * input)
63{
64 clib_maplog_main_t *mm = &maplog_main;
Dave Barach04fee312017-12-01 16:20:32 -050065 clib_maplog_init_args_t _a, *a = &_a;
Dave Barache9d91702017-11-29 16:59:01 -050066 int rv;
Dave Barach6ddede72018-03-22 10:54:45 -040067 int i, limit;
Dave Barache9d91702017-11-29 16:59:01 -050068 test_entry_t *t;
Dave Barach55c79e92017-12-05 14:48:56 -050069 int noclose = 0;
Dave Barach6ddede72018-03-22 10:54:45 -040070 test_type_t which = TEST_NORMAL;
Dave Barach55c79e92017-12-05 14:48:56 -050071
Dave Barach6ddede72018-03-22 10:54:45 -040072 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 Barache9d91702017-11-29 16:59:01 -050081
Dave Barachb7b92992018-10-17 10:38:51 -040082 clib_memset (a, 0, sizeof (*a));
Dave Barach04fee312017-12-01 16:20:32 -050083 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 Barach6ddede72018-03-22 10:54:45 -040091 a->maplog_is_circular = (which == TEST_CIRCULAR) ? 1 : 0;
Dave Barach04fee312017-12-01 16:20:32 -050092
93 rv = clib_maplog_init (a);
Dave Barache9d91702017-11-29 16:59:01 -050094
95 if (rv)
96 {
97 clib_warning ("clib_maplog_init returned %d", rv);
98 exit (1);
99 }
100
Dave Barach6ddede72018-03-22 10:54:45 -0400101 limit = (which == TEST_CIRCULAR) ? (64 + 2) : 64 * 5;
102
103 for (i = 0; i < limit; i++)
Dave Barache9d91702017-11-29 16:59:01 -0500104 {
105 t = clib_maplog_get_entry (mm);
Dave Barach55c79e92017-12-05 14:48:56 -0500106 t->serial_number = i + 1;
Dave Barache9d91702017-11-29 16:59:01 -0500107 }
108
Dave Barach55c79e92017-12-05 14:48:56 -0500109 if (noclose)
Dave Barachb7b92992018-10-17 10:38:51 -0400110 clib_memset (mm, 0, sizeof (*mm));
Dave Barach55c79e92017-12-05 14:48:56 -0500111 else
112 clib_maplog_close (mm);
Dave Barache9d91702017-11-29 16:59:01 -0500113
Dave Barach04fee312017-12-01 16:20:32 -0500114 clib_maplog_process ("/tmp/maplog_test", process_maplog_records);
115
Dave Barache9d91702017-11-29 16:59:01 -0500116 return 0;
117}
118
119#ifdef CLIB_UNIX
120int
121main (int argc, char *argv[])
122{
123 unformat_input_t i;
124 int ret;
125
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200126 clib_mem_init (0, 64ULL << 20);
127
Dave Barache9d91702017-11-29 16:59:01 -0500128 unformat_init_command_line (&i, argv);
129 ret = test_maplog_main (&i);
130 unformat_free (&i);
131
132 return ret;
133}
134#endif /* CLIB_UNIX */
135
136
137/*
138 * fd.io coding-style-patch-verification: ON
139 *
140 * Local Variables:
141 * eval: (c-set-style "gnu")
142 * End:
143 */