Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
| 3 | * cj.c |
| 4 | * |
| 5 | * Copyright (c) 2013 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 20 | /** |
| 21 | * @file |
| 22 | * Circular joournal diagnostic mechanism. |
| 23 | * |
| 24 | * The @c cj thread-safe circular log buffer scheme is occasionally useful |
| 25 | * when chasing bugs. Calls to it should not be checked in. |
| 26 | */ |
| 27 | /*? %%clicmd:group_label Circular Journal %% ?*/ |
| 28 | /*? %%syscfg:group_label Circular Journal %% ?*/ |
| 29 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <vlib/vlib.h> |
| 32 | |
| 33 | #include <vlib/unix/cj.h> |
| 34 | |
| 35 | cj_main_t cj_main; |
| 36 | |
| 37 | void |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 38 | cj_log (u32 type, void *data0, void *data1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | { |
| 40 | u64 new_tail; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 41 | cj_main_t *cjm = &cj_main; |
| 42 | cj_record_t *r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | |
| 44 | if (cjm->enable == 0) |
| 45 | return; |
| 46 | |
| 47 | new_tail = __sync_add_and_fetch (&cjm->tail, 1); |
| 48 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 49 | r = (cj_record_t *) & (cjm->records[new_tail & (cjm->num_records - 1)]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | r->time = vlib_time_now (cjm->vlib_main); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 51 | r->thread_index = vlib_get_thread_index (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | r->type = type; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 53 | r->data[0] = pointer_to_uword (data0); |
| 54 | r->data[1] = pointer_to_uword (data1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 57 | void |
| 58 | cj_stop (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 60 | cj_main_t *cjm = &cj_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | |
| 62 | cjm->enable = 0; |
| 63 | } |
| 64 | |
| 65 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 66 | clib_error_t * |
| 67 | cj_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 69 | cj_main_t *cjm = &cj_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
| 71 | cjm->vlib_main = vm; |
| 72 | return 0; |
| 73 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 74 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | VLIB_INIT_FUNCTION (cj_init); |
| 76 | |
| 77 | static clib_error_t * |
| 78 | cj_config (vlib_main_t * vm, unformat_input_t * input) |
| 79 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 80 | cj_main_t *cjm = &cj_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | int matched = 0; |
| 82 | int enable = 0; |
| 83 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 84 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | { |
| 86 | if (unformat (input, "records %d", &cjm->num_records)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 87 | matched = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | else if (unformat (input, "on")) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 89 | enable = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 91 | return clib_error_return (0, "cj_config: unknown input '%U'", |
| 92 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if (matched == 0) |
| 96 | return 0; |
| 97 | |
| 98 | cjm->num_records = max_pow2 (cjm->num_records); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 99 | vec_validate (cjm->records, cjm->num_records - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | memset (cjm->records, 0xff, cjm->num_records * sizeof (cj_record_t)); |
| 101 | cjm->tail = ~0; |
| 102 | cjm->enable = enable; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 107 | /*? |
| 108 | * Configure the circular journal diagnostic mechanism. This is only useful |
| 109 | * if you, the deveoper, have written code to make use of the circular |
| 110 | * journal. |
| 111 | * |
| 112 | * @cfgcmd{records, <number>} |
| 113 | * Configure the number of records to allocate for the circular journal. |
| 114 | * |
| 115 | * @cfgcmd{on} |
| 116 | * Enable the collection of records in the circular journal at the |
| 117 | * earliest opportunity. |
| 118 | ?*/ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | VLIB_CONFIG_FUNCTION (cj_config, "cj"); |
| 120 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 121 | void |
| 122 | cj_enable_disable (int is_enable) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 124 | cj_main_t *cjm = &cj_main; |
| 125 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | if (cjm->num_records) |
| 127 | cjm->enable = is_enable; |
| 128 | else |
| 129 | vlib_cli_output (cjm->vlib_main, "CJ not configured..."); |
| 130 | } |
| 131 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 132 | static inline void |
| 133 | cj_dump_one_record (cj_record_t * r) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | { |
| 135 | fprintf (stderr, "[%d]: %10.6f T%02d %llx %llx\n", |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 136 | r->thread_index, r->time, r->type, |
| 137 | (long long unsigned int) r->data[0], |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 138 | (long long unsigned int) r->data[1]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 141 | static void |
| 142 | cj_dump_internal (u8 filter0_enable, u64 filter0, |
| 143 | u8 filter1_enable, u64 filter1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 145 | cj_main_t *cjm = &cj_main; |
| 146 | cj_record_t *r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | u32 i, index; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 148 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | if (cjm->num_records == 0) |
| 150 | { |
| 151 | fprintf (stderr, "CJ not configured...\n"); |
| 152 | return; |
| 153 | } |
| 154 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 155 | if (cjm->tail == (u64) ~ 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | { |
| 157 | fprintf (stderr, "No data collected...\n"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | /* Has the trace wrapped? */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 162 | index = (cjm->tail + 1) & (cjm->num_records - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | r = &(cjm->records[index]); |
| 164 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 165 | if (r->thread_index != (u32) ~ 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 167 | /* Yes, dump from tail + 1 to the end */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | for (i = index; i < cjm->num_records; i++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 169 | { |
| 170 | if (filter0_enable && (r->data[0] != filter0)) |
| 171 | goto skip; |
| 172 | if (filter1_enable && (r->data[1] != filter1)) |
| 173 | goto skip; |
| 174 | cj_dump_one_record (r); |
| 175 | skip: |
| 176 | r++; |
| 177 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | } |
| 179 | /* dump from the beginning through the final tail */ |
| 180 | r = cjm->records; |
| 181 | for (i = 0; i <= cjm->tail; i++) |
| 182 | { |
| 183 | if (filter0_enable && (r->data[0] != filter0)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 184 | goto skip2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | if (filter1_enable && (r->data[1] != filter1)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 186 | goto skip2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | cj_dump_one_record (r); |
| 188 | skip2: |
| 189 | r++; |
| 190 | } |
| 191 | } |
| 192 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 193 | void |
| 194 | cj_dump (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | { |
| 196 | cj_dump_internal (0, 0, 0, 0); |
| 197 | } |
| 198 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 199 | void |
| 200 | cj_dump_filter_data0 (u64 filter0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 202 | cj_dump_internal (1 /* enable f0 */ , filter0, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 205 | void |
| 206 | cj_dump_filter_data1 (u64 filter1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 208 | cj_dump_internal (0, 0, 1 /* enable f1 */ , filter1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 211 | void |
| 212 | cj_dump_filter_data12 (u64 filter0, u64 filter1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | { |
| 214 | cj_dump_internal (1, filter0, 1, filter1); |
| 215 | } |
| 216 | |
| 217 | static clib_error_t * |
| 218 | cj_command_fn (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 219 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | { |
| 221 | int is_enable = -1; |
| 222 | int is_dump = -1; |
| 223 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 224 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 225 | { |
| 226 | if (unformat (input, "enable") || unformat (input, "on")) |
| 227 | is_enable = 1; |
| 228 | else if (unformat (input, "disable") || unformat (input, "off")) |
| 229 | is_enable = 0; |
| 230 | else if (unformat (input, "dump")) |
| 231 | is_dump = 1; |
| 232 | else |
| 233 | return clib_error_return (0, "unknown input `%U'", |
| 234 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if (is_enable >= 0) |
| 238 | cj_enable_disable (is_enable); |
| 239 | |
| 240 | if (is_dump > 0) |
| 241 | cj_dump (); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 246 | /*? |
| 247 | * Enable, disable the collection of diagnostic data into a |
| 248 | * circular journal or dump the circular journal diagnostic data. |
| 249 | * This is only useful if you, the deveoper, have written code to make |
| 250 | * use of the circular journal. |
| 251 | * |
| 252 | * When dumping the data it is formatted and sent to @c stderr of the |
| 253 | * VPP process; when running VPP in <code>unix interactive</code> mode |
| 254 | * this is typically the same place as the Debug CLI. |
| 255 | ?*/ |
| 256 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 257 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | VLIB_CLI_COMMAND (cj_command,static) = { |
| 259 | .path = "cj", |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 260 | .short_help = "cj <enable | disable | dump>", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | .function = cj_command_fn, |
| 262 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 263 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 265 | |
| 266 | /* |
| 267 | * fd.io coding-style-patch-verification: ON |
| 268 | * |
| 269 | * Local Variables: |
| 270 | * eval: (c-set-style "gnu") |
| 271 | * End: |
| 272 | */ |