PIPS
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "misc-local.h"
Go to the source code of this file.
Data Structures | |
struct | _MemSpyStack |
Macros | |
#define | ABS(x) ((x>0)?(x):-(x)) |
MEM_SPY : Package to track memory usage. More... | |
#define | TailleDeMaPile 30 |
My own stack (do not rely on any package!) More... | |
Typedefs | |
typedef struct _MemSpyStack | _MemSpyStack |
typedef struct _MemSpyStack * | MemSpyStack |
Functions | |
void | mem_spy_init (int v, double g, measurement_type t, int unit) |
void | mem_spy_reset () |
static int | current_memory_size () |
void | mem_spy_begin () |
void | mem_spy_end (char *s) |
void | mem_spy_info () |
To print mallinfo, for debugging memory leaks. More... | |
Variables | |
static MemSpyStack | current_size_stack = NULL |
To store the memory size at program section entry. More... | |
static MemSpyStack | cumul_size_stack = NULL |
To store the cumulated memory size of inner guarded sections. More... | |
static int | MemSpyUsed = 0 |
To print only wanted results (and be sure that stacks have been initialized :-) More... | |
static int | verbosity = 2 |
To control verbosity: from 2, max, to -1, shut up. More... | |
static double | granularity = 0. |
To control granularity: threshold in MB for reporting changes in memory use. More... | |
static measurement_type | measurement = NET_MEASURE |
measurement chosen More... | |
static int | log_of_unit = 0 |
log2 of unit chosen: 0 = Byte, 10 = KB, 20 = MB More... | |
MEM_SPY : Package to track memory usage.
Beatrice Creusillet - August 1995 Usage:
~ mem_spy_init() allocates and initializes all that is needed. ~ mem_spy_reset() frees the allocated memory after using mem_spy. ~ put mem_spy_begin() at the entry of the code section you want to spy, and mem_spy_end("<whatever you want>") at the end. It will print a line on stderr with the following format:
MEM_SPY <whatever you want> begin: *MB end: *MB diff: *MB proper: *MB
each * being a floating number.
~ <begin> is the memory usage at the entry of the code section. ~ <end> is the memory usage at the end of the code section. ~ <diff> is the memory allocation between the two points. ~ <proper> is <diff> minus the memory usage of nested code fragment guarded with mem_spy_begin and mem_spy_end;
You can use mem_spy_begin and mem_spy_end in functions even if mem_spy_init and mem_spy_reset are not used on all paths leading to them. On these paths, no output will be produced (this is what the variable MemSpyUsed is used for).
Notice that MEM_SPY does not rely on any other package, except stdio, string and malloc. Thus, you can use it wherever you want.
TailleDeMaPile = Max number of nested (mem_spy_begin, mem_spy_end). Please change this number accordingly to your needs.
Modifications:
#define TailleDeMaPile 30 |
typedef struct _MemSpyStack _MemSpyStack |
typedef struct _MemSpyStack * MemSpyStack |
|
static |
struct mallinfo heap_info;
heap_info = mallinfo();
sbrk(0) - etext
memory_size = heap_info.uordblks-8*heap_info.ordblks;
memory_size = heap_info.uordbytes;
Definition at line 149 of file mem_spy.c.
References abort, GROSS_MEASURE, measurement, NET_MEASURE, and SBRK_MEASURE.
Referenced by mem_spy_begin(), and mem_spy_end().
void mem_spy_begin | ( | ) |
Do nothing if not between <mem_spy_init> ... <mem_spy_reset>
Do not go beyond stack limits
Push the current memory size
Push 0 on the cumul_size_stack, since there are currently no nested guarded code fragment
Definition at line 175 of file mem_spy.c.
References assert, cumul_size_stack, current_memory_size(), current_size_stack, _MemSpyStack::elt, _MemSpyStack::index, log_of_unit, MemSpyUsed, and TailleDeMaPile.
void mem_spy_end | ( | char * | s | ) |
Do nothing if not between <mem_spy_init> ... <mem_spy_reset>
Too many dynamic calls to mem_spy_end()?
Pop memory size at entry of the code fragment
Pop cumulated memory size of nested guarded code fragments
Calculate consumed and proper memory sizes
if verbosity==-1, nothing is printed
Prettyprint the results
Add the consumed memory size to the previous level cumulated memory size of nested guarded code fragments.
Definition at line 200 of file mem_spy.c.
References abort, ABS, cumul_size_stack, current_memory_size(), current_size_stack, _MemSpyStack::elt, fprintf(), granularity, _MemSpyStack::index, log_of_unit, MemSpyUsed, and verbosity.
void mem_spy_info | ( | ) |
To print mallinfo, for debugging memory leaks.
struct mallinfo heap_info = mallinfo();
fprintf(stderr, "total space in arena: \t%d", heap_info.arena); fprintf(stderr, "number of ordinary blocks: \t%d", heap_info.ordblks); fprintf(stderr, "number of small blocks: \t%d", heap_info.smblks); fprintf(stderr, "number of holding blocks: \t%d", heap_info.hblks); fprintf(stderr, "space in holding block headers: \t%d", heap_info.hblkhd); fprintf(stderr, "space in small blocks in use: \t%d", heap_info.usmblks); fprintf(stderr, "space in free small blocks: \t%d", heap_info.fsmblks); fprintf(stderr, "space in ordinary blocks in use: \t%d", heap_info.uordblks); fprintf(stderr, "space in free ordinary blocks: \t%d", heap_info.fordblks); fprintf(stderr, "cost of enabling keep option: \t%d", heap_info.keepcost); fprintf(stderr, "max size of small blocks: \t%d", heap_info.mxfast); fprintf(stderr, "number of small blocks in a holding block: \t%d", heap_info.nlblks); fprintf(stderr, "small block rounding factor: \t%d", heap_info.grain); fprintf(stderr, "space (including overhead) allocated in ord. blks: \t%d", heap_info.uordbytes); fprintf(stderr, "number of ordinary blocks allocated: \t%d", heap_info.allocated); fprintf(stderr, "bytes used in maintaining the free tree: \t%d", heap_info.treeoverhead);
void mem_spy_init | ( | int | v, |
double | g, | ||
measurement_type | t, | ||
int | unit | ||
) |
Definition at line 113 of file mem_spy.c.
References assert, cumul_size_stack, current_size_stack, granularity, _MemSpyStack::index, log_of_unit, malloc(), measurement, MemSpyUsed, and verbosity.
void mem_spy_reset | ( | ) |
Too many dynamic calls to mem_spy_begin()?
Definition at line 129 of file mem_spy.c.
References cumul_size_stack, current_size_stack, fprintf(), free(), _MemSpyStack::index, MemSpyUsed, and verbosity.
|
static |
To store the cumulated memory size of inner guarded sections.
Definition at line 92 of file mem_spy.c.
Referenced by mem_spy_begin(), mem_spy_end(), mem_spy_init(), and mem_spy_reset().
|
static |
To store the memory size at program section entry.
Definition at line 89 of file mem_spy.c.
Referenced by mem_spy_begin(), mem_spy_end(), mem_spy_init(), and mem_spy_reset().
|
static |
To control granularity: threshold in MB for reporting changes in memory use.
0.004 is about one page.
Definition at line 105 of file mem_spy.c.
Referenced by mem_spy_end(), and mem_spy_init().
|
static |
log2 of unit chosen: 0 = Byte, 10 = KB, 20 = MB
Definition at line 111 of file mem_spy.c.
Referenced by mem_spy_begin(), mem_spy_end(), and mem_spy_init().
|
static |
measurement chosen
Definition at line 108 of file mem_spy.c.
Referenced by current_memory_size(), and mem_spy_init().
|
static |
To print only wanted results (and be sure that stacks have been initialized :-)
Definition at line 97 of file mem_spy.c.
Referenced by mem_spy_begin(), mem_spy_end(), mem_spy_init(), and mem_spy_reset().
|
static |
To control verbosity: from 2, max, to -1, shut up.
Definition at line 100 of file mem_spy.c.
Referenced by mem_spy_end(), mem_spy_init(), and mem_spy_reset().