1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
* Reads a malloc() trace that's been processed by tracemunge, and performs
* the malloc/realloc/frees from that trace in the same sequence so we can
* simulate the effects of changes in malloc algorithms on the application
* which generated the trace.
*/
/* Mark Moraes, D. E. Shaw & Co. */
/*
* TODO! Put a hash function in this code so we can get rid of tracemunge.
* Use better parsing than sscanf. split()?
*/
#include <stdio.h>
#include <string.h>
/*
* ANSI systems had better have this. Non-ANSI systems had better not
* complain about things that are implicitly declared int or void.
*/
#if defined(STDHEADERS)
# include <stdlib.h>
# include <unistd.h>
#endif
#include "zmalloc.h"
char *progname;
/* For getopt() */
extern int getopt();
extern int optind;
extern char *optarg;
#define MAXTIME 100000
static char *bufs[MAXTIME];
static unsigned long sizes[MAXTIME];
unsigned long alloced = 0;
static unsigned long maxalloced = 0;
int verbose = 0;
#ifdef MYMALLOC
extern char *_malloc_loword;
extern char *_malloc_hiword;
#else
extern char *sbrk();
#endif
extern void doline();
void
usage()
{
fprintf(stderr, "Usage: %s [-d] [-m mmapfile] [-T tracefile] [-v]\n",
progname);
exit(1);
}
int
main(argc, argv)
int argc;
char **argv;
{
FILE *trfp = NULL;
int c;
char *before, *after, *trfname = NULL;
unsigned long grew;
int use_mmap = 0;
char buf[1024];
progname = argv[0] ? argv[0] : "(no-argv[0])";
while((c = getopt(argc, argv, "dm:vT:")) != EOF) {
/* optarg has the current argument if the option was followed by ':'*/
switch (c) {
case 'd':
/* Full heap debugging - S-L-O-W */
#ifdef MYMALLOC
mal_debug(3);
#endif
break;
case 'm':
use_mmap = 1;
#ifdef MYMALLOC
mal_mmap(optarg);
#else
fprintf(stderr, "%s: -m option needs CSRI malloc\n",
progname);
#endif
break;
case 'v':
verbose++;
break;
case 'T':
#ifdef MYMALLOC
trfp = fopen(optarg, "w");
if (trfp == NULL) {
perror(progname);
fprintf(stderr, "%s: could not open file `%s' for writing",
progname, optarg);
exit(1);
}
mal_setstatsfile(trfp);
mal_trace(1);
trfname = optarg;
#else
fprintf(stderr, "%s: -T option needs CSRI malloc",
progname);
#endif
break;
case '?':
usage();
break;
}
}
/* Any filenames etc. after all the options */
if (optind < argc) {
usage();
}
#ifndef MYMALLOC
before = sbrk(0);
#endif
while (fgets(buf, sizeof buf, stdin) != NULL) {
doline(buf);
}
#ifndef MYMALLOC
after = sbrk(0);
#else
/* assumes contiguous, ick! should provide a mal_stats function */
before = _malloc_loword;
after = _malloc_hiword;
#endif
grew = after - before;
(void) fprintf(stdout, "Sbrked %ld, MaxAlloced %ld, Wastage %.2f\n",
grew, maxalloced,
grew == 0 ? 0.0 :
(1.0 - ((double) maxalloced) / grew));
#ifdef MYMALLOC
if (verbose)
(void) mal_statsdump(stderr);
#endif
if (trfp) {
if (fflush(trfp) != 0 || fclose(trfp) != 0) {
perror(progname);
fprintf(stderr, "%s: error closing tracefile `%s'",
progname, trfname);
}
}
return 0;
}
void
doline(buf)
char *buf;
{
int n, blk;
switch (buf[0]) {
case '+':
if (buf[1] == '+') {
if (sscanf(buf, "%*[+] %d %*d 0x%*x %*d 0x%*x %d", &n, &blk) == 2 ||
sscanf(buf, "%*[+] %d %*d 0x%*x %d", &n, &blk) == 2) {
if (bufs[blk] == NULL) {
fprintf(stderr, "block %d not yet allocated", blk);
}
bufs[blk] = realloc(bufs[blk], n);
alloced += (n - sizes[blk]);
sizes[blk] = n;
if (alloced > maxalloced)
maxalloced = alloced;
}
} else if (sscanf(buf, "%*[+] %d %*d 0x%*x %d", &n, &blk) == 2) {
if (bufs[blk] != NULL) {
fprintf(stderr, "block %d not yet freed\n", blk);
}
bufs[blk] = malloc(n);
sizes[blk] = n;
alloced += n;
if (alloced > maxalloced)
maxalloced = alloced;
}
break;
case '-':
if (sscanf(buf, "- %*d 0x%*x %d", &blk) == 1) {
if (bufs[blk] == NULL) {
fprintf(stderr, "block %d not yet allocated", blk);
} else {
free(bufs[blk]);
alloced -= sizes[blk];
bufs[blk] = NULL;
sizes[n] = 0;
}
}
break;
case 'a':
case 'h': /* heapstart or heapend */
case 'w':
case 'n':
case '\n':
break;
}
if (verbose)
printf("%lu %s", alloced, buf);
}
|