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 *------------------------------------------------------------------
21 #include <vlib/vlib.h>
23 #include <vlib/unix/cj.h>
28 cj_log (u32 type, void *data0, void *data1)
31 cj_main_t *cjm = &cj_main;
37 new_tail = __sync_add_and_fetch (&cjm->tail, 1);
39 r = (cj_record_t *) & (cjm->records[new_tail & (cjm->num_records - 1)]);
40 r->time = vlib_time_now (cjm->vlib_main);
41 r->cpu = os_get_cpu_number ();
43 r->data[0] = pointer_to_uword (data0);
44 r->data[1] = pointer_to_uword (data1);
50 cj_main_t *cjm = &cj_main;
57 cj_init (vlib_main_t * vm)
59 cj_main_t *cjm = &cj_main;
65 VLIB_INIT_FUNCTION (cj_init);
68 cj_config (vlib_main_t * vm, unformat_input_t * input)
70 cj_main_t *cjm = &cj_main;
74 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
76 if (unformat (input, "records %d", &cjm->num_records))
78 else if (unformat (input, "on"))
81 return clib_error_return (0, "cj_config: unknown input '%U'",
82 format_unformat_error, input);
88 cjm->num_records = max_pow2 (cjm->num_records);
89 vec_validate (cjm->records, cjm->num_records - 1);
90 memset (cjm->records, 0xff, cjm->num_records * sizeof (cj_record_t));
97 VLIB_CONFIG_FUNCTION (cj_config, "cj");
100 cj_enable_disable (int is_enable)
102 cj_main_t *cjm = &cj_main;
104 if (cjm->num_records)
105 cjm->enable = is_enable;
107 vlib_cli_output (cjm->vlib_main, "CJ not configured...");
111 cj_dump_one_record (cj_record_t * r)
113 fprintf (stderr, "[%d]: %10.6f T%02d %llx %llx\n",
114 r->cpu, r->time, r->type, (long long unsigned int) r->data[0],
115 (long long unsigned int) r->data[1]);
119 cj_dump_internal (u8 filter0_enable, u64 filter0,
120 u8 filter1_enable, u64 filter1)
122 cj_main_t *cjm = &cj_main;
126 if (cjm->num_records == 0)
128 fprintf (stderr, "CJ not configured...\n");
132 if (cjm->tail == (u64) ~ 0)
134 fprintf (stderr, "No data collected...\n");
138 /* Has the trace wrapped? */
139 index = (cjm->tail + 1) & (cjm->num_records - 1);
140 r = &(cjm->records[index]);
142 if (r->cpu != (u32) ~ 0)
144 /* Yes, dump from tail + 1 to the end */
145 for (i = index; i < cjm->num_records; i++)
147 if (filter0_enable && (r->data[0] != filter0))
149 if (filter1_enable && (r->data[1] != filter1))
151 cj_dump_one_record (r);
156 /* dump from the beginning through the final tail */
158 for (i = 0; i <= cjm->tail; i++)
160 if (filter0_enable && (r->data[0] != filter0))
162 if (filter1_enable && (r->data[1] != filter1))
164 cj_dump_one_record (r);
173 cj_dump_internal (0, 0, 0, 0);
177 cj_dump_filter_data0 (u64 filter0)
179 cj_dump_internal (1 /* enable f0 */ , filter0, 0, 0);
183 cj_dump_filter_data1 (u64 filter1)
185 cj_dump_internal (0, 0, 1 /* enable f1 */ , filter1);
189 cj_dump_filter_data12 (u64 filter0, u64 filter1)
191 cj_dump_internal (1, filter0, 1, filter1);
194 static clib_error_t *
195 cj_command_fn (vlib_main_t * vm,
196 unformat_input_t * input, vlib_cli_command_t * cmd)
201 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
203 if (unformat (input, "enable") || unformat (input, "on"))
205 else if (unformat (input, "disable") || unformat (input, "off"))
207 else if (unformat (input, "dump"))
210 return clib_error_return (0, "unknown input `%U'",
211 format_unformat_error, input);
215 cj_enable_disable (is_enable);
224 VLIB_CLI_COMMAND (cj_command,static) = {
227 .function = cj_command_fn,
233 * fd.io coding-style-patch-verification: ON
236 * eval: (c-set-style "gnu")