pjs/tools/trace-malloc/spacecategory.c

997 строки
28 KiB
C
Исходник Обычный вид История

2002-04-17 03:36:47 +04:00
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
2002-04-17 03:36:47 +04:00
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
2002-04-17 03:36:47 +04:00
*
* The Original Code is spacecategory.c code, released
* Apr 12, 2002.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
2002-04-17 03:36:47 +04:00
*
* Contributor(s):
* Suresh Duddi <dp@netscape.com>, 12-April-2002
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
2002-04-17 03:36:47 +04:00
*
* ***** END LICENSE BLOCK ***** */
2002-04-17 03:36:47 +04:00
/*
** spacecategory.c
**
** Cagtegorizes each allocation using a predefined set of rules
** and presents a tree of categories for browsing.
*/
/*
** Required include files.
*/
#include "spacetrace.h"
#include <ctype.h>
#include <string.h>
2002-04-20 04:25:55 +04:00
/*
** Ugh, MSVC6's qsort is too slow...
*/
#include "nsQuickSort.h"
2002-04-17 03:36:47 +04:00
#if defined(HAVE_BOUTELL_GD)
/*
** See http://www.boutell.com/gd for the GD graphics library.
** Ports for many platorms exist.
** Your box may already have the lib (mine did, redhat 7.1 workstation).
*/
#include <gd.h>
#include <gdfontt.h>
#include <gdfonts.h>
#include <gdfontmb.h>
#endif /* HAVE_BOUTELL_GD */
/*
** AddRule
**
** Add a rule into the list of rules maintainted in global
*/
int
AddRule(STGlobals * g, STCategoryRule * rule)
2002-04-17 03:36:47 +04:00
{
if (g->mNRules % ST_ALLOC_STEP == 0) {
2002-04-17 03:36:47 +04:00
/* Need more space */
STCategoryRule **newrules;
2002-04-17 03:36:47 +04:00
newrules = (STCategoryRule **) realloc(g->mCategoryRules,
(g->mNRules +
ST_ALLOC_STEP) *
sizeof(STCategoryRule *));
if (!newrules) {
2002-04-17 03:36:47 +04:00
REPORT_ERROR(__LINE__, AddRule_No_Memory);
return -1;
}
g->mCategoryRules = newrules;
}
g->mCategoryRules[g->mNRules++] = rule;
return 0;
}
/*
** AddChild
**
** Add the node as a child of the parent node
*/
int
AddChild(STCategoryNode * parent, STCategoryNode * child)
2002-04-17 03:36:47 +04:00
{
if (parent->nchildren % ST_ALLOC_STEP == 0) {
2002-04-17 03:36:47 +04:00
/* need more space */
STCategoryNode **newnodes;
2002-04-17 03:36:47 +04:00
newnodes = (STCategoryNode **) realloc(parent->children,
(parent->nchildren +
ST_ALLOC_STEP) *
sizeof(STCategoryNode *));
if (!newnodes) {
2002-04-17 03:36:47 +04:00
REPORT_ERROR(__LINE__, AddChild_No_Memory);
return -1;
}
parent->children = newnodes;
}
parent->children[parent->nchildren++] = child;
return 0;
}
int
ReParent(STCategoryNode * parent, STCategoryNode * child)
2002-04-17 03:36:47 +04:00
{
PRUint32 i;
2002-04-17 03:36:47 +04:00
if (child->parent == parent)
return 0;
/* Remove child from old parent */
if (child->parent) {
for (i = 0; i < child->parent->nchildren; i++) {
if (child->parent->children[i] == child) {
2002-04-17 03:36:47 +04:00
/* Remove child from list */
if (i + 1 < child->parent->nchildren)
memmove(&child->parent->children[i],
&child->parent->children[i + 1],
(child->parent->nchildren - i -
1) * sizeof(STCategoryNode *));
2002-04-17 03:36:47 +04:00
child->parent->nchildren--;
break;
}
}
}
/* Add child into new parent */
AddChild(parent, child);
return 0;
}
/*
** findCategoryNode
**
** Given a category name, finds the Node corresponding to the category
*/
STCategoryNode *
findCategoryNode(const char *catName, STGlobals * g)
2002-04-17 03:36:47 +04:00
{
PRUint32 i;
for (i = 0; i < g->mNCategoryMap; i++) {
2002-04-17 03:36:47 +04:00
if (!strcmp(g->mCategoryMap[i]->categoryName, catName))
return g->mCategoryMap[i]->node;
}
2002-04-19 00:11:45 +04:00
/* Check if we are looking for the root node */
if (!strcmp(catName, ST_ROOT_CATEGORY_NAME))
return &g->mCategoryRoot;
2002-04-17 03:36:47 +04:00
return NULL;
}
/*
** AddCategoryNode
**
** Adds a mapping between a category and its Node into the categoryMap
*/
int
AddCategoryNode(STCategoryNode * node, STGlobals * g)
2002-04-17 03:36:47 +04:00
{
if (g->mNCategoryMap % ST_ALLOC_STEP == 0) {
2002-04-17 03:36:47 +04:00
/* Need more space */
STCategoryMapEntry **newmap =
(STCategoryMapEntry **) realloc(g->mCategoryMap,
(g->mNCategoryMap +
ST_ALLOC_STEP) *
sizeof(STCategoryMapEntry *));
if (!newmap) {
2002-04-17 03:36:47 +04:00
REPORT_ERROR(__LINE__, AddCategoryNode_No_Memory);
return -1;
}
g->mCategoryMap = newmap;
}
g->mCategoryMap[g->mNCategoryMap] =
(STCategoryMapEntry *) calloc(1, sizeof(STCategoryMapEntry));
if (!g->mCategoryMap[g->mNCategoryMap]) {
2002-04-17 03:36:47 +04:00
REPORT_ERROR(__LINE__, AddCategoryNode_No_Memory);
return -1;
}
g->mCategoryMap[g->mNCategoryMap]->categoryName = node->categoryName;
g->mCategoryMap[g->mNCategoryMap]->node = node;
g->mNCategoryMap++;
return 0;
}
/*
** NewCategoryNode
**
** Creates a new category node for category name 'catname' and makes
** 'parent' its parent.
*/
STCategoryNode *
NewCategoryNode(const char *catName, STCategoryNode * parent, STGlobals * g)
2002-04-17 03:36:47 +04:00
{
STCategoryNode *node;
2002-04-17 03:36:47 +04:00
node = (STCategoryNode *) calloc(1, sizeof(STCategoryNode));
if (!node)
return NULL;
node->runs =
(STRun **) calloc(g->mCommandLineOptions.mContexts, sizeof(STRun *));
if (NULL == node->runs) {
free(node);
return NULL;
}
2002-04-17 03:36:47 +04:00
node->categoryName = catName;
/* Set parent of child */
node->parent = parent;
/* Set child in parent */
AddChild(parent, node);
/* Add node into mapping table */
AddCategoryNode(node, g);
return node;
}
/*
** ProcessCategoryLeafRule
**
** Add this into the tree as a leaf node. It doesn't know who its parent is. For now we make
2002-04-17 03:36:47 +04:00
** root as its parent
*/
int
ProcessCategoryLeafRule(STCategoryRule * leafRule, STCategoryNode * root,
STGlobals * g)
2002-04-17 03:36:47 +04:00
{
STCategoryRule *rule;
STCategoryNode *node;
2002-04-17 03:36:47 +04:00
rule = (STCategoryRule *) calloc(1, sizeof(STCategoryRule));
if (!rule)
return -1;
/* Take ownership of all elements of rule */
*rule = *leafRule;
/* Find/Make a STCategoryNode and add it into the tree */
node = findCategoryNode(rule->categoryName, g);
if (!node)
node = NewCategoryNode(rule->categoryName, root, g);
/* Make sure rule knows which node to access */
rule->node = node;
/* Add rule into rulelist */
AddRule(g, rule);
return 0;
}
/*
** ProcessCategoryParentRule
**
** Rule has all the children of category as patterns. Sets up the tree so that
** the parent child relationship is honored.
*/
int
ProcessCategoryParentRule(STCategoryRule * parentRule, STCategoryNode * root,
STGlobals * g)
2002-04-17 03:36:47 +04:00
{
STCategoryNode *node;
STCategoryNode *child;
2002-04-17 03:36:47 +04:00
PRUint32 i;
/* Find the parent node in the tree. If not make one and add it into the tree */
node = findCategoryNode(parentRule->categoryName, g);
if (!node) {
2002-04-17 03:36:47 +04:00
node = NewCategoryNode(parentRule->categoryName, root, g);
if (!node)
return -1;
}
/* For every child node, Find/Create it and make it the child of this node */
for (i = 0; i < parentRule->npats; i++) {
2002-04-17 03:36:47 +04:00
child = findCategoryNode(parentRule->pats[i], g);
if (!child) {
2002-04-17 03:36:47 +04:00
child = NewCategoryNode(parentRule->pats[i], node, g);
if (!child)
return -1;
}
else {
2002-04-17 03:36:47 +04:00
/* Reparent child to node. This is because when we created the node
** we would have created it as the child of root. Now we need to
** remove it from root's child list and add it into this node
*/
2002-04-17 03:36:47 +04:00
ReParent(node, child);
}
}
return 0;
}
/*
** initCategories
**
** Initialize all categories. This reads in a file that says how to categorize
** each callsite, creates a tree of these categories and makes a list of these
** patterns in order for matching
*/
int
initCategories(STGlobals * g)
2002-04-17 03:36:47 +04:00
{
FILE *fp;
char buf[1024], *in;
int n;
2002-04-17 03:36:47 +04:00
PRBool inrule, leaf;
STCategoryRule rule;
fp = fopen(g->mCommandLineOptions.mCategoryFile, "r");
if (!fp) {
/* It isn't an error to not have a categories file */
REPORT_INFO("No categories file.");
2002-04-17 03:36:47 +04:00
return -1;
}
inrule = PR_FALSE;
leaf = PR_FALSE;
memset(&rule, 0, sizeof(rule));
while (fgets(buf, sizeof(buf), fp) != NULL) {
2002-04-17 03:36:47 +04:00
/* Lose the \n */
n = strlen(buf);
if (buf[n - 1] == '\n')
2002-04-17 03:36:47 +04:00
buf[--n] = '\0';
in = buf;
/* skip comments */
if (*in == '#')
continue;
/* skip empty lines. If we are in a rule, end the rule. */
while (*in && isspace(*in))
2002-04-17 03:36:47 +04:00
in++;
if (*in == '\0') {
if (inrule) {
/* End the rule : leaf or non-leaf */
2002-04-17 03:36:47 +04:00
if (leaf)
ProcessCategoryLeafRule(&rule, &g->mCategoryRoot, g);
else
/* non-leaf */
ProcessCategoryParentRule(&rule, &g->mCategoryRoot, g);
inrule = PR_FALSE;
memset(&rule, 0, sizeof(rule));
}
continue;
}
/* if we are in a rule acculumate */
if (inrule) {
rule.pats[rule.npats] = strdup(in);
rule.patlen[rule.npats++] = strlen(in);
2002-04-17 03:36:47 +04:00
}
else if (*in == '<') {
2002-04-17 03:36:47 +04:00
/* Start a category */
inrule = PR_TRUE;
leaf = PR_TRUE;
/* Get the category name */
in++;
n = strlen(in);
if (in[n - 1] == '>')
in[n - 1] = '\0';
2002-04-17 03:36:47 +04:00
rule.categoryName = strdup(in);
}
else {
2002-04-17 03:36:47 +04:00
/* this is a non-leaf category. Should be of the form CategoryName
** followed by list of child category names one per line
*/
2002-04-17 03:36:47 +04:00
inrule = PR_TRUE;
leaf = PR_FALSE;
rule.categoryName = strdup(in);
}
}
/* If we were in a rule when processing the last line, end the rule */
if (inrule) {
/* End the rule : leaf or non-leaf */
2002-04-17 03:36:47 +04:00
if (leaf)
ProcessCategoryLeafRule(&rule, &g->mCategoryRoot, g);
else
/* non-leaf */
ProcessCategoryParentRule(&rule, &g->mCategoryRoot, g);
}
/* Add the final "uncategorized" category. We make new memory locations
** for all these to conform to the general principle of all strings are allocated
** so it makes release logic very simple.
*/
2002-04-17 03:36:47 +04:00
memset(&rule, 0, sizeof(rule));
rule.categoryName = strdup("uncategorized");
rule.pats[0] = strdup("");
rule.patlen[0] = 0;
2002-04-17 03:36:47 +04:00
rule.npats = 1;
ProcessCategoryLeafRule(&rule, &g->mCategoryRoot, g);
return 0;
}
/*
** callsiteMatchesRule
**
** Returns the corresponding node if callsite matches the rule. Rule is a sequence
** of patterns that must match contiguously the callsite.
*/
int
callsiteMatchesRule(tmcallsite * aCallsite, STCategoryRule * aRule)
2002-04-17 03:36:47 +04:00
{
PRUint32 patnum = 0;
const char *methodName = NULL;
while (patnum < aRule->npats && aCallsite && aCallsite->method) {
2002-04-17 03:36:47 +04:00
methodName = tmmethodnode_name(aCallsite->method);
if (!methodName)
return 0;
if (!*aRule->pats[patnum]
|| !strncmp(methodName, aRule->pats[patnum],
aRule->patlen[patnum])) {
2002-04-17 03:36:47 +04:00
/* We have matched so far. Proceed up the stack and to the next pattern */
patnum++;
aCallsite = aCallsite->parent;
}
else {
2002-04-17 03:36:47 +04:00
/* Deal with mismatch */
if (patnum > 0) {
2002-04-17 03:36:47 +04:00
/* contiguous mismatch. Stop */
return 0;
}
/* We still haven't matched the first pattern. Proceed up the stack without
** moving to the next pattern.
*/
2002-04-17 03:36:47 +04:00
aCallsite = aCallsite->parent;
}
}
if (patnum == aRule->npats) {
2002-04-17 03:36:47 +04:00
/* all patterns matched. We have a winner. */
#if defined(DEBUG_dp) && 0
fprintf(stderr, "[%s] match\n", aRule->categoryName);
#endif
return 1;
}
return 0;
}
#ifdef DEBUG_dp
PRIntervalTime _gMatchTime = 0;
PRUint32 _gMatchCount = 0;
PRUint32 _gMatchRules = 0;
#endif
2002-04-17 03:36:47 +04:00
/*
** matchAllocation
**
** Runs through all rules and returns the node corresponding to
** a match of the allocation.
*/
STCategoryNode *
matchAllocation(STGlobals * g, STAllocation * aAllocation)
2002-04-17 03:36:47 +04:00
{
#ifdef DEBUG_dp
PRIntervalTime start = PR_IntervalNow();
#endif
2002-04-17 03:36:47 +04:00
PRUint32 rulenum;
STCategoryNode *node = NULL;
STCategoryRule *rule;
2002-04-17 03:36:47 +04:00
for (rulenum = 0; rulenum < g->mNRules; rulenum++) {
#ifdef DEBUG_dp
_gMatchRules++;
#endif
2002-04-17 03:36:47 +04:00
rule = g->mCategoryRules[rulenum];
if (callsiteMatchesRule(aAllocation->mEvents[0].mCallsite, rule)) {
node = rule->node;
break;
}
2002-04-17 03:36:47 +04:00
}
#ifdef DEBUG_dp
_gMatchCount++;
_gMatchTime += PR_IntervalNow() - start;
#endif
return node;
2002-04-17 03:36:47 +04:00
}
/*
** categorizeAllocation
**
** Given an allocation, it adds it into the category tree at the right spot
** by comparing the allocation to the rules and adding into the right node.
** Also, does propogation of cost upwards in the tree.
** The root of the tree is in the globls as the tree is dependent on the
** category file (options) rather than the run.
*/
int
categorizeAllocation(STOptions * inOptions, STContext * inContext,
STAllocation * aAllocation, STGlobals * g)
2002-04-17 03:36:47 +04:00
{
/* Run through the rules in order to see if this allcation matches
** any of them.
*/
STCategoryNode *node;
2002-04-17 03:36:47 +04:00
node = matchAllocation(g, aAllocation);
if (!node) {
2002-04-17 03:36:47 +04:00
/* ugh! it should atleast go into the "uncategorized" node. wierd!
*/
2002-04-17 03:36:47 +04:00
REPORT_ERROR(__LINE__, categorizeAllocation);
return -1;
}
/* Create run for node if not already */
if (!node->runs[inContext->mIndex]) {
2002-04-17 03:36:47 +04:00
/*
** Create run with positive timestamp as we can harvest it later
** for callsite details summarization
*/
node->runs[inContext->mIndex] =
createRun(inContext, PR_IntervalNow());
if (!node->runs[inContext->mIndex]) {
2002-04-17 03:36:47 +04:00
REPORT_ERROR(__LINE__, categorizeAllocation_No_Memory);
return -1;
}
}
/* Add allocation into node. We expand the table of allocations in steps */
if (node->runs[inContext->mIndex]->mAllocationCount % ST_ALLOC_STEP == 0) {
2002-04-17 03:36:47 +04:00
/* Need more space */
STAllocation **allocs;
allocs =
(STAllocation **) realloc(node->runs[inContext->mIndex]->
mAllocations,
(node->runs[inContext->mIndex]->
mAllocationCount +
ST_ALLOC_STEP) *
sizeof(STAllocation *));
if (!allocs) {
2002-04-17 03:36:47 +04:00
REPORT_ERROR(__LINE__, categorizeAllocation_No_Memory);
return -1;
}
node->runs[inContext->mIndex]->mAllocations = allocs;
2002-04-17 03:36:47 +04:00
}
node->runs[inContext->mIndex]->mAllocations[node->
runs[inContext->mIndex]->
mAllocationCount++] =
aAllocation;
2002-04-17 03:36:47 +04:00
2002-04-19 00:11:45 +04:00
/*
** Make sure run's stats are calculated. We don't go update the parents of allocation
** at this time. That will happen when we focus on this category. This updating of
** stats will provide us fast categoryreports.
*/
recalculateAllocationCost(inOptions, inContext,
node->runs[inContext->mIndex], aAllocation,
PR_FALSE);
2002-04-19 00:11:45 +04:00
2002-04-17 03:36:47 +04:00
/* Propogate upwards the statistics */
/* XXX */
#if defined(DEBUG_dp) && 0
fprintf(stderr, "DEBUG: [%s] match\n", node->categoryName);
#endif
return 0;
}
typedef PRBool STCategoryNodeProcessor(STRequest * inRequest,
STOptions * inOptions,
STContext * inContext,
void *clientData,
STCategoryNode * node);
PRBool
freeNodeRunProcessor(STRequest * inRequest, STOptions * inOptions,
STContext * inContext, void *clientData,
STCategoryNode * node)
{
if (node->runs && node->runs[inContext->mIndex]) {
freeRun(node->runs[inContext->mIndex]);
node->runs[inContext->mIndex] = NULL;
}
return PR_TRUE;
}
2002-04-17 03:36:47 +04:00
PRBool
freeNodeRunsProcessor(STRequest * inRequest, STOptions * inOptions,
STContext * inContext, void *clientData,
STCategoryNode * node)
2002-04-17 03:36:47 +04:00
{
if (node->runs) {
PRUint32 loop = 0;
for (loop = 0; loop < globals.mCommandLineOptions.mContexts; loop++) {
if (node->runs[loop]) {
freeRun(node->runs[loop]);
node->runs[loop] = NULL;
}
}
free(node->runs);
node->runs = NULL;
2002-04-17 03:36:47 +04:00
}
2002-04-17 03:36:47 +04:00
return PR_TRUE;
}
#if defined(DEBUG_dp)
PRBool
printNodeProcessor(STRequest * inRequest, STOptions * inOptions,
STContext * inContext, void *clientData,
STCategoryNode * node)
2002-04-17 03:36:47 +04:00
{
STCategoryNode *root = (STCategoryNode *) clientData;
2002-05-01 03:44:03 +04:00
fprintf(stderr, "%-25s [ %9s size", node->categoryName,
FormatNumber(node->run ? node->run->mStats[inContext->mIndex].
mSize : 0));
2002-05-01 03:44:03 +04:00
fprintf(stderr, ", %5.1f%%",
node->run ? ((double) node->run->mStats[inContext->mIndex].mSize /
root->run->mStats[inContext->mIndex].mSize *
100) : 0);
2002-05-01 03:44:03 +04:00
fprintf(stderr, ", %7s allocations ]\n",
FormatNumber(node->run ? node->run->mStats[inContext->mIndex].
mCompositeCount : 0));
2002-04-17 03:36:47 +04:00
return PR_TRUE;
}
#endif
typedef struct __struct_optcon
{
STOptions *mOptions;
STContext *mContext;
}
optcon;
2002-04-20 04:25:55 +04:00
/*
** compareNode
**
** qsort callback.
** Compare the nodes as specified by the options.
*/
int
compareNode(const void *aNode1, const void *aNode2, void *aContext)
2002-04-20 04:25:55 +04:00
{
int retval = 0;
STCategoryNode *node1, *node2;
2002-04-20 04:25:55 +04:00
PRUint32 a, b;
optcon *oc = (optcon *) aContext;
2002-04-20 04:25:55 +04:00
if (!aNode1 || !aNode2 || !oc->mOptions || !oc->mContext)
2002-04-20 04:25:55 +04:00
return 0;
node1 = *((STCategoryNode **) aNode1);
node2 = *((STCategoryNode **) aNode2);
if (node1 && node2) {
if (oc->mOptions->mOrderBy == ST_COUNT) {
a = (node1->runs[oc->mContext->mIndex]) ? node1->runs[oc->
mContext->
mIndex]->
mStats[oc->mContext->mIndex].mCompositeCount : 0;
b = (node2->runs[oc->mContext->mIndex]) ? node2->runs[oc->
mContext->
mIndex]->
mStats[oc->mContext->mIndex].mCompositeCount : 0;
2002-04-20 04:25:55 +04:00
}
else {
2002-04-20 04:25:55 +04:00
/* Default is by size */
a = (node1->runs[oc->mContext->mIndex]) ? node1->runs[oc->
mContext->
mIndex]->
mStats[oc->mContext->mIndex].mSize : 0;
b = (node2->runs[oc->mContext->mIndex]) ? node2->runs[oc->
mContext->
mIndex]->
mStats[oc->mContext->mIndex].mSize : 0;
2002-04-20 04:25:55 +04:00
}
if (a < b)
retval = __LINE__;
else
retval = -__LINE__;
2002-04-20 04:25:55 +04:00
}
return retval;
}
PRBool
sortNodeProcessor(STRequest * inRequest, STOptions * inOptions,
STContext * inContext, void *clientData,
STCategoryNode * node)
2002-04-20 04:25:55 +04:00
{
if (node->nchildren) {
optcon context;
context.mOptions = inOptions;
context.mContext = inContext;
NS_QuickSort(node->children, node->nchildren,
sizeof(STCategoryNode *), compareNode, &context);
}
2002-04-20 04:25:55 +04:00
return PR_TRUE;
}
2002-04-17 03:36:47 +04:00
/*
** walkTree
**
2002-04-19 00:11:45 +04:00
** General purpose tree walker. Issues callback for each node.
** If 'maxdepth' > 0, then stops after processing that depth. Root is
** depth 0, the nodes below it are depth 1 etc...
2002-04-17 03:36:47 +04:00
*/
#define MODINC(n, mod) ((n+1) % mod)
void
walkTree(STCategoryNode * root, STCategoryNodeProcessor func,
STRequest * inRequest, STOptions * inOptions, STContext * inContext,
void *clientData, int maxdepth)
2002-04-17 03:36:47 +04:00
{
STCategoryNode *nodes[1024], *node;
2002-04-17 03:36:47 +04:00
PRUint32 begin, end, i;
int ret = 0;
2002-04-19 00:11:45 +04:00
int curdepth = 0, ncurdepth = 0;
2002-04-17 03:36:47 +04:00
nodes[0] = root;
begin = 0;
end = 1;
2002-04-19 00:11:45 +04:00
ncurdepth = 1;
while (begin != end) {
2002-04-17 03:36:47 +04:00
node = nodes[begin];
ret = (*func) (inRequest, inOptions, inContext, clientData, node);
if (ret == PR_FALSE) {
2002-04-17 03:36:47 +04:00
/* Abort */
break;
}
begin = MODINC(begin, 1024);
for (i = 0; i < node->nchildren; i++) {
2002-04-17 03:36:47 +04:00
nodes[end] = node->children[i];
end = MODINC(end, 1024);
}
2002-04-19 00:11:45 +04:00
/* Depth tracking. Do it only if walkTree is contrained by a maxdepth */
if (maxdepth > 0 && --ncurdepth == 0) {
2002-04-19 00:11:45 +04:00
/*
** No more children in current depth. The rest of the nodes
** we have in our list should be nodes in the depth below us.
*/
2002-04-19 00:11:45 +04:00
ncurdepth = (begin < end) ? (end - begin) : (1024 - begin + end);
if (++curdepth > maxdepth) {
2002-04-19 00:11:45 +04:00
/*
** Gone too deep. Stop.
*/
2002-04-19 00:11:45 +04:00
break;
}
}
2002-04-17 03:36:47 +04:00
}
return;
}
int
freeRule(STCategoryRule * rule)
2002-04-17 03:36:47 +04:00
{
PRUint32 i;
char *p = (char *) rule->categoryName;
2002-04-17 03:36:47 +04:00
PR_FREEIF(p);
for (i = 0; i < rule->npats; i++)
free(rule->pats[i]);
free(rule);
return 0;
}
void
freeNodeRuns(STCategoryNode * root)
2002-04-17 03:36:47 +04:00
{
walkTree(root, freeNodeRunsProcessor, NULL, NULL, NULL, NULL, 0);
2002-04-17 03:36:47 +04:00
}
void
freeNodeMap(STGlobals * g)
2002-04-17 03:36:47 +04:00
{
PRUint32 i;
/* all nodes are in the map table. Just delete all of those. */
for (i = 0; i < g->mNCategoryMap; i++) {
2002-04-17 03:36:47 +04:00
free(g->mCategoryMap[i]->node);
free(g->mCategoryMap[i]);
}
free(g->mCategoryMap);
}
int
freeCategories(STGlobals * g)
2002-04-17 03:36:47 +04:00
{
PRUint32 i;
/*
** walk the tree and free runs held in nodes
*/
freeNodeRuns(&g->mCategoryRoot);
2002-04-17 03:36:47 +04:00
/*
** delete nodemap. This is the where nodes get deleted.
*/
2002-04-17 03:36:47 +04:00
freeNodeMap(g);
/*
** delete rule stuff
*/
for (i = 0; i < g->mNRules; i++) {
2002-04-17 03:36:47 +04:00
freeRule(g->mCategoryRules[i]);
}
free(g->mCategoryRules);
return 0;
}
/*
** categorizeRun
**
** categorize all the allocations of the run using the rules into
** a tree rooted at globls.mCategoryRoot
*/
int
categorizeRun(STOptions * inOptions, STContext * inContext,
const STRun * aRun, STGlobals * g)
2002-04-17 03:36:47 +04:00
{
PRUint32 i;
2002-04-17 03:36:47 +04:00
#if defined(DEBUG_dp)
PRIntervalTime start = PR_IntervalNow();
2002-04-17 03:36:47 +04:00
fprintf(stderr, "DEBUG: categorizing run...\n");
#endif
/*
** First, cleanup our tree
*/
walkTree(&g->mCategoryRoot, freeNodeRunProcessor, NULL, inOptions,
inContext, NULL, 0);
if (g->mNCategoryMap > 0) {
for (i = 0; i < aRun->mAllocationCount; i++) {
categorizeAllocation(inOptions, inContext, aRun->mAllocations[i],
g);
}
2002-04-17 03:36:47 +04:00
}
/*
** the run is always going to be the one corresponding to the root node
*/
g->mCategoryRoot.runs[inContext->mIndex] = (STRun *) aRun;
2002-04-17 03:36:47 +04:00
g->mCategoryRoot.categoryName = ST_ROOT_CATEGORY_NAME;
#if defined(DEBUG_dp)
fprintf(stderr,
"DEBUG: categorizing ends: %dms [%d rules, %d allocations]\n",
PR_IntervalToMilliseconds(PR_IntervalNow() - start), g->mNRules,
aRun->mAllocationCount);
fprintf(stderr, "DEBUG: match : %dms [%d calls, %d rule-compares]\n",
PR_IntervalToMilliseconds(_gMatchTime), _gMatchCount,
_gMatchRules);
2002-04-17 03:36:47 +04:00
#endif
2002-04-20 04:25:55 +04:00
/*
** sort the tree based on our sort criterion
*/
walkTree(&g->mCategoryRoot, sortNodeProcessor, NULL, inOptions, inContext,
NULL, 0);
2002-04-20 04:25:55 +04:00
#if defined(DEBUG_dp)
walkTree(&g->mCategoryRoot, printNodeProcessor, NULL, inOptions,
inContext, &g->mCategoryRoot, 0);
2002-04-20 04:25:55 +04:00
#endif
2002-04-17 03:36:47 +04:00
return 0;
}
2002-04-19 00:11:45 +04:00
/*
** displayCategoryReport
**
** Generate the category report - a list of all categories and details about each
** depth parameter controls how deep we traverse the category tree.
*/
PRBool
displayCategoryNodeProcessor(STRequest * inRequest, STOptions * inOptions,
STContext * inContext, void *clientData,
STCategoryNode * node)
2002-04-19 00:11:45 +04:00
{
STCategoryNode *root = (STCategoryNode *) clientData;
2002-04-19 00:11:45 +04:00
PRUint32 byteSize = 0, heapCost = 0, count = 0;
double percent = 0;
STOptions customOps;
2002-04-19 00:11:45 +04:00
if (node->runs[inContext->mIndex]) {
2002-04-19 00:11:45 +04:00
/*
** Byte size
*/
byteSize =
node->runs[inContext->mIndex]->mStats[inContext->mIndex].mSize;
2002-04-19 00:11:45 +04:00
/*
** Composite count
*/
count =
node->runs[inContext->mIndex]->mStats[inContext->mIndex].
mCompositeCount;
2002-04-19 00:11:45 +04:00
/*
** Heap operation cost
**/
heapCost =
node->runs[inContext->mIndex]->mStats[inContext->mIndex].
mHeapRuntimeCost;
2002-04-19 00:11:45 +04:00
/*
** % of total size
*/
if (root->runs[inContext->mIndex]) {
percent =
((double) byteSize) /
root->runs[inContext->mIndex]->mStats[inContext->mIndex].
mSize * 100;
2002-04-19 00:11:45 +04:00
}
}
PR_fprintf(inRequest->mFD, " <tr>\n" " <td>");
2002-04-19 00:11:45 +04:00
/* a link to topcallsites report with focus on category */
memcpy(&customOps, inOptions, sizeof(customOps));
PR_snprintf(customOps.mCategoryName, sizeof(customOps.mCategoryName),
"%s", node->categoryName);
htmlAnchor(inRequest, "top_callsites.html", node->categoryName, NULL,
"category-callsites", &customOps);
PR_fprintf(inRequest->mFD,
"</td>\n" " <td align=right>%u</td>\n"
2002-04-19 00:11:45 +04:00
" <td align=right>%4.1f%%</td>\n"
" <td align=right>%u</td>\n" " <td align=right>"
ST_MICROVAL_FORMAT "</td>\n" " </tr>\n", byteSize, percent,
count, ST_MICROVAL_PRINTABLE(heapCost));
2002-04-19 00:11:45 +04:00
return PR_TRUE;
}
int
displayCategoryReport(STRequest * inRequest, STCategoryNode * root, int depth)
2002-04-19 00:11:45 +04:00
{
PR_fprintf(inRequest->mFD,
"<table class=\"category-list data\">\n"
" <tr class=\"row-header\">\n"
2002-04-19 00:11:45 +04:00
" <th>Category</th>\n"
" <th>Composite Byte Size</th>\n"
" <th>%% of Total Size</th>\n"
" <th>Heap Object Count</th>\n"
" <th>Composite Heap Operations Seconds</th>\n" " </tr>\n");
2002-04-19 00:11:45 +04:00
walkTree(root, displayCategoryNodeProcessor, inRequest,
&inRequest->mOptions, inRequest->mContext, root, depth);
2002-04-19 00:11:45 +04:00
PR_fprintf(inRequest->mFD, "</table>\n");
2002-04-19 00:11:45 +04:00
return 0;
}