dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / vppinfra / vppinfra / test_elog.c
1 /*
2  * Copyright (c) 2015 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   Copyright (c) 2005 Eliot Dresselhaus
17
18   Permission is hereby granted, free of charge, to any person obtaining
19   a copy of this software and associated documentation files (the
20   "Software"), to deal in the Software without restriction, including
21   without limitation the rights to use, copy, modify, merge, publish,
22   distribute, sublicense, and/or sell copies of the Software, and to
23   permit persons to whom the Software is furnished to do so, subject to
24   the following conditions:
25
26   The above copyright notice and this permission notice shall be
27   included in all copies or substantial portions of the Software.
28
29   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37
38 #include <vppinfra/elog.h>
39 #include <vppinfra/error.h>
40 #include <vppinfra/format.h>
41 #include <vppinfra/random.h>
42 #include <vppinfra/serialize.h>
43 #include <vppinfra/unix.h>
44
45 int
46 test_elog_main (unformat_input_t * input)
47 {
48   clib_error_t *error = 0;
49   u32 i, n_iter, seed, max_events;
50   elog_main_t _em, *em = &_em;
51   u32 verbose;
52   f64 min_sample_time;
53   char *dump_file, *load_file, *merge_file, **merge_files;
54   u8 *tag, **tags;
55
56   n_iter = 100;
57   max_events = 100000;
58   seed = 1;
59   verbose = 0;
60   dump_file = 0;
61   load_file = 0;
62   merge_files = 0;
63   tags = 0;
64   min_sample_time = 2;
65   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
66     {
67       if (unformat (input, "iter %d", &n_iter))
68         ;
69       else if (unformat (input, "seed %d", &seed))
70         ;
71       else if (unformat (input, "dump %s", &dump_file))
72         ;
73       else if (unformat (input, "load %s", &load_file))
74         ;
75       else if (unformat (input, "tag %s", &tag))
76         vec_add1 (tags, tag);
77       else if (unformat (input, "merge %s", &merge_file))
78         vec_add1 (merge_files, merge_file);
79
80       else if (unformat (input, "verbose %=", &verbose, 1))
81         ;
82       else if (unformat (input, "max-events %d", &max_events))
83         ;
84       else if (unformat (input, "sample-time %f", &min_sample_time))
85         ;
86       else
87         {
88           error = clib_error_create ("unknown input `%U'\n",
89                                      format_unformat_error, input);
90           goto done;
91         }
92     }
93
94 #ifdef CLIB_UNIX
95   if (load_file)
96     {
97       if ((error = elog_read_file (em, load_file)))
98         goto done;
99     }
100
101   else if (merge_files)
102     {
103       uword i;
104       elog_main_t *ems;
105
106       vec_clone (ems, merge_files);
107
108       elog_init (em, max_events);
109       for (i = 0; i < vec_len (ems); i++)
110         {
111           if ((error =
112                elog_read_file (i == 0 ? em : &ems[i], merge_files[i])))
113             goto done;
114           if (i > 0)
115             {
116               elog_merge (em, tags[0], &ems[i], tags[i]);
117               tags[0] = 0;
118             }
119         }
120     }
121
122   else
123 #endif /* CLIB_UNIX */
124     {
125       f64 t[2];
126
127       elog_init (em, max_events);
128       elog_enable_disable (em, 1);
129       t[0] = unix_time_now ();
130
131       for (i = 0; i < n_iter; i++)
132         {
133           u32 j, n, sum;
134
135           n = 1 + (random_u32 (&seed) % 128);
136           sum = 0;
137           for (j = 0; j < n; j++)
138             sum += random_u32 (&seed);
139
140           {
141             ELOG_TYPE_XF (e);
142             ELOG (em, e, sum);
143           }
144
145           {
146             ELOG_TYPE_XF (e);
147             ELOG (em, e, sum + 1);
148           }
149
150           {
151             struct
152             {
153               u32 string_index;
154               f32 f;
155             } *d;
156             ELOG_TYPE_DECLARE (e) =
157             {
158               .format = "fumble %s %.9f",.format_args =
159                 "t4f4",.n_enum_strings = 4,.enum_strings =
160               {
161             "string0", "string1", "string2", "string3",},};
162
163             d = ELOG_DATA (em, e);
164
165             d->string_index = sum & 3;
166             d->f = (sum & 0xff) / 128.;
167           }
168
169           {
170             ELOG_TYPE_DECLARE (e) =
171             {
172             .format = "bar %d.%d.%d.%d",.format_args = "i1i1i1i1",};
173             ELOG_TRACK (my_track);
174             u8 *d = ELOG_TRACK_DATA (em, e, my_track);
175             d[0] = i + 0;
176             d[1] = i + 1;
177             d[2] = i + 2;
178             d[3] = i + 3;
179           }
180
181           {
182             ELOG_TYPE_DECLARE (e) =
183             {
184             .format = "bar `%s'",.format_args = "s20",};
185             struct
186             {
187               char s[20];
188             } *d;
189             u8 *v;
190
191             d = ELOG_DATA (em, e);
192             v = format (0, "foo %d%c", i, 0);
193             clib_memcpy (d->s, v, clib_min (vec_len (v), sizeof (d->s)));
194           }
195
196           {
197             ELOG_TYPE_DECLARE (e) =
198             {
199             .format = "bar `%s'",.format_args = "T4",};
200             struct
201             {
202               u32 offset;
203             } *d;
204
205             d = ELOG_DATA (em, e);
206             d->offset = elog_string (em, "string table %d", i);
207           }
208         }
209
210       do
211         {
212           t[1] = unix_time_now ();
213         }
214       while (t[1] - t[0] < min_sample_time);
215     }
216
217 #ifdef CLIB_UNIX
218   if (dump_file)
219     {
220       if ((error = elog_write_file (em, dump_file)))
221         goto done;
222     }
223 #endif
224
225   if (verbose)
226     {
227       elog_event_t *e, *es;
228       es = elog_get_events (em);
229       vec_foreach (e, es)
230       {
231         clib_warning ("%18.9f: %12U %U\n", e->time,
232                       format_elog_track, em, e, format_elog_event, em, e);
233       }
234     }
235
236 done:
237   if (error)
238     clib_error_report (error);
239   return 0;
240 }
241
242 #ifdef CLIB_UNIX
243 int
244 main (int argc, char *argv[])
245 {
246   unformat_input_t i;
247   int r;
248
249   unformat_init_command_line (&i, argv);
250   r = test_elog_main (&i);
251   unformat_free (&i);
252   return r;
253 }
254 #endif
255
256 /*
257  * fd.io coding-style-patch-verification: ON
258  *
259  * Local Variables:
260  * eval: (c-set-style "gnu")
261  * End:
262  */