Browse code

mem: f_malloc extended for per module memory stats

Stefan Mititelu authored on 02/11/2015 12:48:28 • Daniel-Constantin Mierla committed on 02/11/2015 12:48:28
Showing 2 changed files
... ...
@@ -265,7 +265,8 @@ static inline
265 265
 #ifdef DBG_F_MALLOC 
266 266
 void fm_split_frag(struct fm_block* qm, struct fm_frag* frag,
267 267
 					unsigned long size,
268
-					const char* file, const char* func, unsigned int line)
268
+					const char* file, const char* func, unsigned int line,
269
+					const char* mname)
269 270
 #else
270 271
 void fm_split_frag(struct fm_block* qm, struct fm_frag* frag,
271 272
 					unsigned long size)
... ...
@@ -291,6 +292,7 @@ void fm_split_frag(struct fm_block* qm, struct fm_frag* frag,
291 292
 		n->file=file;
292 293
 		n->func="frag. from fm_split_frag";
293 294
 		n->line=line;
295
+		n->mname=mname;
294 296
 #endif
295 297
 		n->check=ST_CHECK_PATTERN;
296 298
 		/* reinsert n in free list*/
... ...
@@ -419,8 +421,8 @@ struct fm_frag* fm_search_defrag(struct fm_block* qm, unsigned long size)
419 421
  * \return address of allocated memory
420 422
  */
421 423
 #ifdef DBG_F_MALLOC
422
-void* fm_malloc(void* qmp, unsigned long size,
423
-					const char* file, const char* func, unsigned int line)
424
+void* fm_malloc(void* qmp, unsigned long size, const char* file,
425
+                    const char* func, unsigned int line, const char* mname)
424 426
 #else
425 427
 void* fm_malloc(void* qmp, unsigned long size)
426 428
 #endif
... ...
@@ -496,7 +498,7 @@ found:
496 498
 
497 499
 	/*see if use full frag or split it in two*/
498 500
 #ifdef DBG_F_MALLOC
499
-	fm_split_frag(qm, frag, size, file, func, line);
501
+	fm_split_frag(qm, frag, size, file, func, line, mname);
500 502
 #else
501 503
 	fm_split_frag(qm, frag, size);
502 504
 #endif
... ...
@@ -506,6 +508,7 @@ finish:
506 508
 #ifdef DBG_F_MALLOC
507 509
 	frag->file=file;
508 510
 	frag->func=func;
511
+	frag->mname=mname;
509 512
 	frag->line=line;
510 513
 	MDBG("fm_malloc(%p, %lu) returns address %p \n", qm, size,
511 514
 		(char*)frag+sizeof(struct fm_frag));
... ...
@@ -557,8 +560,8 @@ static void fm_join_frag(struct fm_block* qm, struct fm_frag* f)
557 560
  * \param p freed memory
558 561
  */
559 562
 #ifdef DBG_F_MALLOC
560
-void fm_free(void* qmp, void* p, const char* file, const char* func, 
561
-				unsigned int line)
563
+void fm_free(void* qmp, void* p, const char* file, const char* func,
564
+				unsigned int line, const char* mname)
562 565
 #else
563 566
 void fm_free(void* qmp, void* p)
564 567
 #endif
... ...
@@ -602,6 +605,7 @@ void fm_free(void* qmp, void* p)
602 605
 	f->file=file;
603 606
 	f->func=func;
604 607
 	f->line=line;
608
+	f->mname=mname;
605 609
 #endif
606 610
 #ifdef MEM_JOIN_FREE
607 611
 	if(unlikely(cfg_get(core, core_cfg, mem_join)!=0))
... ...
@@ -622,7 +626,8 @@ void fm_free(void* qmp, void* p)
622 626
  */
623 627
 #ifdef DBG_F_MALLOC
624 628
 void* fm_realloc(void* qmp, void* p, unsigned long size,
625
-					const char* file, const char* func, unsigned int line)
629
+					const char* file, const char* func, unsigned int line,
630
+					const char *mname)
626 631
 #else
627 632
 void* fm_realloc(void* qmp, void* p, unsigned long size)
628 633
 #endif
