blob: edb61bd84d0c95a9ae1a3c15250c9f4275826c2b [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 Barach04fee312017-12-01 16:20:32 -050026static void
27process_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 Barach55c79e92017-12-05 14:48:56 -050041 /* Padding at the end of a damaged log? */
42 if (e->serial_number == 0ULL)
43 break;
Dave Barach04fee312017-12-01 16:20:32 -050044 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 Barache9d91702017-11-29 16:59:01 -050055int
56test_maplog_main (unformat_input_t * input)
57{
58 clib_maplog_main_t *mm = &maplog_main;
Dave Barach04fee312017-12-01 16:20:32 -050059 clib_maplog_init_args_t _a, *a = &_a;
Dave Barache9d91702017-11-29 16:59:01 -050060 int rv;
61 int i;
62 test_entry_t *t;
Dave Barach55c79e92017-12-05 14:48:56 -050063 int noclose = 0;
64
65 if (unformat (input, "noclose"))
66 noclose = 1;
Dave Barache9d91702017-11-29 16:59:01 -050067
Dave Barach04fee312017-12-01 16:20:32 -050068 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 Barache9d91702017-11-29 16:59:01 -050079
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 Barach55c79e92017-12-05 14:48:56 -050089 t->serial_number = i + 1;
Dave Barache9d91702017-11-29 16:59:01 -050090 }
91
Dave Barach55c79e92017-12-05 14:48:56 -050092 if (noclose)
93 memset (mm, 0, sizeof (*mm));
94 else
95 clib_maplog_close (mm);
Dave Barache9d91702017-11-29 16:59:01 -050096
Dave Barach04fee312017-12-01 16:20:32 -050097 clib_maplog_process ("/tmp/maplog_test", process_maplog_records);
98
Dave Barache9d91702017-11-29 16:59:01 -050099 return 0;
100}
101
102#ifdef CLIB_UNIX
103int
104main (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 */