xref: /netbsd-src/sys/dev/raidframe/rf_debugMem.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: rf_debugMem.c,v 1.1 1998/11/13 04:20:28 oster Exp $	*/
2 /*
3  * Copyright (c) 1995 Carnegie-Mellon University.
4  * All rights reserved.
5  *
6  * Author: Daniel Stodolsky, Mark Holland, Jim Zelenka
7  *
8  * Permission to use, copy, modify and distribute this software and
9  * its documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie the
26  * rights to redistribute these changes.
27  */
28 
29 /* debugMem.c:  memory usage debugging stuff.
30  * Malloc, Calloc, and Free are #defined everywhere
31  * to do_malloc, do_calloc, and do_free.
32  *
33  * if RF_UTILITY is nonzero, it means were compiling one of the
34  * raidframe utility programs, such as rfctrl or smd.  In this
35  * case, we eliminate all references to the threads package
36  * and to the allocation list stuff.
37  */
38 
39 /* :
40  * Log: rf_debugMem.c,v
41  * Revision 1.38  1996/08/20 14:45:43  jimz
42  * add debugging to track memory allocated (amount only, w/out
43  * excessive sanity checking)
44  *
45  * Revision 1.37  1996/07/27  23:36:08  jimz
46  * Solaris port of simulator
47  *
48  * Revision 1.36  1996/07/18  22:57:14  jimz
49  * port simulator to AIX
50  *
51  * Revision 1.35  1996/06/13  08:55:38  jimz
52  * make error messages refer to file, line of original
53  * allocation
54  *
55  * Revision 1.34  1996/06/10  11:55:47  jimz
56  * Straightened out some per-array/not-per-array distinctions, fixed
57  * a couple bugs related to confusion. Added shutdown lists. Removed
58  * layout shutdown function (now subsumed by shutdown lists).
59  *
60  * Revision 1.33  1996/06/09  02:36:46  jimz
61  * lots of little crufty cleanup- fixup whitespace
62  * issues, comment #ifdefs, improve typing in some
63  * places (esp size-related)
64  *
65  * Revision 1.32  1996/06/05  18:06:02  jimz
66  * Major code cleanup. The Great Renaming is now done.
67  * Better modularity. Better typing. Fixed a bunch of
68  * synchronization bugs. Made a lot of global stuff
69  * per-desc or per-array. Removed dead code.
70  *
71  * Revision 1.31  1996/05/30  23:22:16  jimz
72  * bugfixes of serialization, timing problems
73  * more cleanup
74  *
75  * Revision 1.30  1996/05/30  11:29:41  jimz
76  * Numerous bug fixes. Stripe lock release code disagreed with the taking code
77  * about when stripes should be locked (I made it consistent: no parity, no lock)
78  * There was a lot of extra serialization of I/Os which I've removed- a lot of
79  * it was to calculate values for the cache code, which is no longer with us.
80  * More types, function, macro cleanup. Added code to properly quiesce the array
81  * on shutdown. Made a lot of stuff array-specific which was (bogusly) general
82  * before. Fixed memory allocation, freeing bugs.
83  *
84  * Revision 1.29  1996/05/27  18:56:37  jimz
85  * more code cleanup
86  * better typing
87  * compiles in all 3 environments
88  *
89  * Revision 1.28  1996/05/23  21:46:35  jimz
90  * checkpoint in code cleanup (release prep)
91  * lots of types, function names have been fixed
92  *
93  * Revision 1.27  1996/05/23  00:33:23  jimz
94  * code cleanup: move all debug decls to rf_options.c, all extern
95  * debug decls to rf_options.h, all debug vars preceded by rf_
96  *
97  * Revision 1.26  1996/05/21  18:53:46  jimz
98  * return NULL for failed allocations, not panic
99  *
100  * Revision 1.25  1996/05/20  16:14:19  jimz
101  * switch to rf_{mutex,cond}_{init,destroy}
102  *
103  * Revision 1.24  1996/05/18  19:51:34  jimz
104  * major code cleanup- fix syntax, make some types consistent,
105  * add prototypes, clean out dead code, et cetera
106  *
107  * Revision 1.23  1996/05/17  12:42:35  jimz
108  * wrap get_threadid stuff in #ifndef UTILITY for utils which use
109  * redzone allocation stuff
110  *
111  * Revision 1.22  1996/05/16  23:06:09  jimz
112  * don't warn about NULL alists
113  *
114  * Revision 1.21  1996/05/16  22:25:02  jimz
115  * show allocations for [MC]allocAndAdd
116  *
117  * Revision 1.20  1996/05/15  18:30:22  jimz
118  * print memory allocation as well as frees if memDebug > 1
119  *
120  * Revision 1.19  1996/05/07  17:41:17  jimz
121  * add "level 2" for memDebug, which will print freed address ranges
122  *
123  * Revision 1.18  1996/05/02  20:41:53  jimz
124  * really fix malloc problem out-of-kernel in memory_hash_insert()
125  *
126  * Revision 1.17  1996/05/02  20:04:29  jimz
127  * fixed malloc deadlock previous change introduced
128  *
129  * Revision 1.16  1996/05/01  16:27:26  jimz
130  * get rid of ALLOCMH
131  * stop using ccmn_ memory management
132  *
133  * Revision 1.15  1995/12/12  18:10:06  jimz
134  * MIN -> RF_MIN, MAX -> RF_MAX, ASSERT -> RF_ASSERT
135  * fix 80-column brain damage in comments
136  *
137  * Revision 1.14  1995/12/01  15:56:17  root
138  * added copyright info
139  *
140  */
141 
142 #include "rf_types.h"
143 #include "rf_sys.h"
144 
145 #if RF_UTILITY == 0
146 #include "rf_threadstuff.h"
147 #include "rf_threadid.h"
148 #include "rf_options.h"
149 #else /* RF_UTILITY == 0 */
150 #include "rf_utility.h"
151 #endif /* RF_UTILITY == 0 */
152 
153 #ifndef KERNEL
154 #include <stdio.h>
155 #include <assert.h>
156 #endif /* !KERNEL */
157 #include "rf_debugMem.h"
158 #include "rf_general.h"
159 
160 static long tot_mem_in_use = 0, max_mem = 0;
161 
162 /* Hash table of information about memory allocations */
163 #define RF_MH_TABLESIZE 1000
164 
165 struct mh_struct {
166     void              *address;
167     int                size;
168     int                line;
169     char              *filen;
170     char               allocated;
171     struct mh_struct  *next;
172 };
173 static struct mh_struct *mh_table[RF_MH_TABLESIZE];
174 RF_DECLARE_MUTEX(rf_debug_mem_mutex)
175 static int mh_table_initialized=0;
176 
177 static void memory_hash_insert(void *addr, int size, int line, char *filen);
178 static int memory_hash_remove(void *addr, int sz);
179 
180 #ifndef KERNEL                    /* no redzones or "real_" routines in the kernel */
181 
182 static void rf_redzone_free_failed(void *ptr, int size, int line, char *file);
183 
184 void *rf_real_redzone_malloc(_size_)
185   int  _size_;
186 {
187   char *p;
188 
189   rf_validate_mh_table();
190   p = malloc((_size_)+16);
191   if (p == NULL)
192     return(p);
193   RF_ASSERT (p);
194   *((long *) p) = (_size_) ;
195   ((char *) p)[(_size_)+8] = '!';
196   ((char *) p)[(_size_)+15] = '!';
197   p += 8;
198   return(p);
199 }
200 
201 void *rf_real_redzone_calloc(_n_,_size_)
202 int _n_,_size_;
203 {
204   char *p;
205   int _sz_;
206 
207   rf_validate_mh_table();
208   _sz_  = (_n_) * (_size_);
209   p = malloc((_sz_)+16);
210   if (p == NULL)
211     return(p);
212   bzero(p,(_sz_)+16);
213   *((long *) p) = (_sz_) ;
214   ((char *) p)[(_sz_)+8] = '!';
215   ((char *) p)[(_sz_)+15] = '!';
216   p += 8;
217   return(p);
218 }
219 
220 void rf_real_redzone_free(p, line, filen)
221 char *p;
222 int line;
223 char *filen;
224 {
225   unsigned long _size_;
226 
227   rf_validate_mh_table();
228   p -= 8;
229   _size_ = *((long *) p);
230   if  ((((char *) p)[(_size_)+8] != '!') || (((char *) p)[(_size_)+15] != '!'))
231     rf_redzone_free_failed(p,(_size_),line,filen);
232   free(p);
233 }
234 
235 unsigned long rf_mem_alloc = 0;
236 
237 char *rf_real_Malloc(size, line, file)
238   int    size;
239   int    line;
240   char  *file;
241 {
242   void *pp;
243   char *p;
244   int tid;
245 
246   RF_LOCK_MUTEX(rf_debug_mem_mutex);
247   rf_redzone_malloc(pp, size);
248   p = pp;
249   if (p == NULL) {
250     RF_ERRORMSG3("Unable to malloc %d bytes at line %d file %s\n", size,
251       line, file);
252   }
253   if (rf_memAmtDebug) {
254     rf_mem_alloc += size;
255     printf("%lu    size %d %s:%d\n", rf_mem_alloc, size, file, line);
256   }
257 #if RF_UTILITY == 0
258   if (rf_memDebug > 1) {
259     rf_get_threadid(tid);
260     printf("[%d] malloc 0x%lx - 0x%lx (%d) %s %d\n", tid, p, p+size, size,
261         file, line);
262   }
263 #endif /* RF_UTILITY == 0 */
264   if (rf_memDebug)
265     rf_record_malloc(p, size, line, file);
266   RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
267   return(p);
268 }
269 
270 #if RF_UTILITY == 0
271 char *rf_real_MallocAndAdd(size, alist, line, file)
272   int                  size;
273   RF_AllocListElem_t  *alist;
274   int                  line;
275   char                *file;
276 {
277 	void *pp;
278 	char *p;
279 	int tid;
280 
281 	RF_LOCK_MUTEX(rf_debug_mem_mutex);
282 	rf_redzone_malloc(pp, size);
283 	p = pp;
284 	if (p == NULL) {
285 		RF_ERRORMSG3("Unable to malloc %d bytes at line %d file %s\n", size,
286 			line, file);
287 	}
288 	if (rf_memAmtDebug) {
289 		rf_mem_alloc += size;
290 		printf("%lu    size %d %s:%d\n", rf_mem_alloc, size, file, line);
291 	}
292 	if (rf_memDebug > 1) {
293 		rf_get_threadid(tid);
294 		printf("[%d] malloc+add 0x%lx - 0x%lx (%d) %s %d\n", tid, p, p+size,
295 			size, file, line);
296 	}
297 	if (alist) {
298 		rf_real_AddToAllocList(alist, pp, size, 0);
299 	}
300 	if (rf_memDebug)
301 		rf_record_malloc(p, size, line, file);
302 	RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
303 	return(p);
304 }
305 #endif /* RF_UTILITY == 0 */
306 
307 char *rf_real_Calloc(nel, elsz, line, file)
308   int    nel;
309   int    elsz;
310   int    line;
311   char  *file;
312 {
313   int tid, size;
314   void *pp;
315   char *p;
316 
317   size = nel * elsz;
318   RF_LOCK_MUTEX(rf_debug_mem_mutex);
319   rf_redzone_calloc(pp, nel, elsz);
320   p = pp;
321   if (p == NULL) {
322     RF_ERRORMSG4("Unable to calloc %d objects of size %d at line %d file %s\n",
323       nel, elsz, line, file);
324     return(NULL);
325   }
326   if (rf_memAmtDebug) {
327     rf_mem_alloc += size;
328     printf("%lu    size %d %s:%d\n", rf_mem_alloc, size, file, line);
329   }
330 #if RF_UTILITY == 0
331   if (rf_memDebug > 1) {
332     rf_get_threadid(tid);
333     printf("[%d] calloc 0x%lx - 0x%lx (%d,%d) %s %d\n", tid, p, p+size, nel,
334         elsz, file, line);
335   }
336 #endif /* RF_UTILITY == 0 */
337   if (rf_memDebug) {
338     rf_record_malloc(p, size, line, file);
339   }
340   RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
341   return(p);
342 }
343 
344 #if RF_UTILITY == 0
345 char *rf_real_CallocAndAdd(nel, elsz, alist, line, file)
346   int                  nel;
347   int                  elsz;
348   RF_AllocListElem_t  *alist;
349   int                  line;
350   char                *file;
351 {
352 	int tid, size;
353 	void *pp;
354 	char *p;
355 
356 	size = nel * elsz;
357 	RF_LOCK_MUTEX(rf_debug_mem_mutex);
358 	rf_redzone_calloc(pp, nel, elsz);
359 	p = pp;
360 	if (p == NULL) {
361 		RF_ERRORMSG4("Unable to calloc %d objs of size %d at line %d file %s\n",
362 			nel, elsz, line, file);
363 		return(NULL);
364 	}
365 	if (rf_memAmtDebug) {
366 		rf_mem_alloc += size;
367 		printf("%lu    size %d %s:%d\n", rf_mem_alloc, size, file, line);
368 	}
369 	if (rf_memDebug > 1) {
370 		rf_get_threadid(tid);
371 		printf("[%d] calloc+add 0x%lx - 0x%lx (%d,%d) %s %d\n", tid, p,
372 			p+size, nel, elsz, file, line);
373 	}
374 	if (alist) {
375 		rf_real_AddToAllocList(alist, pp, size, 0);
376 	}
377 	if (rf_memDebug)
378 		rf_record_malloc(p, size, line, file);
379 	RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
380 	return(p);
381 }
382 #endif /* RF_UTILITY == 0 */
383 
384 void rf_real_Free(p, sz, line, file)
385   void  *p;
386   int    sz;
387   int    line;
388   char  *file;
389 {
390   int tid;
391 
392 #if RF_UTILITY == 0
393   if (rf_memDebug > 1) {
394     rf_get_threadid(tid);
395     printf("[%d] free 0x%lx - 0x%lx (%d) %s %d\n", tid, p, ((char *)p)+sz, sz,
396       file, line);
397   }
398 #endif /* RF_UTILITY == 0 */
399   RF_LOCK_MUTEX(rf_debug_mem_mutex);
400   if (rf_memAmtDebug) {
401     rf_mem_alloc -= sz;
402     printf("%lu  - size %d %s:%d\n", rf_mem_alloc, sz, file, line);
403   }
404   if (rf_memDebug) {
405     rf_unrecord_malloc(p,sz);
406   }
407   rf_redzone_free(p);
408   RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
409 }
410 
411 void rf_validate_mh_table()
412 {
413   int i, size;
414   struct mh_struct *p;
415   char *cp;
416 
417   return;
418   for (i=0; i<RF_MH_TABLESIZE; i++) {
419     for (p=mh_table[i]; p; p=p->next) if (p->allocated) {
420       cp = ((char *) p->address) - 8;
421       size = *((long *) cp);
422       if  ((((char *) cp)[(size)+8] != '!') || (((char *) cp)[(size)+15] != '!')) {
423 	rf_redzone_free_failed(cp,(size),__LINE__,__FILE__);
424       }
425     }
426   }
427 }
428 
429 static void rf_redzone_free_failed(ptr,size,line,file)
430   void  *ptr;
431   int    size;
432   int    line;
433   char  *file;
434 {
435   RF_ERRORMSG4("Free of 0x%lx (recorded size %d) at %d of %s detected redzone overrun\n",ptr,size,line,file);
436   RF_ASSERT(0);
437 }
438 
439 #endif /* !KERNEL */
440 
441 void rf_record_malloc(p, size, line, filen)
442 void *p;
443 int size, line;
444 char *filen;
445 {
446   RF_ASSERT(size != 0);
447 
448   /*RF_LOCK_MUTEX(rf_debug_mem_mutex);*/
449   memory_hash_insert(p, size, line, filen);
450   tot_mem_in_use += size;
451   /*RF_UNLOCK_MUTEX(rf_debug_mem_mutex);*/
452   if ( (long) p == rf_memDebugAddress) {
453     printf("Allocate: debug address allocated from line %d file %s\n",line,filen);
454   }
455 }
456 
457 void rf_unrecord_malloc(p, sz)
458 void *p;
459 int sz;
460 {
461   int size;
462 
463   /*RF_LOCK_MUTEX(rf_debug_mem_mutex);*/
464   size = memory_hash_remove(p, sz);
465   tot_mem_in_use -= size;
466   /*RF_UNLOCK_MUTEX(rf_debug_mem_mutex);*/
467   if ( (long) p == rf_memDebugAddress) {
468     printf("Free: Found debug address\n");         /* this is really only a flag line for gdb */
469   }
470 }
471 
472 void rf_print_unfreed()
473 {
474   int i, foundone=0;
475   struct mh_struct *p;
476 
477   for (i=0; i<RF_MH_TABLESIZE; i++) {
478     for (p=mh_table[i]; p; p=p->next) if (p->allocated) {
479       if (!foundone) printf("\n\nThere are unfreed memory locations at program shutdown:\n");
480       foundone = 1;
481       printf("Addr 0x%lx Size %d line %d file %s\n",
482 	     (long)p->address,p->size,p->line,p->filen);
483     }
484   }
485   if (tot_mem_in_use) {
486     printf("%ld total bytes in use\n", tot_mem_in_use);
487   }
488 }
489 
490 int rf_ConfigureDebugMem(listp)
491   RF_ShutdownList_t  **listp;
492 {
493   int i, rc;
494 
495   rc = rf_create_managed_mutex(listp, &rf_debug_mem_mutex);
496   if (rc) {
497     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
498       __LINE__, rc);
499     return(rc);
500   }
501   if (rf_memDebug) {
502     for (i=0; i<RF_MH_TABLESIZE; i++)
503       mh_table[i] = NULL;
504     mh_table_initialized=1;
505   }
506   return(0);
507 }
508 
509 #define HASHADDR(_a_)      ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE )
510 
511 static void memory_hash_insert(addr, size, line, filen)
512 void *addr;
513 int size, line;
514 char *filen;
515 {
516     unsigned long bucket = HASHADDR(addr);
517     struct mh_struct *p;
518 
519     RF_ASSERT(mh_table_initialized);
520 
521     /* search for this address in the hash table */
522     for (p=mh_table[bucket]; p && (p->address != addr); p=p->next);
523     if (!p) {
524 #ifdef KERNEL
525       RF_Malloc(p,sizeof(struct mh_struct),(struct mh_struct *));
526 #else /* KERNEL */
527       p = (struct mh_struct *)malloc(sizeof(struct mh_struct));
528 #endif /* KERNEL */
529       RF_ASSERT(p);
530       p->next = mh_table[bucket];
531       mh_table[bucket] = p;
532       p->address = addr;
533       p->allocated = 0;
534     }
535     if (p->allocated) {
536       printf("ERROR:  reallocated address 0x%lx from line %d, file %s without intervening free\n",(long) addr, line, filen);
537       printf("        last allocated from line %d file %s\n",p->line, p->filen);
538       RF_ASSERT(0);
539     }
540     p->size = size; p->line = line; p->filen = filen;
541     p->allocated = 1;
542 }
543 
544 static int memory_hash_remove(addr, sz)
545 void *addr;
546 int sz;
547 {
548     unsigned long bucket = HASHADDR(addr);
549     struct mh_struct *p;
550 
551     RF_ASSERT(mh_table_initialized);
552     for (p=mh_table[bucket]; p && (p->address != addr); p=p->next);
553     if (!p) {
554       printf("ERROR:  freeing never-allocated address 0x%lx\n",(long) addr);
555       RF_PANIC();
556     }
557     if (!p->allocated) {
558       printf("ERROR:  freeing unallocated address 0x%lx.  Last allocation line %d file %s\n",(long) addr, p->line, p->filen);
559       RF_PANIC();
560     }
561     if (sz > 0 && p->size != sz) {         /* you can suppress this error by using a negative value as the size to free */
562       printf("ERROR:  incorrect size at free for address 0x%lx: is %d should be %d.  Alloc at line %d of file %s\n",(unsigned long) addr, sz, p->size,p->line, p->filen);
563       RF_PANIC();
564     }
565     p->allocated = 0;
566     return(p->size);
567 }
568 
569 void rf_ReportMaxMem()
570 {
571     printf("Max memory used:  %d bytes\n",(int)max_mem);
572 #ifndef KERNEL
573     fflush(stdout);
574     fprintf(stderr,"Max memory used:  %d bytes\n",max_mem);
575     fflush(stderr);
576 #endif /* !KERNEL */
577 }
578