#include "config.h"
#include "util/log.h"
#include "util/rbtree.h"
#include "util/locks.h"
#include "util/fptr_wlist.h"
#include <sys/stat.h>
struct codeline {
rbnode_t node;
char* codeline;
char* func;
uint64_t alloc;
uint64_t free;
uint64_t calls;
};
static void
usage()
{
printf("usage: memstats <logfile>\n");
printf("statistics are printed on stdout.\n");
exit(1);
}
static int
match(char* line)
{
if(strlen(line) < 32)
return 0;
if(!strstr(line, " info: "))
return 0;
if(strstr(line, "info: stat "))
return 0;
if(strstr(line+30, "malloc("))
return 1;
else if(strstr(line+30, "calloc("))
return 1;
return 0;
}
static struct codeline*
get_codeline(rbtree_t* tree, char* key, char* func)
{
struct codeline* cl = (struct codeline*)rbtree_search(tree, key);
if(!cl) {
cl = calloc(1, sizeof(*cl));
if(!cl) return 0;
cl->codeline = strdup(key);
if(!cl->codeline) return 0;
cl->func = strdup(func);
if(!cl->func) return 0;
cl->alloc = 0;
cl->node.key = cl->codeline;
(void)rbtree_insert(tree, &cl->node);
}
return cl;
}
static void
read_malloc_stat(char* line, rbtree_t* tree)
{
char codeline[10240];
char name[10240];
int skip = 0;
long num = 0;
struct codeline* cl = 0;
line = strstr(line, "info: ")+6;
if(sscanf(line, "%s %s %n", codeline, name, &skip) != 2) {
printf("%s\n", line);
fatal_exit("unhandled malloc");
}
if(sscanf(line+skip+7, "%ld", &num) != 1) {
printf("%s\n%s\n", line, line+skip+7);
fatal_exit("unhandled malloc");
}
cl = get_codeline(tree, codeline, name);
if(!cl)
fatal_exit("alloc failure");
cl->alloc += num;
cl->calls ++;
}
static void
read_calloc_stat(char* line, rbtree_t* tree)
{
char codeline[10240];
char name[10240];
int skip = 0;
long num = 0, sz = 0;
struct codeline* cl = 0;
line = strstr(line, "info: ")+6;
if(sscanf(line, "%s %s %n", codeline, name, &skip) != 2) {
printf("%s\n", line);
fatal_exit("unhandled calloc");
}
if(sscanf(line+skip+7, "%ld, %ld", &num, &sz) != 2) {
printf("%s\n%s\n", line, line+skip+7);
fatal_exit("unhandled calloc");
}
cl = get_codeline(tree, codeline, name);
if(!cl)
fatal_exit("alloc failure");
cl->alloc += num*sz;
cl->calls ++;
}
static off_t
get_file_size(const char* fname)
{
struct stat s;
if(stat(fname, &s) < 0) {
fatal_exit("could not stat %s: %s", fname, strerror(errno));
}
return s.st_size;
}
static void
readfile(rbtree_t* tree, const char* fname)
{
off_t total = get_file_size(fname);
off_t done = (off_t)0;
int report = 0;
FILE* in = fopen(fname, "r");
char buf[102400];
if(!in)
fatal_exit("could not open %s: %s", fname, strerror(errno));
printf("Reading %s of size " ARG_LL "d\n", fname, (long long)total);
while(fgets(buf, 102400, in)) {
buf[102400-1] = 0;
done += (off_t)strlen(buf);
if((int)(((double)done / (double)total)*100.) > report) {
report = (int)(((double)done / (double)total)*100.);
fprintf(stderr, " %d%%", report);
}
if(!match(buf))
continue;
else if(strstr(buf+30, "malloc("))
read_malloc_stat(buf, tree);
else if(strstr(buf+30, "calloc("))
read_calloc_stat(buf, tree);
else {
printf("%s\n", buf);
fatal_exit("unhandled input");
}
}
fprintf(stderr, " done\n");
fclose(in);
}
static void
printstats(rbtree_t* tree)
{
struct codeline* cl;
uint64_t total = 0, tcalls = 0;
RBTREE_FOR(cl, struct codeline*, tree) {
printf("%12lld / %8lld in %s %s\n", (long long)cl->alloc,
(long long)cl->calls, cl->codeline, cl->func);
total += cl->alloc;
tcalls += cl->calls;
}
printf("------------\n");
printf("%12lld / %8lld total in %ld code lines\n", (long long)total,
(long long)tcalls, (long)tree->count);
printf("\n");
}
int main(int argc, const char* argv[])
{
rbtree_t* tree = 0;
if(argc != 2) {
usage();
}
tree = rbtree_create(codeline_cmp);
if(!tree)
fatal_exit("alloc failure");
readfile(tree, argv[1]);
printstats(tree);
return 0;
}