... ...
@@ -648,7 +653,7 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
648 653
 	if (size==0) {
649 654
 		if (p)
650 655
 #ifdef DBG_F_MALLOC
651
-			fm_free(qm, p, file, func, line);
656
+			fm_free(qm, p, file, func, line, mname);
652 657
 #else
653 658
 			fm_free(qm, p);
654 659
 #endif
... ...
@@ -656,7 +661,7 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
656 661
 	}
657 662
 	if (p==0)
658 663
 #ifdef DBG_F_MALLOC
659
-		return fm_malloc(qm, size, file, func, line);
664
+		return fm_malloc(qm, size, file, func, line, mname);
660 665
 #else
661 666
 		return fm_malloc(qm, size);
662 667
 #endif
... ...
@@ -671,7 +676,7 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
671 676
 		/* shrink */
672 677
 #ifdef DBG_F_MALLOC
673 678
 		MDBG("fm_realloc: shrinking from %lu to %lu\n", f->size, size);
674
-		fm_split_frag(qm, f, size, file, "frag. from fm_realloc", line);
679
+		fm_split_frag(qm, f, size, file, "frag. from fm_realloc", line, mname);
675 680
 #else
676 681
 		fm_split_frag(qm, f, size);
677 682
 #endif
... ...
@@ -695,7 +700,7 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
695 700
 			if (f->size > size){
696 701
 		#ifdef DBG_F_MALLOC
697 702
 				fm_split_frag(qm, f, size, file, "fragm. from fm_realloc",
698
-						line);
703
+						line, mname);
699 704
 		#else
700 705
 				fm_split_frag(qm, f, size);
701 706
 		#endif
... ...
@@ -703,7 +708,7 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
703 708
 		}else{
704 709
 			/* could not join => realloc */
705 710
 	#ifdef DBG_F_MALLOC
706
-			ptr=fm_malloc(qm, size, file, func, line);
711
+			ptr=fm_malloc(qm, size, file, func, line, mname);
707 712
 	#else
708 713
 			ptr=fm_malloc(qm, size);
709 714
 	#endif
... ...
@@ -712,7 +717,7 @@ void* fm_realloc(void* qmp, void* p, unsigned long size)
712 717
 				memcpy(ptr, p, orig_size);
713 718
 			}
714 719
 	#ifdef DBG_F_MALLOC
715
-			fm_free(qm, p, file, func, line);
720
+			fm_free(qm, p, file, func, line, mname);
716 721
 	#else
717 722
 			fm_free(qm, p);
718 723
 	#endif
... ...
@@ -873,17 +878,6 @@ unsigned long fm_available(void* qmp)
873 878
 
874 879
 #ifdef DBG_F_MALLOC
875 880
 
876
-typedef struct _mem_counter{
877
-	const char *file;
878
-	const char *func;
879
-	unsigned long line;
880
-	
881
-	unsigned long size;
882
-	int count;
883
-	
884
-	struct _mem_counter *next;
885
-} mem_counter;
886
-
887 881
 static mem_counter* get_mem_counter(mem_counter **root,struct fm_frag* f)
888 882
 {
889 883
 	mem_counter *x;
... ...
@@ -897,6 +891,7 @@ make_new:
897 891
 	x->file = f->file;
898 892
 	x->func = f->func;
899 893
 	x->line = f->line;
894
+	x->mname = f->mname;
900 895
 	x->count = 0;
901 896
 	x->size = 0;
902 897
 	x->next = *root;
... ...
@@ -947,9 +942,60 @@ void fm_sums(void* qmp)
947 942
 	}
948 943
 	LOG_(DEFAULT_FACILITY, memlog, "fm_status: ",
949 944
 			"-----------------------------\n");
