Debian Codegraph @

c/src/core/memory.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "core/memory.h"
23
24 #ifdef PN_MEMDEBUG
25 #include "logger_private.h"
26
27 #include "proton/object.h"
28 #include "proton/cid.h"
29
30 #include <stdlib.h>
31
32 #include <signal.h>
33
34 // Non portable actual size of allocated block
35 // malloc_usable_size() for glibc
36 // _msize() for MSCRT
37 // malloc_size() for BSDs
38 #ifdef __GLIBC__
39 #include <malloc.h>
40 #define msize malloc_usable_size
41 #elif defined(_WIN32)
42 #include <malloc.h>
43 #define msize _msize
44 #elif defined(__APPLE__) || defined(__FreeBSD__)
45 #include <malloc/malloc.h>
46 #define msize malloc_size
47 #else
48 #define msize(x) (0)
49 #endif
50
51 static struct stats {
52 const char* name;
53 size_t count_alloc;
54 size_t count_dealloc;
55 size_t requested;
56 size_t alloc;
57 size_t dealloc;
58 size_t count_suballoc;
59 size_t count_subrealloc;
60 size_t count_subdealloc;
61 size_t subrequested;
62 size_t suballoc;
63 size_t subdealloc;
64 } stats[CID_pn_raw_connection+1] = {{0}}; // Just happens to be the last CID
65
66 static bool debug_memory = false;
67
68 // Hidden export
69 PN_EXTERN void pn_mem_dump_stats(void);
70
71 // Dump out memory use
72 void pn_mem_dump_stats(void)
73 {
74 pn_logger_t *logger = pn_default_logger();
75
76 size_t total_alloc = 0;
77 size_t total_dealloc = 0;
78 size_t count_alloc = 0;
79 size_t count_dealloc = 0;
80
81 if (PN_SHOULD_LOG(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_DEBUG)) {
82 pn_logger_logf(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_DEBUG, "Memory allocation stats:");
83 pn_logger_logf(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_DEBUG, "%15s: %5s %5s %5s %9s %9s",
84 "class", "alloc", "free", "rallc", "bytes alloc", "bytes free"
85 );
86 }
87 for (int i = 1; i<=CID_pn_listener_socket; i++) {
88 struct stats *entry = &stats[i];
89 count_alloc += entry->count_alloc+entry->count_suballoc;
90 count_dealloc += entry->count_dealloc+entry->count_subdealloc;
91 total_alloc += entry->alloc+entry->suballoc;
92 total_dealloc += entry->dealloc+entry->subdealloc;
93 if (entry->name && PN_SHOULD_LOG(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_DEBUG)) {
94 pn_logger_logf(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_DEBUG, "%15s: direct %5d %5d %9d %9d",
95 entry->name,
96 entry->count_alloc, entry->count_dealloc,
97 entry->alloc, entry->dealloc
98 );
99 pn_logger_logf(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_DEBUG, "%15s: indirect %5d %5d %5d %9d %9d",
100 entry->name,
101 entry->count_suballoc, entry->count_subdealloc, entry->count_subrealloc,
102 entry->suballoc, entry->subdealloc
103 );
104 }
105 }
106
107 if (!PN_SHOULD_LOG(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_INFO)) return;
108
109 pn_logger_logf(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_INFO, "%15s: %7d %5d %9d %9d",
110 "Totals", count_alloc, count_dealloc, total_alloc, total_dealloc
111 );
112 }
113
114 #ifdef _WIN32
115 void pni_mem_setup_logging(void)
116 {
117 }
118 #else
119 static void pn_mem_dump_stats_int(int i)
120 {
121 pn_logger_t *logger = pn_default_logger();
122 pn_logger_t save = *logger;
123
124 pn_logger_set_mask(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_DEBUG | PN_LEVEL_INFO);
125
126 pn_mem_dump_stats();
127
128 *logger = save;
129 }
130
131 void pni_mem_setup_logging(void)
132 {
133 debug_memory = true;
134 signal(SIGUSR1, pn_mem_dump_stats_int);
135 }
136 #endif
137
138 void pni_init_memory(void)
139 {
140 }
141
142 void pni_fini_memory(void)
143 {
144 pn_logger_t *logger = pn_default_logger();
145 pn_logger_t save = *logger;
146
147 if (debug_memory)
148 pn_logger_set_mask(logger, PN_SUBSYSTEM_MEMORY, PN_LEVEL_DEBUG | PN_LEVEL_INFO);
149
150 pn_mem_dump_stats();
151
152 *logger = save;
153 }
154
155 static inline struct stats *pni_track_common(const pn_class_t *clazz, void *o, size_t *size)
156 {
157 struct stats *entry = &stats[pn_class_id(clazz)];
158 if (!entry->name) {entry->name = pn_class_name(clazz);}
159 *size = msize(o);
160 return entry;
161 }
162
163 static void pni_track_alloc(const pn_class_t *clazz, void *o, size_t requested)
164 {
165 size_t size;
166 struct stats *entry = pni_track_common(clazz, o, &size);
167
168 entry->count_alloc++;
169 entry->alloc += size;
170 entry->requested += requested;
171 }
172
173 static void pni_track_dealloc(const pn_class_t *clazz, void *o)
174 {
175 size_t size;
176 struct stats *entry = pni_track_common(clazz, o, &size);
177
178 entry->count_dealloc++;
179 entry->dealloc += size;
180 }
181
182 static void pni_track_suballoc(const pn_class_t *clazz, void *o, size_t requested)
183 {
184 size_t size;
185 struct stats *entry = pni_track_common(clazz, o, &size);
186
187 entry->count_suballoc++;
188 entry->subrequested = requested;
189 entry->suballoc += size;
190 }
191
192 static void pni_track_subdealloc(const pn_class_t *clazz, void *o)
193 {
194 size_t size;
195 struct stats *entry = pni_track_common(clazz, o, &size);
196
197 entry->count_subdealloc++;
198 entry->subdealloc += size;
199 }
200
201 static void pni_track_subrealloc(const pn_class_t *clazz, void *o, size_t oldsize, size_t requested)
202 {
203 size_t size;
204 struct stats *entry = pni_track_common(clazz, o, &size);
205
206 if (oldsize) {
207 entry->count_subrealloc++;
208 } else {
209 entry->count_suballoc++;
210 }
211
212 int change = size - oldsize;
213 if (change > 0) entry->suballoc += change;
214 if (change < 0) entry->subdealloc += -change;
215 }
216
217 void *pni_mem_zallocate(const pn_class_t *clazz, size_t size)
218 {
219 void *o = calloc(1, size);
220 pni_track_alloc(clazz, o, size);
221 return o;
222 }
223
224 void *pni_mem_allocate(const pn_class_t *clazz, size_t size)
225 {
226 void * o = malloc(size);
227 pni_track_alloc(clazz, o, size);
228 return o;
229 }
230
231 void pni_mem_deallocate(const pn_class_t *clazz, void *object)
232 {
233 if (!object) return;
234 pni_track_dealloc(clazz, object);
235 free(object);
236 }
237
238 void *pni_mem_suballocate(const pn_class_t *clazz, void *object, size_t size)
239 {
240 void * o = malloc(size);
241 pni_track_suballoc(clazz, o, size);
242 return o;
243 }
244
245 void *pni_mem_subreallocate(const pn_class_t *clazz, void *object, void *buffer, size_t size)
246 {
247 size_t oldsize = buffer ? msize(buffer) : 0;
248 void *o = realloc(buffer, size);
249 pni_track_subrealloc(clazz, o, oldsize, size);
250 return o;
251 }
252
253 void pni_mem_subdeallocate(const pn_class_t *clazz, void *object, void *buffer)
254 {
255 if (!buffer) return;
256 pni_track_subdealloc(clazz, buffer);
257 free(buffer);
258 }
259 #else
260
261 // Versions with no memory debugging - so we can compile with no performance penalty
262
263 #include <stdlib.h>
264
265 void pni_init_memory(void) {}
266 void pni_fini_memory(void) {}
267
268 void pni_mem_setup_logging(void) {}
269
270 void *pni_mem_allocate(const pn_class_t *clazz, size_t size) { return malloc(size); }
271 void *pni_mem_zallocate(const pn_class_t *clazz, size_t size) { return calloc(1, size); }
272 void pni_mem_deallocate(const pn_class_t *clazz, void *object) { free(object); }
273
274 void *pni_mem_suballocate(const pn_class_t *clazz, void *object, size_t size) { return malloc(size); }
275 void *pni_mem_subreallocate(const pn_class_t *clazz, void *object, void *buffer, size_t size) { return realloc(buffer, size); }
276 void pni_mem_subdeallocate(const pn_class_t *clazz, void *object, void *buffer) { free(buffer); }
277
278 #endif