Browse code

mem: tlsf_malloc extended for per module memory stats

Daniel-Constantin Mierla authored on 02/11/2015 12:49:04
Showing 2 changed files
... ...
@@ -108,6 +108,7 @@ tlsf_static_assert(ALIGN_SIZE == SMALL_BLOCK_SIZE / SL_INDEX_COUNT);
108 108
 typedef struct {
109 109
 	const char* file;
110 110
 	const char* func;
111
+	const char* mname;
111 112
 	unsigned int line;
112 113
 } alloc_info_t;
113 114
 #endif
... ...
@@ -598,7 +599,7 @@ static block_header_t* block_locate_free(control_t* control, size_t size)
598 599
 }
599 600
 #ifdef DBG_TLSF_MALLOC
600 601
 static void* block_prepare_used(control_t* control, block_header_t* block, size_t size,
601
-		const char *file, const char *function, unsigned long line)
602
+		const char *file, const char *function, unsigned long line, const char *mname)
602 603
 #else
603 604
 static void* block_prepare_used(control_t* control, block_header_t* block, size_t size)
604 605
 #endif
... ...
@@ -617,6 +618,7 @@ static void* block_prepare_used(control_t* control, block_header_t* block, size_
617 618
 		block->alloc_info.file = file;
618 619
 		block->alloc_info.func = function;
619 620
 		block->alloc_info.line = line;
621
+		block->alloc_info.mname = mname;
620 622
 #endif
621 623
 	}
622 624
 	return p;
... ...
@@ -952,7 +954,7 @@ pool_t tlsf_get_pool(tlsf_t tlsf)
952 954
 
953 955
 #ifdef DBG_TLSF_MALLOC
954 956
 void* tlsf_malloc(tlsf_t tlsf, size_t size,
955
-		const char *file, const char *function, unsigned int line)
957
+		const char *file, const char *function, unsigned int line, const char *mname)
956 958
 #else
957 959
 void* tlsf_malloc(tlsf_t tlsf, size_t size)
958 960
 #endif
... ...
@@ -964,7 +966,7 @@ void* tlsf_malloc(tlsf_t tlsf, size_t size)
964 966
 	void *ptr;
965 967
 
966 968
 	MDBG("tlsf_malloc(%p, %zu) called from %s: %s(%u)\n", tlsf, size, file, function, line);
967
-	ptr = block_prepare_used(control, block, adjust, file, function, line);
969
+	ptr = block_prepare_used(control, block, adjust, file, function, line, mname);
968 970
 	MDBG("tlsf_malloc(%p, %zu) returns address %p \n", tlsf, size,
969 971
 		ptr);
970 972
 	return ptr;
... ...
@@ -975,7 +977,7 @@ void* tlsf_malloc(tlsf_t tlsf, size_t size)
975 977
 
976 978
 #ifdef DBG_TLSF_MALLOC
977 979
 void tlsf_free(tlsf_t tlsf, void* ptr,
978
-		const char *file, const char *function, unsigned int line)
980
+		const char *file, const char *function, unsigned int line, const char *mname)
979 981
 #else
980 982
 void tlsf_free(tlsf_t tlsf, void* ptr)
981 983
 #endif
... ...
@@ -1013,6 +1015,7 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
1013 1015
 		block->alloc_info.file = file;
1014 1016
 		block->alloc_info.func = function;
1015 1017
 		block->alloc_info.line = line;
1018
+		block->alloc_info.mname = mname;
1016 1019
 #endif
1017 1020
 		block_mark_as_free(block);
1018 1021
 		block = block_merge_prev(control, block);
... ...
@@ -1042,7 +1045,7 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
1042 1045
 */
1043 1046
 #ifdef DBG_TLSF_MALLOC
1044 1047
 void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size,
1045
-		const char *file, const char *function, unsigned int line)
1048
+		const char *file, const char *function, unsigned int line, const char *mname)
1046 1049
 #else
1047 1050
 void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
1048 1051
 #endif
... ...
@@ -1054,7 +1057,7 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
1054 1057
 	if (ptr && size == 0)
