Dave Barach | 2c35e58 | 2017-04-03 10:22:17 -0400 | [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 | #include <vppinfra/pool.h> |
| 45 | #include <vppinfra/hash.h> |
| 46 | |
| 47 | int |
| 48 | elog_merge_main (unformat_input_t * input) |
| 49 | { |
| 50 | clib_error_t *error = 0; |
| 51 | elog_main_t _em, *em = &_em; |
| 52 | u32 verbose; |
| 53 | char *dump_file, *merge_file, **merge_files; |
| 54 | u8 *tag, **tags; |
| 55 | f64 align_tweak; |
| 56 | f64 *align_tweaks; |
| 57 | uword i; |
| 58 | elog_main_t *ems; |
| 59 | |
| 60 | verbose = 0; |
| 61 | dump_file = 0; |
| 62 | merge_files = 0; |
| 63 | tags = 0; |
| 64 | align_tweaks = 0; |
| 65 | |
| 66 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 67 | { |
| 68 | if (unformat (input, "dump %s", &dump_file)) |
| 69 | ; |
| 70 | else if (unformat (input, "tag %s", &tag)) |
| 71 | vec_add1 (tags, tag); |
| 72 | else if (unformat (input, "merge %s", &merge_file)) |
| 73 | vec_add1 (merge_files, merge_file); |
| 74 | |
| 75 | else if (unformat (input, "verbose %=", &verbose, 1)) |
| 76 | ; |
| 77 | else if (unformat (input, "align-tweak %f", &align_tweak)) |
| 78 | vec_add1 (align_tweaks, align_tweak); |
| 79 | else |
| 80 | { |
| 81 | error = clib_error_create ("unknown input `%U'\n", |
| 82 | format_unformat_error, input); |
| 83 | goto done; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | vec_clone (ems, merge_files); |
| 88 | |
| 89 | /* Supply default tags as needed */ |
| 90 | if (vec_len (tags) < vec_len (ems)) |
| 91 | { |
| 92 | for (i = vec_len (tags); i < vec_len (ems); i++) |
| 93 | vec_add1 (tags, format (0, "F%d%c", i, 0)); |
| 94 | } |
| 95 | |
| 96 | for (i = 0; i < vec_len (ems); i++) |
| 97 | { |
| 98 | if ((error = elog_read_file ((i == 0) ? em : &ems[i], merge_files[i]))) |
| 99 | goto done; |
| 100 | if (i > 0) |
| 101 | { |
| 102 | align_tweak = 0.0; |
| 103 | if (i <= vec_len (align_tweaks)) |
| 104 | align_tweak = align_tweaks[i - 1]; |
| 105 | elog_merge (em, tags[0], &ems[i], tags[i], align_tweak); |
| 106 | tags[0] = 0; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (dump_file) |
| 111 | { |
| 112 | if ((error = |
| 113 | elog_write_file (em, dump_file, 0 /* do not flush ring */ ))) |
| 114 | goto done; |
| 115 | } |
| 116 | |
| 117 | if (verbose) |
| 118 | { |
| 119 | elog_event_t *e, *es; |
| 120 | es = elog_get_events (em); |
| 121 | vec_foreach (e, es) |
| 122 | { |
| 123 | clib_warning ("%18.9f: %12U %U\n", e->time, |
| 124 | format_elog_track, em, e, format_elog_event, em, e); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | done: |
| 129 | if (error) |
| 130 | clib_error_report (error); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int |
| 135 | main (int argc, char *argv[]) |
| 136 | { |
| 137 | unformat_input_t i; |
| 138 | int r; |
| 139 | |
| 140 | clib_mem_init (0, 3ULL << 30); |
| 141 | |
| 142 | unformat_init_command_line (&i, argv); |
| 143 | r = elog_merge_main (&i); |
| 144 | unformat_free (&i); |
| 145 | return r; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * GDB callable function: vl - Return vector length of vector |
| 150 | */ |
| 151 | u32 |
| 152 | vl (void *p) |
| 153 | { |
| 154 | return vec_len (p); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * GDB callable function: pe - call pool_elts - number of elements in a pool |
| 159 | */ |
| 160 | uword |
| 161 | pe (void *v) |
| 162 | { |
| 163 | return (pool_elts (v)); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * GDB callable function: he - call hash_elts - number of elements in a hash |
| 168 | */ |
| 169 | uword |
| 170 | he (void *v) |
| 171 | { |
| 172 | return (hash_elts (v)); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * fd.io coding-style-patch-verification: ON |
| 177 | * |
| 178 | * Local Variables: |
| 179 | * eval: (c-set-style "gnu") |
| 180 | * End: |
| 181 | */ |