1
/* The contents of this file are subject to the Netscape Public */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
23
* Garrett Arch Blythe, 03/04/2002 Added interval hit counting.
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the NPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the NPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
41
This is part of a MOZ_COVERAGE build. (set MOZ_COVERAGE=1 and rebuild)
42
When trace.dll is linked in to the build, it counts the number of times
43
each function is called. When the program exits, it breaks out the
44
functions by module, sorts them by number of calls, then dumps them into
45
win32.order where the module is built. The order files are used by the
46
liker to rearrange the functions in the library.
58
// Interval for which we will count hits, in milliseconds.
59
// This tends to sample the need for a particular function over time.
61
// Decreasing the interval makes the code act more like call count
63
// Increasing the interval makes the code act more like no ordering
65
// Some middle ground in between is best. Tweak away....
67
// We all know that not ordering the link order at all is not an
69
// The folly of call count sorting is that you may group together
70
// methods which are rarely used but often called with rountines that
71
// are actually needed during the entire run of the application.
72
// If you can apply a time filter to smooth out the "page level" or
73
// "working set" needs to have a function in memory, then you should
74
// get a much better real world ordering.
76
#define HIT_INTERVAL 1000
83
static Reporter theReporter;
87
Hash of function names, and call counts]
89
static PLDHashTable Calls;
92
struct PLDHashEntryHdr hdr;
95
unsigned hits; // interval hits.
96
DWORD tick; // last valid tick, used to count hits.
99
static PLDHashTableOps Ops = {
103
PL_DHashVoidPtrKeyStub,
104
PL_DHashMatchEntryStub,
105
PL_DHashMoveEntryStub,
106
PL_DHashClearEntryStub,
112
Node() {function = 0; count = 0; hits = 0; next = 0;};
121
Hash of each module. Contains a sorted linked list of each function and
122
its number of calls for that module.
125
static PLDHashTable Modules;
127
struct ModulesEntry {
128
struct PLDHashEntryHdr hdr;
133
static PLDHashTableOps ModOps = {
138
PL_DHashMatchStringKey,
139
PL_DHashMoveEntryStub,
140
PL_DHashClearEntryStub,
146
Counts the number of times a function is called.
152
static int initialized = 0;
154
addr = (void*) ((unsigned) addr - 5);
157
initialized = PL_DHashTableInit(&Calls, &Ops, 0, sizeof(CallEntry), 16);
162
entry = (CallEntry*) PL_DHashTableOperate(&Calls, addr, PL_DHASH_ADD);
170
// Another call recorded.
175
// Only record a hit if the appropriate amount of time has passed.
177
DWORD curtick = GetTickCount();
178
if(curtick >= (entry->tick + HIT_INTERVAL))
181
// Record the interval hit.
185
entry->tick = curtick;
190
assembly to call Log, and count this function
194
__declspec(naked dllexport)
198
push ecx // save ecx for caller
199
push dword ptr [esp+4] // the caller's address is the only param
200
call Log // ...to Log()
202
pop ecx // restore ecx for caller
208
Steps through the hash of modules and dumps the function counts out to
212
static PLDHashOperator PR_CALLBACK
213
DumpFiles(PLDHashTable* table, PLDHashEntryHdr* hdr,
214
PRUint32 number, void* arg)
216
ModulesEntry* entry = (ModulesEntry*) hdr;
217
Node* cur = entry->byCount;
219
char pdbName[MAX_PATH];
222
strcpy(pdbName, entry->moduleName);
223
strcat(pdbName, ".pdb");
225
if (!::SearchTreeForFile(MOZ_SRC, pdbName, dest) ) {
226
fprintf(logfile,"+++ERROR Could not find %s\n",pdbName);
227
return PL_DHASH_NEXT;
229
dest[strlen(dest)-strlen(pdbName)-strlen("WIN32_D.OBJ\\")] = 0;
230
strcat(dest,"win32.order");
231
orderFile = fopen(dest,"w");
232
fprintf(logfile,"Creating order file %s\n",dest);
235
if (cur->function[0] == '_') // demangle "C" style function names
236
fprintf(orderFile,"%s ; %d %d\n", cur->function+1, cur->hits, cur->count );
238
fprintf(orderFile,"%s ; %d %d\n", cur->function, cur->hits, cur->count );
244
return PL_DHASH_NEXT;
248
We have a function name. Figure out which module it is from. Then add that
249
function and its call count into the module's sorted list.
252
static PLDHashOperator PR_CALLBACK
253
ListCounts(PLDHashTable* table, PLDHashEntryHdr* hdr,
254
PRUint32 number, void* arg)
257
CallEntry* entry = (CallEntry*) hdr;
259
IMAGEHLP_MODULE module;
260
module.SizeOfStruct = sizeof(module);
262
ok = ::SymGetModuleInfo(::GetCurrentProcess(),
263
(unsigned) entry->addr,
266
char buf[sizeof(IMAGEHLP_SYMBOL) + 512];
267
PIMAGEHLP_SYMBOL symbol = (PIMAGEHLP_SYMBOL) buf;
268
symbol->SizeOfStruct = sizeof(buf);
269
symbol->MaxNameLength = 512;
272
ok = ::SymGetSymFromAddr(::GetCurrentProcess(),
273
(unsigned) entry->addr,
279
if (displacement > 0)
280
return PL_DHASH_NEXT;
281
static int modInitialized = 0;
282
if (!modInitialized) {
283
modInitialized = PL_DHashTableInit(&Modules, &ModOps, 0, sizeof(ModulesEntry), 16);
285
return PL_DHASH_NEXT;
289
= (ModulesEntry*) PL_DHashTableOperate(&Modules,
293
return PL_DHASH_STOP; // OOM
295
if (!mod->moduleName) {
296
mod->moduleName = strdup(module.ModuleName);
297
mod->byCount = new Node();
298
mod->byCount->function = strdup(symbol->Name);
299
mod->byCount->count = entry->count;
300
mod->byCount->hits = entry->hits;
303
Node* cur = mod->byCount;
304
Node* foo = new Node();
305
foo->function = strdup(symbol->Name);
306
foo->count = entry->count;
307
foo->hits = entry->hits;
310
#if defined(SORT_BY_CALL_COUNT)
311
(cur->count < entry->count)
313
((cur->hits < entry->hits) || (cur->hits == entry->hits && cur->count < entry->count))
316
if (!strcmp(cur->function,symbol->Name))
317
return PL_DHASH_NEXT;
318
foo->next = mod->byCount;
322
if (!strcmp(cur->function,symbol->Name))
323
return PL_DHASH_NEXT;
325
#if defined(SORT_BY_CALL_COUNT)
326
(cur->next->count > entry->count)
328
((cur->next->hits > entry->hits) || (cur->next->hits == entry->hits && cur->next->count > entry->count))
333
foo->next = cur->next;
338
return PL_DHASH_NEXT;
341
Reporter::~Reporter()
343
SymInitialize(GetCurrentProcess(), 0, TRUE);
345
DWORD options = SymGetOptions();
347
// We want the nasty name, as we'll have to pass it back to the
349
options &= ~SYMOPT_UNDNAME;
350
SymSetOptions(options);
352
char logName[MAX_PATH];
353
strcpy(logName,MOZ_SRC);
354
strcat(logName,"\\tracelog");
355
logfile = fopen(logName,"w");
357
// break the function names out by module and sort them.
358
PL_DHashTableEnumerate(&Calls, ListCounts, NULL);
359
// dump the order files for each module.
360
PL_DHashTableEnumerate(&Modules, DumpFiles, NULL);
363
SymCleanup(GetCurrentProcess());