1055 1058
 	{
1056 1059
 #ifdef DBG_TLSF_MALLOC
1057
-		tlsf_free(tlsf, ptr, file, function, line);
1060
+		tlsf_free(tlsf, ptr, file, function, line, mname);
1058 1061
 #else
1059 1062
 		tlsf_free(tlsf, ptr);
1060 1063
 #endif
... ...
@@ -1063,7 +1066,7 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
1063 1066
 	else if (!ptr)
1064 1067
 	{
1065 1068
 #ifdef DBG_TLSF_MALLOC
1066
-		p = tlsf_malloc(tlsf, size, file, function, line);
1069
+		p = tlsf_malloc(tlsf, size, file, function, line, mname);
1067 1070
 #else
1068 1071
 		p = tlsf_malloc(tlsf, size);
1069 1072
 #endif
... ...
@@ -1086,7 +1089,7 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
1086 1089
 		if (adjust > cursize && (!block_is_free(next) || adjust > combined))
1087 1090
 		{
1088 1091
 #ifdef DBG_TLSF_MALLOC
1089
-			p = tlsf_malloc(tlsf, size, file, function, line);
1092
+			p = tlsf_malloc(tlsf, size, file, function, line, mname);
1090 1093
 #else
1091 1094
 			p = tlsf_malloc(tlsf, size);
1092 1095
 #endif
... ...
@@ -1095,7 +1098,7 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
1095 1098
 				const size_t minsize = tlsf_min(cursize, size);
1096 1099
 				memcpy(p, ptr, minsize);
1097 1100
 #ifdef DBG_TLSF_MALLOC
1098
-				tlsf_free(tlsf, ptr, file, function, line);
1101
+				tlsf_free(tlsf, ptr, file, function, line, mname);
1099 1102
 #else
1100 1103
 				tlsf_free(tlsf, ptr);
1101 1104
 #endif
... ...
@@ -1189,16 +1192,6 @@ void tlsf_status(tlsf_t pool)
1189 1192
 }
1190 1193
 
1191 1194
 #ifdef DBG_TLSF_MALLOC
1192
-typedef struct _mem_counter{
1193
-	const char *file;
1194
-	const char *func;
1195
-	unsigned long line;
1196
-
1197
-	unsigned long size;
1198
-	int count;
1199
-
1200
-	struct _mem_counter *next;
1201
-} mem_counter;
1202 1195
 
1203 1196
 static mem_counter* get_mem_counter(mem_counter **root, block_header_t* f)
1204 1197
 {
... ...
@@ -1211,6 +1204,7 @@ make_new:
1211 1204
 	x = malloc(sizeof(mem_counter));
1212 1205
 	x->file = f->alloc_info.file;
1213 1206
 	x->func = f->alloc_info.func;
1207
+	x->mname= f->alloc_info.mname;
1214 1208
 	x->line = f->alloc_info.line;
1215 1209
 	x->count = 0;
1216 1210
 	x->size = 0;
... ...
@@ -1257,13 +1251,68 @@ void tlsf_sums(tlsf_t pool)
1257 1251
 	LOG_(DEFAULT_FACILITY, memlog, "tlsf_sums: ",
1258 1252
 			"-----------------------------\n");
1259 1253
 }
1254
+
1255
+void tlsf_mod_get_stats(tlsf_t pool, void **rootp)
1256
+{
1257
+	if (!rootp) {
1258
+		return ;
1259
+	}
1260
+
1261
+	LM_DBG("get tlsf memory statistics\n");
1262
+
1263
+	mem_counter **root = (mem_counter **) rootp;
1264
+	block_header_t* block = pool + tlsf_size() - sizeof(block_header_t*);
1265
+	mem_counter *x;
1266
+
1267
+	while (block && !block_is_last(block))
1268
+	{
1269
+		if(!block_is_free(block)) {
1270
+			x = get_mem_counter(root, block);
1271
+			x->count++;
1272
+			x->size+=block_size(block);
1273
+		}
1274
+
1275
+		block = block_next(block);
1276
+	}
1277
+}
1278
+
1279
+void tlsf_mod_free_stats(void *rootp)
1280
+{
1281
+	if (!rootp) {
1282
+		return ;
1283
+	}
1284
+
1285
+	LM_DBG("free tlsf memory statistics\n");
1286
+
1287
+	mem_counter *root = (mem_counter *) rootp;
1288
+	mem_counter *x;
1289
+	x = root;
1290
+	while (x) {
1291
+		root = x->next;
1292
+		free(x);
1293
+		x = root;
1294
+	}
1295
+}
1296
+
1260 1297
 #else
1261 1298
 void tlsf_sums(tlsf_t pool)
1262 1299
 {}
1300
+
1301
+void tlsf_mod_get_stats(tlsf_t pool, void **rootp)
1302
+{
1303
+	LM_WARN("Enable DBG_TLSF_MALLOC for getting statistics\n");
1304
+	return ;
1305
+}
1306
+
1307
+void tlsf_mod_free_stats(void *rootp)
1308
+{
1309
+	LM_WARN("Enable DBG_TLSF_MALLOC for freeing statistics\n");
1310
+	return ;
1311
+}
1263 1312
 #endif /* defined DBG_TLSF_MALLOC */
1264 1313
 
1265 1314
 /*memory manager core api*/
1266
-static char *_tlsf_mem_name = "f_malloc";
1315
+static char *_tlsf_mem_name = "tlsf_malloc";
1267 1316
 
1268 1317
 /* PKG - private memory API*/
1269 1318
 static void *_tlsf_pkg_pool = 0;
... ...
@@ -1309,6 +1358,8 @@ int tlsf_malloc_init_pkg_manager(void)
1309 1358
 	ma.xavailable = tlsf_available;
1310 1359
 	ma.xsums      = tlsf_sums;
1311 1360
 	ma.xdestroy   = tlsf_malloc_destroy_pkg_manager;
1361
+	ma.xstats     = tlsf_mod_get_stats;
1362
+	ma.xfstats    = tlsf_mod_free_stats;
1312 1363
 
1313 1364
 	return pkg_init_api(&ma);
1314 1365
 }
... ...
@@ -1320,38 +1371,38 @@ static tlsf_t _tlsf_shm_block = 0;
1320 1371
 /*SHM wrappers to sync the access to memory block*/
1321 1372
 #ifdef DBG_TLSF_MALLOC
1322 1373
 void* tlsf_shm_malloc(void* tlsfmp, unsigned long size,
1323
-					const char* file, const char* func, unsigned int line)
1374
+					const char* file, const char* func, unsigned int line, const char* mname)
1324 1375
 {
1325 1376
 	void *r;
1326 1377
 	shm_lock();
1327
-	r = tlsf_malloc(tlsfmp, size, file, func, line);
1378
+	r = tlsf_malloc(tlsfmp, size, file, func, line, mname);
1328 1379
 	shm_unlock();
1329 1380
 	return r;
1330 1381
 }
