aboutsummaryrefslogtreecommitdiffstats
path: root/src/vlib/stats/provider_mem.c
blob: d52ea4aa0e5025c73c7a4884753b72a2f8beb302 (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
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2022 Cisco Systems, Inc.
 */

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vlib/stats/stats.h>

static clib_mem_heap_t **memory_heaps_vec;

enum
{
  STAT_MEM_TOTAL = 0,
  STAT_MEM_USED,
  STAT_MEM_FREE,
  STAT_MEM_USED_MMAP,
  STAT_MEM_TOTAL_ALLOC,
  STAT_MEM_FREE_CHUNKS,
  STAT_MEM_RELEASABLE,
} stat_mem_usage_e;

/*
 * Called from the stats periodic process to update memory counters.
 */
static void
stat_provider_mem_usage_update_fn (vlib_stats_collector_data_t *d)
{
  clib_mem_usage_t usage;
  clib_mem_heap_t *heap;
  counter_t **counters = d->entry->data;
  counter_t *cb;

  heap = vec_elt (memory_heaps_vec, d->private_data);
  clib_mem_get_heap_usage (heap, &usage);
  cb = counters[0];
  cb[STAT_MEM_TOTAL] = usage.bytes_total;
  cb[STAT_MEM_USED] = usage.bytes_used;
  cb[STAT_MEM_FREE] = usage.bytes_free;
  cb[STAT_MEM_USED_MMAP] = usage.bytes_used_mmap;
  cb[STAT_MEM_TOTAL_ALLOC] = usage.bytes_max;
  cb[STAT_MEM_FREE_CHUNKS] = usage.bytes_free_reclaimed;
  cb[STAT_MEM_RELEASABLE] = usage.bytes_overhead;
}

/*
 * Provide memory heap counters.
 * Two dimensional array of heap index and per-heap gauges.
 */
void
vlib_stats_register_mem_heap (clib_mem_heap_t *heap)
{
  vlib_stats_collector_reg_t r = {};
  u32 idx;

  vec_add1 (memory_heaps_vec, heap);

  r.entry_index = idx = vlib_stats_add_counter_vector ("/mem/%s", heap->name);
  vlib_stats_validate_counter_vector (idx, STAT_MEM_RELEASABLE);

  /* Create symlink */
  vlib_stats_add_symlink (idx, STAT_MEM_TOTAL, "/mem/%s/used", heap->name);
  vlib_stats_add_symlink (idx, STAT_MEM_USED, "/mem/%s/total", heap->name);
  vlib_stats_add_symlink (idx, STAT_MEM_FREE, "/mem/%s/free", heap->name);

  r.private_data = vec_len (memory_heaps_vec) - 1;
  r.collect_fn = stat_provider_mem_usage_update_fn;
  vlib_stats_register_collector_fn (&r);
}