]> git.proxmox.com Git - mirror_frr.git/blame - lib/memory.c
lib: add tracepoint for route table get
[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"
d92658f4 32#include "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{
d92658f4
QY
81 tracepoint(frr_libfrr, memfree, mt, ptr);
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{
d92658f4
QY
95 tracepoint(frr_libfrr, memalloc, mt, ptr, size);
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
fa7fe831 130void qfree(struct memtype *mt, void *ptr)
3b4cd783 131{
fa7fe831 132 if (ptr)
602a6584 133 mt_count_free(mt, ptr);
fa7fe831 134 free(ptr);
3b4cd783
DL
135}
136
fa7fe831 137int qmem_walk(qmem_walk_fn *func, void *arg)
3b4cd783 138{
fa7fe831
DL
139 struct memgroup *mg;
140 struct memtype *mt;
141 int rv;
142
143 for (mg = mg_first; mg; mg = mg->next) {
144 if ((rv = func(arg, mg, NULL)))
145 return rv;
146 for (mt = mg->types; mt; mt = mt->next)
147 if ((rv = func(arg, mg, mt)))
148 return rv;
149 }
150 return 0;
3b4cd783 151}
7a13c923 152
fa7fe831 153struct exit_dump_args {
9eed278b 154 FILE *fp;
fa7fe831
DL
155 const char *prefix;
156 int error;
7a13c923
DL
157};
158
fa7fe831 159static int qmem_exit_walker(void *arg, struct memgroup *mg, struct memtype *mt)
7a13c923 160{
fa7fe831
DL
161 struct exit_dump_args *eda = arg;
162
163 if (!mt) {
9eed278b 164 fprintf(eda->fp,
3efd0893 165 "%s: showing active allocations in memory group %s\n",
d62a17ae 166 eda->prefix, mg->name);
fa7fe831
DL
167
168 } else if (mt->n_alloc) {
169 char size[32];
767439c5
DL
170 if (!mg->active_at_exit)
171 eda->error++;
fa7fe831 172 snprintf(size, sizeof(size), "%10zu", mt->size);
9eed278b 173 fprintf(eda->fp, "%s: memstats: %-30s: %6zu * %s\n",
d62a17ae 174 eda->prefix, mt->name, mt->n_alloc,
175 mt->size == SIZE_VAR ? "(variably sized)" : size);
fa7fe831
DL
176 }
177 return 0;
7a13c923
DL
178}
179
9eed278b 180int log_memstats(FILE *fp, const char *prefix)
7a13c923 181{
996c9314 182 struct exit_dump_args eda = {.fp = fp, .prefix = prefix, .error = 0};
fa7fe831 183 qmem_walk(qmem_exit_walker, &eda);
9eed278b 184 return eda.error;
7a13c923 185}