📄 trmem.c
字号:
Input Parameter:. fp - file pointer. If fp is NULL, stderr is assumed. +*/void MPIU_trdump( FILE *fp ){ TRSPACE *head; int id; if (fp == 0) fp = stderr; head = TRhead; while (head) { msg_fprintf( fp, "[%d] %lu at [%lx], id = ", world_rank, head->size, (PointerInt)head + sizeof(TrSPACE) ); if (head->id >= 0) { head->fname[TR_FNAME_LEN-1] = 0; msg_fprintf( fp, "%d %s[%d]\n", head->id, head->fname, head->lineno ); } else { /* Decode the package values */ head->fname[TR_FNAME_LEN-1] = 0; id = head->id; msg_fprintf( fp, "%d %s[%d]\n", id, head->fname, head->lineno ); } head = head->next; }/* msg_fprintf( fp, "# [%d] The maximum space allocated was %ld bytes [%ld]\n", world_rank, TRMaxMem, TRMaxMemId ); */}/* Configure will set HAVE_SEARCH for these systems. We assume that the system does NOT have search.h unless otherwise noted. The otherwise noted lets the non-configure approach work on our two major systems */#if defined(HAVE_SEARCH_H)/* The following routine uses the tsearch routines to summarize the memory heap by id */#include <search.h>typedef struct { int id, size, lineno; char *fname; } TRINFO;static int IntCompare( TRINFO *a, TRINFO *b ){ return a->id - b->id;}static FILE *TRFP = 0;/*ARGSUSED*/static void PrintSum( TRINFO **a, VISIT order, int level ){ if (order == postorder || order == leaf) msg_fprintf( TRFP, "[%d]%s[%d] has %d\n", (*a)->id, (*a)->fname, (*a)->lineno, (*a)->size );}/*+C MPIU_trSummary - Summarize the allocate memory blocks by id Input Parameter:. fp - file pointer Note: This routine is the same as MPIU_trDump on those systems that do not include /usr/include/search.h . +*/void MPIU_trSummary( FILE *fp ){ TRSPACE *head; TRINFO *root, *key, **fnd; TRINFO nspace[1000]; root = 0; head = TRhead; key = nspace; while (head) { key->id = head->id; key->size = 0; key->lineno = head->lineno; key->fname = head->fname;#if defined(USE_TSEARCH_WITH_CHARP) fnd = (TRINFO **)tsearch( (char *) key, (char **) &root, IntCompare );#else fnd = (TRINFO **)tsearch( (void *) key, (void **) &root, (int (*)())IntCompare );#endif if (*fnd == key) { key->size = 0; key++; } (*fnd)->size += head->size; head = head->next; } /* Print the data */ TRFP = fp; twalk( (char *)root, (void (*)())PrintSum ); /* msg_fprintf( fp, "# [%d] The maximum space allocated was %d bytes [%d]\n", world_rank, TRMaxMem, TRMaxMemId ); */}#elsevoid MPIU_trSummary( FILE *fp ){ msg_fprintf( fp, "# [%d] The maximum space allocated was %ld bytes [%ld]\n", world_rank, TRMaxMem, TRMaxMemId );} #endif/*+ MPIU_trid - set an "id" field to be used with each fragment +*/void MPIU_trid( int id ){ TRid = id;}/*+C MPIU_trlevel - Set the level of output to be used by the tracing routines Input Parameters:. level = 0 - notracing. level = 1 - trace mallocs. level = 2 - trace frees Note: You can add levels together to get combined tracing. +*/void MPIU_trlevel( int level ){ TRlevel = level;}/*+C MPIU_trpush - Push an "id" value for the tracing space routines Input Parameters:. a - value to push+*/void MPIU_trpush( int a ){ if (TRstackp < MAX_TR_STACK - 1) TRstack[++TRstackp] = a; TRid = a;}/*+C MPIU_trpop - Pop an "id" value for the tracing space routines+*/void MPIU_trpop( void ){ if (TRstackp > 1) { TRstackp--; TRid = TRstack[TRstackp]; } else TRid = 0;}/*+C MPIU_trDebugLevel - set the level of debugging for the space management routines Input Parameter:. level - level of debugging. Currently, either 0 (no checking) or 1 (use MPIU_trvalid at each MPIU_trmalloc or MPIU_trfree).+*/void MPIU_trDebugLevel( int level ){ TRdebugLevel = level;}/*+C MPIU_trcalloc - Calloc with tracing Input Parameters:. nelem - number of elements to allocate. elsize - size of each element. lineno - line number where used. Use __LINE__ for this. fname - file name where used. Use __FILE__ for this Returns: Double aligned pointer to requested storage, or null if not available. +*/void *MPIU_trcalloc( unsigned int nelem, unsigned int elsize, int lineno, const char fname[] ){ void *p; p = MPIU_trmalloc( (unsigned)(nelem*elsize), lineno, fname ); if (p) { memset(p,0,nelem*elsize); } return p;}/*+C MPIU_trrealloc - Realloc with tracing Input Parameters:. p - pointer to old storage. size - number of bytes to allocate. lineno - line number where used. Use __LINE__ for this. fname - file name where used. Use __FILE__ for this Returns: Double aligned pointer to requested storage, or null if not available. This implementation ALWAYS allocates new space and copies the contents into the new space. +*/void *MPIU_trrealloc( void *p, int size, int lineno, const char fname[] ){ void *pnew; char *pa; int nsize; TRSPACE *head;/* We should really use the size of the old block... */ pa = (char *)p; head = (TRSPACE *)(pa - sizeof(TrSPACE)); if (head->cookie != COOKIE_VALUE) { /* Damaged header */ msg_fprintf( stderr, "[%d] Block at address %lx is corrupted; cannot realloc;\n\may be block not allocated with MPIU_trmalloc or MALLOC\n", world_rank, (PointerInt)pa ); return 0; } pnew = MPIU_trmalloc( (unsigned)size, lineno, fname ); if (!pnew) return p; nsize = size; if (head->size < (unsigned long)nsize) nsize = (int)(head->size); memcpy( pnew, p, nsize ); MPIU_trfree( p, lineno, fname ); return pnew;}/*+C MPIU_trstrdup - Strdup with tracing Input Parameters:. str - string to duplicate. lineno - line number where used. Use __LINE__ for this. fname - file name where used. Use __FILE__ for this Returns: Pointer to copy of the input string. +*/void *MPIU_trstrdup( const char *str, int lineno, const char *fname ){ void *p; unsigned len = strlen( str ) + 1; p = MPIU_trmalloc( len, lineno, (char *)fname ); if (p) { memcpy( p, str, len ); } return p;}#define TR_MAX_DUMP 100/* The following routine attempts to give useful information about the memory usage when an "out-of-memory" error is encountered. The rules are: If there are less than TR_MAX_DUMP blocks, output those. Otherwise, try to find multiple instances of the same routine/line #, and print a summary by number: file line number-of-blocks total-number-of-blocks We have to do a sort-in-place for this *//* Sort by file/line number. Do this without calling a system routine or allocating ANY space (space is being optimized here). We do this by first recursively sorting halves of the list and then merging them. *//* Forward refs for these local routines */TRSPACE *MPIU_trImerge ( TRSPACE *, TRSPACE * );TRSPACE *MPIU_trIsort ( TRSPACE *, int );void MPIU_trSortBlocks ( void ); /* Merge two lists, returning the head of the merged list */TRSPACE *MPIU_trImerge( TRSPACE *l1, TRSPACE *l2 ){ TRSPACE *head = 0, *tail = 0; int sign; while (l1 && l2) { sign = strncmp(l1->fname, l2->fname, TR_FNAME_LEN - 1 ); if (sign > 0 || (sign == 0 && l1->lineno >= l2->lineno)) { if (head) tail->next = l1; else head = tail = l1; tail = l1; l1 = l1->next; } else { if (head) tail->next = l2; else head = tail = l2; tail = l2; l2 = l2->next; } } /* Add the remaining elements to the end */ if (l1) tail->next = l1; if (l2) tail->next = l2; return head;}/* Sort head with n elements, returning the head */TRSPACE *MPIU_trIsort( TRSPACE *head, int n ){ TRSPACE *p, *l1, *l2; int m, i; if (n <= 1) return head; /* This guarentees that m, n are both > 0 */ m = n / 2; p = head; for (i=0; i<m-1; i++) p = p->next; /* p now points to the END of the first list */ l2 = p->next; p->next = 0; l1 = MPIU_trIsort( head, m ); l2 = MPIU_trIsort( l2, n - m ); return MPIU_trImerge( l1, l2 );}void MPIU_trSortBlocks( void ){ TRSPACE *head; int cnt; head = TRhead; cnt = 0; while (head) { cnt ++; head = head->next; } TRhead = MPIU_trIsort( TRhead, cnt );}/* Takes sorted input and dumps as an aggregate */void MPIU_trdumpGrouped( FILE *fp ){ TRSPACE *head, *cur; int nblocks, nbytes; if (fp == 0) fp = stderr; MPIU_trSortBlocks(); head = TRhead; cur = 0; while (head) { cur = head->next; nblocks = 1; nbytes = (int)head->size; while (cur && strncmp(cur->fname,head->fname,TR_FNAME_LEN-1) == 0 && cur->lineno == head->lineno ) { nblocks++; nbytes += (int)cur->size; cur = cur->next; } msg_fprintf( fp, "[%d] File %13s line %5d: %d bytes in %d allocation%c\n", world_rank, head->fname, head->lineno, nbytes, nblocks, (nblocks > 1) ? 's' : ' ' ); head = cur; } fflush( fp );}void MPIU_TrSetMaxMem( int size ){ TRMaxMemAllow = size;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -