michael@0: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include /* XXX push error reporting out to clients? */ michael@0: #ifndef XP_WIN michael@0: #include michael@0: #else michael@0: #include michael@0: #endif michael@0: #include "prlog.h" michael@0: #include "plhash.h" michael@0: /* make sure this happens before tmreader.h */ michael@0: #define PL_ARENA_CONST_ALIGN_MASK 2 michael@0: #include "plarena.h" michael@0: michael@0: #include "prnetdb.h" michael@0: #include "nsTraceMalloc.h" michael@0: #include "tmreader.h" michael@0: michael@0: #undef DEBUG_tmreader michael@0: michael@0: static int accum_byte(FILE *fp, uint32_t *uip) michael@0: { michael@0: int c = getc(fp); michael@0: if (c == EOF) michael@0: return 0; michael@0: *uip = (*uip << 8) | c; michael@0: return 1; michael@0: } michael@0: michael@0: static int get_uint32(FILE *fp, uint32_t *uip) michael@0: { michael@0: int c; michael@0: uint32_t ui; michael@0: michael@0: c = getc(fp); michael@0: if (c == EOF) michael@0: return 0; michael@0: ui = 0; michael@0: if (c & 0x80) { michael@0: c &= 0x7f; michael@0: if (c & 0x40) { michael@0: c &= 0x3f; michael@0: if (c & 0x20) { michael@0: c &= 0x1f; michael@0: if (c & 0x10) { michael@0: if (!accum_byte(fp, &ui)) michael@0: return 0; michael@0: } else { michael@0: ui = (uint32_t) c; michael@0: } michael@0: if (!accum_byte(fp, &ui)) michael@0: return 0; michael@0: } else { michael@0: ui = (uint32_t) c; michael@0: } michael@0: if (!accum_byte(fp, &ui)) michael@0: return 0; michael@0: } else { michael@0: ui = (uint32_t) c; michael@0: } michael@0: if (!accum_byte(fp, &ui)) michael@0: return 0; michael@0: } else { michael@0: ui = (uint32_t) c; michael@0: } michael@0: *uip = ui; michael@0: return 1; michael@0: } michael@0: michael@0: static char *get_string(FILE *fp) michael@0: { michael@0: char *cp; michael@0: int c; michael@0: static char buf[256]; michael@0: static char *bp = buf, *ep = buf + sizeof buf; michael@0: static size_t bsize = sizeof buf; michael@0: michael@0: cp = bp; michael@0: do { michael@0: c = getc(fp); michael@0: if (c == EOF) michael@0: return 0; michael@0: if (cp == ep) { michael@0: if (bp == buf) { michael@0: bp = malloc(2 * bsize); michael@0: if (bp) michael@0: memcpy(bp, buf, bsize); michael@0: } else { michael@0: bp = realloc(bp, 2 * bsize); michael@0: } michael@0: if (!bp) michael@0: return 0; michael@0: cp = bp + bsize; michael@0: bsize *= 2; michael@0: ep = bp + bsize; michael@0: } michael@0: *cp++ = c; michael@0: } while (c != '\0'); michael@0: return strdup(bp); michael@0: } michael@0: michael@0: static int get_tmevent(FILE *fp, tmevent *event) michael@0: { michael@0: int c; michael@0: char *s; michael@0: michael@0: c = getc(fp); michael@0: if (c == EOF) michael@0: return 0; michael@0: event->type = (char) c; michael@0: if (!get_uint32(fp, &event->serial)) michael@0: return 0; michael@0: switch (c) { michael@0: case TM_EVENT_LIBRARY: michael@0: s = get_string(fp); michael@0: if (!s) michael@0: return 0; michael@0: event->u.libname = s; michael@0: #ifdef DEBUG_tmreader michael@0: fprintf(stderr, "tmevent %c %u libname=\"%s\"\n", event->type, event->serial, michael@0: event->u.libname); michael@0: #endif michael@0: break; michael@0: michael@0: case TM_EVENT_FILENAME: michael@0: s = get_string(fp); michael@0: if (!s) michael@0: return 0; michael@0: event->u.srcname = s; michael@0: #ifdef DEBUG_tmreader michael@0: fprintf(stderr, "tmevent %c %u srcname=\"%s\"\n", michael@0: event->type, event->serial, event->u.srcname); michael@0: #endif michael@0: break; michael@0: michael@0: case TM_EVENT_METHOD: michael@0: if (!get_uint32(fp, &event->u.method.library)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.method.filename)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.method.linenumber)) michael@0: return 0; michael@0: s = get_string(fp); michael@0: if (!s) michael@0: return 0; michael@0: event->u.method.name = s; michael@0: #ifdef DEBUG_tmreader michael@0: fprintf(stderr, "tmevent %c %u library=%u filename=%u linenumber=%u " michael@0: "name=\"%s\"\n", michael@0: event->type, event->serial, michael@0: event->u.method.library, event->u.method.filename, michael@0: event->u.method.linenumber, event->u.method.name); michael@0: #endif michael@0: break; michael@0: michael@0: case TM_EVENT_CALLSITE: michael@0: if (!get_uint32(fp, &event->u.site.parent)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.site.method)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.site.offset)) michael@0: return 0; michael@0: #ifdef DEBUG_tmreader michael@0: fprintf(stderr, "tmevent %c %u parent=%u method=%u offset=%u\n", michael@0: event->type, event->serial, michael@0: event->u.site.parent, event->u.site.method, michael@0: event->u.site.offset); michael@0: #endif michael@0: break; michael@0: michael@0: case TM_EVENT_MALLOC: michael@0: case TM_EVENT_CALLOC: michael@0: case TM_EVENT_FREE: michael@0: if (!get_uint32(fp, &event->u.alloc.interval)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.alloc.cost)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.alloc.ptr)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.alloc.size)) michael@0: return 0; michael@0: event->u.alloc.oldserial = 0; michael@0: event->u.alloc.oldptr = 0; michael@0: event->u.alloc.oldsize = 0; michael@0: #ifdef DEBUG_tmreader michael@0: fprintf(stderr, "tmevent %c %u interval=%u cost=%u ptr=0x%x size=%u\n", michael@0: event->type, event->serial, michael@0: event->u.alloc.interval, event->u.alloc.cost, michael@0: event->u.alloc.ptr, event->u.alloc.size); michael@0: #endif michael@0: #if defined(DEBUG_dp) michael@0: if (c == TM_EVENT_MALLOC) michael@0: printf("%d malloc %d 0x%p\n", event->u.alloc.cost, michael@0: event->u.alloc.size, event->u.alloc.ptr); michael@0: else if (c == TM_EVENT_CALLOC) michael@0: printf("%d calloc %d 0x%p\n", event->u.alloc.cost, michael@0: event->u.alloc.size, event->u.alloc.ptr); michael@0: else michael@0: printf("%d free %d 0x%p\n", event->u.alloc.cost, michael@0: event->u.alloc.size, event->u.alloc.ptr); michael@0: #endif michael@0: break; michael@0: michael@0: case TM_EVENT_REALLOC: michael@0: if (!get_uint32(fp, &event->u.alloc.interval)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.alloc.cost)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.alloc.ptr)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.alloc.size)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.alloc.oldserial)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.alloc.oldptr)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.alloc.oldsize)) michael@0: return 0; michael@0: #ifdef DEBUG_tmreader michael@0: fprintf(stderr, "tmevent %c %u interval=%u cost=%u ptr=0x%x size=%u " michael@0: "oldserial=%u oldptr=0x%x oldsize=%u\n", michael@0: event->type, event->serial, michael@0: event->u.alloc.interval, event->u.alloc.cost, michael@0: event->u.alloc.ptr, event->u.alloc.size, michael@0: event->u.alloc.oldserial, event->u.alloc.oldptr, michael@0: event->u.alloc.oldsize); michael@0: #endif michael@0: #if defined(DEBUG_dp) michael@0: printf("%d realloc %d 0x%p %d\n", event->u.alloc.cost, michael@0: event->u.alloc.size, event->u.alloc.ptr, event->u.alloc.oldsize); michael@0: #endif michael@0: break; michael@0: michael@0: case TM_EVENT_STATS: michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.calltree_maxstack)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.calltree_maxdepth)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.calltree_parents)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.calltree_maxkids)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.calltree_kidhits)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.calltree_kidmisses)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.calltree_kidsteps)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.callsite_recurrences)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.backtrace_calls)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.backtrace_failures)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.btmalloc_failures)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.dladdr_failures)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.malloc_calls)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.malloc_failures)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.calloc_calls)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.calloc_failures)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.realloc_calls)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.realloc_failures)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.free_calls)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.tmstats.null_free_calls)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.calltree_maxkids_parent)) michael@0: return 0; michael@0: if (!get_uint32(fp, &event->u.stats.calltree_maxstack_top)) michael@0: return 0; michael@0: #ifdef DEBUG_tmreader michael@0: fprintf(stderr, "tmevent %c %u\n", event->type, event->serial); michael@0: #endif michael@0: break; michael@0: default: michael@0: fprintf(stderr, "Unknown event type 0x%x\n", (unsigned int)event->type); michael@0: return 0; michael@0: } michael@0: return 1; michael@0: } michael@0: michael@0: static void *arena_alloc(void* pool, size_t size) michael@0: { michael@0: PLArenaPool* arena = (PLArenaPool*)pool; michael@0: void* result; michael@0: PL_ARENA_ALLOCATE(result, arena, size); michael@0: memset(result, 0, size); michael@0: return result; michael@0: } michael@0: michael@0: static void *generic_alloctable(void *pool, size_t size) michael@0: { michael@0: return arena_alloc(pool, size); michael@0: } michael@0: michael@0: static void generic_freetable(void *pool, void *item) michael@0: { michael@0: /* do nothing - arena-allocated */ michael@0: } michael@0: michael@0: static PLHashEntry *filename_allocentry(void *pool, const void *key) michael@0: { michael@0: return (PLHashEntry*)arena_alloc(pool, sizeof(PLHashEntry)); michael@0: } michael@0: michael@0: static PLHashEntry *callsite_allocentry(void *pool, const void *key) michael@0: { michael@0: return (PLHashEntry*)arena_alloc(pool, sizeof(tmcallsite)); michael@0: } michael@0: michael@0: static void init_graphnode(tmgraphnode* node) michael@0: { michael@0: node->in = node->out = NULL; michael@0: node->up = node->down = node->next = NULL; michael@0: node->low = 0; michael@0: node->allocs.bytes.direct = node->allocs.bytes.total = 0; michael@0: node->allocs.calls.direct = node->allocs.calls.total = 0; michael@0: node->frees.bytes.direct = node->frees.bytes.total = 0; michael@0: node->frees.calls.direct = node->frees.calls.total = 0; michael@0: node->sqsum = 0; michael@0: node->sort = -1; michael@0: } michael@0: michael@0: static PLHashEntry *graphnode_allocentry(void *pool, const void *key) michael@0: { michael@0: tmgraphnode* node = (tmgraphnode*)arena_alloc(pool, sizeof(tmgraphnode)); michael@0: if (!node) michael@0: return NULL; michael@0: init_graphnode(node); michael@0: return &node->entry; michael@0: } michael@0: michael@0: static void init_method(tmmethodnode *node) michael@0: { michael@0: node->graphnode.in = node->graphnode.out = NULL; michael@0: node->graphnode.up = node->graphnode.down = node->graphnode.next = NULL; michael@0: node->graphnode.low = 0; michael@0: node->graphnode.allocs.bytes.direct = node->graphnode.allocs.bytes.total = 0; michael@0: node->graphnode.allocs.calls.direct = node->graphnode.allocs.calls.total = 0; michael@0: node->graphnode.frees.bytes.direct = node->graphnode.frees.bytes.total = 0; michael@0: node->graphnode.frees.calls.direct = node->graphnode.frees.calls.total = 0; michael@0: node->graphnode.sqsum = 0; michael@0: node->graphnode.sort = -1; michael@0: node->sourcefile = NULL; michael@0: node->linenumber = 0; michael@0: } michael@0: michael@0: static PLHashEntry *method_allocentry(void *pool, const void *key) michael@0: { michael@0: tmmethodnode *node = michael@0: (tmmethodnode*) arena_alloc(pool, sizeof(tmmethodnode)); michael@0: if (!node) michael@0: return NULL; michael@0: init_method(node); michael@0: return &node->graphnode.entry; michael@0: } michael@0: michael@0: static void graphnode_freeentry(void *pool, PLHashEntry *he, unsigned flag) michael@0: { michael@0: /* Always free the value, which points to a strdup'd string. */ michael@0: free(he->value); michael@0: #if 0 /* using arenas now, no freeing! */ michael@0: /* Free the whole thing if we're told to. */ michael@0: if (flag == HT_FREE_ENTRY) michael@0: free((void*) he); michael@0: #endif michael@0: } michael@0: michael@0: static void component_freeentry(void *pool, PLHashEntry *he, unsigned flag) michael@0: { michael@0: if (flag == HT_FREE_ENTRY) { michael@0: tmgraphnode *comp = (tmgraphnode*) he; michael@0: michael@0: /* Free the key, which was strdup'd (N.B. value also points to it). */ michael@0: free((void*) tmcomponent_name(comp)); michael@0: #if 0 /* using arenas now, no freeing! */ michael@0: free((void*) comp); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: static PLHashAllocOps filename_hashallocops = { michael@0: generic_alloctable, generic_freetable, michael@0: filename_allocentry, graphnode_freeentry michael@0: }; michael@0: michael@0: static PLHashAllocOps callsite_hashallocops = { michael@0: generic_alloctable, generic_freetable, michael@0: callsite_allocentry, graphnode_freeentry michael@0: }; michael@0: michael@0: static PLHashAllocOps graphnode_hashallocops = { michael@0: generic_alloctable, generic_freetable, michael@0: graphnode_allocentry, graphnode_freeentry michael@0: }; michael@0: michael@0: static PLHashAllocOps method_hashallocops = { michael@0: generic_alloctable, generic_freetable, michael@0: method_allocentry, graphnode_freeentry michael@0: }; michael@0: michael@0: static PLHashAllocOps component_hashallocops = { michael@0: generic_alloctable, generic_freetable, michael@0: graphnode_allocentry, component_freeentry michael@0: }; michael@0: michael@0: static PLHashNumber hash_serial(const void *key) michael@0: { michael@0: return (PLHashNumber) key; michael@0: } michael@0: michael@0: tmreader *tmreader_new(const char *program, void *data) michael@0: { michael@0: tmreader *tmr; michael@0: michael@0: tmr = calloc(1, sizeof *tmr); michael@0: if (!tmr) michael@0: return NULL; michael@0: tmr->program = program; michael@0: tmr->data = data; michael@0: PL_INIT_ARENA_POOL(&tmr->arena, "TMReader", 256*1024); michael@0: michael@0: tmr->libraries = PL_NewHashTable(100, hash_serial, PL_CompareValues, michael@0: PL_CompareStrings, &graphnode_hashallocops, michael@0: &tmr->arena); michael@0: tmr->filenames = PL_NewHashTable(100, hash_serial, PL_CompareValues, michael@0: PL_CompareStrings, &filename_hashallocops, michael@0: &tmr->arena); michael@0: tmr->components = PL_NewHashTable(10000, PL_HashString, PL_CompareStrings, michael@0: PL_CompareValues, &component_hashallocops, michael@0: &tmr->arena); michael@0: tmr->methods = PL_NewHashTable(10000, hash_serial, PL_CompareValues, michael@0: PL_CompareStrings, &method_hashallocops, michael@0: &tmr->arena); michael@0: tmr->callsites = PL_NewHashTable(200000, hash_serial, PL_CompareValues, michael@0: PL_CompareValues, &callsite_hashallocops, michael@0: &tmr->arena); michael@0: tmr->calltree_root.entry.value = (void*) strdup("root"); michael@0: michael@0: if (!tmr->libraries || !tmr->components || !tmr->methods || michael@0: !tmr->callsites || !tmr->calltree_root.entry.value || michael@0: !tmr->filenames) { michael@0: tmreader_destroy(tmr); michael@0: return NULL; michael@0: } michael@0: return tmr; michael@0: } michael@0: michael@0: void tmreader_destroy(tmreader *tmr) michael@0: { michael@0: if (tmr->libraries) michael@0: PL_HashTableDestroy(tmr->libraries); michael@0: if (tmr->filenames) michael@0: PL_HashTableDestroy(tmr->filenames); michael@0: if (tmr->components) michael@0: PL_HashTableDestroy(tmr->components); michael@0: if (tmr->methods) michael@0: PL_HashTableDestroy(tmr->methods); michael@0: if (tmr->callsites) michael@0: PL_HashTableDestroy(tmr->callsites); michael@0: PL_FinishArenaPool(&tmr->arena); michael@0: free(tmr); michael@0: } michael@0: michael@0: int tmreader_eventloop(tmreader *tmr, const char *filename, michael@0: tmeventhandler eventhandler) michael@0: { michael@0: FILE *fp; michael@0: char buf[NS_TRACE_MALLOC_MAGIC_SIZE]; michael@0: tmevent event; michael@0: static const char magic[] = NS_TRACE_MALLOC_MAGIC; michael@0: michael@0: if (strcmp(filename, "-") == 0) { michael@0: fp = stdin; michael@0: } else { michael@0: #if defined(XP_WIN32) michael@0: fp = fopen(filename, "rb"); michael@0: #else michael@0: fp = fopen(filename, "r"); michael@0: #endif michael@0: if (!fp) { michael@0: fprintf(stderr, "%s: can't open %s: %s.\n", michael@0: tmr->program, filename, strerror(errno)); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: if (read(fileno(fp), buf, sizeof buf) != sizeof buf || michael@0: strncmp(buf, magic, sizeof buf) != 0) { michael@0: fprintf(stderr, "%s: bad magic string %s at start of %s.\n", michael@0: tmr->program, buf, filename); michael@0: fprintf(stderr, "either the data file is out of date,\nor your tools are out of date.\n"); michael@0: return 0; michael@0: } michael@0: michael@0: /* Read in ticks per second. Used to convert platform specific intervals to time values */ michael@0: if (read(fileno(fp), &tmr->ticksPerSec, sizeof tmr->ticksPerSec) != sizeof tmr->ticksPerSec) { michael@0: fprintf(stderr, "%s: Cannot read ticksPerSec. Log file read error.\n", michael@0: tmr->program); michael@0: return 0; michael@0: } michael@0: tmr->ticksPerSec = PR_ntohl(tmr->ticksPerSec); michael@0: #ifdef DEBUG_dp michael@0: printf("DEBUG: ticks per sec = %d\n", tmr->ticksPerSec); michael@0: #endif michael@0: while (get_tmevent(fp, &event)) { michael@0: switch (event.type) { michael@0: case TM_EVENT_LIBRARY: { michael@0: const void *key; michael@0: PLHashNumber hash; michael@0: PLHashEntry **hep, *he; michael@0: michael@0: key = (const void*) (uintptr_t) event.serial; michael@0: hash = hash_serial(key); michael@0: hep = PL_HashTableRawLookup(tmr->libraries, hash, key); michael@0: he = *hep; michael@0: PR_ASSERT(!he); michael@0: if (he) exit(2); michael@0: michael@0: he = PL_HashTableRawAdd(tmr->libraries, hep, hash, key, michael@0: event.u.libname); michael@0: if (!he) { michael@0: perror(tmr->program); michael@0: return -1; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case TM_EVENT_FILENAME: { michael@0: const void *key; michael@0: PLHashNumber hash; michael@0: PLHashEntry **hep, *he; michael@0: michael@0: key = (const void*) (uintptr_t) event.serial; michael@0: hash = hash_serial(key); michael@0: hep = PL_HashTableRawLookup(tmr->filenames, hash, key); michael@0: he = *hep; michael@0: PR_ASSERT(!he); michael@0: if (he) exit(2); michael@0: michael@0: he = PL_HashTableRawAdd(tmr->filenames, hep, hash, key, michael@0: event.u.srcname); michael@0: if (!he) { michael@0: perror(tmr->program); michael@0: return -1; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case TM_EVENT_METHOD: { michael@0: const void *key, *sourcekey; michael@0: PLHashNumber hash, sourcehash; michael@0: PLHashEntry **hep, *he, **sourcehep, *sourcehe; michael@0: char *name, *head, *mark, save; michael@0: tmgraphnode *comp, *lib; michael@0: tmmethodnode *meth; michael@0: michael@0: key = (const void*) (uintptr_t) event.serial; michael@0: hash = hash_serial(key); michael@0: hep = PL_HashTableRawLookup(tmr->methods, hash, key); michael@0: he = *hep; michael@0: PR_ASSERT(!he); michael@0: if (he) exit(2); michael@0: michael@0: name = event.u.method.name; michael@0: he = PL_HashTableRawAdd(tmr->methods, hep, hash, key, name); michael@0: if (!he) { michael@0: perror(tmr->program); michael@0: return -1; michael@0: } michael@0: meth = (tmmethodnode*) he; michael@0: michael@0: meth->linenumber = event.u.method.linenumber; michael@0: sourcekey = (const void*) (uintptr_t) event.u.method.filename; michael@0: sourcehash = hash_serial(sourcekey); michael@0: sourcehep = PL_HashTableRawLookup(tmr->filenames, sourcehash, sourcekey); michael@0: sourcehe = *sourcehep; michael@0: meth->sourcefile = filename_name(sourcehe); michael@0: michael@0: head = name; michael@0: mark = strchr(name, ':'); michael@0: if (!mark) { michael@0: mark = name; michael@0: while (*mark != '\0' && *mark == '_') michael@0: mark++; michael@0: head = mark; michael@0: mark = strchr(head, '_'); michael@0: if (!mark) { michael@0: mark = strchr(head, '+'); michael@0: if (!mark) michael@0: mark = head + strlen(head); michael@0: } michael@0: } michael@0: michael@0: save = *mark; michael@0: *mark = '\0'; michael@0: hash = PL_HashString(head); michael@0: hep = PL_HashTableRawLookup(tmr->components, hash, head); michael@0: he = *hep; michael@0: if (he) { michael@0: comp = (tmgraphnode*) he; michael@0: } else { michael@0: head = strdup(head); michael@0: if (head) { michael@0: he = PL_HashTableRawAdd(tmr->components, hep, hash, head, michael@0: head); michael@0: } michael@0: if (!he) { michael@0: perror(tmr->program); michael@0: return -1; michael@0: } michael@0: comp = (tmgraphnode*) he; michael@0: michael@0: key = (const void*) (uintptr_t) event.u.method.library; michael@0: hash = hash_serial(key); michael@0: lib = (tmgraphnode*) michael@0: *PL_HashTableRawLookup(tmr->libraries, hash, key); michael@0: if (lib) { michael@0: comp->up = lib; michael@0: comp->next = lib->down; michael@0: lib->down = comp; michael@0: } michael@0: } michael@0: *mark = save; michael@0: michael@0: meth->graphnode.up = comp; michael@0: meth->graphnode.next = comp->down; michael@0: comp->down = &(meth->graphnode); michael@0: break; michael@0: } michael@0: michael@0: case TM_EVENT_CALLSITE: { michael@0: const void *key, *mkey; michael@0: PLHashNumber hash, mhash; michael@0: PLHashEntry **hep, *he; michael@0: tmcallsite *site, *parent; michael@0: tmmethodnode *meth; michael@0: michael@0: key = (const void*) (uintptr_t) event.serial; michael@0: hash = hash_serial(key); michael@0: hep = PL_HashTableRawLookup(tmr->callsites, hash, key); michael@0: he = *hep; michael@0: michael@0: /* there should not be an entry here! */ michael@0: PR_ASSERT(!he); michael@0: if (he) exit(2); michael@0: michael@0: if (event.u.site.parent == 0) { michael@0: parent = &tmr->calltree_root; michael@0: } else { michael@0: parent = tmreader_callsite(tmr, event.u.site.parent); michael@0: if (!parent) { michael@0: fprintf(stderr, "%s: no parent for %lu (%lu)!\n", michael@0: tmr->program, (unsigned long) event.serial, michael@0: (unsigned long) event.u.site.parent); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: he = PL_HashTableRawAdd(tmr->callsites, hep, hash, key, NULL); michael@0: if (!he) { michael@0: perror(tmr->program); michael@0: return -1; michael@0: } michael@0: michael@0: site = (tmcallsite*) he; michael@0: site->parent = parent; michael@0: site->siblings = parent->kids; michael@0: parent->kids = site; michael@0: site->kids = NULL; michael@0: michael@0: mkey = (const void*) (uintptr_t) event.u.site.method; michael@0: mhash = hash_serial(mkey); michael@0: meth = (tmmethodnode*) michael@0: *PL_HashTableRawLookup(tmr->methods, mhash, mkey); michael@0: site->method = meth; michael@0: site->offset = event.u.site.offset; michael@0: site->allocs.bytes.direct = site->allocs.bytes.total = 0; michael@0: site->allocs.calls.direct = site->allocs.calls.total = 0; michael@0: site->frees.bytes.direct = site->frees.bytes.total = 0; michael@0: site->frees.calls.direct = site->frees.calls.total = 0; michael@0: break; michael@0: } michael@0: michael@0: case TM_EVENT_MALLOC: michael@0: case TM_EVENT_CALLOC: michael@0: case TM_EVENT_REALLOC: { michael@0: tmcallsite *site; michael@0: uint32_t size, oldsize; michael@0: double delta, sqdelta, sqszdelta = 0; michael@0: tmgraphnode *comp, *lib; michael@0: tmmethodnode *meth; michael@0: michael@0: site = tmreader_callsite(tmr, event.serial); michael@0: if (!site) { michael@0: fprintf(stderr, "%s: no callsite for '%c' (%lu)!\n", michael@0: tmr->program, event.type, (unsigned long) event.serial); michael@0: continue; michael@0: } michael@0: michael@0: size = event.u.alloc.size; michael@0: oldsize = event.u.alloc.oldsize; michael@0: delta = (double)size - (double)oldsize; michael@0: site->allocs.bytes.direct += (unsigned long)delta; michael@0: if (event.type != TM_EVENT_REALLOC) michael@0: site->allocs.calls.direct++; michael@0: meth = site->method; michael@0: if (meth) { michael@0: meth->graphnode.allocs.bytes.direct += (unsigned long)delta; michael@0: sqdelta = delta * delta; michael@0: if (event.type == TM_EVENT_REALLOC) { michael@0: sqszdelta = ((double)size * size) michael@0: - ((double)oldsize * oldsize); michael@0: meth->graphnode.sqsum += sqszdelta; michael@0: } else { michael@0: meth->graphnode.sqsum += sqdelta; michael@0: meth->graphnode.allocs.calls.direct++; michael@0: } michael@0: comp = meth->graphnode.up; michael@0: if (comp) { michael@0: comp->allocs.bytes.direct += (unsigned long)delta; michael@0: if (event.type == TM_EVENT_REALLOC) { michael@0: comp->sqsum += sqszdelta; michael@0: } else { michael@0: comp->sqsum += sqdelta; michael@0: comp->allocs.calls.direct++; michael@0: } michael@0: lib = comp->up; michael@0: if (lib) { michael@0: lib->allocs.bytes.direct += (unsigned long)delta; michael@0: if (event.type == TM_EVENT_REALLOC) { michael@0: lib->sqsum += sqszdelta; michael@0: } else { michael@0: lib->sqsum += sqdelta; michael@0: lib->allocs.calls.direct++; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case TM_EVENT_FREE: { michael@0: tmcallsite *site; michael@0: uint32_t size; michael@0: tmgraphnode *comp, *lib; michael@0: tmmethodnode *meth; michael@0: michael@0: site = tmreader_callsite(tmr, event.serial); michael@0: if (!site) { michael@0: fprintf(stderr, "%s: no callsite for '%c' (%lu)!\n", michael@0: tmr->program, event.type, (unsigned long) event.serial); michael@0: continue; michael@0: } michael@0: size = event.u.alloc.size; michael@0: site->frees.bytes.direct += size; michael@0: site->frees.calls.direct++; michael@0: meth = site->method; michael@0: if (meth) { michael@0: meth->graphnode.frees.bytes.direct += size; michael@0: meth->graphnode.frees.calls.direct++; michael@0: comp = meth->graphnode.up; michael@0: if (comp) { michael@0: comp->frees.bytes.direct += size; michael@0: comp->frees.calls.direct++; michael@0: lib = comp->up; michael@0: if (lib) { michael@0: lib->frees.bytes.direct += size; michael@0: lib->frees.calls.direct++; michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case TM_EVENT_STATS: michael@0: break; michael@0: } michael@0: michael@0: eventhandler(tmr, &event); michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: tmgraphnode *tmreader_library(tmreader *tmr, uint32_t serial) michael@0: { michael@0: const void *key; michael@0: PLHashNumber hash; michael@0: michael@0: key = (const void*) (uintptr_t) serial; michael@0: hash = hash_serial(key); michael@0: return (tmgraphnode*) *PL_HashTableRawLookup(tmr->libraries, hash, key); michael@0: } michael@0: michael@0: tmgraphnode *tmreader_filename(tmreader *tmr, uint32_t serial) michael@0: { michael@0: const void *key; michael@0: PLHashNumber hash; michael@0: michael@0: key = (const void*) (uintptr_t) serial; michael@0: hash = hash_serial(key); michael@0: return (tmgraphnode*) *PL_HashTableRawLookup(tmr->filenames, hash, key); michael@0: } michael@0: michael@0: tmgraphnode *tmreader_component(tmreader *tmr, const char *name) michael@0: { michael@0: PLHashNumber hash; michael@0: michael@0: hash = PL_HashString(name); michael@0: return (tmgraphnode*) *PL_HashTableRawLookup(tmr->components, hash, name); michael@0: } michael@0: michael@0: tmmethodnode *tmreader_method(tmreader *tmr, uint32_t serial) michael@0: { michael@0: const void *key; michael@0: PLHashNumber hash; michael@0: michael@0: key = (const void*) (uintptr_t) serial; michael@0: hash = hash_serial(key); michael@0: return (tmmethodnode*) *PL_HashTableRawLookup(tmr->methods, hash, key); michael@0: } michael@0: michael@0: tmcallsite *tmreader_callsite(tmreader *tmr, uint32_t serial) michael@0: { michael@0: const void *key; michael@0: PLHashNumber hash; michael@0: michael@0: key = (const void*) (uintptr_t) serial; michael@0: hash = hash_serial(key); michael@0: return (tmcallsite*) *PL_HashTableRawLookup(tmr->callsites, hash, key); michael@0: } michael@0: michael@0: int tmgraphnode_connect(tmgraphnode *from, tmgraphnode *to, tmcallsite *site) michael@0: { michael@0: tmgraphlink *outlink; michael@0: tmgraphedge *edge; michael@0: michael@0: for (outlink = from->out; outlink; outlink = outlink->next) { michael@0: if (outlink->node == to) { michael@0: /* michael@0: * Say the stack looks like this: ... => JS => js => JS => js. michael@0: * We must avoid overcounting JS=>js because the first edge total michael@0: * includes the second JS=>js edge's total (which is because the michael@0: * lower site's total includes all its kids' totals). michael@0: */ michael@0: edge = TM_LINK_TO_EDGE(outlink, TM_EDGE_OUT_LINK); michael@0: if (!to->low || to->low < from->low) { michael@0: /* Add the direct and total counts to edge->allocs. */ michael@0: edge->allocs.bytes.direct += site->allocs.bytes.direct; michael@0: edge->allocs.bytes.total += site->allocs.bytes.total; michael@0: edge->allocs.calls.direct += site->allocs.calls.direct; michael@0: edge->allocs.calls.total += site->allocs.calls.total; michael@0: michael@0: /* Now update the free counts. */ michael@0: edge->frees.bytes.direct += site->frees.bytes.direct; michael@0: edge->frees.bytes.total += site->frees.bytes.total; michael@0: edge->frees.calls.direct += site->frees.calls.direct; michael@0: edge->frees.calls.total += site->frees.calls.total; michael@0: } michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: edge = (tmgraphedge*) malloc(sizeof(tmgraphedge)); michael@0: if (!edge) michael@0: return 0; michael@0: edge->links[TM_EDGE_OUT_LINK].node = to; michael@0: edge->links[TM_EDGE_OUT_LINK].next = from->out; michael@0: from->out = &edge->links[TM_EDGE_OUT_LINK]; michael@0: edge->links[TM_EDGE_IN_LINK].node = from; michael@0: edge->links[TM_EDGE_IN_LINK].next = to->in; michael@0: to->in = &edge->links[TM_EDGE_IN_LINK]; michael@0: edge->allocs = site->allocs; michael@0: edge->frees = site->frees; michael@0: return 1; michael@0: }