1331 1382
 void* tlsf_shm_realloc(void* tlsfmp, void* p, unsigned long size,
1332
-					const char* file, const char* func, unsigned int line)
1383
+					const char* file, const char* func, unsigned int line, const char* mname)
1333 1384
 {
1334 1385
 	void *r;
1335 1386
 	shm_lock();
1336
-	r = tlsf_realloc(tlsfmp, p, size, file, func, line);
1387
+	r = tlsf_realloc(tlsfmp, p, size, file, func, line, mname);
1337 1388
 	shm_unlock();
1338 1389
 	return r;
1339 1390
 }
1340 1391
 void* tlsf_shm_resize(void* tlsfmp, void* p, unsigned long size,
1341
-					const char* file, const char* func, unsigned int line)
1392
+					const char* file, const char* func, unsigned int line, const char* mname)
1342 1393
 {
1343 1394
 	void *r;
1344 1395
 	shm_lock();
1345
-	if(p) tlsf_free(tlsfmp, p, file, func, line);
1346
-	r = tlsf_malloc(tlsfmp, size, file, func, line);
1396
+	if(p) tlsf_free(tlsfmp, p, file, func, line, mname);
1397
+	r = tlsf_malloc(tlsfmp, size, file, func, line, mname);
1347 1398
 	shm_unlock();
1348 1399
 	return r;
1349 1400
 }
1350 1401
 void tlsf_shm_free(void* tlsfmp, void* p, const char* file, const char* func,
1351
-				unsigned int line)
1402
+				unsigned int line, const char* mname)
1352 1403
 {
1353 1404
 	shm_lock();
1354
-	tlsf_free(tlsfmp, p, file, func, line);
1405
+	tlsf_free(tlsfmp, p, file, func, line, mname);
1355 1406
 	shm_unlock();
1356 1407
 }
1357 1408
 #else
... ...
@@ -1457,6 +1508,8 @@ int tlsf_malloc_init_shm_manager(void)
1457 1508
 	ma.xavailable     = tlsf_shm_available;
1458 1509
 	ma.xsums          = tlsf_shm_sums;
1459 1510
 	ma.xdestroy       = tlsf_malloc_destroy_shm_manager;
1511
+	ma.xstats         = tlsf_mod_get_stats;
1512
+	ma.xfstats        = tlsf_mod_free_stats;
1460 1513
 
1461 1514
 	if(shm_init_api(&ma)<0) {
1462 1515
 		LM_ERR("cannot initialize the core shm api\n");
... ...
@@ -47,11 +47,11 @@ void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
47 47
 /* malloc/memalign/realloc/free replacements. */
48 48
 #ifdef DBG_TLSF_MALLOC
49 49
 void* tlsf_malloc(tlsf_t tlsf, size_t size,
50
-		const char *file, const char *function, unsigned int line);
50
+		const char *file, const char *function, unsigned int line, const char *mname);
51 51
 void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size,
52
-		const char *file, const char *function, unsigned int line);
52
+		const char *file, const char *function, unsigned int line, const char *mname);
53 53
 void tlsf_free(tlsf_t tlsf, void* ptr,
54
-		const char *file, const char *function, unsigned int line);
54
+		const char *file, const char *function, unsigned int line, const char *mname);
55 55
 #else
56 56
 void* tlsf_malloc(tlsf_t tlsf, size_t bytes);
57 57
 void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
... ...
@@ -80,6 +80,20 @@ void tlsf_meminfo(tlsf_t pool, struct mem_info *info);
80 80
 void tlsf_status(tlsf_t pool);
81 81
 void tlsf_sums(tlsf_t pool);
82 82
 size_t tlsf_available(tlsf_t pool);
83
+void tlsf_mod_get_stats(tlsf_t pool, void **root);
84
+void tlsf_mod_free_stats(void *root);
85
+
86
+typedef struct _mem_counter{
87
+	const char *file;
88
+	const char *func;
89
+	const char *mname;
90
+	unsigned long line;
91
+
92
+	unsigned long size;
93
+	int count;
94
+
95
+	struct _mem_counter *next;
96
+} mem_counter;
83 97
 
84 98
 #if defined(__cplusplus)
85 99
 };