aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/bufmon/bufmon.c
blob: 2a35acce4825f02fe42ac370e6261ff4b2468aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <vlib/vlib.h>
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>

typedef struct
{
  u64 in;
  u64 out;
  u64 alloc;
  u64 free;
} bufmon_per_node_data_t;

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
  bufmon_per_node_data_t *pnd;
  u32 cur_node;
} bufmon_per_thread_data_t;

typedef struct
{
  bufmon_per_thread_data_t *ptd;
  int enabled;
} bufmon_main_t;

static bufmon_main_t bufmon_main;

static u32
bufmon_alloc_free_callback (vlib_main_t *vm, u32 n_buffers, const int is_free)
{
  bufmon_main_t *bm = &bufmon_main;
  bufmon_per_thread_data_t *ptd;
  bufmon_per_node_data_t *pnd;
  u32 cur_node;

  if (PREDICT_FALSE (vm->thread_index >= vec_len (bm->ptd)))
    {
      clib_warning ("bufmon: thread index %d unknown for buffer %s (%d)",
		    vm->thread_index, is_free ? "free" : "alloc", n_buffers);
      return n_buffers;
    }

  ptd = vec_elt_at_index (bm->ptd, vm->thread_index);

  cur_node = ptd->cur_node;
  if (cur_node >= vec_len (ptd->pnd))
    {
      cur_node = vlib_get_current_process_node_index (vm);
      vec_validate_aligned (ptd->pnd, cur_node, CLIB_CACHE_LINE_BYTES);
    }

  pnd = vec_elt_at_index (ptd->pnd, cur_node);

  if (is_free)
    pnd->free += n_buffers;
  else
    pnd->alloc += n_buffers;

  return n_buffers;
}

static u32
bufmon_alloc_callback (vlib_main_t *vm, u8 buffer_pool_index, u32 *buffers,
		       u32 n_buffers)
{
  return bufmon_alloc_free_callback (vm, n_buffers, 0 /* is_free */);
}

static u32
bufmon_free_callback (vlib_main_t *vm, u8 buffer_pool_index, u32 *buffers,
		      u32 n_buffers)
{
  return bufmon_alloc_free_callback (vm, n_buffers, 1 /* is_free */);
}

static u32
bufmon_count_buffers (vlib_main_t *vm, vlib_frame_t *frame)
{
  vlib_buffer_t *b[VLIB_FRAME_SIZE];
  u32 *from = vlib_frame_vector_args (frame);
  const u32 n = frame->n_vectors;
  u32 nc = 0;
  u32 i;

  vlib_get_buffers (vm, from, b, n);

  for (i = 0; i < n; i++)
    {
      const vlib_buffer_t *cb = b[i];
      while (cb->flags & VLIB_BUFFER_NEXT_PRESENT)
	{
	  nc++;
	  cb = vlib_get_buffer (vm, cb->next_buffer);
	}
    }

  return n + nc;
}

static uword
bufmon_dispatch_wrapper (vlib_main_t *vm, vlib_node_runtime_t *node,
			 vlib_frame_t *frame)
{
  vlib_node_main_t *nm = &vm->node_main;
  bufmon_main_t *bm = &bufmon_main;
  bufmon_per_thread_data_t *ptd;
  bufmon_per_node_data_t *pnd;
  int pending_frames;
  uword rv;

  vec_validate_aligned (bm->ptd, vm->thread_index, CLIB_CACHE_LINE_BYTES);
  ptd = vec_elt_at_index (bm->ptd, vm->thread_index);
  vec_validate_aligned (ptd->pnd, node->node_index, CLIB_CACHE_LINE_BYTES);
  pnd = vec_elt_at_index (ptd->pnd, node->node_index);

  if (frame)
    pnd->in += bufmon_count_buffers (vm, frame);

  pending_frames = vec_len (nm->pending_frames);
  ptd->cur_node = node->node_index;

  rv = node->function (vm, node, frame);

  ptd->cur_node = ~0;
  for (; pending_frames < vec_len (nm->pending_frames); pending_frames++)
    {
      vlib_pending_frame_t *p =
	vec_elt_at_index (nm->pending_frames, pending_frames);
      pnd->out += bufmon_count_buffers (vm, vlib_get_frame (vm, p->frame));
    }

  return rv;
}

static void
bufmon_unregister_callbacks (vlib_main_t *vm)
{
  vlib_buffer_set_alloc_free_callback (vm, 0, 0);
  foreach_vlib_main ()
    vlib_node_set_dispatch_wrapper (this_vlib_main, 0);
}