945
+
946
+}
947
+
948
+
949
+void fm_mod_get_stats(void *qmp, void **fm_rootp)
950
+{
951
+	if (!fm_rootp) {
952
+		return ;
953
+	}
954
+
955
+	LM_DBG("get fm memory statistics\n");
956
+
957
+	struct fm_block *qm = (struct fm_block *) qmp;
958
+	mem_counter **fm_root = (mem_counter **) fm_rootp;
959
+	struct fm_frag* f;
960
+	int i;
961
+	mem_counter *x;
962
+
963
+	if (!qm) return ;
964
+
965
+	/* update fragment detail list */
966
+	for (f=qm->first_frag, i=0; (char*)f<(char*)qm->last_frag;
967
+			f=FRAG_NEXT(f), i++){
968
+		if (f->is_free==0){
969
+			x = get_mem_counter(fm_root,f);
970
+			x->count++;
971
+			x->size+=f->size;
972
+		}
973
+	}
974
+
975
+    return ;
976
+}
977
+
978
+void fm_mod_free_stats(void *fm_rootp)
979
+{
980
+	if (!fm_rootp) {
981
+		return ;
982
+	}
983
+
984
+	LM_DBG("free fm memory statistics\n");
985
+
986
+	mem_counter *root = (mem_counter *) fm_rootp;
987
+	mem_counter *new, *old;
988
+	new = root;
989
+	old = root;
990
+
991
+	while (new) {
992
+		old = new;
993
+		new = new->next;
994
+		free(old);
995
+	}
950 996
 }
951 997
 #else
952
-void fm_sums(void* qmp)
998
+void fm_sums(void *qmp)
953 999
 {
954 1000
 	struct fm_block* qm;
955 1001
 	int memlog;
... ...
@@ -959,6 +1005,18 @@ void fm_sums(void* qmp)
959 1005
 	LOG_(DEFAULT_FACILITY, memlog, "fm_sums: ", "not available (%p)\n", qm);
960 1006
 	return;
961 1007
 }
1008
+
1009
+void fm_mod_get_stats(void *qmp, void **fm_rootp)
1010
+{
1011
+	LM_WARN("Enable DBG_F_MALLOC for getting statistics\n");
1012
+	return ;
1013
+}
1014
+
1015
+void fm_mod_free_stats(void *fm_rootp)
1016
+{
1017
+	LM_WARN("Enable DBG_F_MALLOC for freeing statistics\n");
1018
+	return ;
1019
+}
962 1020
 #endif /* DBG_F_MALLOC */
963 1021
 
964 1022
 
... ...
@@ -1009,6 +1067,8 @@ int fm_malloc_init_pkg_manager(void)
1009 1067
 	ma.xavailable = fm_available;
1010 1068
 	ma.xsums      = fm_sums;
1011 1069
 	ma.xdestroy   = fm_malloc_destroy_pkg_manager;
1070
+	ma.xstats     = fm_mod_get_stats;
1071
+	ma.xfstats    = fm_mod_free_stats;
1012 1072
 
1013 1073
 	return pkg_init_api(&ma);
1014 1074
 }
... ...
@@ -1021,38 +1081,38 @@ static struct fm_block *_fm_shm_block = 0;
1021 1081
 /*SHM wrappers to sync the access to memory block*/
1022 1082
 #ifdef DBG_F_MALLOC
1023 1083
 void* fm_shm_malloc(void* qmp, unsigned long size,
1024
-					const char* file, const char* func, unsigned int line)
1084
+					const char* file, const char* func, unsigned int line, const char* mname)
1025 1085
 {
1026 1086
 	void *r;
1027 1087
 	shm_lock();
1028
-	r = fm_malloc(qmp, size, file, func, line);
1088
+	r = fm_malloc(qmp, size, file, func, line, mname);
1029 1089
 	shm_unlock();
1030 1090
 	return r;
1031 1091
 }
1032 1092
 void* fm_shm_realloc(void* qmp, void* p, unsigned long size,
1033
-					const char* file, const char* func, unsigned int line)
1093
+					const char* file, const char* func, unsigned int line, const char* mname)
1034 1094
 {
1035 1095
 	void *r;
1036 1096
 	shm_lock();
1037
-	r = fm_realloc(qmp, p, size, file, func, line);
1097
+	r = fm_realloc(qmp, p, size, file, func, line, mname);
1038 1098
 	shm_unlock();
1039 1099
 	return r;
1040 1100
 }
