2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
22 * Circular joournal diagnostic mechanism.
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.
27 /*? %%clicmd:group_label Circular Journal %% ?*/
28 /*? %%syscfg:group_label Circular Journal %% ?*/
31 #include <vlib/vlib.h>
33 #include <vlib/unix/cj.h>
38 cj_log (u32 type, void *data0, void *data1)
41 cj_main_t *cjm = &cj_main;
47 new_tail = __sync_add_and_fetch (&cjm->tail, 1);
49 r = (cj_record_t *) & (cjm->records[new_tail & (cjm->num_records - 1)]);
50 r->time = vlib_time_now (cjm->vlib_main);
51 r->thread_index = vlib_get_thread_index ();
53 r->data[0] = pointer_to_uword (data0);
54 r->data[1] = pointer_to_uword (data1);
60 cj_main_t *cjm = &cj_main;
67 cj_init (vlib_main_t * vm)
69 cj_main_t *cjm = &cj_main;
75 VLIB_INIT_FUNCTION (cj_init);
78 cj_config (vlib_main_t * vm, unformat_input_t * input)
80 cj_main_t *cjm = &cj_main;
84 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
86 if (unformat (input, "records %d", &cjm->num_records))
88 else if (unformat (input, "on"))
91 return clib_error_return (0, "cj_config: unknown input '%U'",
92 format_unformat_error, input);
98 cjm->num_records = max_pow2 (cjm->num_records);
99 vec_validate (cjm->records, cjm->num_records - 1);
100 memset (cjm->records, 0xff, cjm->num_records * sizeof (cj_record_t));
102 cjm->enable = enable;
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
112 * @cfgcmd{records, <number>}
113 * Configure the number of records to allocate for the circular journal.
116 * Enable the collection of records in the circular journal at the
117 * earliest opportunity.
119 VLIB_CONFIG_FUNCTION (cj_config, "cj");
122 cj_enable_disable (int is_enable)
124 cj_main_t *cjm = &cj_main;
126 if (cjm->num_records)
127 cjm->enable = is_enable;
129 vlib_cli_output (cjm->vlib_main, "CJ not configured...");
133 cj_dump_one_record (cj_record_t * r)
135 fprintf (stderr, "[%d]: %10.6f T%02d %llx %llx\n",
136 r->thread_index, r->time, r->type,
137 (long long unsigned int) r->data[0],
138 (long long unsigned int) r->data[1]);
142 cj_dump_internal (u8 filter0_enable, u64 filter0,
143 u8 filter1_enable, u64 filter1)
145 cj_main_t *cjm = &cj_main;
149 if (cjm->num_records == 0)
151 fprintf (stderr, "CJ not configured...\n");
155 if (cjm->tail == (u64) ~ 0)
157 fprintf (stderr, "No data collected...\n");
161 /* Has the trace wrapped? */
162 index = (cjm->tail + 1) & (cjm->num_records - 1);
163 r = &(cjm->records[index]);
165 if (r->thread_index != (u32) ~ 0)
167 /* Yes, dump from tail + 1 to the end */
168 for (i = index; i < cjm->num_records; i++)
170 if (filter0_enable && (r->data[0] != filter0))
172 if (filter1_enable && (r->data[1] != filter1))
174 cj_dump_one_record (r);
179 /* dump from the beginning through the final tail */
181 for (i = 0; i <= cjm->tail; i++)
183 if (filter0_enable && (r->data[0] != filter0))
185 if (filter1_enable && (r->data[1] != filter1))
187 cj_dump_one_record (r);
196 cj_dump_internal (0, 0, 0, 0);
200 cj_dump_filter_data0 (u64 filter0)
202 cj_dump_internal (1 /* enable f0 */ , filter0, 0, 0);
206 cj_dump_filter_data1 (u64 filter1)
208 cj_dump_internal (0, 0, 1 /* enable f1 */ , filter1);
212 cj_dump_filter_data12 (u64 filter0, u64 filter1)
214 cj_dump_internal (1, filter0, 1, filter1);
217 static clib_error_t *
218 cj_command_fn (vlib_main_t * vm,
219 unformat_input_t * input, vlib_cli_command_t * cmd)
224 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
226 if (unformat (input, "enable") || unformat (input, "on"))
228 else if (unformat (input, "disable") || unformat (input, "off"))
230 else if (unformat (input, "dump"))
233 return clib_error_return (0, "unknown input `%U'",
234 format_unformat_error, input);
238 cj_enable_disable (is_enable);
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.
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.
258 VLIB_CLI_COMMAND (cj_command,static) = {
260 .short_help = "cj <enable | disable | dump>",
261 .function = cj_command_fn,
267 * fd.io coding-style-patch-verification: ON
270 * eval: (c-set-style "gnu")