Skip to content

Commit

Permalink
Merge pull request #2859 from LabNConsulting/working/master/meminfo
Browse files Browse the repository at this point in the history
lib: qmem show changes (header and max)
  • Loading branch information
eqvinox authored Aug 30, 2018
2 parents f177317 + 7a15333 commit 66a9aa8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
22 changes: 20 additions & 2 deletions lib/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@ DEFINE_MTYPE(LIB, PREFIX_FLOWSPEC, "Prefix Flowspec")

static inline void mt_count_alloc(struct memtype *mt, size_t size, void *ptr)
{
size_t current;
size_t oldsize;

atomic_fetch_add_explicit(&mt->n_alloc, 1, memory_order_relaxed);
current = 1 + atomic_fetch_add_explicit(&mt->n_alloc, 1,
memory_order_relaxed);

oldsize = atomic_load_explicit(&mt->n_max, memory_order_relaxed);
if (current > oldsize)
/* note that this may fail, but approximation is sufficient */
atomic_compare_exchange_weak_explicit(&mt->n_max, &oldsize,
current,
memory_order_relaxed,
memory_order_relaxed);

oldsize = atomic_load_explicit(&mt->size, memory_order_relaxed);
if (oldsize == 0)
Expand All @@ -51,7 +61,15 @@ static inline void mt_count_alloc(struct memtype *mt, size_t size, void *ptr)
#ifdef HAVE_MALLOC_USABLE_SIZE
size_t mallocsz = malloc_usable_size(ptr);

atomic_fetch_add_explicit(&mt->total, mallocsz, memory_order_relaxed);
current = mallocsz + atomic_fetch_add_explicit(&mt->total, mallocsz,
memory_order_relaxed);
oldsize = atomic_load_explicit(&mt->max_size, memory_order_relaxed);
if (current > oldsize)
/* note that this may fail, but approximation is sufficient */
atomic_compare_exchange_weak_explicit(&mt->max_size, &oldsize,
current,
memory_order_relaxed,
memory_order_relaxed);
#endif
}

Expand Down
2 changes: 2 additions & 0 deletions lib/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ struct memtype {
struct memtype *next, **ref;
const char *name;
_Atomic size_t n_alloc;
_Atomic size_t n_max;
_Atomic size_t size;
#ifdef HAVE_MALLOC_USABLE_SIZE
_Atomic size_t total;
_Atomic size_t max_size;
#endif
};

Expand Down
30 changes: 24 additions & 6 deletions lib/memory_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,45 @@ static int show_memory_mallinfo(struct vty *vty)
static int qmem_walker(void *arg, struct memgroup *mg, struct memtype *mt)
{
struct vty *vty = arg;
if (!mt)
if (!mt) {
vty_out(vty, "--- qmem %s ---\n", mg->name);
else {
vty_out(vty, "%-30s: %8s %-8s%s %8s %9s\n",
"Type", "Current#", " Size",
#ifdef HAVE_MALLOC_USABLE_SIZE
" Total",
#else
"",
#endif
"Max#",
#ifdef HAVE_MALLOC_USABLE_SIZE
"MaxBytes"
#else
""
#endif
);
} else {
if (mt->n_alloc != 0) {
char size[32];
snprintf(size, sizeof(size), "%6zu", mt->size);

#ifdef HAVE_MALLOC_USABLE_SIZE
#define TSTR " %9zu"
#define TARG , mt->total
#define TARG2 , mt->max_size
#else
#define TSTR ""
#define TARG
#define TARG2
#endif
vty_out(vty, "%-30s: %10zu %-16s"TSTR"\n", mt->name,
vty_out(vty, "%-30s: %8zu %-8s"TSTR" %8zu"TSTR"\n",
mt->name,
mt->n_alloc,
mt->size == 0 ? ""
: mt->size == SIZE_VAR
? "(variably sized)"
? "variable"
: size
TARG);
TARG,
mt->n_max
TARG2);
}
}
return 0;
Expand Down

0 comments on commit 66a9aa8

Please sign in to comment.