0 | |
/*
|
1 | |
* Copyright (C) 2006 The Android Open Source Project
|
2 | |
*
|
3 | |
* Licensed under the Apache License, Version 2.0 (the "License");
|
4 | |
* you may not use this file except in compliance with the License.
|
5 | |
* You may obtain a copy of the License at
|
6 | |
*
|
7 | |
* http://www.apache.org/licenses/LICENSE-2.0
|
8 | |
*
|
9 | |
* Unless required by applicable law or agreed to in writing, software
|
10 | |
* distributed under the License is distributed on an "AS IS" BASIS,
|
11 | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | |
* See the License for the specific language governing permissions and
|
13 | |
* limitations under the License.
|
14 | |
*/
|
15 | |
|
16 | |
/*
|
17 | |
* Process dmtrace output.
|
18 | |
*
|
19 | |
* This is the wrong way to go about it -- C is a clumsy language for
|
20 | |
* shuffling data around. It'll do for a first pass.
|
21 | |
*/
|
22 | |
#define NOT_VM
|
23 | |
#include "Profile.h" // from VM header
|
24 | |
|
25 | |
#include <stdio.h>
|
26 | |
#include <stdlib.h>
|
27 | |
#include <string.h>
|
28 | |
#include <unistd.h>
|
29 | |
#include <inttypes.h>
|
30 | |
#include <time.h>
|
31 | |
#include <errno.h>
|
32 | |
#include <assert.h>
|
33 | |
|
34 | |
/* Version number in the key file.
|
35 | |
* Version 1 uses one byte for the thread id.
|
36 | |
* Version 2 uses two bytes for the thread ids.
|
37 | |
* Version 3 encodes the record size and adds an optional extra timestamp field.
|
38 | |
*/
|
39 | |
int versionNumber;
|
40 | |
|
41 | |
/* arbitrarily limit indentation */
|
42 | |
#define MAX_STACK_DEPTH 10000
|
43 | |
|
44 | |
/* thread list in key file is not reliable, so just max out */
|
45 | |
#define MAX_THREADS 32768
|
46 | |
|
47 | |
/* Size of temporary buffers for escaping html strings */
|
48 | |
#define HTML_BUFSIZE 10240
|
49 | |
|
50 | |
char *htmlHeader =
|
51 | |
"<html>\n<head>\n<script type=\"text/javascript\" src=\"%ssortable.js\"></script>\n"
|
52 | |
"<script langugage=\"javascript\">\n"
|
53 | |
"function toggle(item) {\n"
|
54 | |
" obj=document.getElementById(item);\n"
|
55 | |
" visible=(obj.style.display!=\"none\" && obj.style.display!=\"\");\n"
|
56 | |
" key=document.getElementById(\"x\" + item);\n"
|
57 | |
" if (visible) {\n"
|
58 | |
" obj.style.display=\"none\";\n"
|
59 | |
" key.innerHTML=\"+\";\n"
|
60 | |
" } else {\n"
|
61 | |
" obj.style.display=\"block\";\n"
|
62 | |
" key.innerHTML=\"-\";\n"
|
63 | |
" }\n"
|
64 | |
"}\n"
|
65 | |
"function onMouseOver(obj) {\n"
|
66 | |
" obj.style.background=\"lightblue\";\n"
|
67 | |
"}\n"
|
68 | |
"function onMouseOut(obj) {\n"
|
69 | |
" obj.style.background=\"white\";\n"
|
70 | |
"}\n"
|
71 | |
"</script>\n"
|
72 | |
"<style type=\"text/css\">\n"
|
73 | |
"div { font-family: courier; font-size: 13 }\n"
|
74 | |
"div.parent { margin-left: 15; display: none }\n"
|
75 | |
"div.leaf { margin-left: 10 }\n"
|
76 | |
"div.header { margin-left: 10 }\n"
|
77 | |
"div.link { margin-left: 10; cursor: move }\n"
|
78 | |
"span.parent { padding-right: 10; }\n"
|
79 | |
"span.leaf { padding-right: 10; }\n"
|
80 | |
"a img { border: 0;}\n"
|
81 | |
"table.sortable th { border-width: 0px 1px 1px 1px; background-color: #ccc;}\n"
|
82 | |
"a { text-decoration: none; }\n"
|
83 | |
"a:hover { text-decoration: underline; }\n"
|
84 | |
"table.sortable th, table.sortable td { text-align: left;}"
|
85 | |
"table.sortable tr.odd td { background-color: #ddd; }\n"
|
86 | |
"table.sortable tr.even td { background-color: #fff; }\n"
|
87 | |
"</style>\n"
|
88 | |
"</head><body>\n\n";
|
89 | |
|
90 | |
char *htmlFooter = "\n</body>\n</html>\n";
|
91 | |
char *profileSeparator =
|
92 | |
"======================================================================";
|
93 | |
|
94 | |
const char* tableHeader =
|
95 | |
"<table class='sortable' id='%s'><tr>\n"
|
96 | |
"<th>Method</th>\n"
|
97 | |
"<th>Run 1 (us)</th>\n"
|
98 | |
"<th>Run 2 (us)</th>\n"
|
99 | |
"<th>Diff (us)</th>\n"
|
100 | |
"<th>Diff (%%)</th>\n"
|
101 | |
"<th>1: # calls</th>\n"
|
102 | |
"<th>2: # calls</th>\n"
|
103 | |
"</tr>\n";
|
104 | |
|
105 | |
const char* tableHeaderMissing =
|
106 | |
"<table class='sortable' id='%s'>\n"
|
107 | |
"<th>Method</th>\n"
|
108 | |
"<th>Exclusive</th>\n"
|
109 | |
"<th>Inclusive</th>\n"
|
110 | |
"<th># calls</th>\n";
|
111 | |
|
112 | |
#define GRAPH_LABEL_VISITED 0x0001
|
113 | |
#define GRAPH_NODE_VISITED 0x0002
|
114 | |
|
115 | |
/*
|
116 | |
* Values from the header of the data file.
|
117 | |
*/
|
118 | |
typedef struct DataHeader {
|
119 | |
unsigned int magic;
|
120 | |
short version;
|
121 | |
short offsetToData;
|
122 | |
long long startWhen;
|
123 | |
short recordSize;
|
124 | |
} DataHeader;
|
125 | |
|
126 | |
/*
|
127 | |
* Entry from the thread list.
|
128 | |
*/
|
129 | |
typedef struct ThreadEntry {
|
130 | |
int threadId;
|
131 | |
const char* threadName;
|
132 | |
} ThreadEntry;
|
133 | |
|
134 | |
struct MethodEntry;
|
135 | |
typedef struct TimedMethod {
|
136 | |
struct TimedMethod *next;
|
137 | |
uint64_t elapsedInclusive;
|
138 | |
int numCalls;
|
139 | |
struct MethodEntry *method;
|
140 | |
} TimedMethod;
|
141 | |
|
142 | |
typedef struct ClassEntry {
|
143 | |
const char *className;
|
144 | |
uint64_t elapsedExclusive;
|
145 | |
int numMethods;
|
146 | |
struct MethodEntry **methods; /* list of methods in this class */
|
147 | |
int numCalls[2]; /* 0=normal, 1=recursive */
|
148 | |
} ClassEntry;
|
149 | |
|
150 | |
typedef struct UniqueMethodEntry {
|
151 | |
uint64_t elapsedExclusive;
|
152 | |
int numMethods;
|
153 | |
struct MethodEntry **methods; /* list of methods with same name */
|
154 | |
int numCalls[2]; /* 0=normal, 1=recursive */
|
155 | |
} UniqueMethodEntry;
|
156 | |
|
157 | |
/*
|
158 | |
* Entry from the method list.
|
159 | |
*/
|
160 | |
typedef struct MethodEntry {
|
161 | |
int64_t methodId;
|
162 | |
const char* className;
|
163 | |
const char* methodName;
|
164 | |
const char* signature;
|
165 | |
const char* fileName;
|
166 | |
int lineNum;
|
167 | |
uint64_t elapsedExclusive;
|
168 | |
uint64_t elapsedInclusive;
|
169 | |
uint64_t topExclusive; /* non-recursive exclusive time */
|
170 | |
uint64_t recursiveInclusive;
|
171 | |
struct TimedMethod *parents[2]; /* 0=normal, 1=recursive */
|
172 | |
struct TimedMethod *children[2]; /* 0=normal, 1=recursive */
|
173 | |
int numCalls[2]; /* 0=normal, 1=recursive */
|
174 | |
int index; /* used after sorting to number methods */
|
175 | |
int recursiveEntries; /* number of entries on the stack */
|
176 | |
int graphState; /* used when graphing to see if this method has been visited before */
|
177 | |
} MethodEntry;
|
178 | |
|
179 | |
/*
|
180 | |
* The parsed contents of the key file.
|
181 | |
*/
|
182 | |
typedef struct DataKeys {
|
183 | |
char* fileData; /* contents of the entire file */
|
184 | |
long fileLen;
|
185 | |
int numThreads;
|
186 | |
ThreadEntry* threads;
|
187 | |
int numMethods;
|
188 | |
MethodEntry* methods; /* 2 extra methods: "toplevel" and "unknown" */
|
189 | |
} DataKeys;
|
190 | |
|
191 | |
#define TOPLEVEL_INDEX 0
|
192 | |
#define UNKNOWN_INDEX 1
|
193 | |
|
194 | |
typedef struct StackEntry {
|
195 | |
MethodEntry *method;
|
196 | |
uint64_t entryTime;
|
197 | |
} StackEntry;
|
198 | |
|
199 | |
typedef struct CallStack {
|
200 | |
int top;
|
201 | |
StackEntry calls[MAX_STACK_DEPTH];
|
202 | |
uint64_t lastEventTime;
|
203 | |
uint64_t threadStartTime;
|
204 | |
} CallStack;
|
205 | |
|
206 | |
typedef struct DiffEntry {
|
207 | |
MethodEntry* method1;
|
208 | |
MethodEntry* method2;
|
209 | |
int64_t differenceExclusive;
|
210 | |
int64_t differenceInclusive;
|
211 | |
double differenceExclusivePercentage;
|
212 | |
double differenceInclusivePercentage;
|
213 | |
} DiffEntry;
|
214 | |
|
215 | |
// Global options
|
216 | |
typedef struct Options {
|
217 | |
const char* traceFileName;
|
218 | |
const char* diffFileName;
|
219 | |
const char* graphFileName;
|
220 | |
int keepDotFile;
|
221 | |
int dump;
|
222 | |
int outputHtml;
|
223 | |
const char* sortableUrl;
|
224 | |
int threshold;
|
225 | |
} Options;
|
226 | |
|
227 | |
typedef struct TraceData {
|
228 | |
int numClasses;
|
229 | |
ClassEntry *classes;
|
230 | |
CallStack *stacks[MAX_THREADS];
|
231 | |
int depth[MAX_THREADS];
|
232 | |
int numUniqueMethods;
|
233 | |
UniqueMethodEntry *uniqueMethods;
|
234 | |
} TraceData;
|
235 | |
|
236 | |
static Options gOptions;
|
237 | |
|
238 | |
/* Escapes characters in the source string that are html special entities.
|
239 | |
* The escaped string is written to "dest" which must be large enough to
|
240 | |
* hold the result. A pointer to "dest" is returned. The characters and
|
241 | |
* their corresponding escape sequences are:
|
242 | |
* '<' <
|
243 | |
* '>' >
|
244 | |
* '&' &
|
245 | |
*/
|
246 | |
char *htmlEscape(const char *src, char *dest, int len)
|
247 | |
{
|
248 | |
char *destStart = dest;
|
249 | |
|
250 | |
if (src == NULL)
|
251 | |
return NULL;
|
252 | |
|
253 | |
int nbytes = 0;
|
254 | |
while (*src) {
|
255 | |
if (*src == '<') {
|
256 | |
nbytes += 4;
|
257 | |
if (nbytes >= len)
|
258 | |
break;
|
259 | |
*dest++ = '&';
|
260 | |
*dest++ = 'l';
|
261 | |
*dest++ = 't';
|
262 | |
*dest++ = ';';
|
263 | |
} else if (*src == '>') {
|
264 | |
nbytes += 4;
|
265 | |
if (nbytes >= len)
|
266 | |
break;
|
267 | |
*dest++ = '&';
|
268 | |
*dest++ = 'g';
|
269 | |
*dest++ = 't';
|
270 | |
*dest++ = ';';
|
271 | |
} else if (*src == '&') {
|
272 | |
nbytes += 5;
|
273 | |
if (nbytes >= len)
|
274 | |
break;
|
275 | |
*dest++ = '&';
|
276 | |
*dest++ = 'a';
|
277 | |
*dest++ = 'm';
|
278 | |
*dest++ = 'p';
|
279 | |
*dest++ = ';';
|
280 | |
} else {
|
281 | |
nbytes += 1;
|
282 | |
if (nbytes >= len)
|
283 | |
break;
|
284 | |
*dest++ = *src;
|
285 | |
}
|
286 | |
src += 1;
|
287 | |
}
|
288 | |
if (nbytes >= len) {
|
289 | |
fprintf(stderr, "htmlEscape(): buffer overflow\n");
|
290 | |
exit(1);
|
291 | |
}
|
292 | |
*dest = 0;
|
293 | |
|
294 | |
return destStart;
|
295 | |
}
|
296 | |
|
297 | |
/* Initializes a MethodEntry
|
298 | |
*/
|
299 | |
void initMethodEntry(MethodEntry *method, int64_t methodId,
|
300 | |
const char *className, const char *methodName,
|
301 | |
const char *signature, const char* fileName,
|
302 | |
const char* lineNumStr)
|
303 | |
{
|
304 | |
method->methodId = methodId;
|
305 | |
method->className = className;
|
306 | |
method->methodName = methodName;
|
307 | |
method->signature = signature;
|
308 | |
method->fileName = fileName;
|
309 | |
method->lineNum = (lineNumStr != NULL) ? atoi(lineNumStr) : -1;
|
310 | |
method->elapsedExclusive = 0;
|
311 | |
method->elapsedInclusive = 0;
|
312 | |
method->topExclusive = 0;
|
313 | |
method->recursiveInclusive = 0;
|
314 | |
method->parents[0] = NULL;
|
315 | |
method->parents[1] = NULL;
|
316 | |
method->children[0] = NULL;
|
317 | |
method->children[1] = NULL;
|
318 | |
method->numCalls[0] = 0;
|
319 | |
method->numCalls[1] = 0;
|
320 | |
method->index = 0;
|
321 | |
method->recursiveEntries = 0;
|
322 | |
}
|
323 | |
|
324 | |
/*
|
325 | |
* This comparison function is called from qsort() to sort
|
326 | |
* methods into decreasing order of exclusive elapsed time.
|
327 | |
*/
|
328 | |
int compareElapsedExclusive(const void *a, const void *b) {
|
329 | |
uint64_t elapsed1, elapsed2;
|
330 | |
int result;
|
331 | |
|
332 | |
const MethodEntry *methodA = *(const MethodEntry**)a;
|
333 | |
const MethodEntry *methodB = *(const MethodEntry**)b;
|
334 | |
elapsed1 = methodA->elapsedExclusive;
|
335 | |
elapsed2 = methodB->elapsedExclusive;
|
336 | |
if (elapsed1 < elapsed2)
|
337 | |
return 1;
|
338 | |
if (elapsed1 > elapsed2)
|
339 | |
return -1;
|
340 | |
|
341 | |
/* If the elapsed times of two methods are equal, then sort them
|
342 | |
* into alphabetical order.
|
343 | |
*/
|
344 | |
result = strcmp(methodA->className, methodB->className);
|
345 | |
if (result == 0) {
|
346 | |
if (methodA->methodName == NULL || methodB->methodName == NULL) {
|
347 | |
int64_t idA = methodA->methodId;
|
348 | |
int64_t idB = methodB->methodId;
|
349 | |
if (idA < idB)
|
350 | |
return -1;
|
351 | |
if (idA > idB)
|
352 | |
return 1;
|
353 | |
return 0;
|
354 | |
}
|
355 | |
result = strcmp(methodA->methodName, methodB->methodName);
|
356 | |
if (result == 0)
|
357 | |
result = strcmp(methodA->signature, methodB->signature);
|
358 | |
}
|
359 | |
return result;
|
360 | |
}
|
361 | |
|
362 | |
/*
|
363 | |
* This comparison function is called from qsort() to sort
|
364 | |
* methods into decreasing order of inclusive elapsed time.
|
365 | |
*/
|
366 | |
int compareElapsedInclusive(const void *a, const void *b) {
|
367 | |
const MethodEntry *methodA, *methodB;
|
368 | |
uint64_t elapsed1, elapsed2;
|
369 | |
int result;
|
370 | |
|
371 | |
methodA = *(MethodEntry const **)a;
|
372 | |
methodB = *(MethodEntry const **)b;
|
373 | |
elapsed1 = methodA->elapsedInclusive;
|
374 | |
elapsed2 = methodB->elapsedInclusive;
|
375 | |
if (elapsed1 < elapsed2)
|
376 | |
return 1;
|
377 | |
if (elapsed1 > elapsed2)
|
378 | |
return -1;
|
379 | |
|
380 | |
/* If the elapsed times of two methods are equal, then sort them
|
381 | |
* into alphabetical order.
|
382 | |
*/
|
383 | |
result = strcmp(methodA->className, methodB->className);
|
384 | |
if (result == 0) {
|
385 | |
if (methodA->methodName == NULL || methodB->methodName == NULL) {
|
386 | |
int64_t idA = methodA->methodId;
|
387 | |
int64_t idB = methodB->methodId;
|
388 | |
if (idA < idB)
|
389 | |
return -1;
|
390 | |
if (idA > idB)
|
391 | |
return 1;
|
392 | |
return 0;
|
393 | |
}
|
394 | |
result = strcmp(methodA->methodName, methodB->methodName);
|
395 | |
if (result == 0)
|
396 | |
result = strcmp(methodA->signature, methodB->signature);
|
397 | |
}
|
398 | |
return result;
|
399 | |
}
|
400 | |
|
401 | |
/*
|
402 | |
* This comparison function is called from qsort() to sort
|
403 | |
* TimedMethods into decreasing order of inclusive elapsed time.
|
404 | |
*/
|
405 | |
int compareTimedMethod(const void *a, const void *b) {
|
406 | |
const TimedMethod *timedA, *timedB;
|
407 | |
uint64_t elapsed1, elapsed2;
|
408 | |
int result;
|
409 | |
|
410 | |
timedA = (TimedMethod const *)a;
|
411 | |
timedB = (TimedMethod const *)b;
|
412 | |
elapsed1 = timedA->elapsedInclusive;
|
413 | |
elapsed2 = timedB->elapsedInclusive;
|
414 | |
if (elapsed1 < elapsed2)
|
415 | |
return 1;
|
416 | |
if (elapsed1 > elapsed2)
|
417 | |
return -1;
|
418 | |
|
419 | |
/* If the elapsed times of two methods are equal, then sort them
|
420 | |
* into alphabetical order.
|
421 | |
*/
|
422 | |
MethodEntry *methodA = timedA->method;
|
423 | |
MethodEntry *methodB = timedB->method;
|
424 | |
result = strcmp(methodA->className, methodB->className);
|
425 | |
if (result == 0) {
|
426 | |
if (methodA->methodName == NULL || methodB->methodName == NULL) {
|
427 | |
int64_t idA = methodA->methodId;
|
428 | |
int64_t idB = methodB->methodId;
|
429 | |
if (idA < idB)
|
430 | |
return -1;
|
431 | |
if (idA > idB)
|
432 | |
return 1;
|
433 | |
return 0;
|
434 | |
}
|
435 | |
result = strcmp(methodA->methodName, methodB->methodName);
|
436 | |
if (result == 0)
|
437 | |
result = strcmp(methodA->signature, methodB->signature);
|
438 | |
}
|
439 | |
return result;
|
440 | |
}
|
441 | |
|
442 | |
/*
|
443 | |
* This comparison function is called from qsort() to sort
|
444 | |
* MethodEntry pointers into alphabetical order of class names.
|
445 | |
*/
|
446 | |
int compareClassNames(const void *a, const void *b) {
|
447 | |
int result;
|
448 | |
|
449 | |
const MethodEntry *methodA = *(const MethodEntry**)a;
|
450 | |
const MethodEntry *methodB = *(const MethodEntry**)b;
|
451 | |
result = strcmp(methodA->className, methodB->className);
|
452 | |
if (result == 0) {
|
453 | |
int64_t idA = methodA->methodId;
|
454 | |
int64_t idB = methodB->methodId;
|
455 | |
if (idA < idB)
|
456 | |
return -1;
|
457 | |
if (idA > idB)
|
458 | |
return 1;
|
459 | |
return 0;
|
460 | |
}
|
461 | |
return result;
|
462 | |
}
|
463 | |
|
464 | |
/*
|
465 | |
* This comparison function is called from qsort() to sort
|
466 | |
* classes into decreasing order of exclusive elapsed time.
|
467 | |
*/
|
468 | |
int compareClassExclusive(const void *a, const void *b) {
|
469 | |
uint64_t elapsed1, elapsed2;
|
470 | |
int result;
|
471 | |
|
472 | |
const ClassEntry *classA = *(const ClassEntry**)a;
|
473 | |
const ClassEntry *classB = *(const ClassEntry**)b;
|
474 | |
elapsed1 = classA->elapsedExclusive;
|
475 | |
elapsed2 = classB->elapsedExclusive;
|
476 | |
if (elapsed1 < elapsed2)
|
477 | |
return 1;
|
478 | |
if (elapsed1 > elapsed2)
|
479 | |
return -1;
|
480 | |
|
481 | |
/* If the elapsed times of two classs are equal, then sort them
|
482 | |
* into alphabetical order.
|
483 | |
*/
|
484 | |
result = strcmp(classA->className, classB->className);
|
485 | |
if (result == 0) {
|
486 | |
/* Break ties with the first method id. This is probably not
|
487 | |
* needed.
|
488 | |
*/
|
489 | |
int64_t idA = classA->methods[0]->methodId;
|
490 | |
int64_t idB = classB->methods[0]->methodId;
|
491 | |
if (idA < idB)
|
492 | |
return -1;
|
493 | |
if (idA > idB)
|
494 | |
return 1;
|
495 | |
return 0;
|
496 | |
}
|
497 | |
return result;
|
498 | |
}
|
499 | |
|
500 | |
/*
|
501 | |
* This comparison function is called from qsort() to sort
|
502 | |
* MethodEntry pointers into alphabetical order by method name,
|
503 | |
* then by class name.
|
504 | |
*/
|
505 | |
int compareMethodNames(const void *a, const void *b) {
|
506 | |
int result;
|
507 | |
|
508 | |
const MethodEntry *methodA = *(const MethodEntry**)a;
|
509 | |
const MethodEntry *methodB = *(const MethodEntry**)b;
|
510 | |
if (methodA->methodName == NULL || methodB->methodName == NULL) {
|
511 | |
return compareClassNames(a, b);
|
512 | |
}
|
513 | |
result = strcmp(methodA->methodName, methodB->methodName);
|
514 | |
if (result == 0) {
|
515 | |
result = strcmp(methodA->className, methodB->className);
|
516 | |
if (result == 0) {
|
517 | |
int64_t idA = methodA->methodId;
|
518 | |
int64_t idB = methodB->methodId;
|
519 | |
if (idA < idB)
|
520 | |
return -1;
|
521 | |
if (idA > idB)
|
522 | |
return 1;
|
523 | |
return 0;
|
524 | |
}
|
525 | |
}
|
526 | |
return result;
|
527 | |
}
|
528 | |
|
529 | |
/*
|
530 | |
* This comparison function is called from qsort() to sort
|
531 | |
* unique methods into decreasing order of exclusive elapsed time.
|
532 | |
*/
|
533 | |
int compareUniqueExclusive(const void *a, const void *b) {
|
534 | |
uint64_t elapsed1, elapsed2;
|
535 | |
int result;
|
536 | |
|
537 | |
const UniqueMethodEntry *uniqueA = *(const UniqueMethodEntry**)a;
|
538 | |
const UniqueMethodEntry *uniqueB = *(const UniqueMethodEntry**)b;
|
539 | |
elapsed1 = uniqueA->elapsedExclusive;
|
540 | |
elapsed2 = uniqueB->elapsedExclusive;
|
541 | |
if (elapsed1 < elapsed2)
|
542 | |
return 1;
|
543 | |
if (elapsed1 > elapsed2)
|
544 | |
return -1;
|
545 | |
|
546 | |
/* If the elapsed times of two methods are equal, then sort them
|
547 | |
* into alphabetical order.
|
548 | |
*/
|
549 | |
result = strcmp(uniqueA->methods[0]->className,
|
550 | |
uniqueB->methods[0]->className);
|
551 | |
if (result == 0) {
|
552 | |
int64_t idA = uniqueA->methods[0]->methodId;
|
553 | |
int64_t idB = uniqueB->methods[0]->methodId;
|
554 | |
if (idA < idB)
|
555 | |
return -1;
|
556 | |
if (idA > idB)
|
557 | |
return 1;
|
558 | |
return 0;
|
559 | |
}
|
560 | |
return result;
|
561 | |
}
|
562 | |
|
563 | |
/*
|
564 | |
* Free a DataKeys struct.
|
565 | |
*/
|
566 | |
void freeDataKeys(DataKeys* pKeys)
|
567 | |
{
|
568 | |
if (pKeys == NULL)
|
569 | |
return;
|
570 | |
|
571 | |
free(pKeys->fileData);
|
572 | |
free(pKeys->threads);
|
573 | |
free(pKeys->methods);
|
574 | |
free(pKeys);
|
575 | |
}
|
576 | |
|
577 | |
/*
|
578 | |
* Find the offset to the next occurrence of the specified character.
|
579 | |
*
|
580 | |
* "data" should point somewhere within the current line. "len" is the
|
581 | |
* number of bytes left in the buffer.
|
582 | |
*
|
583 | |
* Returns -1 if we hit the end of the buffer.
|
584 | |
*/
|
585 | |
int findNextChar(const char* data, int len, char lookFor)
|
586 | |
{
|
587 | |
const char* start = data;
|
588 | |
|
589 | |
while (len > 0) {
|
590 | |
if (*data == lookFor)
|
591 | |
return data - start;
|
592 | |
|
593 | |
data++;
|
594 | |
len--;
|
595 | |
}
|
596 | |
|
597 | |
return -1;
|
598 | |
}
|
599 | |
|
600 | |
/*
|
601 | |
* Count the number of lines until the next token.
|
602 | |
*
|
603 | |
* Returns -1 if none found before EOF.
|
604 | |
*/
|
605 | |
int countLinesToToken(const char* data, int len)
|
606 | |
{
|
607 | |
int count = 0;
|
608 | |
int next;
|
609 | |
|
610 | |
while (*data != TOKEN_CHAR) {
|
611 | |
next = findNextChar(data, len, '\n');
|
612 | |
if (next < 0)
|
613 | |
return -1;
|
614 | |
count++;
|
615 | |
data += next+1;
|
616 | |
len -= next+1;
|
617 | |
}
|
618 | |
|
619 | |
return count;
|
620 | |
}
|
621 | |
|
622 | |
/*
|
623 | |
* Make sure we're at the start of the right section.
|
624 | |
*
|
625 | |
* Returns the length of the token line, or -1 if something is wrong.
|
626 | |
*/
|
627 | |
int checkToken(const char* data, int len, const char* cmpStr)
|
628 | |
{
|
629 | |
int cmpLen = strlen(cmpStr);
|
630 | |
int next;
|
631 | |
|
632 | |
if (*data != TOKEN_CHAR) {
|
633 | |
fprintf(stderr,
|
634 | |
"ERROR: not at start of %s (found '%.10s')\n", cmpStr, data);
|
635 | |
return -1;
|
636 | |
}
|
637 | |
|
638 | |
next = findNextChar(data, len, '\n');
|
639 | |
if (next < cmpLen+1)
|
640 | |
return -1;
|
641 | |
|
642 | |
if (strncmp(data+1, cmpStr, cmpLen) != 0) {
|
643 | |
fprintf(stderr, "ERROR: '%s' not found (got '%.7s')\n", cmpStr, data+1);
|
644 | |
return -1;
|
645 | |
}
|
646 | |
|
647 | |
return next+1;
|
648 | |
}
|
649 | |
|
650 | |
/*
|
651 | |
* Parse the "*version" section.
|
652 | |
*/
|
653 | |
long parseVersion(DataKeys* pKeys, long offset, int verbose)
|
654 | |
{
|
655 | |
char* data;
|
656 | |
char* dataEnd;
|
657 | |
int i, count, next;
|
658 | |
|
659 | |
if (offset < 0)
|
660 | |
return -1;
|
661 | |
|
662 | |
data = pKeys->fileData + offset;
|
663 | |
dataEnd = pKeys->fileData + pKeys->fileLen;
|
664 | |
next = checkToken(data, dataEnd - data, "version");
|
665 | |
if (next <= 0)
|
666 | |
return -1;
|
667 | |
|
668 | |
data += next;
|
669 | |
|
670 | |
/*
|
671 | |
* Count the number of items in the "version" section.
|
672 | |
*/
|
673 | |
count = countLinesToToken(data, dataEnd - data);
|
674 | |
if (count <= 0) {
|
675 | |
fprintf(stderr,
|
676 | |
"ERROR: failed while reading version (found %d)\n", count);
|
677 | |
return -1;
|
678 | |
}
|
679 | |
|
680 | |
/* find the end of the line */
|
681 | |
next = findNextChar(data, dataEnd - data, '\n');
|
682 | |
if (next < 0)
|
683 | |
return -1;
|
684 | |
|
685 | |
data[next] = '\0';
|
686 | |
versionNumber = strtoul(data, NULL, 0);
|
687 | |
if (verbose)
|
688 | |
printf("VERSION: %d\n", versionNumber);
|
689 | |
|
690 | |
data += next+1;
|
691 | |
|
692 | |
/* skip over the rest of the stuff, which is "name=value" lines */
|
693 | |
for (i = 1; i < count; i++) {
|
694 | |
next = findNextChar(data, dataEnd - data, '\n');
|
695 | |
if (next < 0)
|
696 | |
return -1;
|
697 | |
//data[next] = '\0';
|
698 | |
//printf("IGNORING: '%s'\n", data);
|
699 | |
data += next+1;
|
700 | |
}
|
701 | |
|
702 | |
return data - pKeys->fileData;
|
703 | |
}
|
704 | |
|
705 | |
/*
|
706 | |
* Parse the "*threads" section.
|
707 | |
*/
|
708 | |
long parseThreads(DataKeys* pKeys, long offset)
|
709 | |
{
|
710 | |
char* data;
|
711 | |
char* dataEnd;
|
712 | |
int i, next, tab, count;
|
713 | |
|
714 | |
if (offset < 0)
|
715 | |
return -1;
|
716 | |
|
717 | |
data = pKeys->fileData + offset;
|
718 | |
dataEnd = pKeys->fileData + pKeys->fileLen;
|
719 | |
next = checkToken(data, dataEnd - data, "threads");
|
720 | |
|
721 | |
data += next;
|
722 | |
|
723 | |
/*
|
724 | |
* Count the number of thread entries (one per line).
|
725 | |
*/
|
726 | |
count = countLinesToToken(data, dataEnd - data);
|
727 | |
if (count <= 0) {
|
728 | |
fprintf(stderr,
|
729 | |
"ERROR: failed while reading threads (found %d)\n", count);
|
730 | |
return -1;
|
731 | |
}
|
732 | |
|
733 | |
//printf("+++ found %d threads\n", count);
|
734 | |
pKeys->threads = (ThreadEntry*) malloc(sizeof(ThreadEntry) * count);
|
735 | |
if (pKeys->threads == NULL)
|
736 | |
return -1;
|
737 | |
|
738 | |
/*
|
739 | |
* Extract all entries.
|
740 | |
*/
|
741 | |
for (i = 0; i < count; i++) {
|
742 | |
next = findNextChar(data, dataEnd - data, '\n');
|
743 | |
assert(next > 0);
|
744 | |
data[next] = '\0';
|
745 | |
|
746 | |
tab = findNextChar(data, next, '\t');
|
747 | |
data[tab] = '\0';
|
748 | |
|
749 | |
pKeys->threads[i].threadId = atoi(data);
|
750 | |
pKeys->threads[i].threadName = data + tab +1;
|
751 | |
|
752 | |
data += next+1;
|
753 | |
}
|
754 | |
|
755 | |
pKeys->numThreads = count;
|
756 | |
return data - pKeys->fileData;
|
757 | |
}
|
758 | |
|
759 | |
/*
|
760 | |
* Parse the "*methods" section.
|
761 | |
*/
|
762 | |
long parseMethods(DataKeys* pKeys, long offset)
|
763 | |
{
|
764 | |
char* data;
|
765 | |
char* dataEnd;
|
766 | |
int i, next, count;
|
767 | |
|
768 | |
if (offset < 0)
|
769 | |
return -1;
|
770 | |
|
771 | |
data = pKeys->fileData + offset;
|
772 | |
dataEnd = pKeys->fileData + pKeys->fileLen;
|
773 | |
next = checkToken(data, dataEnd - data, "methods");
|
774 | |
if (next < 0)
|
775 | |
return -1;
|
776 | |
|
777 | |
data += next;
|
778 | |
|
779 | |
/*
|
780 | |
* Count the number of method entries (one per line).
|
781 | |
*/
|
782 | |
count = countLinesToToken(data, dataEnd - data);
|
783 | |
if (count <= 0) {
|
784 | |
fprintf(stderr,
|
785 | |
"ERROR: failed while reading methods (found %d)\n", count);
|
786 | |
return -1;
|
787 | |
}
|
788 | |
|
789 | |
/* Reserve an extra method at location 0 for the "toplevel" method,
|
790 | |
* and another extra method for all other "unknown" methods.
|
791 | |
*/
|
792 | |
count += 2;
|
793 | |
pKeys->methods = (MethodEntry*) malloc(sizeof(MethodEntry) * count);
|
794 | |
if (pKeys->methods == NULL)
|
795 | |
return -1;
|
796 | |
initMethodEntry(&pKeys->methods[TOPLEVEL_INDEX], -2, "(toplevel)",
|
797 | |
NULL, NULL, NULL, NULL);
|
798 | |
initMethodEntry(&pKeys->methods[UNKNOWN_INDEX], -1, "(unknown)",
|
799 | |
NULL, NULL, NULL, NULL);
|
800 | |
|
801 | |
/*
|
802 | |
* Extract all entries, starting with index 2.
|
803 | |
*/
|
804 | |
for (i = UNKNOWN_INDEX + 1; i < count; i++) {
|
805 | |
int tab1, tab2, tab3, tab4, tab5;
|
806 | |
int64_t id;
|
807 | |
char* endptr;
|
808 | |
|
809 | |
next = findNextChar(data, dataEnd - data, '\n');
|
810 | |
assert(next > 0);
|
811 | |
data[next] = '\0';
|
812 | |
|
813 | |
tab1 = findNextChar(data, next, '\t');
|
814 | |
tab2 = findNextChar(data+(tab1+1), next-(tab1+1), '\t');
|
815 | |
tab3 = findNextChar(data+(tab1+tab2+2), next-(tab1+tab2+2), '\t');
|
816 | |
tab4 = findNextChar(data+(tab1+tab2+tab3+3),
|
817 | |
next-(tab1+tab2+tab3+3), '\t');
|
818 | |
tab5 = findNextChar(data+(tab1+tab2+tab3+tab4+4),
|
819 | |
next-(tab1+tab2+tab3+tab4+4), '\t');
|
820 | |
if (tab1 < 0) {
|
821 | |
fprintf(stderr, "ERROR: missing field on method line: '%s'\n",
|
822 | |
data);
|
823 | |
return -1;
|
824 | |
}
|
825 | |
assert(data[tab1] == '\t');
|
826 | |
data[tab1] = '\0';
|
827 | |
|
828 | |
id = strtoul(data, &endptr, 0);
|
829 | |
if (*endptr != '\0') {
|
830 | |
fprintf(stderr, "ERROR: bad method ID '%s'\n", data);
|
831 | |
return -1;
|
832 | |
}
|
833 | |
|
834 | |
// Allow files that specify just a function name, instead of requiring
|
835 | |
// "class \t method \t signature"
|
836 | |
if (tab2 > 0 && tab3 > 0) {
|
837 | |
tab2 += tab1+1;
|
838 | |
tab3 += tab2+1;
|
839 | |
assert(data[tab2] == '\t');
|
840 | |
assert(data[tab3] == '\t');
|
841 | |
data[tab2] = data[tab3] = '\0';
|
842 | |
|
843 | |
// This is starting to get awkward. Allow filename and line #.
|
844 | |
if (tab4 > 0 && tab5 > 0) {
|
845 | |
tab4 += tab3+1;
|
846 | |
tab5 += tab4+1;
|
847 | |
|
848 | |
assert(data[tab4] == '\t');
|
849 | |
assert(data[tab5] == '\t');
|
850 | |
data[tab4] = data[tab5] = '\0';
|
851 | |
|
852 | |
initMethodEntry(&pKeys->methods[i], id, data + tab1 +1,
|
853 | |
data + tab2 +1, data + tab3 +1, data + tab4 +1,
|
854 | |
data + tab5 +1);
|
855 | |
} else {
|
856 | |
initMethodEntry(&pKeys->methods[i], id, data + tab1 +1,
|
857 | |
data + tab2 +1, data + tab3 +1, NULL, NULL);
|
858 | |
}
|
859 | |
} else {
|
860 | |
initMethodEntry(&pKeys->methods[i], id, data + tab1 +1,
|
861 | |
NULL, NULL, NULL, NULL);
|
862 | |
}
|
863 | |
|
864 | |
data += next+1;
|
865 | |
}
|
866 | |
|
867 | |
pKeys->numMethods = count;
|
868 | |
return data - pKeys->fileData;
|
869 | |
}
|
870 | |
|
871 | |
/*
|
872 | |
* Parse the "*end" section.
|
873 | |
*/
|
874 | |
long parseEnd(DataKeys* pKeys, long offset)
|
875 | |
{
|
876 | |
char* data;
|
877 | |
char* dataEnd;
|
878 | |
int next;
|
879 | |
|
880 | |
if (offset < 0)
|
881 | |
return -1;
|
882 | |
|
883 | |
data = pKeys->fileData + offset;
|
884 | |
dataEnd = pKeys->fileData + pKeys->fileLen;
|
885 | |
next = checkToken(data, dataEnd - data, "end");
|
886 | |
if (next < 0)
|
887 | |
return -1;
|
888 | |
|
889 | |
data += next;
|
890 | |
|
891 | |
return data - pKeys->fileData;
|
892 | |
}
|
893 | |
|
894 | |
/*
|
895 | |
* Sort the thread list entries.
|
896 | |
*/
|
897 | |
static int compareThreads(const void* thread1, const void* thread2)
|
898 | |
{
|
899 | |
return ((const ThreadEntry*) thread1)->threadId -
|
900 | |
((const ThreadEntry*) thread2)->threadId;
|
901 | |
}
|
902 | |
|
903 | |
void sortThreadList(DataKeys* pKeys)
|
904 | |
{
|
905 | |
qsort(pKeys->threads, pKeys->numThreads, sizeof(pKeys->threads[0]),
|
906 | |
compareThreads);
|
907 | |
}
|
908 | |
|
909 | |
/*
|
910 | |
* Sort the method list entries.
|
911 | |
*/
|
912 | |
static int compareMethods(const void* meth1, const void* meth2)
|
913 | |
{
|
914 | |
int64_t id1, id2;
|
915 | |
|
916 | |
id1 = ((const MethodEntry*) meth1)->methodId;
|
917 | |
id2 = ((const MethodEntry*) meth2)->methodId;
|
918 | |
if (id1 < id2)
|
919 | |
return -1;
|
920 | |
if (id1 > id2)
|
921 | |
return 1;
|
922 | |
return 0;
|
923 | |
}
|
924 | |
|
925 | |
void sortMethodList(DataKeys* pKeys)
|
926 | |
{
|
927 | |
qsort(pKeys->methods, pKeys->numMethods, sizeof(MethodEntry),
|
928 | |
compareMethods);
|
929 | |
}
|
930 | |
|
931 | |
/*
|
932 | |
* Parse the key section, and return a copy of the parsed contents.
|
933 | |
*/
|
934 | |
DataKeys* parseKeys(FILE *fp, int verbose)
|
935 | |
{
|
936 | |
DataKeys* pKeys = NULL;
|
937 | |
long offset;
|
938 | |
int i;
|
939 | |
|
940 | |
pKeys = (DataKeys*) calloc(1, sizeof(DataKeys));
|
941 | |
if (pKeys == NULL)
|
942 | |
goto fail;
|
943 | |
|
944 | |
/*
|
945 | |
* We load the entire file into memory. We do this, rather than memory-
|
946 | |
* mapping it, because we want to change some whitespace to NULs.
|
947 | |
*/
|
948 | |
if (fseek(fp, 0L, SEEK_END) != 0) {
|
949 | |
perror("fseek");
|
950 | |
goto fail;
|
951 | |
}
|
952 | |
pKeys->fileLen = ftell(fp);
|
953 | |
if (pKeys->fileLen == 0) {
|
954 | |
fprintf(stderr, "Key file is empty.\n");
|
955 | |
goto fail;
|
956 | |
}
|
957 | |
rewind(fp);
|
958 | |
|
959 | |
pKeys->fileData = (char*) malloc(pKeys->fileLen);
|
960 | |
if (pKeys->fileData == NULL) {
|
961 | |
fprintf(stderr, "ERROR: unable to alloc %ld bytes\n", pKeys->fileLen);
|
962 | |
goto fail;
|
963 | |
}
|
964 | |
|
965 | |
if (fread(pKeys->fileData, 1, pKeys->fileLen, fp) != (size_t) pKeys->fileLen)
|
966 | |
{
|
967 | |
fprintf(stderr, "ERROR: unable to read %ld bytes from trace file\n",
|
968 | |
pKeys->fileLen);
|
969 | |
goto fail;
|
970 | |
}
|
971 | |
|
972 | |
offset = 0;
|
973 | |
|
974 | |
offset = parseVersion(pKeys, offset, verbose);
|
975 | |
offset = parseThreads(pKeys, offset);
|
976 | |
offset = parseMethods(pKeys, offset);
|
977 | |
offset = parseEnd(pKeys, offset);
|
978 | |
if (offset < 0)
|
979 | |
goto fail;
|
980 | |
|
981 | |
/* Reduce our allocation now that we know where the end of the key section is. */
|
982 | |
pKeys->fileData = (char *)realloc(pKeys->fileData, offset);
|
983 | |
pKeys->fileLen = offset;
|
984 | |
/* Leave fp pointing to the beginning of the data section. */
|
985 | |
fseek(fp, offset, SEEK_SET);
|
986 | |
|
987 | |
sortThreadList(pKeys);
|
988 | |
sortMethodList(pKeys);
|
989 | |
|
990 | |
/*
|
991 | |
* Dump list of threads.
|
992 | |
*/
|
993 | |
if (verbose) {
|
994 | |
printf("Threads (%d):\n", pKeys->numThreads);
|
995 | |
for (i = 0; i < pKeys->numThreads; i++) {
|
996 | |
printf("%2d %s\n",
|
997 | |
pKeys->threads[i].threadId, pKeys->threads[i].threadName);
|
998 | |
}
|
999 | |
}
|
1000 | |
|
1001 | |
#if 0
|
1002 | |
/*
|
1003 | |
* Dump list of methods.
|
1004 | |
*/
|
1005 | |
if (verbose) {
|
1006 | |
printf("Methods (%d):\n", pKeys->numMethods);
|
1007 | |
for (i = 0; i < pKeys->numMethods; i++) {
|
1008 | |
printf("0x%08x %s : %s : %s\n",
|
1009 | |
pKeys->methods[i].methodId, pKeys->methods[i].className,
|
1010 | |
pKeys->methods[i].methodName, pKeys->methods[i].signature);
|
1011 | |
}
|
1012 | |
}
|
1013 | |
#endif
|
1014 | |
|
1015 | |
return pKeys;
|
1016 | |
|
1017 | |
fail:
|
1018 | |
freeDataKeys(pKeys);
|
1019 | |
return NULL;
|
1020 | |
}
|
1021 | |
|
1022 | |
|
1023 | |
/*
|
1024 | |
* Read values from the binary data file.
|
1025 | |
*/
|
1026 | |
|
1027 | |
/* Make the return value "unsigned int" instead of "unsigned short" so that
|
1028 | |
* we can detect EOF.
|
1029 | |
*/
|
1030 | |
unsigned int read2LE(FILE* fp)
|
1031 | |
{
|
1032 | |
unsigned int val;
|
1033 | |
|
1034 | |
val = getc(fp);
|
1035 | |
val |= getc(fp) << 8;
|
1036 | |
return val;
|
1037 | |
}
|
1038 | |
unsigned int read4LE(FILE* fp)
|
1039 | |
{
|
1040 | |
unsigned int val;
|
1041 | |
|
1042 | |
val = getc(fp);
|
1043 | |
val |= getc(fp) << 8;
|
1044 | |
val |= getc(fp) << 16;
|
1045 | |
val |= getc(fp) << 24;
|
1046 | |
return val;
|
1047 | |
}
|
1048 | |
unsigned long long read8LE(FILE* fp)
|
1049 | |
{
|
1050 | |
unsigned long long val;
|
1051 | |
|
1052 | |
val = getc(fp);
|
1053 | |
val |= (unsigned long long) getc(fp) << 8;
|
1054 | |
val |= (unsigned long long) getc(fp) << 16;
|
1055 | |
val |= (unsigned long long) getc(fp) << 24;
|
1056 | |
val |= (unsigned long long) getc(fp) << 32;
|
1057 | |
val |= (unsigned long long) getc(fp) << 40;
|
1058 | |
val |= (unsigned long long) getc(fp) << 48;
|
1059 | |
val |= (unsigned long long) getc(fp) << 56;
|
1060 | |
return val;
|
1061 | |
}
|
1062 | |
|
1063 | |
/*
|
1064 | |
* Parse the header of the data section.
|
1065 | |
*
|
1066 | |
* Returns with the file positioned at the start of the record data.
|
1067 | |
*/
|
1068 | |
int parseDataHeader(FILE *fp, DataHeader* pHeader)
|
1069 | |
{
|
1070 | |
int bytesToRead;
|
1071 | |
|
1072 | |
pHeader->magic = read4LE(fp);
|
1073 | |
pHeader->version = read2LE(fp);
|
1074 | |
pHeader->offsetToData = read2LE(fp);
|
1075 | |
pHeader->startWhen = read8LE(fp);
|
1076 | |
bytesToRead = pHeader->offsetToData - 16;
|
1077 | |
if (pHeader->version == 1) {
|
1078 | |
pHeader->recordSize = 9;
|
1079 | |
} else if (pHeader->version == 2) {
|
1080 | |
pHeader->recordSize = 10;
|
1081 | |
} else if (pHeader->version == 3) {
|
1082 | |
pHeader->recordSize = read2LE(fp);
|
1083 | |
bytesToRead -= 2;
|
1084 | |
} else {
|
1085 | |
fprintf(stderr, "Unsupported trace file version: %d\n", pHeader->version);
|
1086 | |
return -1;
|
1087 | |
}
|
1088 | |
|
1089 | |
if (fseek(fp, bytesToRead, SEEK_CUR) != 0) {
|
1090 | |
return -1;
|
1091 | |
}
|
1092 | |
|
1093 | |
return 0;
|
1094 | |
}
|
1095 | |
|
1096 | |
/*
|
1097 | |
* Look up a method by it's method ID.
|
1098 | |
*
|
1099 | |
* Returns NULL if no matching method was found.
|
1100 | |
*/
|
1101 | |
MethodEntry* lookupMethod(DataKeys* pKeys, int64_t methodId)
|
1102 | |
{
|
1103 | |
int hi, lo, mid;
|
1104 | |
int64_t id;
|
1105 | |
|
1106 | |
lo = 0;
|
1107 | |
hi = pKeys->numMethods - 1;
|
1108 | |
|
1109 | |
while (hi >= lo) {
|
1110 | |
mid = (hi + lo) / 2;
|
1111 | |
|
1112 | |
id = pKeys->methods[mid].methodId;
|
1113 | |
if (id == methodId) /* match */
|
1114 | |
return &pKeys->methods[mid];
|
1115 | |
else if (id < methodId) /* too low */
|
1116 | |
lo = mid + 1;
|
1117 | |
else /* too high */
|
1118 | |
hi = mid - 1;
|
1119 | |
}
|
1120 | |
|
1121 | |
return NULL;
|
1122 | |
}
|
1123 | |
|
1124 | |
/*
|
1125 | |
* Reads the next data record, and assigns the data values to threadId,
|
1126 | |
* methodVal and elapsedTime. On end-of-file, the threadId, methodVal,
|
1127 | |
* and elapsedTime are unchanged. Returns 1 on end-of-file, otherwise
|
1128 | |
* returns 0.
|
1129 | |
*/
|
1130 | |
int readDataRecord(FILE *dataFp, DataHeader* dataHeader,
|
1131 | |
int *threadId, unsigned int *methodVal, uint64_t *elapsedTime)
|
1132 | |
{
|
1133 | |
int id;
|
1134 | |
int bytesToRead;
|
1135 | |
|
1136 | |
bytesToRead = dataHeader->recordSize;
|
1137 | |
if (dataHeader->version == 1) {
|
1138 | |
id = getc(dataFp);
|
1139 | |
bytesToRead -= 1;
|
1140 | |
} else {
|
1141 | |
id = read2LE(dataFp);
|
1142 | |
bytesToRead -= 2;
|
1143 | |
}
|
1144 | |
if (id == EOF)
|
1145 | |
return 1;
|
1146 | |
*threadId = id;
|
1147 | |
|
1148 | |
*methodVal = read4LE(dataFp);
|
1149 | |
*elapsedTime = read4LE(dataFp);
|
1150 | |
bytesToRead -= 8;
|
1151 | |
|
1152 | |
while (bytesToRead-- > 0) {
|
1153 | |
getc(dataFp);
|
1154 | |
}
|
1155 | |
|
1156 | |
if (feof(dataFp)) {
|
1157 | |
fprintf(stderr, "WARNING: hit EOF mid-record\n");
|
1158 | |
return 1;
|
1159 | |
}
|
1160 | |
return 0;
|
1161 | |
}
|
1162 | |
|
1163 | |
/*
|
1164 | |
* Read the key file and use it to produce formatted output from the
|
1165 | |
* data file.
|
1166 | |
*/
|
1167 | |
void dumpTrace()
|
1168 | |
{
|
1169 | |
static const char* actionStr[] = { "ent", "xit", "unr", "???" };
|
1170 | |
MethodEntry bogusMethod = { 0, "???", "???", "???", "???", -1, 0, 0, 0, 0,
|
1171 | |
{NULL, NULL}, {NULL, NULL}, {0, 0}, 0, 0, -1 };
|
1172 | |
char bogusBuf[80];
|
1173 | |
char spaces[MAX_STACK_DEPTH+1];
|
1174 | |
FILE* dataFp = NULL;
|
1175 | |
DataHeader dataHeader;
|
1176 | |
DataKeys* pKeys = NULL;
|
1177 | |
int i;
|
1178 | |
TraceData traceData;
|
1179 | |
|
1180 | |
//printf("Dumping '%s' '%s'\n", dataFileName, keyFileName);
|
1181 | |
|
1182 | |
memset(spaces, '.', MAX_STACK_DEPTH);
|
1183 | |
spaces[MAX_STACK_DEPTH] = '\0';
|
1184 | |
|
1185 | |
for (i = 0; i < MAX_THREADS; i++)
|
1186 | |
traceData.depth[i] = 2; // adjust for return from start function
|
1187 | |
|
1188 | |
dataFp = fopen(gOptions.traceFileName, "rb");
|
1189 | |
if (dataFp == NULL)
|
1190 | |
goto bail;
|
1191 | |
|
1192 | |
if ((pKeys = parseKeys(dataFp, 1)) == NULL)
|
1193 | |
goto bail;
|
1194 | |
|
1195 | |
if (parseDataHeader(dataFp, &dataHeader) < 0)
|
1196 | |
goto bail;
|
1197 | |
|
1198 | |
printf("Trace (threadID action usecs class.method signature):\n");
|
1199 | |
|
1200 | |
while (1) {
|
1201 | |
MethodEntry* method;
|
1202 | |
int threadId;
|
1203 | |
unsigned int methodVal;
|
1204 | |
uint64_t elapsedTime;
|
1205 | |
int action, printDepth;
|
1206 | |
int64_t methodId, lastEnter = 0;
|
1207 | |
int mismatch = 0;
|
1208 | |
char depthNote;
|
1209 | |
|
1210 | |
/*
|
1211 | |
* Extract values from file.
|
1212 | |
*/
|
1213 | |
if (readDataRecord(dataFp, &dataHeader, &threadId, &methodVal, &elapsedTime))
|
1214 | |
break;
|
1215 | |
|
1216 | |
action = METHOD_ACTION(methodVal);
|
1217 | |
methodId = METHOD_ID(methodVal);
|
1218 | |
|
1219 | |
/*
|
1220 | |
* Generate a line of output.
|
1221 | |
*/
|
1222 | |
if (action == METHOD_TRACE_ENTER) {
|
1223 | |
traceData.depth[threadId]++;
|
1224 | |
lastEnter = methodId;
|
1225 | |
} else {
|
1226 | |
/* quick test for mismatched adjacent enter/exit */
|
1227 | |
if (lastEnter != 0 && lastEnter != methodId)
|
1228 | |
mismatch = 1;
|
1229 | |
}
|
1230 | |
|
1231 | |
printDepth = traceData.depth[threadId];
|
1232 | |
depthNote = ' ';
|
1233 | |
if (printDepth < 0) {
|
1234 | |
printDepth = 0;
|
1235 | |
depthNote = '-';
|
1236 | |
} else if (printDepth > MAX_STACK_DEPTH) {
|
1237 | |
printDepth = MAX_STACK_DEPTH;
|
1238 | |
depthNote = '+';
|
1239 | |
}
|
1240 | |
|
1241 | |
method = lookupMethod(pKeys, methodId);
|
1242 | |
if (method == NULL) {
|
1243 | |
method = &bogusMethod;
|
1244 | |
sprintf(bogusBuf, "methodId: %#" PRIx64 "", methodId);
|
1245 | |
method->signature = bogusBuf;
|
1246 | |
}
|
1247 | |
|
1248 | |
if (method->methodName) {
|
1249 | |
printf("%2d %s%c %8lld%c%s%s.%s %s\n", threadId,
|
1250 | |
actionStr[action], mismatch ? '!' : ' ',
|
1251 | |
elapsedTime, depthNote,
|
1252 | |
spaces + (MAX_STACK_DEPTH - printDepth),
|
1253 | |
method->className, method->methodName, method->signature);
|
1254 | |
} else {
|
1255 | |
printf("%2d %s%c %8lld%c%s%s\n", threadId,
|
1256 | |
actionStr[action], mismatch ? '!' : ' ',
|
1257 | |
elapsedTime, depthNote,
|
1258 | |
spaces + (MAX_STACK_DEPTH - printDepth),
|
1259 | |
method->className);
|
1260 | |
}
|
1261 | |
|
1262 | |
if (action != METHOD_TRACE_ENTER) {
|
1263 | |
traceData.depth[threadId]--; /* METHOD_TRACE_EXIT or METHOD_TRACE_UNROLL */
|
1264 | |
lastEnter = 0;
|
1265 | |
}
|
1266 | |
|
1267 | |
mismatch = 0;
|
1268 | |
}
|
1269 | |
|
1270 | |
bail:
|
1271 | |
if (dataFp != NULL)
|
1272 | |
fclose(dataFp);
|
1273 | |
if (pKeys != NULL)
|
1274 | |
freeDataKeys(pKeys);
|
1275 | |
}
|
1276 | |
|
1277 | |
/* This routine adds the given time to the parent and child methods.
|
1278 | |
* This is called when the child routine exits, after the child has
|
1279 | |
* been popped from the stack. The elapsedTime parameter is the
|
1280 | |
* duration of the child routine, including time spent in called routines.
|
1281 | |
*/
|
1282 | |
void addInclusiveTime(MethodEntry *parent, MethodEntry *child,
|
1283 | |
uint64_t elapsedTime)
|
1284 | |
{
|
1285 | |
TimedMethod *pTimed;
|
1286 | |
|
1287 | |
#if 0
|
1288 | |
bool verbose = false;
|
1289 | |
if (strcmp(child->className, debugClassName) == 0)
|
1290 | |
verbose = true;
|
1291 | |
#endif
|
1292 | |
|
1293 | |
int childIsRecursive = (child->recursiveEntries > 0);
|
1294 | |
int parentIsRecursive = (parent->recursiveEntries > 1);
|
1295 | |
|
1296 | |
if (child->recursiveEntries == 0) {
|
1297 | |
child->elapsedInclusive += elapsedTime;
|
1298 | |
} else if (child->recursiveEntries == 1) {
|
1299 | |
child->recursiveInclusive += elapsedTime;
|
1300 | |
}
|
1301 | |
child->numCalls[childIsRecursive] += 1;
|
1302 | |
|
1303 | |
#if 0
|
1304 | |
if (verbose) {
|
1305 | |
fprintf(stderr,
|
1306 | |
"%s %d elapsedTime: %lld eI: %lld, rI: %lld\n",
|
1307 | |
child->className, child->recursiveEntries,
|
1308 | |
elapsedTime, child->elapsedInclusive,
|
1309 | |
child->recursiveInclusive);
|
1310 | |
}
|
1311 | |
#endif
|
1312 | |
|
1313 | |
/* Find the child method in the parent */
|
1314 | |
TimedMethod *children = parent->children[parentIsRecursive];
|
1315 | |
for (pTimed = children; pTimed; pTimed = pTimed->next) {
|
1316 | |
if (pTimed->method == child) {
|
1317 | |
pTimed->elapsedInclusive += elapsedTime;
|
1318 | |
pTimed->numCalls += 1;
|
1319 | |
break;
|
1320 | |
}
|
1321 | |
}
|
1322 | |
if (pTimed == NULL) {
|
1323 | |
/* Allocate a new TimedMethod */
|
1324 | |
pTimed = (TimedMethod *) malloc(sizeof(TimedMethod));
|
1325 | |
pTimed->elapsedInclusive = elapsedTime;
|
1326 | |
pTimed->numCalls = 1;
|
1327 | |
pTimed->method = child;
|
1328 | |
|
1329 | |
/* Add it to the front of the list */
|
1330 | |
pTimed->next = children;
|
1331 | |
parent->children[parentIsRecursive] = pTimed;
|
1332 | |
}
|
1333 | |
|
1334 | |
/* Find the parent method in the child */
|
1335 | |
TimedMethod *parents = child->parents[childIsRecursive];
|
1336 | |
for (pTimed = parents; pTimed; pTimed = pTimed->next) {
|
1337 | |
if (pTimed->method == parent) {
|
1338 | |
pTimed->elapsedInclusive += elapsedTime;
|
1339 | |
pTimed->numCalls += 1;
|
1340 | |
break;
|
1341 | |
}
|
1342 | |
}
|
1343 | |
if (pTimed == NULL) {
|
1344 | |
/* Allocate a new TimedMethod */
|
1345 | |
pTimed = (TimedMethod *) malloc(sizeof(TimedMethod));
|
1346 | |
pTimed->elapsedInclusive = elapsedTime;
|
1347 | |
pTimed->numCalls = 1;
|
1348 | |
pTimed->method = parent;
|
1349 | |
|
1350 | |
/* Add it to the front of the list */
|
1351 | |
pTimed->next = parents;
|
1352 | |
child->parents[childIsRecursive] = pTimed;
|
1353 | |
}
|
1354 | |
|
1355 | |
#if 0
|
1356 | |
if (verbose) {
|
1357 | |
fprintf(stderr,
|
1358 | |
" %s %d eI: %lld\n",
|
1359 | |
parent->className, parent->recursiveEntries,
|
1360 | |
pTimed->elapsedInclusive);
|
1361 | |
}
|
1362 | |
#endif
|
1363 | |
}
|
1364 | |
|
1365 | |
/* Sorts a linked list and returns a newly allocated array containing
|
1366 | |
* the sorted entries.
|
1367 | |
*/
|
1368 | |
TimedMethod *sortTimedMethodList(TimedMethod *list, int *num)
|
1369 | |
{
|
1370 | |
int ii;
|
1371 | |
TimedMethod *pTimed, *sorted;
|
1372 | |
|
1373 | |
/* Count the elements */
|
1374 | |
int num_entries = 0;
|
1375 | |
for (pTimed = list; pTimed; pTimed = pTimed->next)
|
1376 | |
num_entries += 1;
|
1377 | |
*num = num_entries;
|
1378 | |
if (num_entries == 0)
|
1379 | |
return NULL;
|
1380 | |
|
1381 | |
/* Copy all the list elements to a new array and sort them */
|
1382 | |
sorted = (TimedMethod *) malloc(sizeof(TimedMethod) * num_entries);
|
1383 | |
for (ii = 0, pTimed = list; pTimed; pTimed = pTimed->next, ++ii)
|
1384 | |
memcpy(&sorted[ii], pTimed, sizeof(TimedMethod));
|
1385 | |
qsort(sorted, num_entries, sizeof(TimedMethod), compareTimedMethod);
|
1386 | |
|
1387 | |
/* Fix up the "next" pointers so that they work. */
|
1388 | |
for (ii = 0; ii < num_entries - 1; ++ii)
|
1389 | |
sorted[ii].next = &sorted[ii + 1];
|
1390 | |
sorted[num_entries - 1].next = NULL;
|
1391 | |
|
1392 | |
return sorted;
|
1393 | |
}
|
1394 | |
|
1395 | |
/* Define flag values for printInclusiveMethod() */
|
1396 | |
static const int kIsRecursive = 1;
|
1397 | |
|
1398 | |
/* This prints the inclusive stats for all the parents or children of a
|
1399 | |
* method, depending on the list that is passed in.
|
1400 | |
*/
|
1401 | |
void printInclusiveMethod(MethodEntry *method, TimedMethod *list, int numCalls,
|
1402 | |
int flags)
|
1403 | |
{
|
1404 | |
int num;
|
1405 | |
TimedMethod *pTimed;
|
1406 | |
char buf[80];
|
1407 | |
char *anchor_close;
|
1408 | |
char *spaces = " "; /* 6 spaces */
|
1409 | |
int num_spaces = strlen(spaces);
|
1410 | |
char *space_ptr = &spaces[num_spaces];
|
1411 | |
char *className, *methodName, *signature;
|
1412 | |
char classBuf[HTML_BUFSIZE], methodBuf[HTML_BUFSIZE];
|
1413 | |
char signatureBuf[HTML_BUFSIZE];
|
1414 | |
|
1415 | |
anchor_close = "";
|
1416 | |
if (gOptions.outputHtml)
|
1417 | |
anchor_close = "</a>";
|
1418 | |
|
1419 | |
TimedMethod *sorted = sortTimedMethodList(list, &num);
|
1420 | |
double methodTotal = method->elapsedInclusive;
|
1421 | |
for (pTimed = sorted; pTimed; pTimed = pTimed->next) {
|
1422 | |
MethodEntry *relative = pTimed->method;
|
1423 | |
className = (char*)(relative->className);
|
1424 | |
methodName = (char*)(relative->methodName);
|
1425 | |
signature = (char*)(relative->signature);
|
1426 | |
double per = 100.0 * pTimed->elapsedInclusive / methodTotal;
|
1427 | |
sprintf(buf, "[%d]", relative->index);
|
1428 | |
if (gOptions.outputHtml) {
|
1429 | |
int len = strlen(buf);
|
1430 | |
if (len > num_spaces)
|
1431 | |
len = num_spaces;
|
1432 | |
sprintf(buf, "<a href=\"#m%d\">[%d]",
|
1433 | |
relative->index, relative->index);
|
1434 | |
space_ptr = &spaces[len];
|
1435 | |
className = htmlEscape(className, classBuf, HTML_BUFSIZE);
|
1436 | |
methodName = htmlEscape(methodName, methodBuf, HTML_BUFSIZE);
|
1437 | |
signature = htmlEscape(signature, signatureBuf, HTML_BUFSIZE);
|
1438 | |
}
|
1439 | |
int nCalls = numCalls;
|
1440 | |
if (nCalls == 0)
|
1441 | |
nCalls = relative->numCalls[0] + relative->numCalls[1];
|
1442 | |
if (relative->methodName) {
|
1443 | |
if (flags & kIsRecursive) {
|
1444 | |
// Don't display percentages for recursive functions
|
1445 | |
printf("%6s %5s %6s %s%6s%s %6d/%-6d %9llu %s.%s %s\n",
|
1446 | |
"", "", "",
|
1447 | |
space_ptr, buf, anchor_close,
|
1448 | |
pTimed->numCalls, nCalls,
|
1449 | |
pTimed->elapsedInclusive,
|
1450 | |
className, methodName, signature);
|
1451 | |
} else {
|
1452 | |
printf("%6s %5s %5.1f%% %s%6s%s %6d/%-6d %9llu %s.%s %s\n",
|
1453 | |
"", "", per,
|
1454 | |
space_ptr, buf, anchor_close,
|
1455 | |
pTimed->numCalls, nCalls,
|
1456 | |
pTimed->elapsedInclusive,
|
1457 | |
className, methodName, signature);
|
1458 | |
}
|
1459 | |
} else {
|
1460 | |
if (flags & kIsRecursive) {
|
1461 | |
// Don't display percentages for recursive functions
|
1462 | |
printf("%6s %5s %6s %s%6s%s %6d/%-6d %9llu %s\n",
|
1463 | |
"", "", "",
|
1464 | |
space_ptr, buf, anchor_close,
|
1465 | |
pTimed->numCalls, nCalls,
|
1466 | |
pTimed->elapsedInclusive,
|
1467 | |
className);
|
1468 | |
} else {
|
1469 | |
printf("%6s %5s %5.1f%% %s%6s%s %6d/%-6d %9llu %s\n",
|
1470 | |
"", "", per,
|
1471 | |
space_ptr, buf, anchor_close,
|
1472 | |
pTimed->numCalls, nCalls,
|
1473 | |
pTimed->elapsedInclusive,
|
1474 | |
className);
|
1475 | |
}
|
1476 | |
}
|
1477 | |
}
|
1478 | |
}
|
1479 | |
|
1480 | |
void countRecursiveEntries(CallStack *pStack, int top, MethodEntry *method)
|
1481 | |
{
|
1482 | |
int ii;
|
1483 | |
|
1484 | |
method->recursiveEntries = 0;
|
1485 | |
for (ii = 0; ii < top; ++ii) {
|
1486 | |
if (pStack->calls[ii].method == method)
|
1487 | |
method->recursiveEntries += 1;
|
1488 | |
}
|
1489 | |
}
|
1490 | |
|
1491 | |
void stackDump(CallStack *pStack, int top)
|
1492 | |
{
|
1493 | |
int ii;
|
1494 | |
|
1495 | |
for (ii = 0; ii < top; ++ii) {
|
1496 | |
MethodEntry *method = pStack->calls[ii].method;
|
1497 | |
uint64_t entryTime = pStack->calls[ii].entryTime;
|
1498 | |
if (method->methodName) {
|
1499 | |
fprintf(stderr, " %2d: %8llu %s.%s %s\n", ii, entryTime,
|
1500 | |
method->className, method->methodName, method->signature);
|
1501 | |
} else {
|
1502 | |
fprintf(stderr, " %2d: %8llu %s\n", ii, entryTime, method->className);
|
1503 | |
}
|
1504 | |
}
|
1505 | |
}
|
1506 | |
|
1507 | |
void outputTableOfContents()
|
1508 | |
{
|
1509 | |
printf("<a name=\"contents\"></a>\n");
|
1510 | |
printf("<h2>Table of Contents</h2>\n");
|
1511 | |
printf("<ul>\n");
|
1512 | |
printf(" <li><a href=\"#exclusive\">Exclusive profile</a></li>\n");
|
1513 | |
printf(" <li><a href=\"#inclusive\">Inclusive profile</a></li>\n");
|
1514 | |
printf(" <li><a href=\"#class\">Class/method profile</a></li>\n");
|
1515 | |
printf(" <li><a href=\"#method\">Method/class profile</a></li>\n");
|
1516 | |
printf("</ul>\n\n");
|
1517 | |
}
|
1518 | |
|
1519 | |
void outputNavigationBar()
|
1520 | |
{
|
1521 | |
printf("<a href=\"#contents\">[Top]</a>\n");
|
1522 | |
printf("<a href=\"#exclusive\">[Exclusive]</a>\n");
|
1523 | |
printf("<a href=\"#inclusive\">[Inclusive]</a>\n");
|
1524 | |
printf("<a href=\"#class\">[Class]</a>\n");
|
1525 | |
printf("<a href=\"#method\">[Method]</a>\n");
|
1526 | |
printf("<br><br>\n");
|
1527 | |
}
|
1528 | |
|
1529 | |
void printExclusiveProfile(MethodEntry **pMethods, int numMethods,
|
1530 | |
uint64_t sumThreadTime)
|
1531 | |
{
|
1532 | |
int ii;
|
1533 | |
MethodEntry* method;
|
1534 | |
double total, sum, per, sum_per;
|
1535 | |
char classBuf[HTML_BUFSIZE], methodBuf[HTML_BUFSIZE];
|
1536 | |
char signatureBuf[HTML_BUFSIZE];
|
1537 | |
char anchor_buf[80];
|
1538 | |
char *anchor_close = "";
|
1539 | |
|
1540 | |
total = sumThreadTime;
|
1541 | |
anchor_buf[0] = 0;
|
1542 | |
if (gOptions.outputHtml) {
|
1543 | |
anchor_close = "</a>";
|
1544 | |
printf("<a name=\"exclusive\"></a>\n");
|
1545 | |
printf("<hr>\n");
|
1546 | |
outputNavigationBar();
|
1547 | |
} else {
|
1548 | |
printf("\n%s\n", profileSeparator);
|
1549 | |
}
|
1550 | |
|
1551 | |
/* First, sort the methods into decreasing order of inclusive
|
1552 | |
* elapsed time so that we can assign the method indices.
|
1553 | |
*/
|
1554 | |
qsort(pMethods, numMethods, sizeof(MethodEntry*), compareElapsedInclusive);
|
1555 | |
|
1556 | |
for (ii = 0; ii < numMethods; ++ii)
|
1557 | |
pMethods[ii]->index = ii;
|
1558 | |
|
1559 | |
/* Sort the methods into decreasing order of exclusive elapsed time.
|
1560 | |
*/
|
1561 | |
qsort(pMethods, numMethods, sizeof(MethodEntry*),
|
1562 | |
compareElapsedExclusive);
|
1563 | |
|
1564 | |
printf("Total cycles: %llu\n\n", sumThreadTime);
|
1565 | |
if (gOptions.outputHtml) {
|
1566 | |
printf("<br><br>\n");
|
1567 | |
}
|
1568 | |
printf("Exclusive elapsed times for each method, not including time spent in\n");
|
1569 | |
printf("children, sorted by exclusive time.\n\n");
|
1570 | |
if (gOptions.outputHtml) {
|
1571 | |
printf("<br><br>\n<pre>\n");
|
1572 | |
}
|
1573 | |
|
1574 | |
printf(" Usecs self %% sum %% Method\n");
|
1575 | |
sum = 0;
|
1576 | |
|
1577 | |
for (ii = 0; ii < numMethods; ++ii) {
|
1578 | |
char *className, *methodName, *signature;
|
1579 | |
|
1580 | |
method = pMethods[ii];
|
1581 | |
/* Don't show methods with zero cycles */
|
1582 | |
if (method->elapsedExclusive == 0)
|
1583 | |
break;
|
1584 | |
className = (char*)(method->className);
|
1585 | |
methodName = (char*)(method->methodName);
|
1586 | |
signature = (char*)(method->signature);
|
1587 | |
sum += method->elapsedExclusive;
|
1588 | |
per = 100.0 * method->elapsedExclusive / total;
|
1589 | |
sum_per = 100.0 * sum / total;
|
1590 | |
if (gOptions.outputHtml) {
|
1591 | |
sprintf(anchor_buf, "<a href=\"#m%d\">", method->index);
|
1592 | |
className = htmlEscape(className, classBuf, HTML_BUFSIZE);
|
1593 | |
methodName = htmlEscape(methodName, methodBuf, HTML_BUFSIZE);
|
1594 | |
signature = htmlEscape(signature, signatureBuf, HTML_BUFSIZE);
|
1595 | |
}
|
1596 | |
if (method->methodName) {
|
1597 | |
printf("%9llu %6.2f %6.2f %s[%d]%s %s.%s %s\n",
|
1598 | |
method->elapsedExclusive, per, sum_per,
|
1599 | |
anchor_buf, method->index, anchor_close,
|
1600 | |
className, methodName, signature);
|
1601 | |
} else {
|
1602 | |
printf("%9llu %6.2f %6.2f %s[%d]%s %s\n",
|
1603 | |
method->elapsedExclusive, per, sum_per,
|
1604 | |
anchor_buf, method->index, anchor_close,
|
1605 | |
className);
|
1606 | |
}
|
1607 | |
}
|
1608 | |
if (gOptions.outputHtml) {
|
1609 | |
printf("</pre>\n");
|
1610 | |
}
|
1611 | |
}
|
1612 | |
|
1613 | |
/* check to make sure that the child method meets the threshold of the parent */
|
1614 | |
int checkThreshold(MethodEntry* parent, MethodEntry* child)
|
1615 | |
{
|
1616 | |
double parentTime = parent->elapsedInclusive;
|
1617 | |
double childTime = child->elapsedInclusive;
|
1618 | |
int64_t percentage = (childTime / parentTime) * 100.0;
|
1619 | |
return (percentage < gOptions.threshold) ? 0 : 1;
|
1620 | |
}
|
1621 | |
|
1622 | |
void createLabels(FILE* file, MethodEntry* method)
|
1623 | |
{
|
1624 | |
fprintf(file, "node%d[label = \"[%d] %s.%s (%llu, %llu, %d)\"]\n",
|
1625 | |
method->index, method->index, method->className, method->methodName,
|
1626 | |
method->elapsedInclusive / 1000,
|
1627 | |
method->elapsedExclusive / 1000,
|
1628 | |
method->numCalls[0]);
|
1629 | |
|
1630 | |
method->graphState = GRAPH_LABEL_VISITED;
|
1631 | |
|
1632 | |
TimedMethod* child;
|
1633 | |
for (child = method->children[0] ; child ; child = child->next) {
|
1634 | |
MethodEntry* childMethod = child->method;
|
1635 | |
|
1636 | |
if ((childMethod->graphState & GRAPH_LABEL_VISITED) == 0 && checkThreshold(method, childMethod)) {
|
1637 | |
createLabels(file, child->method);
|
1638 | |
}
|
1639 | |
}
|
1640 | |
}
|
1641 | |
|
1642 | |
void createLinks(FILE* file, MethodEntry* method)
|
1643 | |
{
|
1644 | |
method->graphState |= GRAPH_NODE_VISITED;
|
1645 | |
|
1646 | |
TimedMethod* child;
|
1647 | |
for (child = method->children[0] ; child ; child = child->next) {
|
1648 | |
MethodEntry* childMethod = child->method;
|
1649 | |
if (checkThreshold(method, child->method)) {
|
1650 | |
fprintf(file, "node%d -> node%d\n", method->index, child->method->index);
|
1651 | |
// only visit children that haven't been visited before
|
1652 | |
if ((childMethod->graphState & GRAPH_NODE_VISITED) == 0) {
|
1653 | |
createLinks(file, child->method);
|
1654 | |
}
|
1655 | |
}
|
1656 | |
}
|
1657 | |
}
|
1658 | |
|
1659 | |
void createInclusiveProfileGraphNew(DataKeys* dataKeys)
|
1660 | |
{
|
1661 | |
// create a temporary file in /tmp
|
1662 | |
char path[FILENAME_MAX];
|
1663 | |
if (gOptions.keepDotFile) {
|
1664 | |
snprintf(path, FILENAME_MAX, "%s.dot", gOptions.graphFileName);
|
1665 | |
} else {
|
1666 | |
snprintf(path, FILENAME_MAX, "dot-%d-%d.dot", (int)time(NULL), rand());
|
1667 | |
}
|
1668 | |
|
1669 | |
FILE* file = fopen(path, "w+");
|
1670 | |
|
1671 | |
fprintf(file, "digraph g {\nnode [shape = record,height=.1];\n");
|
1672 | |
|
1673 | |
createLabels(file, dataKeys->methods);
|
1674 | |
createLinks(file, dataKeys->methods);
|
1675 | |
|
1676 | |
fprintf(file, "}");
|
1677 | |
fclose(file);
|
1678 | |
|
1679 | |
// now that we have the dot file generate the image
|
1680 | |
char command[1024];
|
1681 | |
snprintf(command, 1024, "dot -Tpng -o \"%s\" \"%s\"", gOptions.graphFileName, path);
|
1682 | |
|
1683 | |
system(command);
|
1684 | |
|
1685 | |
if (! gOptions.keepDotFile) {
|
1686 | |
remove(path);
|
1687 | |
}
|
1688 | |
}
|
1689 | |
|
1690 | |
void printInclusiveProfile(MethodEntry **pMethods, int numMethods,
|
1691 | |
uint64_t sumThreadTime)
|
1692 | |
{
|
1693 | |
int ii;
|
1694 | |
MethodEntry* method;
|
1695 | |
double total, sum, per, sum_per;
|
1696 | |
char classBuf[HTML_BUFSIZE], methodBuf[HTML_BUFSIZE];
|
1697 | |
char signatureBuf[HTML_BUFSIZE];
|
1698 | |
char anchor_buf[80];
|
1699 | |
char *anchor_close = "";
|
1700 | |
|
1701 | |
total = sumThreadTime;
|
1702 | |
anchor_buf[0] = 0;
|
1703 | |
if (gOptions.outputHtml) {
|
1704 | |
anchor_close = "</a>";
|
1705 | |
printf("<a name=\"inclusive\"></a>\n");
|
1706 | |
printf("<hr>\n");
|
1707 | |
outputNavigationBar();
|
1708 | |
} else {
|
1709 | |
printf("\n%s\n", profileSeparator);
|
1710 | |
}
|
1711 | |
|
1712 | |
/* Sort the methods into decreasing order of inclusive elapsed time. */
|
1713 | |
qsort(pMethods, numMethods, sizeof(MethodEntry*),
|
1714 | |
compareElapsedInclusive);
|
1715 | |
|
1716 | |
printf("\nInclusive elapsed times for each method and its parents and children,\n");
|
1717 | |
printf("sorted by inclusive time.\n\n");
|
1718 | |
|
1719 | |
if (gOptions.outputHtml) {
|
1720 | |
printf("<br><br>\n<pre>\n");
|
1721 | |
}
|
1722 | |
|
1723 | |
printf("index %%/total %%/self index calls usecs name\n");
|
1724 | |
for (ii = 0; ii < numMethods; ++ii) {
|
1725 | |
int num;
|
1726 | |
TimedMethod *pTimed;
|
1727 | |
double excl_per;
|
1728 | |
char buf[40];
|
1729 | |
char *className, *methodName, *signature;
|
1730 | |
|
1731 | |
method = pMethods[ii];
|
1732 | |
/* Don't show methods with zero cycles */
|
1733 | |
if (method->elapsedInclusive == 0)
|
1734 | |
break;
|
1735 | |
|
1736 | |
className = (char*)(method->className);
|
1737 | |
methodName = (char*)(method->methodName);
|
1738 | |
signature = (char*)(method->signature);
|
1739 | |
|
1740 | |
if (gOptions.outputHtml) {
|
1741 | |
printf("<a name=\"m%d\"></a>", method->index);
|
1742 | |
className = htmlEscape(className, classBuf, HTML_BUFSIZE);
|
1743 | |
methodName = htmlEscape(methodName, methodBuf, HTML_BUFSIZE);
|
1744 | |
signature = htmlEscape(signature, signatureBuf, HTML_BUFSIZE);
|
1745 | |
}
|
1746 | |
printf("----------------------------------------------------\n");
|
1747 | |
|
1748 | |
/* Sort and print the parents */
|
1749 | |
int numCalls = method->numCalls[0] + method->numCalls[1];
|
1750 | |
printInclusiveMethod(method, method->parents[0], numCalls, 0);
|
1751 | |
if (method->parents[1]) {
|
1752 | |
printf(" +++++++++++++++++++++++++\n");
|
1753 | |
printInclusiveMethod(method, method->parents[1], numCalls,
|
1754 | |
kIsRecursive);
|
1755 | |
}
|
1756 | |
|
1757 | |
per = 100.0 * method->elapsedInclusive / total;
|
1758 | |
sprintf(buf, "[%d]", ii);
|
1759 | |
if (method->methodName) {
|
1760 | |
printf("%-6s %5.1f%% %5s %6s %6d+%-6d %9llu %s.%s %s\n",
|
1761 | |
buf,
|
1762 | |
per, "", "", method->numCalls[0], method->numCalls[1],
|
1763 | |
method->elapsedInclusive,
|
1764 | |
className, methodName, signature);
|
1765 | |
} else {
|
1766 | |
printf("%-6s %5.1f%% %5s %6s %6d+%-6d %9llu %s\n",
|
1767 | |
buf,
|
1768 | |
per, "", "", method->numCalls[0], method->numCalls[1],
|
1769 | |
method->elapsedInclusive,
|
1770 | |
className);
|
1771 | |
}
|
1772 | |
excl_per = 100.0 * method->topExclusive / method->elapsedInclusive;
|
1773 | |
printf("%6s %5s %5.1f%% %6s %6s %6s %9llu\n",
|
1774 | |
"", "", excl_per, "excl", "", "", method->topExclusive);
|
1775 | |
|
1776 | |
/* Sort and print the children */
|
1777 | |
printInclusiveMethod(method, method->children[0], 0, 0);
|
1778 | |
if (method->children[1]) {
|
1779 | |
printf(" +++++++++++++++++++++++++\n");
|
1780 | |
printInclusiveMethod(method, method->children[1], 0,
|
1781 | |
kIsRecursive);
|
1782 | |
}
|
1783 | |
}
|
1784 | |
if (gOptions.outputHtml) {
|
1785 | |
printf("</pre>\n");
|
1786 | |
}
|
1787 | |
}
|
1788 | |
|
1789 | |
void createClassList(TraceData* traceData, MethodEntry **pMethods, int numMethods)
|
1790 | |
{
|
1791 | |
int ii;
|
1792 | |
|
1793 | |
/* Sort the methods into alphabetical order to find the unique class
|
1794 | |
* names.
|
1795 | |
*/
|
1796 | |
qsort(pMethods, numMethods, sizeof(MethodEntry*), compareClassNames);
|
1797 | |
|
1798 | |
/* Count the number of unique class names. */
|
1799 | |
const char *currentClassName = "";
|
1800 | |
const char *firstClassName = NULL;
|
1801 | |
traceData->numClasses = 0;
|
1802 | |
for (ii = 0; ii < numMethods; ++ii) {
|
1803 | |
if (pMethods[ii]->methodName == NULL) {
|
1804 | |
continue;
|
1805 | |
}
|
1806 | |
if (strcmp(pMethods[ii]->className, currentClassName) != 0) {
|
1807 | |
// Remember the first one
|
1808 | |
if (firstClassName == NULL) {
|
1809 | |
firstClassName = pMethods[ii]->className;
|
1810 | |
}
|
1811 | |
traceData->numClasses += 1;
|
1812 | |
currentClassName = pMethods[ii]->className;
|
1813 | |
}
|
1814 | |
}
|
1815 | |
|
1816 | |
if (traceData->numClasses == 0) {
|
1817 | |
traceData->classes = NULL;
|
1818 | |
return;
|
1819 | |
}
|
1820 | |
|
1821 | |
/* Allocate space for all of the unique class names */
|
1822 | |
traceData->classes = (ClassEntry *) malloc(sizeof(ClassEntry) * traceData->numClasses);
|
1823 | |
|
1824 | |
/* Initialize the classes array */
|
1825 | |
memset(traceData->classes, 0, sizeof(ClassEntry) * traceData->numClasses);
|
1826 | |
ClassEntry *pClass = traceData->classes;
|
1827 | |
pClass->className = currentClassName = firstClassName;
|
1828 | |
int prevNumMethods = 0;
|
1829 | |
for (ii = 0; ii < numMethods; ++ii) {
|
1830 | |
if (pMethods[ii]->methodName == NULL) {
|
1831 | |
continue;
|
1832 | |
}
|
1833 | |
if (strcmp(pMethods[ii]->className, currentClassName) != 0) {
|
1834 | |
pClass->numMethods = prevNumMethods;
|
1835 | |
(++pClass)->className = currentClassName = pMethods[ii]->className;
|
1836 | |
prevNumMethods = 0;
|
1837 | |
}
|
1838 | |
prevNumMethods += 1;
|
1839 | |
}
|
1840 | |
pClass->numMethods = prevNumMethods;
|
1841 | |
|
1842 | |
/* Create the array of MethodEntry pointers for each class */
|
1843 | |
pClass = NULL;
|
1844 | |
currentClassName = "";
|
1845 | |
int nextMethod = 0;
|
1846 | |
for (ii = 0; ii < numMethods; ++ii) {
|
1847 | |
if (pMethods[ii]->methodName == NULL) {
|
1848 | |
continue;
|
1849 | |
}
|
1850 | |
if (strcmp(pMethods[ii]->className, currentClassName) != 0) {
|
1851 | |
currentClassName = pMethods[ii]->className;
|
1852 | |
if (pClass == NULL)
|
1853 | |
pClass = traceData->classes;
|
1854 | |
else
|
1855 | |
pClass++;
|
1856 | |
/* Allocate space for the methods array */
|
1857 | |
int nbytes = sizeof(MethodEntry*) * pClass->numMethods;
|
1858 | |
pClass->methods = (MethodEntry**) malloc(nbytes);
|
1859 | |
nextMethod = 0;
|
1860 | |
}
|
1861 | |
pClass->methods[nextMethod++] = pMethods[ii];
|
1862 | |
}
|
1863 | |
}
|
1864 | |
|
1865 | |
/* Prints a number of html non-breaking spaces according so that the length
|
1866 | |
* of the string "buf" is at least "width" characters wide. If width is
|
1867 | |
* negative, then trailing spaces are added instead of leading spaces.
|
1868 | |
*/
|
1869 | |
void printHtmlField(char *buf, int width)
|
1870 | |
{
|
1871 | |
int ii;
|
1872 | |
|
1873 | |
int leadingSpaces = 1;
|
1874 | |
if (width < 0) {
|
1875 | |
width = -width;
|
1876 | |
leadingSpaces = 0;
|
1877 | |
}
|
1878 | |
int len = strlen(buf);
|
1879 | |
int numSpaces = width - len;
|
1880 | |
if (numSpaces <= 0) {
|
1881 | |
printf("%s", buf);
|
1882 | |
return;
|
1883 | |
}
|
1884 | |
if (leadingSpaces == 0)
|
1885 | |
printf("%s", buf);
|
1886 | |
for (ii = 0; ii < numSpaces; ++ii)
|
1887 | |
printf(" ");
|
1888 | |
if (leadingSpaces == 1)
|
1889 | |
printf("%s", buf);
|
1890 | |
}
|
1891 | |
|
1892 | |
void printClassProfiles(TraceData* traceData, uint64_t sumThreadTime)
|
1893 | |
{
|
1894 | |
int ii, jj;
|
1895 | |
MethodEntry* method;
|
1896 | |
double total, sum, per, sum_per;
|
1897 | |
char classBuf[HTML_BUFSIZE], methodBuf[HTML_BUFSIZE];
|
1898 | |
char signatureBuf[HTML_BUFSIZE];
|
1899 | |
|
1900 | |
total = sumThreadTime;
|
1901 | |
if (gOptions.outputHtml) {
|
1902 | |
printf("<a name=\"class\"></a>\n");
|
1903 | |
printf("<hr>\n");
|
1904 | |
outputNavigationBar();
|
1905 | |
} else {
|
1906 | |
printf("\n%s\n", profileSeparator);
|
1907 | |
}
|
1908 | |
|
1909 | |
if (traceData->numClasses == 0) {
|
1910 | |
printf("\nNo classes.\n");
|
1911 | |
if (gOptions.outputHtml) {
|
1912 | |
printf("<br><br>\n");
|
1913 | |
}
|
1914 | |
return;
|
1915 | |
}
|
1916 | |
|
1917 | |
printf("\nExclusive elapsed time for each class, summed over all the methods\n");
|
1918 | |
printf("in the class.\n\n");
|
1919 | |
if (gOptions.outputHtml) {
|
1920 | |
printf("<br><br>\n");
|
1921 | |
}
|
1922 | |
|
1923 | |
/* For each class, sum the exclusive times in all of the methods
|
1924 | |
* in that class. Also sum the number of method calls. Also
|
1925 | |
* sort the methods so the most expensive appear at the top.
|
1926 | |
*/
|
1927 | |
ClassEntry *pClass = traceData->classes;
|
1928 | |
for (ii = 0; ii < traceData->numClasses; ++ii, ++pClass) {
|
1929 | |
//printf("%s %d methods\n", pClass->className, pClass->numMethods);
|
1930 | |
int numMethods = pClass->numMethods;
|
1931 | |
for (jj = 0; jj < numMethods; ++jj) {
|
1932 | |
method = pClass->methods[jj];
|
1933 | |
pClass->elapsedExclusive += method->elapsedExclusive;
|
1934 | |
pClass->numCalls[0] += method->numCalls[0];
|
1935 | |
pClass->numCalls[1] += method->numCalls[1];
|
1936 | |
}
|
1937 | |
|
1938 | |
/* Sort the methods into decreasing order of exclusive time */
|
1939 | |
qsort(pClass->methods, numMethods, sizeof(MethodEntry*),
|
1940 | |
compareElapsedExclusive);
|
1941 | |
}
|
1942 | |
|
1943 | |
/* Allocate an array of pointers to the classes for more efficient
|
1944 | |
* sorting.
|
1945 | |
*/
|
1946 | |
ClassEntry **pClasses;
|
1947 | |
pClasses = (ClassEntry**) malloc(sizeof(ClassEntry*) * traceData->numClasses);
|
1948 | |
for (ii = 0; ii < traceData->numClasses; ++ii)
|
1949 | |
pClasses[ii] = &traceData->classes[ii];
|
1950 | |
|
1951 | |
/* Sort the classes into decreasing order of exclusive time */
|
1952 | |
qsort(pClasses, traceData->numClasses, sizeof(ClassEntry*), compareClassExclusive);
|
1953 | |
|
1954 | |
if (gOptions.outputHtml) {
|
1955 | |
printf("<div class=\"header\"><span class=\"parent\"> </span> ");
|
1956 | |
printf("Cycles %%/total Cumul.%% Calls+Recur Class</div>\n");
|
1957 | |
} else {
|
1958 | |
printf(" Cycles %%/total Cumul.%% Calls+Recur Class\n");
|
1959 | |
}
|
1960 | |
|
1961 | |
sum = 0;
|
1962 | |
for (ii = 0; ii < traceData->numClasses; ++ii) {
|
1963 | |
char *className, *methodName, *signature;
|
1964 | |
|
1965 | |
/* Skip classes with zero cycles */
|
1966 | |
pClass = pClasses[ii];
|
1967 | |
if (pClass->elapsedExclusive == 0)
|
1968 | |
break;
|
1969 | |
|
1970 | |
per = 100.0 * pClass->elapsedExclusive / total;
|
1971 | |
sum += pClass->elapsedExclusive;
|
1972 | |
sum_per = 100.0 * sum / total;
|
1973 | |
className = (char*)(pClass->className);
|
1974 | |
if (gOptions.outputHtml) {
|
1975 | |
char buf[80];
|
1976 | |
|
1977 | |
className = htmlEscape(className, classBuf, HTML_BUFSIZE);
|
1978 | |
printf("<div class=\"link\" onClick=\"javascript:toggle('d%d')\" onMouseOver=\"javascript:onMouseOver(this)\" onMouseOut=\"javascript:onMouseOut(this)\"><span class=\"parent\" id=\"xd%d\">+</span>", ii, ii);
|
1979 | |
sprintf(buf, "%llu", pClass->elapsedExclusive);
|
1980 | |
printHtmlField(buf, 9);
|
1981 | |
printf(" ");
|
1982 | |
sprintf(buf, "%.1f", per);
|
1983 | |
printHtmlField(buf, 7);
|
1984 | |
printf(" ");
|
1985 | |
sprintf(buf, "%.1f", sum_per);
|
1986 | |
printHtmlField(buf, 7);
|
1987 | |
printf(" ");
|
1988 | |
sprintf(buf, "%d", pClass->numCalls[0]);
|
1989 | |
printHtmlField(buf, 6);
|
1990 | |
printf("+");
|
1991 | |
sprintf(buf, "%d", pClass->numCalls[1]);
|
1992 | |
printHtmlField(buf, -6);
|
1993 | |
printf(" ");
|
1994 | |
printf("%s", className);
|
1995 | |
printf("</div>\n");
|
1996 | |
printf("<div class=\"parent\" id=\"d%d\">\n", ii);
|
1997 | |
} else {
|
1998 | |
printf("---------------------------------------------\n");
|
1999 | |
printf("%9llu %7.1f %7.1f %6d+%-6d %s\n",
|
2000 | |
pClass->elapsedExclusive, per, sum_per,
|
2001 | |
pClass->numCalls[0], pClass->numCalls[1],
|
2002 | |
className);
|
2003 | |
}
|
2004 | |
|
2005 | |
int numMethods = pClass->numMethods;
|
2006 | |
double classExclusive = pClass->elapsedExclusive;
|
2007 | |
double sumMethods = 0;
|
2008 | |
for (jj = 0; jj < numMethods; ++jj) {
|
2009 | |
method = pClass->methods[jj];
|
2010 | |
methodName = (char*)(method->methodName);
|
2011 | |
signature = (char*)(method->signature);
|
2012 | |
per = 100.0 * method->elapsedExclusive / classExclusive;
|
2013 | |
sumMethods += method->elapsedExclusive;
|
2014 | |
sum_per = 100.0 * sumMethods / classExclusive;
|
2015 | |
if (gOptions.outputHtml) {
|
2016 | |
char buf[80];
|
2017 | |
|
2018 | |
methodName = htmlEscape(methodName, methodBuf, HTML_BUFSIZE);
|
2019 | |
signature = htmlEscape(signature, signatureBuf, HTML_BUFSIZE);
|
2020 | |
printf("<div class=\"leaf\"><span class=\"leaf\"> </span>");
|
2021 | |
sprintf(buf, "%llu", method->elapsedExclusive);
|
2022 | |
printHtmlField(buf, 9);
|
2023 | |
printf(" ");
|
2024 | |
sprintf(buf, "%llu", method->elapsedInclusive);
|
2025 | |
printHtmlField(buf, 9);
|
2026 | |
printf(" ");
|
2027 | |
sprintf(buf, "%.1f", per);
|
2028 | |
printHtmlField(buf, 7);
|
2029 | |
printf(" ");
|
2030 | |
sprintf(buf, "%.1f", sum_per);
|
2031 | |
printHtmlField(buf, 7);
|
2032 | |
printf(" ");
|
2033 | |
sprintf(buf, "%d", method->numCalls[0]);
|
2034 | |
printHtmlField(buf, 6);
|
2035 | |
printf("+");
|
2036 | |
sprintf(buf, "%d", method->numCalls[1]);
|
2037 | |
printHtmlField(buf, -6);
|
2038 | |
printf(" ");
|
2039 | |
printf("<a href=\"#m%d\">[%d]</a> %s %s",
|
2040 | |
method->index, method->index, methodName, signature);
|
2041 | |
printf("</div>\n");
|
2042 | |
} else {
|
2043 | |
printf("%9llu %9llu %7.1f %7.1f %6d+%-6d [%d] %s %s\n",
|
2044 | |
method->elapsedExclusive,
|
2045 | |
method->elapsedInclusive,
|
2046 | |
per, sum_per,
|
2047 | |
method->numCalls[0], method->numCalls[1],
|
2048 | |
method->index, methodName, signature);
|
2049 | |
}
|
2050 | |
}
|
2051 | |
if (gOptions.outputHtml) {
|
2052 | |
printf("</div>\n");
|
2053 | |
}
|
2054 | |
}
|
2055 | |
}
|
2056 | |
|
2057 | |
void createUniqueMethodList(TraceData* traceData, MethodEntry **pMethods, int numMethods)
|
2058 | |
{
|
2059 | |
int ii;
|
2060 | |
|
2061 | |
/* Sort the methods into alphabetical order of method names
|
2062 | |
* to find the unique method names.
|
2063 | |
*/
|
2064 | |
qsort(pMethods, numMethods, sizeof(MethodEntry*), compareMethodNames);
|
2065 | |
|
2066 | |
/* Count the number of unique method names, ignoring class and
|
2067 | |
* signature.
|
2068 | |
*/
|
2069 | |
const char *currentMethodName = "";
|
2070 | |
traceData->numUniqueMethods = 0;
|
2071 | |
for (ii = 0; ii < numMethods; ++ii) {
|
2072 | |
if (pMethods[ii]->methodName == NULL)
|
2073 | |
continue;
|
2074 | |
if (strcmp(pMethods[ii]->methodName, currentMethodName) != 0) {
|
2075 | |
traceData->numUniqueMethods += 1;
|
2076 | |
currentMethodName = pMethods[ii]->methodName;
|
2077 | |
}
|
2078 | |
}
|
2079 | |
if (traceData->numUniqueMethods == 0)
|
2080 | |
return;
|
2081 | |
|
2082 | |
/* Allocate space for pointers to all of the unique methods */
|
2083 | |
int nbytes = sizeof(UniqueMethodEntry) * traceData->numUniqueMethods;
|
2084 | |
traceData->uniqueMethods = (UniqueMethodEntry *) malloc(nbytes);
|
2085 | |
|
2086 | |
/* Initialize the uniqueMethods array */
|
2087 | |
memset(traceData->uniqueMethods, 0, nbytes);
|
2088 | |
UniqueMethodEntry *pUnique = traceData->uniqueMethods;
|
2089 | |
currentMethodName = NULL;
|
2090 | |
int prevNumMethods = 0;
|
2091 | |
for (ii = 0; ii < numMethods; ++ii) {
|
2092 | |
if (pMethods[ii]->methodName == NULL)
|
2093 | |
continue;
|
2094 | |
if (currentMethodName == NULL)
|
2095 | |
currentMethodName = pMethods[ii]->methodName;
|
2096 | |
if (strcmp(pMethods[ii]->methodName, currentMethodName) != 0) {
|
2097 | |
currentMethodName = pMethods[ii]->methodName;
|
2098 | |
pUnique->numMethods = prevNumMethods;
|
2099 | |
pUnique++;
|
2100 | |
prevNumMethods = 0;
|
2101 | |
}
|
2102 | |
prevNumMethods += 1;
|
2103 | |
}
|
2104 | |
pUnique->numMethods = prevNumMethods;
|
2105 | |
|
2106 | |
/* Create the array of MethodEntry pointers for each unique method */
|
2107 | |
pUnique = NULL;
|
2108 | |
currentMethodName = "";
|
2109 | |
int nextMethod = 0;
|
2110 | |
for (ii = 0; ii < numMethods; ++ii) {
|
2111 | |
if (pMethods[ii]->methodName == NULL)
|
2112 | |
continue;
|
2113 | |
if (strcmp(pMethods[ii]->methodName, currentMethodName) != 0) {
|
2114 | |
currentMethodName = pMethods[ii]->methodName;
|
2115 | |
if (pUnique == NULL)
|
2116 | |
pUnique = traceData->uniqueMethods;
|
2117 | |
else
|
2118 | |
pUnique++;
|
2119 | |
/* Allocate space for the methods array */
|
2120 | |
int nbytes = sizeof(MethodEntry*) * pUnique->numMethods;
|
2121 | |
pUnique->methods = (MethodEntry**) malloc(nbytes);
|
2122 | |
nextMethod = 0;
|
2123 | |
}
|
2124 | |
pUnique->methods[nextMethod++] = pMethods[ii];
|
2125 | |
}
|
2126 | |
}
|
2127 | |
|
2128 | |
void printMethodProfiles(TraceData* traceData, uint64_t sumThreadTime)
|
2129 | |
{
|
2130 | |
int ii, jj;
|
2131 | |
MethodEntry* method;
|
2132 | |
double total, sum, per, sum_per;
|
2133 | |
char classBuf[HTML_BUFSIZE], methodBuf[HTML_BUFSIZE];
|
2134 | |
char signatureBuf[HTML_BUFSIZE];
|
2135 | |
|
2136 | |
if (traceData->numUniqueMethods == 0)
|
2137 | |
return;
|
2138 | |
|
2139 | |
total = sumThreadTime;
|
2140 | |
if (gOptions.outputHtml) {
|
2141 | |
printf("<a name=\"method\"></a>\n");
|
2142 | |
printf("<hr>\n");
|
2143 | |
outputNavigationBar();
|
2144 | |
} else {
|
2145 | |
printf("\n%s\n", profileSeparator);
|
2146 | |
}
|
2147 | |
|
2148 | |
printf("\nExclusive elapsed time for each method, summed over all the classes\n");
|
2149 | |
printf("that contain a method with the same name.\n\n");
|
2150 | |
if (gOptions.outputHtml) {
|
2151 | |
printf("<br><br>\n");
|
2152 | |
}
|
2153 | |
|
2154 | |
/* For each unique method, sum the exclusive times in all of the methods
|
2155 | |
* with the same name. Also sum the number of method calls. Also
|
2156 | |
* sort the methods so the most expensive appear at the top.
|
2157 | |
*/
|
2158 | |
UniqueMethodEntry *pUnique = traceData->uniqueMethods;
|
2159 | |
for (ii = 0; ii < traceData->numUniqueMethods; ++ii, ++pUnique) {
|
2160 | |
int numMethods = pUnique->numMethods;
|
2161 | |
for (jj = 0; jj < numMethods; ++jj) {
|
2162 | |
method = pUnique->methods[jj];
|
2163 | |
pUnique->elapsedExclusive += method->elapsedExclusive;
|
2164 | |
pUnique->numCalls[0] += method->numCalls[0];
|
2165 | |
pUnique->numCalls[1] += method->numCalls[1];
|
2166 | |
}
|
2167 | |
|
2168 | |
/* Sort the methods into decreasing order of exclusive time */
|
2169 | |
qsort(pUnique->methods, numMethods, sizeof(MethodEntry*),
|
2170 | |
compareElapsedExclusive);
|
2171 | |
}
|
2172 | |
|
2173 | |
/* Allocate an array of pointers to the methods for more efficient
|
2174 | |
* sorting.
|
2175 | |
*/
|
2176 | |
UniqueMethodEntry **pUniqueMethods;
|
2177 | |
int nbytes = sizeof(UniqueMethodEntry*) * traceData->numUniqueMethods;
|
2178 | |
pUniqueMethods = (UniqueMethodEntry**) malloc(nbytes);
|
2179 | |
for (ii = 0; ii < traceData->numUniqueMethods; ++ii)
|
2180 | |
pUniqueMethods[ii] = &traceData->uniqueMethods[ii];
|
2181 | |
|
2182 | |
/* Sort the methods into decreasing order of exclusive time */
|
2183 | |
qsort(pUniqueMethods, traceData->numUniqueMethods, sizeof(UniqueMethodEntry*),
|
2184 | |
compareUniqueExclusive);
|
2185 | |
|
2186 | |
if (gOptions.outputHtml) {
|
2187 | |
printf("<div class=\"header\"><span class=\"parent\"> </span> ");
|
2188 | |
printf("Cycles %%/total Cumul.%% Calls+Recur Method</div>\n");
|
2189 | |
} else {
|
2190 | |
printf(" Cycles %%/total Cumul.%% Calls+Recur Method\n");
|
2191 | |
}
|
2192 | |
|
2193 | |
sum = 0;
|
2194 | |
for (ii = 0; ii < traceData->numUniqueMethods; ++ii) {
|
2195 | |
char *className, *methodName, *signature;
|
2196 | |
|
2197 | |
/* Skip methods with zero cycles */
|
2198 | |
pUnique = pUniqueMethods[ii];
|
2199 | |
if (pUnique->elapsedExclusive == 0)
|
2200 | |
break;
|
2201 | |
|
2202 | |
per = 100.0 * pUnique->elapsedExclusive / total;
|
2203 | |
sum += pUnique->elapsedExclusive;
|
2204 | |
sum_per = 100.0 * sum / total;
|
2205 | |
methodName = (char*)(pUnique->methods[0]->methodName);
|
2206 | |
if (gOptions.outputHtml) {
|
2207 | |
char buf[80];
|
2208 | |
|
2209 | |
methodName = htmlEscape(methodName, methodBuf, HTML_BUFSIZE);
|
2210 | |
printf("<div class=\"link\" onClick=\"javascript:toggle('e%d')\" onMouseOver=\"javascript:onMouseOver(this)\" onMouseOut=\"javascript:onMouseOut(this)\"><span class=\"parent\" id=\"xe%d\">+</span>", ii, ii);
|
2211 | |
sprintf(buf, "%llu", pUnique->elapsedExclusive);
|
2212 | |
printHtmlField(buf, 9);
|
2213 | |
printf(" ");
|
2214 | |
sprintf(buf, "%.1f", per);
|
2215 | |
printHtmlField(buf, 7);
|
2216 | |
printf(" ");
|
2217 | |
sprintf(buf, "%.1f", sum_per);
|
2218 | |
printHtmlField(buf, 7);
|
2219 | |
printf(" ");
|
2220 | |
sprintf(buf, "%d", pUnique->numCalls[0]);
|
2221 | |
printHtmlField(buf, 6);
|
2222 | |
printf("+");
|
2223 | |
sprintf(buf, "%d", pUnique->numCalls[1]);
|
2224 | |
printHtmlField(buf, -6);
|
2225 | |
printf(" ");
|
2226 | |
printf("%s", methodName);
|
2227 | |
printf("</div>\n");
|
2228 | |
printf("<div class=\"parent\" id=\"e%d\">\n", ii);
|
2229 | |
} else {
|
2230 | |
printf("---------------------------------------------\n");
|
2231 | |
printf("%9llu %7.1f %7.1f %6d+%-6d %s\n",
|
2232 | |
pUnique->elapsedExclusive, per, sum_per,
|
2233 | |
pUnique->numCalls[0], pUnique->numCalls[1],
|
2234 | |
methodName);
|
2235 | |
}
|
2236 | |
int numMethods = pUnique->numMethods;
|
2237 | |
double methodExclusive = pUnique->elapsedExclusive;
|
2238 | |
double sumMethods = 0;
|
2239 | |
for (jj = 0; jj < numMethods; ++jj) {
|
2240 | |
method = pUnique->methods[jj];
|
2241 | |
className = (char*)(method->className);
|
2242 | |
signature = (char*)(method->signature);
|
2243 | |
per = 100.0 * method->elapsedExclusive / methodExclusive;
|
2244 | |
sumMethods += method->elapsedExclusive;
|
2245 | |
sum_per = 100.0 * sumMethods / methodExclusive;
|
2246 | |
if (gOptions.outputHtml) {
|
2247 | |
char buf[80];
|
2248 | |
|
2249 | |
className = htmlEscape(className, classBuf, HTML_BUFSIZE);
|
2250 | |
signature = htmlEscape(signature, signatureBuf, HTML_BUFSIZE);
|
2251 | |
printf("<div class=\"leaf\"><span class=\"leaf\"> </span>");
|
2252 | |
sprintf(buf, "%llu", method->elapsedExclusive);
|
2253 | |
printHtmlField(buf, 9);
|
2254 | |
printf(" ");
|
2255 | |
sprintf(buf, "%llu", method->elapsedInclusive);
|
2256 | |
printHtmlField(buf, 9);
|
2257 | |
printf(" ");
|
2258 | |
sprintf(buf, "%.1f", per);
|
2259 | |
printHtmlField(buf, 7);
|
2260 | |
printf(" ");
|
2261 | |
sprintf(buf, "%.1f", sum_per);
|
2262 | |
printHtmlField(buf, 7);
|
2263 | |
printf(" ");
|
2264 | |
sprintf(buf, "%d", method->numCalls[0]);
|
2265 | |
printHtmlField(buf, 6);
|
2266 | |
printf("+");
|
2267 | |
sprintf(buf, "%d", method->numCalls[1]);
|
2268 | |
printHtmlField(buf, -6);
|
2269 | |
printf(" ");
|
2270 | |
printf("<a href=\"#m%d\">[%d]</a> %s.%s %s",
|
2271 | |
method->index, method->index,
|
2272 | |
className, methodName, signature);
|
2273 | |
printf("</div>\n");
|
2274 | |
} else {
|
2275 | |
printf("%9llu %9llu %7.1f %7.1f %6d+%-6d [%d] %s.%s %s\n",
|
2276 | |
method->elapsedExclusive,
|
2277 | |
method->elapsedInclusive,
|
2278 | |
per, sum_per,
|
2279 | |
method->numCalls[0], method->numCalls[1],
|
2280 | |
method->index, className, methodName, signature);
|
2281 | |
}
|
2282 | |
}
|
2283 | |
if (gOptions.outputHtml) {
|
2284 | |
printf("</div>\n");
|
2285 | |
}
|
2286 | |
}
|
2287 | |
}
|
2288 | |
|
2289 | |
/*
|
2290 | |
* Read the key and data files and return the MethodEntries for those files
|
2291 | |
*/
|
2292 | |
DataKeys* parseDataKeys(TraceData* traceData, const char* traceFileName, uint64_t* threadTime)
|
2293 | |
{
|
2294 | |
DataKeys* dataKeys = NULL;
|
2295 | |
MethodEntry **pMethods = NULL;
|
2296 | |
MethodEntry* method;
|
2297 | |
FILE* dataFp = NULL;
|
2298 | |
DataHeader dataHeader;
|
2299 | |
int ii;
|
2300 | |
uint64_t currentTime;
|
2301 | |
MethodEntry* caller;
|
2302 | |
|
2303 | |
dataFp = fopen(traceFileName, "rb");
|
2304 | |
if (dataFp == NULL)
|
2305 | |
goto bail;
|
2306 | |
|
2307 | |
if ((dataKeys = parseKeys(dataFp, 0)) == NULL)
|
2308 | |
goto bail;
|
2309 | |
|
2310 | |
if (parseDataHeader(dataFp, &dataHeader) < 0)
|
2311 | |
goto bail;
|
2312 | |
|
2313 | |
#if 0
|
2314 | |
FILE *dumpStream = fopen("debug", "w");
|
2315 | |
#endif
|
2316 | |
while (1) {
|
2317 | |
int threadId;
|
2318 | |
unsigned int methodVal;
|
2319 | |
int action;
|
2320 | |
int64_t methodId;
|
2321 | |
CallStack *pStack;
|
2322 | |
/*
|
2323 | |
* Extract values from file.
|
2324 | |
*/
|
2325 | |
if (readDataRecord(dataFp, &dataHeader, &threadId, &methodVal, ¤tTime))
|
2326 | |
break;
|
2327 | |
|
2328 | |
action = METHOD_ACTION(methodVal);
|
2329 | |
methodId = METHOD_ID(methodVal);
|
2330 | |
|
2331 | |
/* Get the call stack for this thread */
|
2332 | |
pStack = traceData->stacks[threadId];
|
2333 | |
|
2334 | |
/* If there is no call stack yet for this thread, then allocate one */
|
2335 | |
if (pStack == NULL) {
|
2336 | |
pStack = malloc(sizeof(CallStack));
|
2337 | |
pStack->top = 0;
|
2338 | |
pStack->lastEventTime = currentTime;
|
2339 | |
pStack->threadStartTime = currentTime;
|
2340 | |
traceData->stacks[threadId] = pStack;
|
2341 | |
}
|
2342 | |
|
2343 | |
/* Lookup the current method */
|
2344 | |
method = lookupMethod(dataKeys, methodId);
|
2345 | |
if (method == NULL)
|
2346 | |
method = &dataKeys->methods[UNKNOWN_INDEX];
|
2347 | |
|
2348 | |
#if 0
|
2349 | |
if (method->methodName) {
|
2350 | |
fprintf(dumpStream, "%2d %-8llu %d %8llu r %d c %d %s.%s %s\n",
|
2351 | |
threadId, currentTime, action, pStack->threadStartTime,
|
2352 | |
method->recursiveEntries,
|
2353 | |
pStack->top, method->className, method->methodName,
|
2354 | |
method->signature);
|
2355 | |
} else {
|
2356 | |
fprintf(dumpStream, "%2d %-8llu %d %8llu r %d c %d %s\n",
|
2357 | |
threadId, currentTime, action, pStack->threadStartTime,
|
2358 | |
method->recursiveEntries,
|
2359 | |
pStack->top, method->className);
|
2360 | |
}
|
2361 | |
#endif
|
2362 | |
|
2363 | |
if (action == METHOD_TRACE_ENTER) {
|
2364 | |
/* This is a method entry */
|
2365 | |
if (pStack->top >= MAX_STACK_DEPTH) {
|
2366 | |
fprintf(stderr, "Stack overflow (exceeded %d frames)\n",
|
2367 | |
MAX_STACK_DEPTH);
|
2368 | |
exit(1);
|
2369 | |
}
|
2370 | |
|
2371 | |
/* Get the caller method */
|
2372 | |
if (pStack->top >= 1)
|
2373 | |
caller = pStack->calls[pStack->top - 1].method;
|
2374 | |
else
|
2375 | |
caller = &dataKeys->methods[TOPLEVEL_INDEX];
|
2376 | |
countRecursiveEntries(pStack, pStack->top, caller);
|
2377 | |
caller->elapsedExclusive += currentTime - pStack->lastEventTime;
|
2378 | |
#if 0
|
2379 | |
if (caller->elapsedExclusive > 10000000)
|
2380 | |
fprintf(dumpStream, "%llu current %llu last %llu diff %llu\n",
|
2381 | |
caller->elapsedExclusive, currentTime,
|
2382 | |
pStack->lastEventTime,
|
2383 | |
currentTime - pStack->lastEventTime);
|
2384 | |
#endif
|
2385 | |
if (caller->recursiveEntries <= 1) {
|
2386 | |
caller->topExclusive += currentTime - pStack->lastEventTime;
|
2387 | |
}
|
2388 | |
|
2389 | |
/* Push the method on the stack for this thread */
|
2390 | |
pStack->calls[pStack->top].method = method;
|
2391 | |
pStack->calls[pStack->top++].entryTime = currentTime;
|
2392 | |
} else {
|
2393 | |
/* This is a method exit */
|
2394 | |
uint64_t entryTime = 0;
|
2395 | |
|
2396 | |
/* Pop the method off the stack for this thread */
|
2397 | |
if (pStack->top > 0) {
|
2398 | |
pStack->top -= 1;
|
2399 | |
entryTime = pStack->calls[pStack->top].entryTime;
|
2400 | |
if (method != pStack->calls[pStack->top].method) {
|
2401 | |
if (method->methodName) {
|
2402 | |
fprintf(stderr,
|
2403 | |
"Exit from method %s.%s %s does not match stack:\n",
|
2404 | |
method->className, method->methodName,
|
2405 | |
method->signature);
|
2406 | |
} else {
|
2407 | |
fprintf(stderr,
|
2408 | |
"Exit from method %s does not match stack:\n",
|
2409 | |
method->className);
|
2410 | |
}
|
2411 | |
stackDump(pStack, pStack->top + 1);
|
2412 | |
exit(1);
|
2413 | |
}
|
2414 | |
}
|
2415 | |
|
2416 | |
/* Get the caller method */
|
2417 | |
if (pStack->top >= 1)
|
2418 | |
caller = pStack->calls[pStack->top - 1].method;
|
2419 | |
else
|
2420 | |
caller = &dataKeys->methods[TOPLEVEL_INDEX];
|
2421 | |
countRecursiveEntries(pStack, pStack->top, caller);
|
2422 | |
countRecursiveEntries(pStack, pStack->top, method);
|
2423 | |
uint64_t elapsed = currentTime - entryTime;
|
2424 | |
addInclusiveTime(caller, method, elapsed);
|
2425 | |
method->elapsedExclusive += currentTime - pStack->lastEventTime;
|
2426 | |
if (method->recursiveEntries == 0) {
|
2427 | |
method->topExclusive += currentTime - pStack->lastEventTime;
|
2428 | |
}
|
2429 | |
}
|
2430 | |
/* Remember the time of the last entry or exit event */
|
2431 | |
pStack->lastEventTime = currentTime;
|
2432 | |
}
|
2433 | |
|
2434 | |
/* If we have calls on the stack when the trace ends, then clean
|
2435 | |
* up the stack and add time to the callers by pretending that we
|
2436 | |
* are exiting from their methods now.
|
2437 | |
*/
|
2438 | |
CallStack *pStack;
|
2439 | |
int threadId;
|
2440 | |
uint64_t sumThreadTime = 0;
|
2441 | |
for (threadId = 0; threadId < MAX_THREADS; ++threadId) {
|
2442 | |
pStack = traceData->stacks[threadId];
|
2443 | |
|
2444 | |
/* If this thread never existed, then continue with next thread */
|
2445 | |
if (pStack == NULL)
|
2446 | |
continue;
|
2447 | |
|
2448 | |
/* Also, add up the time taken by all of the threads */
|
2449 | |
sumThreadTime += pStack->lastEventTime - pStack->threadStartTime;
|
2450 | |
|
2451 | |
for (ii = 0; ii < pStack->top; ++ii) {
|
2452 | |
if (ii == 0)
|
2453 | |
caller = &dataKeys->methods[TOPLEVEL_INDEX];
|
2454 | |
else
|
2455 | |
caller = pStack->calls[ii - 1].method;
|
2456 | |
method = pStack->calls[ii].method;
|
2457 | |
countRecursiveEntries(pStack, ii, caller);
|
2458 | |
countRecursiveEntries(pStack, ii, method);
|
2459 | |
|
2460 | |
uint64_t entryTime = pStack->calls[ii].entryTime;
|
2461 | |
uint64_t elapsed = pStack->lastEventTime - entryTime;
|
2462 | |
addInclusiveTime(caller, method, elapsed);
|
2463 | |
}
|
2464 | |
}
|
2465 | |
caller = &dataKeys->methods[TOPLEVEL_INDEX];
|
2466 | |
caller->elapsedInclusive = sumThreadTime;
|
2467 | |
|
2468 | |
#if 0
|
2469 | |
fclose(dumpStream);
|
2470 | |
#endif
|
2471 | |
|
2472 | |
if (threadTime != NULL) {
|
2473 | |
*threadTime = sumThreadTime;
|
2474 | |
}
|
2475 | |
|
2476 | |
bail:
|
2477 | |
if (dataFp != NULL)
|
2478 | |
fclose(dataFp);
|
2479 | |
|
2480 | |
return dataKeys;
|
2481 | |
}
|
2482 | |
|
2483 | |
MethodEntry** parseMethodEntries(DataKeys* dataKeys)
|
2484 | |
{
|
2485 | |
int ii;
|
2486 | |
/* Create a new array of pointers to the methods and sort the pointers
|
2487 | |
* instead of the actual MethodEntry structs. We need to do this
|
2488 | |
* because there are other lists that contain pointers to the
|
2489 | |
* MethodEntry structs.
|
2490 | |
*/
|
2491 | |
MethodEntry** pMethods = (MethodEntry**) malloc(sizeof(MethodEntry*) * dataKeys->numMethods);
|
2492 | |
for (ii = 0; ii < dataKeys->numMethods; ++ii) {
|
2493 | |
MethodEntry* entry = &dataKeys->methods[ii];
|
2494 | |
pMethods[ii] = entry;
|
2495 | |
}
|
2496 | |
|
2497 | |
return pMethods;
|
2498 | |
}
|
2499 | |
|
2500 | |
/*
|
2501 | |
* Produce a function profile from the following methods
|
2502 | |
*/
|
2503 | |
void profileTrace(TraceData* traceData, MethodEntry **pMethods, int numMethods, uint64_t sumThreadTime)
|
2504 | |
{
|
2505 | |
/* Print the html header, if necessary */
|
2506 | |
if (gOptions.outputHtml) {
|
2507 | |
printf(htmlHeader, gOptions.sortableUrl);
|
2508 | |
outputTableOfContents();
|
2509 | |
}
|
2510 | |
|
2511 | |
printExclusiveProfile(pMethods, numMethods, sumThreadTime);
|
2512 | |
printInclusiveProfile(pMethods, numMethods, sumThreadTime);
|
2513 | |
|
2514 | |
createClassList(traceData, pMethods, numMethods);
|
2515 | |
printClassProfiles(traceData, sumThreadTime);
|
2516 | |
|
2517 | |
createUniqueMethodList(traceData, pMethods, numMethods);
|
2518 | |
printMethodProfiles(traceData, sumThreadTime);
|
2519 | |
|
2520 | |
if (gOptions.outputHtml) {
|
2521 | |
printf("%s", htmlFooter);
|
2522 | |
}
|
2523 | |
}
|
2524 | |
|
2525 | |
int compareMethodNamesForDiff(const void *a, const void *b)
|
2526 | |
{
|
2527 | |
int result;
|
2528 | |
|
2529 | |
const MethodEntry *methodA = *(const MethodEntry**)a;
|
2530 | |
const MethodEntry *methodB = *(const MethodEntry**)b;
|
2531 | |
if (methodA->methodName == NULL || methodB->methodName == NULL) {
|
2532 | |
return compareClassNames(a, b);
|
2533 | |
}
|
2534 | |
result = strcmp(methodA->methodName, methodB->methodName);
|
2535 | |
if (result == 0) {
|
2536 | |
result = strcmp(methodA->signature, methodB->signature);
|
2537 | |
if (result == 0) {
|
2538 | |
return strcmp(methodA->className, methodB->className);
|
2539 | |
}
|
2540 | |
}
|
2541 | |
return result;
|
2542 | |
}
|
2543 | |
|
2544 | |
int findMatch(MethodEntry** methods, int size, MethodEntry* matchThis)
|
2545 | |
{
|
2546 | |
int i;
|
2547 | |
|
2548 | |
for (i = 0 ; i < size ; i++) {
|
2549 | |
MethodEntry* method = methods[i];
|
2550 | |
|
2551 | |
if (method != NULL && !compareMethodNamesForDiff(&method, &matchThis)) {
|
2552 | |
// printf("%s.%s == %s.%s<br>\n", matchThis->className, matchThis->methodName,
|
2553 | |
// method->className, method->methodName);
|
2554 | |
|
2555 | |
return i;
|
2556 | |
/* if (!compareMethodNames(&method, &matchThis)) {
|
2557 | |
return i;
|
2558 | |
}
|
2559 | |
*/ }
|
2560 | |
}
|
2561 | |
|
2562 | |
return -1;
|
2563 | |
}
|
2564 | |
|
2565 | |
int compareDiffEntriesExculsive(const void *a, const void *b)
|
2566 | |
{
|
2567 | |
int result;
|
2568 | |
|
2569 | |
const DiffEntry* entryA = (const DiffEntry*)a;
|
2570 | |
const DiffEntry* entryB = (const DiffEntry*)b;
|
2571 | |
|
2572 | |
if (entryA->differenceExclusive < entryB->differenceExclusive) {
|
2573 | |
return 1;
|
2574 | |
} else if (entryA->differenceExclusive > entryB->differenceExclusive) {
|
2575 | |
return -1;
|
2576 | |
}
|
2577 | |
|
2578 | |
return 0;
|
2579 | |
}
|
2580 | |
|
2581 | |
int compareDiffEntriesInculsive(const void *a, const void *b)
|
2582 | |
{
|
2583 | |
int result;
|
2584 | |
|
2585 | |
const DiffEntry* entryA = (const DiffEntry*)a;
|
2586 | |
const DiffEntry* entryB = (const DiffEntry*)b;
|
2587 | |
|
2588 | |
if (entryA->differenceInclusive < entryB->differenceInclusive) {
|
2589 | |
return 1;
|
2590 | |
} else if (entryA->differenceInclusive > entryB->differenceInclusive) {
|
2591 | |
return -1;
|
2592 | |
}
|
2593 | |
|
2594 | |
return 0;
|
2595 | |
}
|
2596 | |
|
2597 | |
void printMissingMethod(MethodEntry* method)
|
2598 | |
{
|
2599 | |
char classBuf[HTML_BUFSIZE];
|
2600 | |
char methodBuf[HTML_BUFSIZE];
|
2601 | |
char* className;
|
2602 | |
char* methodName;
|
2603 | |
|
2604 | |
className = htmlEscape(method->className, classBuf, HTML_BUFSIZE);
|
2605 | |
methodName = htmlEscape(method->methodName, methodBuf, HTML_BUFSIZE);
|
2606 | |
|
2607 | |
if (gOptions.outputHtml) printf("<tr><td>\n");
|
2608 | |
|
2609 | |
printf("%s.%s ", className, methodName);
|
2610 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2611 | |
|
2612 | |
printf("%lld ", method->elapsedExclusive);
|
2613 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2614 | |
|
2615 | |
printf("%lld ", method->elapsedInclusive);
|
2616 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2617 | |
|
2618 | |
printf("%d\n", method->numCalls[0]);
|
2619 | |
if (gOptions.outputHtml) printf("</td><td>\n");
|
2620 | |
}
|
2621 | |
|
2622 | |
|
2623 | |
void createDiff(DataKeys* d1, uint64_t sum1, DataKeys* d2, uint64_t sum2)
|
2624 | |
{
|
2625 | |
MethodEntry** methods1 = parseMethodEntries(d1);
|
2626 | |
MethodEntry** methods2 = parseMethodEntries(d2);
|
2627 | |
|
2628 | |
// sort and assign the indicies
|
2629 | |
int i;
|
2630 | |
qsort(methods1, d1->numMethods, sizeof(MethodEntry*), compareElapsedInclusive);
|
2631 | |
for (i = 0; i < d1->numMethods; ++i) {
|
2632 | |
methods1[i]->index = i;
|
2633 | |
}
|
2634 | |
|
2635 | |
qsort(methods2, d2->numMethods, sizeof(MethodEntry*), compareElapsedInclusive);
|
2636 | |
for (i = 0; i < d2->numMethods; ++i) {
|
2637 | |
methods2[i]->index = i;
|
2638 | |
}
|
2639 | |
|
2640 | |
int max = (d1->numMethods < d2->numMethods) ? d2->numMethods : d1->numMethods;
|
2641 | |
max++;
|
2642 | |
DiffEntry* diffs = (DiffEntry*)malloc(max * sizeof(DiffEntry));
|
2643 | |
memset(diffs, 0, max * sizeof(DiffEntry));
|
2644 | |
DiffEntry* ptr = diffs;
|
2645 | |
|
2646 | |
// printf("<br>d1->numMethods: %d d1->numMethods: %d<br>\n", d1->numMethods, d2->numMethods);
|
2647 | |
|
2648 | |
int matches = 0;
|
2649 | |
|
2650 | |
for (i = 0 ; i < d1->numMethods ; i++) {
|
2651 | |
int match = findMatch(methods2, d2->numMethods, methods1[i]);
|
2652 | |
if (match >= 0) {
|
2653 | |
ptr->method1 = methods1[i];
|
2654 | |
ptr->method2 = methods2[match];
|
2655 | |
|
2656 | |
uint64_t e1 = ptr->method1->elapsedExclusive;
|
2657 | |
uint64_t e2 = ptr->method2->elapsedExclusive;
|
2658 | |
if (e1 > 0) {
|
2659 | |
ptr->differenceExclusive = e2 - e1;
|
2660 | |
ptr->differenceExclusivePercentage = ((double)e2 / (double)e1) * 100.0;
|
2661 | |
}
|
2662 | |
|
2663 | |
uint64_t i1 = ptr->method1->elapsedInclusive;
|
2664 | |
uint64_t i2 = ptr->method2->elapsedInclusive;
|
2665 | |
if (i1 > 0) {
|
2666 | |
ptr->differenceInclusive = i2 - i1;
|
2667 | |
ptr->differenceInclusivePercentage = ((double)i2 / (double)i1) * 100.0;
|
2668 | |
}
|
2669 | |
|
2670 | |
// clear these out so we don't find them again and we know which ones
|
2671 | |
// we have left over
|
2672 | |
methods1[i] = NULL;
|
2673 | |
methods2[match] = NULL;
|
2674 | |
ptr++;
|
2675 | |
|
2676 | |
matches++;
|
2677 | |
}
|
2678 | |
}
|
2679 | |
ptr->method1 = NULL;
|
2680 | |
ptr->method2 = NULL;
|
2681 | |
|
2682 | |
qsort(diffs, matches, sizeof(DiffEntry), compareDiffEntriesExculsive);
|
2683 | |
ptr = diffs;
|
2684 | |
|
2685 | |
if (gOptions.outputHtml) {
|
2686 | |
printf(htmlHeader, gOptions.sortableUrl);
|
2687 | |
printf("<h3>Table of Contents</h3>\n");
|
2688 | |
printf("<ul>\n");
|
2689 | |
printf("<li><a href='#exclusive'>Exclusive</a>\n");
|
2690 | |
printf("<li><a href='#inclusive'>Inclusive</a>\n");
|
2691 | |
printf("</ul>\n");
|
2692 | |
printf("Run 1: %s<br>\n", gOptions.diffFileName);
|
2693 | |
printf("Run 2: %s<br>\n", gOptions.traceFileName);
|
2694 | |
printf("<a name=\"exclusive\"></a><h3 id=\"exclusive\">Exclusive</h3>\n");
|
2695 | |
printf(tableHeader, "exclusive_table");
|
2696 | |
}
|
2697 | |
|
2698 | |
char classBuf[HTML_BUFSIZE];
|
2699 | |
char methodBuf[HTML_BUFSIZE];
|
2700 | |
char* className;
|
2701 | |
char* methodName;
|
2702 | |
|
2703 | |
while (ptr->method1 != NULL && ptr->method2 != NULL) {
|
2704 | |
if (gOptions.outputHtml) printf("<tr><td>\n");
|
2705 | |
|
2706 | |
className = htmlEscape(ptr->method1->className, classBuf, HTML_BUFSIZE);
|
2707 | |
methodName = htmlEscape(ptr->method1->methodName, methodBuf, HTML_BUFSIZE);
|
2708 | |
|
2709 | |
printf("%s.%s ", className, methodName);
|
2710 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2711 | |
|
2712 | |
printf("%lld ", ptr->method1->elapsedExclusive);
|
2713 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2714 | |
|
2715 | |
printf("%llu ", ptr->method2->elapsedExclusive);
|
2716 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2717 | |
|
2718 | |
printf("%lld ", ptr->differenceExclusive);
|
2719 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2720 | |
|
2721 | |
printf("%.2f\n", ptr->differenceExclusivePercentage);
|
2722 | |
if (gOptions.outputHtml) printf("</td><td>\n");
|
2723 | |
|
2724 | |
printf("%d\n", ptr->method1->numCalls[0]);
|
2725 | |
if (gOptions.outputHtml) printf("</td><td>\n");
|
2726 | |
|
2727 | |
printf("%d\n", ptr->method2->numCalls[0]);
|
2728 | |
if (gOptions.outputHtml) printf("</td></tr>\n");
|
2729 | |
|
2730 | |
ptr++;
|
2731 | |
}
|
2732 | |
|
2733 | |
if (gOptions.outputHtml) printf("</table>\n");
|
2734 | |
|
2735 | |
if (gOptions.outputHtml) {
|
2736 | |
printf(htmlHeader, gOptions.sortableUrl);
|
2737 | |
printf("Run 1: %s<br>\n", gOptions.diffFileName);
|
2738 | |
printf("Run 2: %s<br>\n", gOptions.traceFileName);
|
2739 | |
printf("<a name=\"inclusive\"></a><h3 id=\"inculisve\">Inclusive</h3>\n");
|
2740 | |
printf(tableHeader, "inclusive_table");
|
2741 | |
}
|
2742 | |
|
2743 | |
qsort(diffs, matches, sizeof(DiffEntry), compareDiffEntriesInculsive);
|
2744 | |
ptr = diffs;
|
2745 | |
|
2746 | |
while (ptr->method1 != NULL && ptr->method2 != NULL) {
|
2747 | |
if (gOptions.outputHtml) printf("<tr><td>\n");
|
2748 | |
|
2749 | |
className = htmlEscape(ptr->method1->className, classBuf, HTML_BUFSIZE);
|
2750 | |
methodName = htmlEscape(ptr->method1->methodName, methodBuf, HTML_BUFSIZE);
|
2751 | |
|
2752 | |
printf("%s.%s ", className, methodName);
|
2753 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2754 | |
|
2755 | |
printf("%lld ", ptr->method1->elapsedInclusive);
|
2756 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2757 | |
|
2758 | |
printf("%llu ", ptr->method2->elapsedInclusive);
|
2759 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2760 | |
|
2761 | |
printf("%lld ", ptr->differenceInclusive);
|
2762 | |
if (gOptions.outputHtml) printf("</td><td>");
|
2763 | |
|
2764 | |
printf("%.2f\n", ptr->differenceInclusivePercentage);
|
2765 | |
if (gOptions.outputHtml) printf("</td><td>\n");
|
2766 | |
|
2767 | |
printf("%d\n", ptr->method1->numCalls[0]);
|
2768 | |
if (gOptions.outputHtml) printf("</td><td>\n");
|
2769 | |
|
2770 | |
printf("%d\n", ptr->method2->numCalls[0]);
|
2771 | |
if (gOptions.outputHtml) printf("</td></tr>\n");
|
2772 | |
|
2773 | |
ptr++;
|
2774 | |
}
|
2775 | |
|
2776 | |
if (gOptions.outputHtml) {
|
2777 | |
printf("</table>\n");
|
2778 | |
printf("<h3>Run 1 methods not found in Run 2</h3>");
|
2779 | |
printf(tableHeaderMissing, "?");
|
2780 | |
}
|
2781 | |
|
2782 | |
for (i = 0; i < d1->numMethods; ++i) {
|
2783 | |
if (methods1[i] != NULL) {
|
2784 | |
printMissingMethod(methods1[i]);
|
2785 | |
}
|
2786 | |
}
|
2787 | |
|
2788 | |
if (gOptions.outputHtml) {
|
2789 | |
printf("</table>\n");
|
2790 | |
printf("<h3>Run 2 methods not found in Run 1</h3>");
|
2791 | |
printf(tableHeaderMissing, "?");
|
2792 | |
}
|
2793 | |
|
2794 | |
for (i = 0; i < d2->numMethods; ++i) {
|
2795 | |
if (methods2[i] != NULL) {
|
2796 | |
printMissingMethod(methods2[i]);
|
2797 | |
}
|
2798 | |
}
|
2799 | |
|
2800 | |
if (gOptions.outputHtml) printf("</body></html\n");
|
2801 | |
}
|
2802 | |
|
2803 | |
int usage(const char *program)
|
2804 | |
{
|
2805 | |
fprintf(stderr, "Copyright (C) 2006 The Android Open Source Project\n\n");
|
2806 | |
fprintf(stderr, "usage: %s [-ho] [-s sortable] [-d trace-file-name] [-g outfile] trace-file-name\n", program);
|
2807 | |
fprintf(stderr, " -d trace-file-name - Diff with this trace\n");
|
2808 | |
fprintf(stderr, " -g outfile - Write graph to 'outfile'\n");
|
2809 | |
fprintf(stderr, " -k - When writing a graph, keep the intermediate DOT file\n");
|
2810 | |
fprintf(stderr, " -h - Turn on HTML output\n");
|
2811 | |
fprintf(stderr, " -o - Dump the dmtrace file instead of profiling\n");
|
2812 | |
fprintf(stderr, " -s - URL base to where the sortable javascript file\n");
|
2813 | |
fprintf(stderr, " -t threshold - Threshold percentage for including nodes in the graph\n");
|
2814 | |
return 2;
|
2815 | |
}
|
2816 | |
|
2817 | |
// Returns true if there was an error
|
2818 | |
int parseOptions(int argc, char **argv)
|
2819 | |
{
|
2820 | |
while (1) {
|
2821 | |
int opt = getopt(argc, argv, "d:hg:kos:t:");
|
2822 | |
if (opt == -1)
|
2823 | |
break;
|
2824 | |
switch (opt) {
|
2825 | |
case 'd':
|
2826 | |
gOptions.diffFileName = optarg;
|
2827 | |
break;
|
2828 | |
case 'g':
|
2829 | |
gOptions.graphFileName = optarg;
|
2830 | |
break;
|
2831 | |
case 'k':
|
2832 | |
gOptions.keepDotFile = 1;
|
2833 | |
break;
|
2834 | |
case 'h':
|
2835 | |
gOptions.outputHtml = 1;
|
2836 | |
break;
|
2837 | |
case 'o':
|
2838 | |
gOptions.dump = 1;
|
2839 | |
break;
|
2840 | |
case 's':
|
2841 | |
gOptions.sortableUrl = optarg;
|
2842 | |
break;
|
2843 | |
case 't':
|
2844 | |
gOptions.threshold = atoi(optarg);
|
2845 | |
break;
|
2846 | |
default:
|
2847 | |
return 1;
|
2848 | |
}
|
2849 | |
}
|
2850 | |
return 0;
|
2851 | |
}
|
2852 | |
|
2853 | |
/*
|
2854 | |
* Parse args.
|
2855 | |
*/
|
2856 | |
int main(int argc, char** argv)
|
2857 | |
{
|
2858 | |
gOptions.threshold = -1;
|
2859 | |
|
2860 | |
// Parse the options
|
2861 | |
if (parseOptions(argc, argv) || argc - optind != 1)
|
2862 | |
return usage(argv[0]);
|
2863 | |
|
2864 | |
gOptions.traceFileName = argv[optind];
|
2865 | |
|
2866 | |
if (gOptions.threshold < 0 || 100 <= gOptions.threshold) {
|
2867 | |
gOptions.threshold = 20;
|
2868 | |
}
|
2869 | |
|
2870 | |
if (gOptions.dump) {
|
2871 | |
dumpTrace();
|
2872 | |
return 0;
|
2873 | |
}
|
2874 | |
|
2875 | |
uint64_t sumThreadTime = 0;
|
2876 | |
|
2877 | |
TraceData data1;
|
2878 | |
DataKeys* dataKeys = parseDataKeys(&data1, gOptions.traceFileName,
|
2879 | |
&sumThreadTime);
|
2880 | |
if (dataKeys == NULL) {
|
2881 | |
fprintf(stderr, "Cannot read \"%s\".\n", gOptions.traceFileName);
|
2882 | |
exit(1);
|
2883 | |
}
|
2884 | |
|
2885 | |
if (gOptions.diffFileName != NULL) {
|
2886 | |
uint64_t sum2;
|
2887 | |
TraceData data2;
|
2888 | |
DataKeys* d2 = parseDataKeys(&data2, gOptions.diffFileName, &sum2);
|
2889 | |
if (d2 == NULL) {
|
2890 | |
fprintf(stderr, "Cannot read \"%s\".\n", gOptions.diffFileName);
|
2891 | |
exit(1);
|
2892 | |
}
|
2893 | |
|
2894 | |
createDiff(d2, sum2, dataKeys, sumThreadTime);
|
2895 | |
|
2896 | |
freeDataKeys(d2);
|
2897 | |
} else {
|
2898 | |
MethodEntry** methods = parseMethodEntries(dataKeys);
|
2899 | |
profileTrace(&data1, methods, dataKeys->numMethods, sumThreadTime);
|
2900 | |
if (gOptions.graphFileName != NULL) {
|
2901 | |
createInclusiveProfileGraphNew(dataKeys);
|
2902 | |
}
|
2903 | |
free(methods);
|
2904 | |
}
|
2905 | |
|
2906 | |
freeDataKeys(dataKeys);
|
2907 | |
|
2908 | |
return 0;
|
2909 | |
}
|