Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 45 | int |
| 46 | test_elog_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 48 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | u32 i, n_iter, seed, max_events; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 50 | elog_main_t _em, *em = &_em; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | u32 verbose; |
| 52 | f64 min_sample_time; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 53 | char *dump_file, *load_file, *merge_file, **merge_files; |
| 54 | u8 *tag, **tags; |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 55 | f64 align_tweak; |
| 56 | f64 *align_tweaks; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
| 58 | n_iter = 100; |
| 59 | max_events = 100000; |
| 60 | seed = 1; |
| 61 | verbose = 0; |
| 62 | dump_file = 0; |
| 63 | load_file = 0; |
| 64 | merge_files = 0; |
| 65 | tags = 0; |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 66 | align_tweaks = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | min_sample_time = 2; |
| 68 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 69 | { |
| 70 | if (unformat (input, "iter %d", &n_iter)) |
| 71 | ; |
| 72 | else if (unformat (input, "seed %d", &seed)) |
| 73 | ; |
| 74 | else if (unformat (input, "dump %s", &dump_file)) |
| 75 | ; |
| 76 | else if (unformat (input, "load %s", &load_file)) |
| 77 | ; |
| 78 | else if (unformat (input, "tag %s", &tag)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 79 | vec_add1 (tags, tag); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | else if (unformat (input, "merge %s", &merge_file)) |
| 81 | vec_add1 (merge_files, merge_file); |
| 82 | |
| 83 | else if (unformat (input, "verbose %=", &verbose, 1)) |
| 84 | ; |
| 85 | else if (unformat (input, "max-events %d", &max_events)) |
| 86 | ; |
| 87 | else if (unformat (input, "sample-time %f", &min_sample_time)) |
| 88 | ; |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 89 | else if (unformat (input, "align-tweak %f", &align_tweak)) |
| 90 | vec_add1 (align_tweaks, align_tweak); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | else |
| 92 | { |
| 93 | error = clib_error_create ("unknown input `%U'\n", |
| 94 | format_unformat_error, input); |
| 95 | goto done; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #ifdef CLIB_UNIX |
| 100 | if (load_file) |
| 101 | { |
| 102 | if ((error = elog_read_file (em, load_file))) |
| 103 | goto done; |
| 104 | } |
| 105 | |
| 106 | else if (merge_files) |
| 107 | { |
| 108 | uword i; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 109 | elog_main_t *ems; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | vec_clone (ems, merge_files); |
| 111 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 112 | /* Supply default tags as needed */ |
| 113 | if (vec_len (tags) < vec_len (ems)) |
| 114 | { |
| 115 | for (i = vec_len (tags); i < vec_len (ems); i++) |
| 116 | vec_add1 (tags, format (0, "F%d%c", i, 0)); |
| 117 | } |
| 118 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | elog_init (em, max_events); |
| 120 | for (i = 0; i < vec_len (ems); i++) |
| 121 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 122 | if ((error = |
| 123 | elog_read_file (i == 0 ? em : &ems[i], merge_files[i]))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | goto done; |
| 125 | if (i > 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 126 | { |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 127 | align_tweak = 0.0; |
| 128 | if (i <= vec_len (align_tweaks)) |
| 129 | align_tweak = align_tweaks[i - 1]; |
| 130 | elog_merge (em, tags[0], &ems[i], tags[i], align_tweak); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | tags[0] = 0; |
| 132 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | else |
| 137 | #endif /* CLIB_UNIX */ |
| 138 | { |
| 139 | f64 t[2]; |
| 140 | |
| 141 | elog_init (em, max_events); |
| 142 | elog_enable_disable (em, 1); |
| 143 | t[0] = unix_time_now (); |
| 144 | |
| 145 | for (i = 0; i < n_iter; i++) |
| 146 | { |
| 147 | u32 j, n, sum; |
| 148 | |
| 149 | n = 1 + (random_u32 (&seed) % 128); |
| 150 | sum = 0; |
| 151 | for (j = 0; j < n; j++) |
| 152 | sum += random_u32 (&seed); |
| 153 | |
| 154 | { |
| 155 | ELOG_TYPE_XF (e); |
| 156 | ELOG (em, e, sum); |
| 157 | } |
| 158 | |
| 159 | { |
| 160 | ELOG_TYPE_XF (e); |
| 161 | ELOG (em, e, sum + 1); |
| 162 | } |
| 163 | |
| 164 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 165 | struct |
| 166 | { |
| 167 | u32 string_index; |
| 168 | f32 f; |
| 169 | } *d; |
| 170 | ELOG_TYPE_DECLARE (e) = |
| 171 | { |
| 172 | .format = "fumble %s %.9f",.format_args = |
| 173 | "t4f4",.n_enum_strings = 4,.enum_strings = |
| 174 | { |
| 175 | "string0", "string1", "string2", "string3",},}; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | |
| 177 | d = ELOG_DATA (em, e); |
| 178 | |
| 179 | d->string_index = sum & 3; |
| 180 | d->f = (sum & 0xff) / 128.; |
| 181 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 182 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 184 | ELOG_TYPE_DECLARE (e) = |
| 185 | { |
| 186 | .format = "bar %d.%d.%d.%d",.format_args = "i1i1i1i1",}; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | ELOG_TRACK (my_track); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 188 | u8 *d = ELOG_TRACK_DATA (em, e, my_track); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | d[0] = i + 0; |
| 190 | d[1] = i + 1; |
| 191 | d[2] = i + 2; |
| 192 | d[3] = i + 3; |
| 193 | } |
| 194 | |
| 195 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 196 | ELOG_TYPE_DECLARE (e) = |
| 197 | { |
| 198 | .format = "bar `%s'",.format_args = "s20",}; |
| 199 | struct |
| 200 | { |
| 201 | char s[20]; |
| 202 | } *d; |
| 203 | u8 *v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | |
| 205 | d = ELOG_DATA (em, e); |
| 206 | v = format (0, "foo %d%c", i, 0); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 207 | clib_memcpy (d->s, v, clib_min (vec_len (v), sizeof (d->s))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 211 | ELOG_TYPE_DECLARE (e) = |
| 212 | { |
| 213 | .format = "bar `%s'",.format_args = "T4",}; |
| 214 | struct |
| 215 | { |
| 216 | u32 offset; |
| 217 | } *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | |
| 219 | d = ELOG_DATA (em, e); |
| 220 | d->offset = elog_string (em, "string table %d", i); |
| 221 | } |
| 222 | } |
| 223 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 224 | do |
| 225 | { |
| 226 | t[1] = unix_time_now (); |
| 227 | } |
| 228 | while (t[1] - t[0] < min_sample_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | #ifdef CLIB_UNIX |
| 232 | if (dump_file) |
| 233 | { |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 234 | if ((error = |
| 235 | elog_write_file (em, dump_file, 0 /* do not flush ring */ ))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | goto done; |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | if (verbose) |
| 241 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 242 | elog_event_t *e, *es; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | es = elog_get_events (em); |
| 244 | vec_foreach (e, es) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 245 | { |
| 246 | clib_warning ("%18.9f: %12U %U\n", e->time, |
| 247 | format_elog_track, em, e, format_elog_event, em, e); |
| 248 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 251 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | if (error) |
| 253 | clib_error_report (error); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 258 | int |
| 259 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | { |
| 261 | unformat_input_t i; |
| 262 | int r; |
| 263 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 264 | clib_mem_init (0, 3ULL << 30); |
| 265 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | unformat_init_command_line (&i, argv); |
| 267 | r = test_elog_main (&i); |
| 268 | unformat_free (&i); |
| 269 | return r; |
| 270 | } |
| 271 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 272 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 273 | /** |
| 274 | * @brief GDB callable function: vl - Return vector length of vector |
| 275 | * |
| 276 | * @param *p - void - address of vector |
| 277 | * |
| 278 | * @return length - u32 |
| 279 | * |
| 280 | */ |
| 281 | u32 |
| 282 | vl (void *p) |
| 283 | { |
| 284 | return vec_len (p); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @brief GDB callable function: pe - call pool_elts - number of elements in a pool |
| 289 | * |
| 290 | * @param *v - void - address of pool |
| 291 | * |
| 292 | * @return number - uword |
| 293 | * |
| 294 | */ |
| 295 | #include <vppinfra/pool.h> |
| 296 | uword |
| 297 | pe (void *v) |
| 298 | { |
| 299 | return (pool_elts (v)); |
| 300 | } |
| 301 | |
| 302 | #include <vppinfra/hash.h> |
| 303 | uword |
| 304 | he (void *v) |
| 305 | { |
| 306 | return (hash_elts (v)); |
| 307 | } |
| 308 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 309 | /* |
| 310 | * fd.io coding-style-patch-verification: ON |
| 311 | * |
| 312 | * Local Variables: |
| 313 | * eval: (c-set-style "gnu") |
| 314 | * End: |
| 315 | */ |