Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: qmem show changes (header and max) #2859

Merged
merged 2 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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