Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2022 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #include <vlib/vlib.h> |
| 6 | #include <vlib/stats/stats.h> |
| 7 | |
| 8 | static int |
| 9 | name_sort_cmp (void *a1, void *a2) |
| 10 | { |
| 11 | vlib_stats_entry_t *n1 = a1; |
| 12 | vlib_stats_entry_t *n2 = a2; |
| 13 | |
| 14 | return strcmp ((char *) n1->name, (char *) n2->name); |
| 15 | } |
| 16 | |
| 17 | static u8 * |
| 18 | format_stat_dir_entry (u8 *s, va_list *args) |
| 19 | { |
| 20 | vlib_stats_entry_t *ep = va_arg (*args, vlib_stats_entry_t *); |
| 21 | char *type_name; |
| 22 | char *format_string; |
| 23 | |
| 24 | format_string = "%-74s %-10s %10lld"; |
| 25 | |
| 26 | switch (ep->type) |
| 27 | { |
| 28 | case STAT_DIR_TYPE_SCALAR_INDEX: |
| 29 | type_name = "ScalarPtr"; |
| 30 | break; |
| 31 | |
| 32 | case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: |
| 33 | case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: |
| 34 | type_name = "CMainPtr"; |
| 35 | break; |
| 36 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 37 | case STAT_DIR_TYPE_NAME_VECTOR: |
| 38 | type_name = "NameVector"; |
| 39 | break; |
| 40 | |
| 41 | case STAT_DIR_TYPE_EMPTY: |
| 42 | type_name = "empty"; |
| 43 | break; |
| 44 | |
| 45 | case STAT_DIR_TYPE_SYMLINK: |
| 46 | type_name = "Symlink"; |
| 47 | break; |
| 48 | |
| 49 | default: |
| 50 | type_name = "illegal!"; |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | return format (s, format_string, ep->name, type_name, 0); |
| 55 | } |
| 56 | static clib_error_t * |
| 57 | show_stat_segment_command_fn (vlib_main_t *vm, unformat_input_t *input, |
| 58 | vlib_cli_command_t *cmd) |
| 59 | { |
| 60 | vlib_stats_segment_t *sm = vlib_stats_get_segment (); |
| 61 | vlib_stats_entry_t *show_data; |
| 62 | int i; |
| 63 | |
| 64 | int verbose = 0; |
| 65 | |
| 66 | if (unformat (input, "verbose")) |
| 67 | verbose = 1; |
| 68 | |
| 69 | /* Lock even as reader, as this command doesn't handle epoch changes */ |
| 70 | vlib_stats_segment_lock (); |
| 71 | show_data = vec_dup (sm->directory_vector); |
| 72 | vlib_stats_segment_unlock (); |
| 73 | |
| 74 | vec_sort_with_function (show_data, name_sort_cmp); |
| 75 | |
| 76 | vlib_cli_output (vm, "%-74s %10s %10s", "Name", "Type", "Value"); |
| 77 | |
| 78 | for (i = 0; i < vec_len (show_data); i++) |
| 79 | { |
| 80 | vlib_stats_entry_t *ep = vec_elt_at_index (show_data, i); |
| 81 | |
| 82 | if (ep->type == STAT_DIR_TYPE_EMPTY) |
| 83 | continue; |
| 84 | |
| 85 | vlib_cli_output (vm, "%-100U", format_stat_dir_entry, |
| 86 | vec_elt_at_index (show_data, i)); |
| 87 | } |
| 88 | |
| 89 | if (verbose) |
| 90 | { |
| 91 | ASSERT (sm->heap); |
| 92 | vlib_cli_output (vm, "%U", format_clib_mem_heap, sm->heap, |
| 93 | 0 /* verbose */); |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static clib_error_t * |
| 100 | show_stat_segment_hash_command_fn (vlib_main_t *vm, unformat_input_t *input, |
| 101 | vlib_cli_command_t *cmd) |
| 102 | { |
| 103 | vlib_stats_segment_t *sm = vlib_stats_get_segment (); |
| 104 | char *name; |
| 105 | u32 i; |
| 106 | hash_foreach_mem (name, i, sm->directory_vector_by_name, |
| 107 | ({ vlib_cli_output (vm, "%d: %s\n", i, name); })); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | VLIB_CLI_COMMAND (show_stat_segment_hash_command, static) = { |
| 112 | .path = "show statistics hash", |
| 113 | .short_help = "show statistics hash", |
| 114 | .function = show_stat_segment_hash_command_fn, |
| 115 | }; |
| 116 | |
| 117 | VLIB_CLI_COMMAND (show_stat_segment_command, static) = { |
| 118 | .path = "show statistics segment", |
| 119 | .short_help = "show statistics segment [verbose]", |
| 120 | .function = show_stat_segment_command_fn, |
| 121 | }; |