]> git.proxmox.com Git - mirror_frr.git/blame - lib/memory.c
vtysh: fix incorrect memory statistics
[mirror_frr.git] / lib / memory.c
CommitLineData
3b4cd783
DL
1/*
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
3 *
db2d8df6
DL
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
3b4cd783 7 *
db2d8df6
DL
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3b4cd783
DL
15 */
16
17#include <zebra.h>
18
19#include <stdlib.h>
602a6584
DL
20#ifdef HAVE_MALLOC_H
21#include <malloc.h>
22#endif
324be174
DL
23#ifdef HAVE_MALLOC_NP_H
24#include <malloc_np.h>
25#endif
602a6584
DL
26#ifdef HAVE_MALLOC_MALLOC_H
27#include <malloc/malloc.h>
28#endif
3b4cd783
DL
29
30#include "memory.h"
f65e2d40 31#include "log.h"
912d45a1 32#include "libfrr_trace.h"
3b4cd783
DL
33
34static struct memgroup *mg_first = NULL;
35struct memgroup **mg_insert = &mg_first;
36
55c72803 37DEFINE_MGROUP(LIB, "libfrr")
4a1ab8e4
DL
38DEFINE_MTYPE(LIB, TMP, "Temporary memory")
39
602a6584 40static inline void mt_count_alloc(struct memtype *mt, size_t size, void *ptr)
3b4cd783 41{
13f1ba41 42 size_t current;
fa7fe831 43 size_t oldsize;
3b4cd783 44
7a153339
LB
45 current = 1 + atomic_fetch_add_explicit(&mt->n_alloc, 1,
46 memory_order_relaxed);
13f1ba41
LB
47
48 oldsize = atomic_load_explicit(&mt->n_max, memory_order_relaxed);
49 if (current > oldsize)
50 /* note that this may fail, but approximation is sufficient */
51 atomic_compare_exchange_weak_explicit(&mt->n_max, &oldsize,
52 current,
53 memory_order_relaxed,
54 memory_order_relaxed);
a31446a8 55
fa7fe831
DL
56 oldsize = atomic_load_explicit(&mt->size, memory_order_relaxed);
57 if (oldsize == 0)
d62a17ae 58 oldsize = atomic_exchange_explicit(&mt->size, size,
59 memory_order_relaxed);
fa7fe831 60 if (oldsize != 0 && oldsize != size && oldsize != SIZE_VAR)
d62a17ae 61 atomic_store_explicit(&mt->size, SIZE_VAR,
62 memory_order_relaxed);
602a6584
DL
63
64#ifdef HAVE_MALLOC_USABLE_SIZE
65 size_t mallocsz = malloc_usable_size(ptr);
66
7a153339
LB
67 current = mallocsz + atomic_fetch_add_explicit(&mt->total, mallocsz,
68 memory_order_relaxed);
13f1ba41
LB
69 oldsize = atomic_load_explicit(&mt->max_size, memory_order_relaxed);
70 if (current > oldsize)
71 /* note that this may fail, but approximation is sufficient */
72 atomic_compare_exchange_weak_explicit(&mt->max_size, &oldsize,
73 current,
74 memory_order_relaxed,
75 memory_order_relaxed);
602a6584 76#endif
3b4cd783
DL
77}
78
602a6584 79static inline void mt_count_free(struct memtype *mt, void *ptr)
3b4cd783 80{
c7bb4f00 81 frrtrace(2, frr_libfrr, memfree, mt, ptr);
d92658f4 82
fa7fe831
DL
83 assert(mt->n_alloc);
84 atomic_fetch_sub_explicit(&mt->n_alloc, 1, memory_order_relaxed);
602a6584
DL
85
86#ifdef HAVE_MALLOC_USABLE_SIZE
87 size_t mallocsz = malloc_usable_size(ptr);
88
89 atomic_fetch_sub_explicit(&mt->total, mallocsz, memory_order_relaxed);
90#endif
3b4cd783
DL
91}
92
fa7fe831 93static inline void *mt_checkalloc(struct memtype *mt, void *ptr, size_t size)
3b4cd783 94{
c7bb4f00 95 frrtrace(3, frr_libfrr, memalloc, mt, ptr, size);
d92658f4 96
fa7fe831 97 if (__builtin_expect(ptr == NULL, 0)) {
c897c456
CF
98 if (size) {
99 /* malloc(0) is allowed to return NULL */
100 memory_oom(size, mt->name);
101 }
fa7fe831
DL
102 return NULL;
103 }
602a6584 104 mt_count_alloc(mt, size, ptr);
fa7fe831 105 return ptr;
3b4cd783
DL
106}
107
fa7fe831 108void *qmalloc(struct memtype *mt, size_t size)
3b4cd783 109{
fa7fe831 110 return mt_checkalloc(mt, malloc(size), size);
3b4cd783
DL
111}
112
fa7fe831 113void *qcalloc(struct memtype *mt, size_t size)
3b4cd783 114{
fa7fe831 115 return mt_checkalloc(mt, calloc(size, 1), size);
3b4cd783
DL
116}
117
fa7fe831 118void *qrealloc(struct memtype *mt, void *ptr, size_t size)
3b4cd783 119{
fa7fe831 120 if (ptr)
602a6584 121 mt_count_free(mt, ptr);
fa7fe831 122 return mt_checkalloc(mt, ptr ? realloc(ptr, size) : malloc(size), size);
3b4cd783
DL
123}
124
fa7fe831 125void *qstrdup(struct memtype *mt, const char *str)
3b4cd783 126{
4ac99370 127 return str ? mt_checkalloc(mt, strdup(str), strlen(str) + 1) : NULL;
3b4cd783
DL
128}
129
6df43392
IR
130void qcountfree(struct memtype *mt, void *ptr)
131{
132 if (ptr)
133 mt_count_free(mt, ptr);
134}
135
fa7fe831 136void qfree(struct memtype *mt, void *ptr)
3b4cd783 137{
fa7fe831 138 if (ptr)
602a6584 139 mt_count_free(mt, ptr);
fa7fe831 140 free(ptr);
3b4cd783
DL
141}
142
fa7fe831 143int qmem_walk(qmem_walk_fn *func, void *arg)
3b4cd783 144{
fa7fe831
DL
145 struct memgroup *mg;
146 struct memtype *mt;
147 int rv;
148
149 for (mg = mg_first; mg; mg = mg->next) {
150 if ((rv = func(arg, mg, NULL)))
151 return rv;
152 for (mt = mg->types; mt; mt = mt->next)
153 if ((rv = func(arg, mg, mt)))
154 return rv;
155 }
156 return 0;
3b4cd783 157}
7a13c923 158
fa7fe831 159struct exit_dump_args {
9eed278b 160 FILE *fp;
fa7fe831
DL
161 const char *prefix;
162 int error;
7a13c923
DL
163};
164
fa7fe831 165static int qmem_exit_walker(void *arg, struct memgroup *mg, struct memtype *mt)
7a13c923 166{
fa7fe831
DL
167 struct exit_dump_args *eda = arg;
168
169 if (!mt) {
9eed278b 170 fprintf(eda->fp,
3efd0893 171 "%s: showing active allocations in memory group %s\n",
d62a17ae 172 eda->prefix, mg->name);
fa7fe831
DL
173
174 } else if (mt->n_alloc) {
175 char size[32];
767439c5
DL
176 if (!mg->active_at_exit)
177 eda->error++;
fa7fe831 178 snprintf(size, sizeof(size), "%10zu", mt->size);
9eed278b 179 fprintf(eda->fp, "%s: memstats: %-30s: %6zu * %s\n",
d62a17ae 180 eda->prefix, mt->name, mt->n_alloc,
181 mt->size == SIZE_VAR ? "(variably sized)" : size);
fa7fe831
DL
182 }
183 return 0;
7a13c923
DL
184}
185
9eed278b 186int log_memstats(FILE *fp, const char *prefix)
7a13c923 187{
996c9314 188 struct exit_dump_args eda = {.fp = fp, .prefix = prefix, .error = 0};
fa7fe831 189 qmem_walk(qmem_exit_walker, &eda);
9eed278b 190 return eda.error;
7a13c923 191}