Bug 597811: Make mozJSComponentLoader use JSVERSION_LATEST. (r=sayrer)
[mozilla-central.git] / tools / trace-malloc / spacetrace.h
blob1f0905778ca3345d1180f46ffc44383955333d84
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is spacetrace.h/spacetrace.c code, released
17 * Nov 6, 2001.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 2001
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Garrett Arch Blythe, 31-October-2001
26 * Suresh Duddi <dp@netscape.com>
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 #ifndef spacetrace_h__
43 #define spacetrace_h__
46 ** spacetrace.h
48 ** SpaceTrace is meant to take the output of trace-malloc and present
49 ** a picture of allocations over the run of the application.
53 ** Required includes.
55 #include "nspr.h"
56 #include "prlock.h"
57 #include "prrwlock.h"
58 #include "nsTraceMalloc.h"
59 #include "tmreader.h"
60 #include "formdata.h"
63 ** Turn on to attempt adding support for graphs on your platform.
65 #if defined(HAVE_BOUTELL_GD)
66 #define ST_WANT_GRAPHS 1
67 #endif /* HAVE_BOUTELL_GD */
68 #if !defined(ST_WANT_GRAPHS)
69 #define ST_WANT_GRAPHS 0
70 #endif
73 ** REPORT_ERROR
74 ** REPORT_INFO
76 ** Just report errors and stuff in a consistent manner.
78 #define REPORT_ERROR(code, function) \
79 PR_fprintf(PR_STDERR, "error(%d):\t%s\n", code, #function)
80 #define REPORT_ERROR_MSG(code, msg) \
81 PR_fprintf(PR_STDERR, "error(%d):\t%s\n", code, msg)
82 #define REPORT_INFO(msg) \
83 PR_fprintf(PR_STDOUT, "%s: %s\n", globals.mProgramName, (msg))
85 #if defined(DEBUG_blythe) && 1
86 #define REPORT_blythe(code, msg) \
87 PR_fprintf(PR_STDOUT, "gab(%d):\t%s\n", code, msg)
88 #else
89 #define REPORT_blythe(code, msg)
90 #endif /* DEBUG_blythe */
93 ** CALLSITE_RUN
95 ** How to get a callsite run.
96 ** Allows for further indirection if needed later.
98 #define CALLSITE_RUN(callsite) \
99 ((STRun*)((callsite)->data))
102 ** ST_PERMS
103 ** ST_FLAGS
105 ** File permissions we desire.
106 ** 0644
108 #define ST_PERMS (PR_IRUSR | PR_IWUSR | PR_IRGRP | PR_IROTH)
109 #define ST_FLAGS (PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE)
112 ** Sorting order
114 #define ST_WEIGHT 0 /* size * timeval */
115 #define ST_SIZE 1
116 #define ST_TIMEVAL 2
117 #define ST_COUNT 3
118 #define ST_HEAPCOST 4
121 ** Callsite loop direction flags.
123 #define ST_FOLLOW_SIBLINGS 0
124 #define ST_FOLLOW_PARENTS 1
127 ** Graph data.
129 #define STGD_WIDTH 640
130 #define STGD_HEIGHT 480
131 #define STGD_MARGIN 75
132 #define STGD_SPACE_X (STGD_WIDTH - (2 * STGD_MARGIN))
133 #define STGD_SPACE_Y (STGD_HEIGHT - (2 * STGD_MARGIN))
136 ** Minimum lifetime default, in seconds.
138 #define ST_DEFAULT_LIFETIME_MIN 10
141 ** Allocations fall to this boundry size by default.
142 ** Overhead is taken after alignment.
144 ** The msvcrt malloc has an alignment of 16 with an overhead of 8.
145 ** The win32 HeapAlloc has an alignment of 8 with an overhead of 8.
147 #define ST_DEFAULT_ALIGNMENT_SIZE 16
148 #define ST_DEFAULT_OVERHEAD_SIZE 8
151 ** Numer of substring match specifications to allow.
153 #define ST_SUBSTRING_MATCH_MAX 5
156 ** Max Number of patterns per rule
158 #define ST_MAX_PATTERNS_PER_RULE 16
161 ** Rule pointers and child pointers are allocated in steps of ST_ALLOC_STEP
163 #define ST_ALLOC_STEP 16
166 ** Name of the root category. Appears in UI.
168 #define ST_ROOT_CATEGORY_NAME "All"
171 ** Size of our option string buffers.
173 #define ST_OPTION_STRING_MAX 256
176 ** Set the desired resolution of the timevals.
177 ** The resolution is just mimicking what is recorded in the trace-malloc
178 ** output, and that is currently milliseconds.
180 #define ST_TIMEVAL_RESOLUTION 1000
181 #define ST_TIMEVAL_FORMAT "%.3f"
182 #define ST_TIMEVAL_PRINTABLE(timeval) ((PRFloat64)(timeval) / (PRFloat64)ST_TIMEVAL_RESOLUTION)
183 #define ST_TIMEVAL_PRINTABLE64(timeval) ((PRFloat64)((PRInt64)(timeval)) / (PRFloat64)ST_TIMEVAL_RESOLUTION)
184 #define ST_TIMEVAL_MAX ((PRUint32)-1 - ((PRUint32)-1 % ST_TIMEVAL_RESOLUTION))
186 #define ST_MICROVAL_RESOLUTION 1000000
187 #define ST_MICROVAL_FORMAT "%.6f"
188 #define ST_MICROVAL_PRINTABLE(timeval) ((PRFloat64)(timeval) / (PRFloat64)ST_MICROVAL_RESOLUTION)
189 #define ST_MICROVAL_PRINTABLE64(timeval) ((PRFloat64)((PRInt64)(timeval)) / (PRFloat64)ST_MICROVAL_RESOLUTION)
190 #define ST_MICROVAL_MAX ((PRUint32)-1 - ((PRUint32)-1 % ST_MICROVAL_RESOLUTION))
193 ** Forward Declaration
195 typedef struct __struct_STCategoryNode STCategoryNode;
196 typedef struct __struct_STCategoryRule STCategoryRule;
200 ** STAllocEvent
202 ** An event that happens to an allocation (malloc, free, et. al.)
204 typedef struct __struct_STAllocEvent
207 ** The type of allocation event.
208 ** This maps directly to the trace malloc events (i.e. TM_EVENT_MALLOC)
210 char mEventType;
213 ** Each event, foremost, has a chronologically increasing ID in
214 ** relation to other allocation events. This is a time stamp
215 ** of sorts.
217 PRUint32 mTimeval;
220 ** Every event has a heap ID (pointer).
221 ** In the event of a realloc, this is the new heap ID.
222 ** In the event of a free, this is the previous heap ID value.
224 PRUint32 mHeapID;
227 ** Every event, along with the heap ID, tells of the size.
228 ** In the event of a realloc, this is the new size.
229 ** In th event of a free, this is the previous size.
231 PRUint32 mHeapSize;
234 ** Every event has a callsite/stack backtrace.
235 ** In the event of a realloc, this is the new callsite.
236 ** In the event of a free, this is the previous call site.
238 tmcallsite* mCallsite;
239 } STAllocEvent;
242 ** STAllocation
244 ** An allocation is a temporal entity in the heap.
245 ** It possibly lives under different heap IDs (pointers) and different
246 ** sizes during its given time.
247 ** An allocation is defined by the events during its lifetime.
248 ** An allocation's lifetime is defined by the range of event IDs it holds.
250 typedef struct __struct_STAllocation
253 ** The array of events.
255 PRUint32 mEventCount;
256 STAllocEvent* mEvents;
259 ** The lifetime/lifespan of the allocation.
261 PRUint32 mMinTimeval;
262 PRUint32 mMaxTimeval;
265 ** Index of this allocation in the global run.
267 PRUint32 mRunIndex;
270 ** The runtime cost of heap events in this allocation.
271 ** The cost is defined as the number of time units recorded as being
272 ** spent in heap code (time of malloc, free, et al.).
273 ** We do not track individual event cost in order to save space.
275 PRUint32 mHeapRuntimeCost;
276 } STAllocation;
279 ** STCallsiteStats
281 ** Stats regarding a run, kept mainly for callsite runs.
283 typedef struct __struct_STCallsiteStats
286 ** Sum timeval of the allocations.
287 ** Callsite runs total all allocations below the callsite.
289 PRUint64 mTimeval64;
292 ** Sum weight of the allocations.
293 ** Callsite runs total all allocations below the callsite.
295 PRUint64 mWeight64;
298 ** Sum size of the allocations.
299 ** Callsite runs total all allocations below the callsite.
301 PRUint32 mSize;
304 ** A stamp, indicated the relevance of the run.
305 ** If the stamp does not match the origin value, the
306 ** data contained here-in is considered invalid.
308 PRUint32 mStamp;
311 ** A sum total of allocations (note, not sizes) below the callsite.
312 ** This is NOT the same as STRun::mAllocationCount which
313 ** tracks the STRun::mAllocations array size.
315 PRUint32 mCompositeCount;
318 ** A sum total runtime cost of heap operations below the calliste.
319 ** The cost is defined as the number of time units recorded as being
320 ** spent in heap code (time of malloc, free, et al.).
322 PRUint32 mHeapRuntimeCost;
323 } STCallsiteStats;
326 ** STRun
328 ** A run is a closed set of allocations.
329 ** Given a run, we can deduce information about the contained allocations.
330 ** We can also determine if an allocation lives beyond a run (leak).
332 ** A run might be used to represent allocations for an entire application.
333 ** A run might also be used to represent allocations from a single callstack.
335 typedef struct __struct_STRun
338 ** The array of allocations.
340 PRUint32 mAllocationCount;
341 STAllocation** mAllocations;
344 ** Callsites like to keep some information.
345 ** As callsites are possibly shared between all contexts, each
346 ** different context needs to keep different stats.
348 STCallsiteStats *mStats;
350 } STRun;
353 ** Categorize allocations
355 ** The objective is to have a tree of categories with each leaf node of the tree
356 ** matching a set of callsites that belong to the category. Each category can
357 ** signify a functional area like say css and hence the user can browse this
358 ** tree looking for how much of each of these are live at an instant.
362 ** STCategoryNode
365 struct __struct_STCategoryNode
368 ** Category name
370 const char *categoryName;
373 ** Pointer to parent node. NULL for Root.
375 STCategoryNode *parent;
378 ** For non-leaf nodes, an array of children node pointers.
379 ** NULL if leaf node.
381 STCategoryNode** children;
382 PRUint32 nchildren;
385 ** The Run(s). Valid for both leaf and parent nodes.
386 ** One run per --Context to handle multiple data sets.
387 ** The relevant index for the particular request will be
388 ** mIndex stored by the mContext of the request.
390 STRun **runs;
394 struct __struct_STCategoryRule
397 ** The pattern for the rule. Patterns are an array of strings.
398 ** A callsite needs to pass substring match for all the strings.
400 char* pats[ST_MAX_PATTERNS_PER_RULE];
401 PRUint32 patlen[ST_MAX_PATTERNS_PER_RULE];
402 PRUint32 npats;
405 ** Category name that this rule belongs to
407 const char* categoryName;
410 ** The node this should be categorized into
412 STCategoryNode* node;
417 ** CategoryName to Node mapping table
419 typedef struct __struct_STCategoryMapEntry {
420 STCategoryNode* node;
421 const char * categoryName;
422 } STCategoryMapEntry;
425 ** Option genres.
427 ** This helps to determine what functionality each option effects.
428 ** In specific, this will help use determine when and when not to
429 ** totally recaclulate the sorted run and categories.
430 ** Be very aware that adding things to a particular genre, or adding a genre,
431 ** may completely screw up the caching algorithms of SpaceTrace.
432 ** See contextLookup() or ask someone that knows if you are in doubt.
434 typedef enum __enum_STOptionGenre
436 CategoryGenre = 0,
437 DataSortGenre,
438 DataSetGenre,
439 DataSizeGenre,
440 UIGenre,
441 ServerGenre,
442 BatchModeGenre,
445 ** Last one please.
447 MaxGenres
449 STOptionGenre;
452 ** STOptions
454 ** Structure containing the varios options for the code.
455 ** The definition of these options exists in a different file.
456 ** We access that definition via macros to inline our structure definition.
458 #define ST_CMD_OPTION_BOOL(option_name, option_genre, option_help) PRBool m##option_name;
459 #define ST_CMD_OPTION_STRING(option_name, option_genre, default_value, option_help) char m##option_name[ST_OPTION_STRING_MAX];
460 #define ST_CMD_OPTION_STRING_ARRAY(option_name, option_genre, array_size, option_help) char m##option_name[array_size][ST_OPTION_STRING_MAX];
461 #define ST_CMD_OPTION_STRING_PTR_ARRAY(option_name, option_genre, option_help) const char** m##option_name; PRUint32 m##option_name##Count;
462 #define ST_CMD_OPTION_UINT32(option_name, option_genre, default_value, multiplier, option_help) PRUint32 m##option_name;
463 #define ST_CMD_OPTION_UINT64(option_name, option_genre, default_value, multiplier, option_help) PRUint64 m##option_name##64;
465 typedef struct __struct_STOptions
467 #include "stoptions.h"
469 STOptions;
471 typedef struct __struct_STContext
473 ** A per request, thread safe, manner of accessing the contained members.
474 ** A reader/writer lock ensures that the data is properly initialized before
475 ** readers of the data begin their work.
477 ** mRWLock reader/writer lock.
478 ** writer lock is held to ensure initialization, though
479 ** others can be attempting to acquire read locks
480 ** at that time.
481 ** writer lock is also used in destruction to make sure
482 ** there are no more readers of data contained herein.
483 ** reader lock is to allow multiple clients to read the
484 ** data at the same time; implies is they must not
485 ** write anything.
486 ** mIndex Consider this much like thread private data or thread
487 ** local storage in a few places.
488 ** The index is specifically reserved for this context's
489 ** usage in other data structure array's provided
490 ** for the particular thread/client/context.
491 ** This should not be modified after initialization.
492 ** mSortedRun A pre sorted run taken from the global run, with our
493 ** options applied.
494 ** mImageLock An overly simplistic locking mechanism to protect the
495 ** shared image cache.
496 ** The proper implementation would have a reader/writer
497 ** lock per cached image data.
498 ** However, this will prove to be simpler for the time
499 ** being.
500 ** mFootprintCached Whether or not YData contains something useful.
501 ** mTimevalCached Whether or not YData contains something useful.
502 ** mLifespanCached Whether or not YData contains something useful.
503 ** mWeightCached Whether or not YData contains something useful.
504 ** mFootprintYData Precomputed cached graph data.
505 ** mTimevalYData Precomputed cached graph data.
506 ** mLifespanYData Precomputed cached graph data.
507 ** mWeightYData Precomputed cached graph data.
510 PRRWLock* mRWLock;
511 PRUint32 mIndex;
512 STRun* mSortedRun;
513 #if ST_WANT_GRAPHS
514 PRLock* mImageLock;
515 PRBool mFootprintCached;
516 PRBool mTimevalCached;
517 PRBool mLifespanCached;
518 PRBool mWeightCached;
519 PRUint32 mFootprintYData[STGD_SPACE_X];
520 PRUint32 mTimevalYData[STGD_SPACE_X];
521 PRUint32 mLifespanYData[STGD_SPACE_X];
522 PRUint64 mWeightYData64[STGD_SPACE_X];
523 #endif
525 STContext;
528 typedef struct __struct_STContextCacheItem
530 ** This basically pools the common items that the context cache will
531 ** want to track on a per context basis.
533 ** mOptions What options this item represents.
534 ** mContext State/data this cache item is wrapping.
535 ** mReferenceCount A count of clients currently using this item.
536 ** Should this item be 0, then the cache might
537 ** decide to evict this context.
538 ** Should this item not be 0, once it reaches
539 ** zero a condition variable in the context cache
540 ** will be signaled to notify the availability.
541 ** mLastAccessed A timestamp of when this item was last accessed/released.
542 ** Ignore this unless the reference count is 0,
543 ** This is used to evict the oldest unused item from
544 ** the context cache.
545 ** mInUse Mainly PR_FALSE only at the beginning of the process,
546 ** but this indicates that the item has not yet been
547 ** used at all, and thus shouldn't be evaluated for
548 ** a cache hit.
551 STOptions mOptions;
552 STContext mContext;
553 PRInt32 mReferenceCount;
554 PRIntervalTime mLastAccessed;
555 PRBool mInUse;
557 STContextCacheItem;
560 typedef struct __struct_STContextCache
562 ** A thread safe, possibly blocking, cache of context items.
564 ** mLock Must hold the lock to read/access/write to this struct, as
565 ** well as any items it holds.
566 ** mCacheMiss All items are busy and there were no cache matches.
567 ** This condition variable is used to wait until an item becomes
568 ** "available" to be evicted from the cache.
569 ** mItems Array of items.
570 ** mItemCount Number of items in array.
571 ** This is generally the same as the global option's command line
572 ** mContexts....
575 PRLock* mLock;
576 PRCondVar* mCacheMiss;
577 STContextCacheItem* mItems;
578 PRUint32 mItemCount;
580 STContextCache;
584 ** STRequest
586 ** Things specific to a request.
588 typedef struct __struct_STRequest
591 ** Sink/where to output.
593 PRFileDesc* mFD;
596 ** The filename requested.
598 const char* mGetFileName;
601 ** The GET form data, if any.
603 const FormData* mGetData;
606 ** Options specific to this request.
608 STOptions mOptions;
611 ** The context/data/state of the request.
613 STContext* mContext;
614 } STRequest;
618 ** STGlobals
620 ** Various globals we keep around.
622 typedef struct __struct_STGlobals
625 ** The string which identifies this program.
627 const char* mProgramName;
630 ** Options derived from the command line.
631 ** These are used as defaults, and should remain static during
632 ** the run of the application.
634 STOptions mCommandLineOptions;
637 ** Context cache.
638 ** As clients come in, based on their options, a different context
639 ** will be used to service them.
641 STContextCache mContextCache;
644 ** Various counters for different types of events.
646 PRUint32 mMallocCount;
647 PRUint32 mCallocCount;
648 PRUint32 mReallocCount;
649 PRUint32 mFreeCount;
652 ** Total events, operation counter.
654 PRUint32 mOperationCount;
657 ** The "run" of the input.
659 STRun mRun;
662 ** Operation minimum/maximum timevals.
663 ** So that we can determine the overall timeval of the run.
664 ** NOTE: These are NOT the options to control the data set.
666 PRUint32 mMinTimeval;
667 PRUint32 mMaxTimeval;
670 ** Calculates peak allocation overall for all allocations.
672 PRUint32 mPeakMemoryUsed;
673 PRUint32 mMemoryUsed;
676 ** A list of rules for categorization read in from the mCategoryFile
678 STCategoryRule** mCategoryRules;
679 PRUint32 mNRules;
682 ** CategoryName to Node mapping table
684 STCategoryMapEntry** mCategoryMap;
685 PRUint32 mNCategoryMap;
688 ** Categorized allocations. For now we support only one tree.
690 STCategoryNode mCategoryRoot;
693 ** tmreader hash tables.
694 ** Moved into globals since we need to destroy these only after all
695 ** client threads are finishes (after PR_Cleanup).
697 tmreader* mTMR;
698 } STGlobals;
702 ** Function prototypes
704 extern STRun* createRun(STContext* inContext, PRUint32 aStamp);
705 extern void freeRun(STRun* aRun);
706 extern int initCategories(STGlobals* g);
707 extern int categorizeRun(STOptions* inOptions, STContext* inContext, const STRun* aRun, STGlobals* g);
708 extern STCategoryNode* findCategoryNode(const char *catName, STGlobals *g);
709 extern int freeCategories(STGlobals* g);
710 extern int displayCategoryReport(STRequest* inRequest, STCategoryNode *root, int depth);
712 extern int recalculateAllocationCost(STOptions* inOptions, STContext* inContext, STRun* aRun, STAllocation* aAllocation, PRBool updateParent);
713 extern void htmlHeader(STRequest* inRequest, const char* aTitle);
714 extern void htmlFooter(STRequest* inRequest);
715 extern void htmlAnchor(STRequest* inRequest,
716 const char* aHref,
717 const char* aText,
718 const char* aTarget,
719 const char* aClass,
720 STOptions* inOptions);
721 extern char *FormatNumber(PRInt32 num);
724 ** shared globals
726 extern STGlobals globals;
728 #endif /* spacetrace_h__ */