1041 1101
 void* fm_shm_resize(void* qmp, void* p, unsigned long size,
1042
-					const char* file, const char* func, unsigned int line)
1102
+					const char* file, const char* func, unsigned int line, const char* mname)
1043 1103
 {
1044 1104
 	void *r;
1045 1105
 	shm_lock();
1046
-	if(p) fm_free(qmp, p, file, func, line);
1047
-	r = fm_malloc(qmp, size, file, func, line);
1106
+	if(p) fm_free(qmp, p, file, func, line, mname);
1107
+	r = fm_malloc(qmp, size, file, func, line, mname);
1048 1108
 	shm_unlock();
1049 1109
 	return r;
1050 1110
 }
1051 1111
 void fm_shm_free(void* qmp, void* p, const char* file, const char* func,
1052
-				unsigned int line)
1112
+				unsigned int line, const char* mname)
1053 1113
 {
1054 1114
 	shm_lock();
1055
-	fm_free(qmp, p, file, func, line);
1115
+	fm_free(qmp, p, file, func, line, mname);
1056 1116
 	shm_unlock();
1057 1117
 }
1058 1118
 #else
... ...
@@ -1158,6 +1218,8 @@ int fm_malloc_init_shm_manager(void)
1158 1218
 	ma.xavailable     = fm_shm_available;
1159 1219
 	ma.xsums          = fm_shm_sums;
1160 1220
 	ma.xdestroy       = fm_malloc_destroy_shm_manager;
1221
+	ma.xstats         = fm_mod_get_stats;
1222
+	ma.xfstats        = fm_mod_free_stats;
1161 1223
 
1162 1224
 	if(shm_init_api(&ma)<0) {
1163 1225
 		LM_ERR("cannot initialize the core shm api\n");
... ...
@@ -86,6 +86,7 @@ struct fm_frag{
86 86
 #ifdef DBG_F_MALLOC
87 87
 	const char* file;
88 88
 	const char* func;
89
+	const char* mname;
89 90
 	unsigned long line;
90 91
 #endif
91 92
 	unsigned int check;
... ...
@@ -134,7 +135,8 @@ struct fm_block* fm_malloc_init(char* address, unsigned long size, int type);
134 135
  */
135 136
 #ifdef DBG_F_MALLOC
136 137
 void* fm_malloc(void* qmp, unsigned long size,
137
-					const char* file, const char* func, unsigned int line);
138
+					const char* file, const char* func, unsigned int line,
139
+					const char* mname);
138 140
 #else
139 141
 void* fm_malloc(void* qmp, unsigned long size);
140 142
 #endif
... ...
@@ -148,8 +150,8 @@ void* fm_malloc(void* qmp, unsigned long size);
148 150
  * \param p freed memory
149 151
  */
150 152
 #ifdef DBG_F_MALLOC
151
-void  fm_free(void* qmp, void* p, const char* file, const char* func, 
152
-				unsigned int line);
153
+void fm_free(void* qmp, void* p, const char* file, const char* func,
154
+				unsigned int line, const char* mname);
153 155
 #else
154 156
 void  fm_free(void* qmp, void* p);
155 157
 #endif
... ...
@@ -165,8 +167,8 @@ void  fm_free(void* qmp, void* p);
165 167
  * \return reallocated memory block
166 168
  */
167 169
 #ifdef DBG_F_MALLOC
168
-void*  fm_realloc(void* qmp, void* p, unsigned long size, 
169
-					const char* file, const char* func, unsigned int line);
170
+void* fm_realloc(void* qmp, void* p, unsigned long size,
171
+					const char* file, const char* func, unsigned int line, const char *mname);
170 172
 #else
171 173
 void*  fm_realloc(void* qmp, void* p, unsigned long size);
172 174
 #endif
... ...
@@ -204,6 +206,20 @@ unsigned long fm_available(void* qmp);
204 206
  * \param qm memory block
205 207
  */
206 208
 void fm_sums(void* qmp);
209
+void fm_mod_get_stats(void* qm, void **fm_root);
210
+void fm_mod_free_stats(void *root);
211
+
212
+typedef struct _mem_counter{
213
+	const char *file;
214
+	const char *func;
215
+	const char *mname;
216
+	unsigned long line;
217
+
218
+	unsigned long size;
219
+	int count;
220
+
221
+	struct _mem_counter *next;
222
+} mem_counter;
207 223
 
208 224
 #endif
209 225
 #endif