static clib_error_t *
bufmon_register_callbacks (vlib_main_t *vm)
{
  if (vlib_buffer_set_alloc_free_callback (vm, bufmon_alloc_callback,
					   bufmon_free_callback))
    goto err0;

  foreach_vlib_main ()
    if (vlib_node_set_dispatch_wrapper (this_vlib_main,
					bufmon_dispatch_wrapper))
      goto err1;

  return 0;

err1:
  foreach_vlib_main ()
    vlib_node_set_dispatch_wrapper (this_vlib_main, 0);
err0:
  vlib_buffer_set_alloc_free_callback (vm, 0, 0);
  return clib_error_return (0, "failed to register callback");
}

static clib_error_t *
bufmon_enable_disable (vlib_main_t *vm, int enable)
{
  bufmon_main_t *bm = &bufmon_main;

  if (enable)
    {
      if (bm->enabled)
	return 0;
      clib_error_t *error = bufmon_register_callbacks (vm);
      if (error)
	return error;
      bm->enabled = 1;
    }
  else
    {
      if (!bm->enabled)
	return 0;
      bufmon_unregister_callbacks (vm);
      bm->enabled = 0;
    }

  return 0;
}

static clib_error_t *
set_buffer_traces (vlib_main_t *vm, unformat_input_t *input,
		   vlib_cli_command_t *cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  int on = 1;

  if (unformat_user (input, unformat_line_input, line_input))
    {
      while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
	{
	  if (unformat (line_input, "on"))
	    on = 1;
	  else if (unformat (line_input, "off"))
	    on = 0;
	  else
	    {
	      unformat_free (line_input);
	      return clib_error_return (0, "unknown input `%U'",
					format_unformat_error, line_input);
	    }
	}
      unformat_free (line_input);
    }

  return bufmon_enable_disable (vm, on);
}

VLIB_CLI_COMMAND (set_buffer_traces_command, static) = {
  .path = "set buffer traces",
  .short_help = "set buffer traces [on|off]",
  .function = set_buffer_traces,
};

static clib_error_t *
show_buffer_traces (vlib_main_t *vm, unformat_input_t *input,
		    vlib_cli_command_t *cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  const bufmon_main_t *bm = &bufmon_main;
  const bufmon_per_thread_data_t *ptd;
  const bufmon_per_node_data_t *pnd;
  int verbose = 0;
  int status = 0;

  if (unformat_user (input, unformat_line_input, line_input))
    {
      while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
	{
	  if (unformat (line_input, "verbose"))
	    verbose = 1;
	  else if (unformat (line_input, "status"))
	    status = 1;
	  else
	    {
	      unformat_free (line_input);
	      return clib_error_return (0, "unknown input `%U'",
					format_unformat_error, line_input);
	    }
	}
      unformat_free (line_input);
    }

  if (status)
    {
      vlib_cli_output (vm, "buffers tracing is %s",
		       bm->enabled ? "on" : "off");
      return 0;
    }

  vlib_cli_output (vm, "%U\n\n", format_vlib_buffer_pool_all, vm);
  vlib_cli_output (vm, "%30s%20s%20s%20s%20s%20s", "Node", "Allocated",
		   "Freed", "In", "Out", "Buffered");
  vec_foreach (ptd, bm->ptd)
    {
      vec_foreach (pnd, ptd->pnd)
	{
	  const u64 in = pnd->alloc + pnd->in;
	  const u64 out = pnd->free + pnd->out;
	  const i64 buffered = in - out;
	  if (0 == in && 0 == out)
	    continue; /* skip nodes w/o activity */
	  if (0 == buffered && !verbose)
	    continue; /* if not verbose, skip nodes w/o buffered buffers */
	  vlib_cli_output (vm, "%30U%20lu%20lu%20lu%20lu%20ld",
			   format_vlib_node_name, vm, pnd - ptd->pnd,
			   pnd->alloc, pnd->free, pnd->in, pnd->out, buffered);
	}
    }

  return 0;
}

VLIB_CLI_COMMAND (show_buffer_traces_command, static) = {
  .path = "show buffer traces",
  .short_help = "show buffer traces [status|verbose]",
  .function = show_buffer_traces,
};

static clib_error_t *
clear_buffer_traces (vlib_main_t *vm, unformat_input_t *input,
		     vlib_cli_command_t *cmd)
{
  const bufmon_main_t *bm = &bufmon_main;
  const bufmon_per_thread_data_t *ptd;
  const bufmon_per_node_data_t *pnd;

  vec_foreach (ptd, bm->ptd)
    vec_foreach (pnd, ptd->pnd)
      vec_reset_length (pnd);

  return 0;
}

VLIB_CLI_COMMAND (clear_buffers_trace_command, static) = {
  .path = "clear buffer traces",
  .short_help = "clear buffer traces",
  .function = clear_buffer_traces,
};

VLIB_PLUGIN_REGISTER () = {
  .version = VPP_BUILD_VER,
  .description = "Buffers monitoring plugin",
};