This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspacetrace.c
6353 lines (5703 loc) · 207 KB
/
spacetrace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
** spacetrace.c
**
** SpaceTrace is meant to take the output of trace-malloc and present
** a picture of allocations over the run of the application.
*/
/*
** Required include files.
*/
#include "spacetrace.h"
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <time.h>
#if defined(XP_WIN32)
#include <malloc.h> /* _heapMin */
#endif
#if defined(HAVE_BOUTELL_GD)
/*
** See http://www.boutell.com/gd for the GD graphics library.
** Ports for many platorms exist.
** Your box may already have the lib (mine did, redhat 7.1 workstation).
*/
#include <gd.h>
#include <gdfontt.h>
#include <gdfonts.h>
#include <gdfontmb.h>
#endif /* HAVE_BOUTELL_GD */
#include "nsQuickSort.h"
#include "prlong.h"
/*
** strcasecmp API please.
*/
#if defined(_MSC_VER)
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
/*
** the globals variables. happy joy.
*/
STGlobals globals;
/*
** have the heap cleanup at opportune times, if possible.
*/
void
heapCompact(void)
{
#if defined(XP_WIN32)
_heapmin();
#endif
}
#define ST_CMD_OPTION_BOOL(option_name, option_genre, option_help) \
PR_fprintf(PR_STDOUT, "--%s\nDisabled by default.\n%s\n", #option_name, option_help);
#define ST_CMD_OPTION_STRING(option_name, option_genre, default_value, option_help) \
PR_fprintf(PR_STDOUT, "--%s=<value>\nDefault value is \"%s\".\n%s\n", #option_name, default_value, option_help);
#define ST_CMD_OPTION_STRING_ARRAY(option_name, option_genre, array_size, option_help) \
PR_fprintf(PR_STDOUT, "--%s=<value>\nUp to %u occurrences allowed.\n%s\n", #option_name, array_size, option_help);
#define ST_CMD_OPTION_STRING_PTR_ARRAY(option_name, option_genre, option_help) \
PR_fprintf(PR_STDOUT, "--%s=<value>\nUnlimited occurrences allowed.\n%s\n", #option_name, option_help);
#define ST_CMD_OPTION_UINT32(option_name, option_genre, default_value, multiplier, option_help) \
PR_fprintf(PR_STDOUT, "--%s=<value>\nDefault value is %u.\n%s\n", #option_name, default_value, option_help);
#define ST_CMD_OPTION_UINT64(option_name, option_genre, default_value, multiplier, option_help) \
PR_fprintf(PR_STDOUT, "--%s=<value>\nDefault value is %llu.\n%s\n", #option_name, default_value, option_help);
/*
** showHelp
**
** Give simple command line help.
** Returns !0 if the help was showed.
*/
int
showHelp(void)
{
int retval = 0;
if (PR_FALSE != globals.mCommandLineOptions.mHelp) {
PR_fprintf(PR_STDOUT, "Usage:\t%s [OPTION]... [-|filename]\n\n",
globals.mProgramName);
#include "stoptions.h"
/*
** Showed something.
*/
retval = __LINE__;
}
return retval;
}
/*
** ticks2xsec
**
** Convert platform specific ticks to second units
** Returns 0 on success.
*/
uint32_t
ticks2xsec(tmreader * aReader, uint32_t aTicks, uint32_t aResolution)
{
return (uint32_t)((aResolution * aTicks)/aReader->ticksPerSec);
}
#define ticks2msec(reader, ticks) ticks2xsec((reader), (ticks), 1000)
#define ticks2usec(reader, ticks) ticks2xsec((reader), (ticks), 1000000)
/*
** initOptions
**
** Determine global settings for the application.
** Returns 0 on success.
*/
int
initOptions(int aArgCount, char **aArgArray)
{
int retval = 0;
int traverse = 0;
/*
** Set the initial global default options.
*/
#define ST_CMD_OPTION_BOOL(option_name, option_genre, option_help) globals.mCommandLineOptions.m##option_name = PR_FALSE;
#define ST_CMD_OPTION_STRING(option_name, option_genre, default_value, option_help) PR_snprintf(globals.mCommandLineOptions.m##option_name, sizeof(globals.mCommandLineOptions.m##option_name), "%s", default_value);
#define ST_CMD_OPTION_STRING_ARRAY(option_name, option_genre, array_size, option_help) { int loop; for(loop = 0; loop < array_size; loop++) { globals.mCommandLineOptions.m##option_name[loop][0] = '\0'; } }
#define ST_CMD_OPTION_STRING_PTR_ARRAY(option_name, option_genre, option_help) globals.mCommandLineOptions.m##option_name = NULL; globals.mCommandLineOptions.m##option_name##Count = 0;
#define ST_CMD_OPTION_UINT32(option_name, option_genre, default_value, multiplier, option_help) globals.mCommandLineOptions.m##option_name = default_value * multiplier;
#define ST_CMD_OPTION_UINT64(option_name, option_genre, default_value, multiplier, option_help) { uint64_t def64 = default_value; uint64_t mul64 = multiplier; globals.mCommandLineOptions.m##option_name##64 = def64 * mul64; }
#include "stoptions.h"
/*
** Go through all arguments.
** Two dashes lead off an option.
** Any single dash leads off help, unless it is a lone dash (stdin).
** Anything else will be attempted as a file to be processed.
*/
for (traverse = 1; traverse < aArgCount; traverse++) {
if ('-' == aArgArray[traverse][0] && '-' == aArgArray[traverse][1]) {
const char *option = &aArgArray[traverse][2];
/*
** Initial if(0) needed to make "else if"s valid.
*/
if (0) {
}
#define ST_CMD_OPTION_BOOL(option_name, option_genre, option_help) \
else if(0 == strcasecmp(option, #option_name)) \
{ \
globals.mCommandLineOptions.m##option_name = PR_TRUE; \
}
#define ST_CMD_OPTION_STRING(option_name, option_genre, default_value, option_help) \
else if(0 == strncasecmp(option, #option_name "=", strlen(#option_name "="))) \
{ \
PR_snprintf(globals.mCommandLineOptions.m##option_name, sizeof(globals.mCommandLineOptions.m##option_name), "%s", option + strlen(#option_name "=")); \
}
#define ST_CMD_OPTION_STRING_ARRAY(option_name, option_genre, array_size, option_help) \
else if(0 == strncasecmp(option, #option_name "=", strlen(#option_name "="))) \
{ \
int arrLoop = 0; \
\
for(arrLoop = 0; arrLoop < array_size; arrLoop++) \
{ \
if('\0' == globals.mCommandLineOptions.m##option_name[arrLoop][0]) \
{ \
break; \
} \
}\
\
if(arrLoop != array_size) \
{ \
PR_snprintf(globals.mCommandLineOptions.m##option_name[arrLoop], sizeof(globals.mCommandLineOptions.m##option_name[arrLoop]), "%s", option + strlen(#option_name "=")); \
} \
else \
{ \
REPORT_ERROR_MSG(__LINE__, option); \
retval = __LINE__; \
globals.mCommandLineOptions.mHelp = PR_TRUE; \
} \
}
#define ST_CMD_OPTION_STRING_PTR_ARRAY(option_name, option_genre, option_help) \
else if(0 == strncasecmp(option, #option_name "=", strlen(#option_name "="))) \
{ \
const char** expand = NULL; \
\
expand = (const char**)realloc((void*)globals.mCommandLineOptions.m##option_name, sizeof(const char*) * (globals.mCommandLineOptions.m##option_name##Count + 1)); \
if(NULL != expand) \
{ \
globals.mCommandLineOptions.m##option_name = expand; \
globals.mCommandLineOptions.m##option_name[globals.mCommandLineOptions.m##option_name##Count] = option + strlen(#option_name "="); \
globals.mCommandLineOptions.m##option_name##Count++; \
} \
else \
{ \
retval = __LINE__; \
globals.mCommandLineOptions.mHelp = PR_TRUE; \
} \
}
#define ST_CMD_OPTION_UINT32(option_name, option_genre, default_value, multiplier, option_help) \
else if(0 == strncasecmp(option, #option_name "=", strlen(#option_name "="))) \
{ \
int32_t scanRes = 0; \
\
scanRes = PR_sscanf(option + strlen(#option_name "="), "%u", &globals.mCommandLineOptions.m##option_name); \
if(1 != scanRes) \
{ \
REPORT_ERROR_MSG(__LINE__, option); \
retval = __LINE__; \
globals.mCommandLineOptions.mHelp = PR_TRUE; \
} \
}
#define ST_CMD_OPTION_UINT64(option_name, option_genre, default_value, multiplier, option_help) \
else if(0 == strncasecmp(option, #option_name "=", strlen(#option_name "="))) \
{ \
int32_t scanRes = 0; \
\
scanRes = PR_sscanf(option + strlen(#option_name "="), "%llu", &globals.mCommandLineOptions.m##option_name##64); \
if(1 != scanRes) \
{ \
REPORT_ERROR_MSG(__LINE__, option); \
retval = __LINE__; \
globals.mCommandLineOptions.mHelp = PR_TRUE; \
} \
}
#include "stoptions.h"
/*
** If no match on options, this else will get hit.
*/
else {
REPORT_ERROR_MSG(__LINE__, option);
retval = __LINE__;
globals.mCommandLineOptions.mHelp = PR_TRUE;
}
}
else if ('-' == aArgArray[traverse][0]
&& '\0' != aArgArray[traverse][1]) {
/*
** Show help, bad/legacy option.
*/
REPORT_ERROR_MSG(__LINE__, aArgArray[traverse]);
retval = __LINE__;
globals.mCommandLineOptions.mHelp = PR_TRUE;
}
else {
/*
** Default is same as FileName option, the file to process.
*/
PR_snprintf(globals.mCommandLineOptions.mFileName,
sizeof(globals.mCommandLineOptions.mFileName), "%s",
aArgArray[traverse]);
}
}
/*
** initialize the categories
*/
initCategories(&globals);
return retval;
}
#if ST_WANT_GRAPHS
/*
** createGraph
**
** Create a GD image with the common properties of a graph.
** Upon return, you normally allocate legend colors,
** draw your graph inside the region
** STGD_MARGIN,STGD_MARGIN,STGD_WIDTH-STGD_MARGIN,STGD_HEIGH-STGD_MARGIN,
** and then call drawGraph to format the surrounding information.
**
** You should use the normal GD image release function, gdImageDestroy
** when done with it.
**
** Image attributes:
** STGD_WIDTHxSTGD_HEIGHT
** trasparent (white) background
** incremental display
*/
gdImagePtr
createGraph(int *aTransparencyColor)
{
gdImagePtr retval = NULL;
if (NULL != aTransparencyColor) {
*aTransparencyColor = -1;
retval = gdImageCreate(STGD_WIDTH, STGD_HEIGHT);
if (NULL != retval) {
/*
** Background color (first one).
*/
*aTransparencyColor = gdImageColorAllocate(retval, 255, 255, 255);
if (-1 != *aTransparencyColor) {
/*
** As transparency.
*/
gdImageColorTransparent(retval, *aTransparencyColor);
}
/*
** And to set interlacing.
*/
gdImageInterlace(retval, 1);
}
else {
REPORT_ERROR(__LINE__, gdImageCreate);
}
}
else {
REPORT_ERROR(__LINE__, createGraph);
}
return retval;
}
#endif /* ST_WANT_GRAPHS */
#if ST_WANT_GRAPHS
/*
** drawGraph
**
** This function mainly exists to simplify putitng all the pretty lace
** around a home made graph.
*/
void
drawGraph(gdImagePtr aImage, int aColor,
const char *aGraphTitle,
const char *aXAxisTitle,
const char *aYAxisTitle,
uint32_t aXMarkCount,
uint32_t * aXMarkPercents,
const char **aXMarkTexts,
uint32_t aYMarkCount,
uint32_t * aYMarkPercents,
const char **aYMarkTexts,
uint32_t aLegendCount,
int *aLegendColors, const char **aLegendTexts)
{
if (NULL != aImage && NULL != aGraphTitle &&
NULL != aXAxisTitle && NULL != aYAxisTitle &&
(0 == aXMarkCount || (NULL != aXMarkPercents && NULL != aXMarkTexts))
&& (0 == aYMarkCount
|| (NULL != aYMarkPercents && NULL != aYMarkTexts))
&& (0 == aLegendCount
|| (NULL != aLegendColors && NULL != aLegendTexts))) {
int margin = 1;
uint32_t traverse = 0;
uint32_t target = 0;
const int markSize = 2;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
time_t theTimeT = time(NULL);
char *theTime = ctime(&theTimeT);
const char *logo = "SpaceTrace";
gdFontPtr titleFont = gdFontMediumBold;
gdFontPtr markFont = gdFontTiny;
gdFontPtr dateFont = gdFontTiny;
gdFontPtr axisFont = gdFontSmall;
gdFontPtr legendFont = gdFontTiny;
gdFontPtr logoFont = gdFontTiny;
/*
** Fixup the color.
** Black by default.
*/
if (-1 == aColor) {
aColor = gdImageColorAllocate(aImage, 0, 0, 0);
}
if (-1 == aColor) {
aColor = gdImageColorClosest(aImage, 0, 0, 0);
}
/*
** Output the box.
*/
x1 = STGD_MARGIN - margin;
y1 = STGD_MARGIN - margin;
x2 = STGD_WIDTH - x1;
y2 = STGD_HEIGHT - y1;
gdImageRectangle(aImage, x1, y1, x2, y2, aColor);
margin++;
/*
** Need to make small markings on the graph to indicate where the
** labels line up exactly.
** While we're at it, draw the label text.
*/
for (traverse = 0; traverse < aXMarkCount; traverse++) {
target =
((STGD_WIDTH -
(STGD_MARGIN * 2)) * aXMarkPercents[traverse]) / 100;
x1 = STGD_MARGIN + target;
y1 = STGD_MARGIN - margin;
x2 = x1;
y2 = y1 - markSize;
gdImageLine(aImage, x1, y1, x2, y2, aColor);
y1 = STGD_HEIGHT - y1;
y2 = STGD_HEIGHT - y2;
gdImageLine(aImage, x1, y1, x2, y2, aColor);
if (NULL != aXMarkTexts[traverse]) {
x1 = STGD_MARGIN + target - (markFont->h / 2);
y1 = STGD_HEIGHT - STGD_MARGIN + margin + markSize +
(strlen(aXMarkTexts[traverse]) * markFont->w);
gdImageStringUp(aImage, markFont, x1, y1,
(unsigned char *) aXMarkTexts[traverse],
aColor);
}
}
for (traverse = 0; traverse < aYMarkCount; traverse++) {
target =
((STGD_HEIGHT - (STGD_MARGIN * 2)) * (100 -
aYMarkPercents
[traverse])) / 100;
x1 = STGD_MARGIN - margin;
y1 = STGD_MARGIN + target;
x2 = x1 - markSize;
y2 = y1;
gdImageLine(aImage, x1, y1, x2, y2, aColor);
x1 = STGD_WIDTH - x1;
x2 = STGD_WIDTH - x2;
gdImageLine(aImage, x1, y1, x2, y2, aColor);
if (NULL != aYMarkTexts[traverse]) {
x1 = STGD_MARGIN - margin - markSize -
(strlen(aYMarkTexts[traverse]) * markFont->w);
y1 = STGD_MARGIN + target - (markFont->h / 2);
gdImageString(aImage, markFont, x1, y1,
(unsigned char *) aYMarkTexts[traverse],
aColor);
}
}
margin += markSize;
/*
** Title will be centered above the image.
*/
x1 = (STGD_WIDTH / 2) - ((strlen(aGraphTitle) * titleFont->w) / 2);
y1 = ((STGD_MARGIN - margin) / 2) - (titleFont->h / 2);
gdImageString(aImage, titleFont, x1, y1,
(unsigned char *) aGraphTitle, aColor);
/*
** Upper left will be the date.
*/
x1 = 0;
y1 = 0;
traverse = strlen(theTime) - 1;
if (isspace(theTime[traverse])) {
theTime[traverse] = '\0';
}
gdImageString(aImage, dateFont, x1, y1, (unsigned char *) theTime,
aColor);
/*
** Lower right will be the logo.
*/
x1 = STGD_WIDTH - (strlen(logo) * logoFont->w);
y1 = STGD_HEIGHT - logoFont->h;
gdImageString(aImage, logoFont, x1, y1, (unsigned char *) logo,
aColor);
/*
** X and Y axis titles
*/
x1 = (STGD_WIDTH / 2) - ((strlen(aXAxisTitle) * axisFont->w) / 2);
y1 = STGD_HEIGHT - axisFont->h;
gdImageString(aImage, axisFont, x1, y1, (unsigned char *) aXAxisTitle,
aColor);
x1 = 0;
y1 = (STGD_HEIGHT / 2) + ((strlen(aYAxisTitle) * axisFont->w) / 2);
gdImageStringUp(aImage, axisFont, x1, y1,
(unsigned char *) aYAxisTitle, aColor);
/*
** The legend.
** Centered on the right hand side, going up.
*/
x1 = STGD_WIDTH - STGD_MARGIN + margin +
(aLegendCount * legendFont->h) / 2;
x2 = STGD_WIDTH - (aLegendCount * legendFont->h);
if (x1 > x2) {
x1 = x2;
}
y1 = 0;
for (traverse = 0; traverse < aLegendCount; traverse++) {
y2 = (STGD_HEIGHT / 2) +
((strlen(aLegendTexts[traverse]) * legendFont->w) / 2);
if (y2 > y1) {
y1 = y2;
}
}
for (traverse = 0; traverse < aLegendCount; traverse++) {
gdImageStringUp(aImage, legendFont, x1, y1,
(unsigned char *) aLegendTexts[traverse],
aLegendColors[traverse]);
x1 += legendFont->h;
}
}
}
#endif /* ST_WANT_GRAPHS */
#if defined(HAVE_BOUTELL_GD)
/*
** pngSink
**
** GD callback, used to write out the png.
*/
int
pngSink(void *aContext, const char *aBuffer, int aLen)
{
return PR_Write((PRFileDesc *) aContext, aBuffer, aLen);
}
#endif /* HAVE_BOUTELL_GD */
/*
** FormatNumber
**
** Formats a number with thousands separator. Don't free the result. Returns
** static data.
*/
char *
FormatNumber(int32_t num)
{
static char buf[64];
char tmpbuf[64];
int len = 0;
int bufindex = sizeof(buf) - 1;
int mod3;
PR_snprintf(tmpbuf, sizeof(tmpbuf), "%d", num);
/* now insert the thousands separator */
mod3 = 0;
len = strlen(tmpbuf);
while (len >= 0) {
if (tmpbuf[len] >= '0' && tmpbuf[len] <= '9') {
if (mod3 == 3) {
buf[bufindex--] = ',';
mod3 = 0;
}
mod3++;
}
buf[bufindex--] = tmpbuf[len--];
}
return buf + bufindex + 1;
}
/*
** actualByteSize
**
** Apply alignment and overhead to size to figure out actual byte size
*/
uint32_t
actualByteSize(STOptions * inOptions, uint32_t retval)
{
/*
** Need to bump the result by our alignment and overhead.
** The idea here is that an allocation actually costs you more than you
** thought.
**
** The msvcrt malloc has an alignment of 16 with an overhead of 8.
** The win32 HeapAlloc has an alignment of 8 with an overhead of 8.
*/
if (0 != retval) {
uint32_t eval = 0;
uint32_t over = 0;
eval = retval - 1;
if (0 != inOptions->mAlignBy) {
over = eval % inOptions->mAlignBy;
}
retval = eval + inOptions->mOverhead + inOptions->mAlignBy - over;
}
return retval;
}
/*
** byteSize
**
** Figuring the byte size of an allocation.
** Might expand in the future to report size at a given time.
** For now, just use last relevant event.
*/
uint32_t
byteSize(STOptions * inOptions, STAllocation * aAlloc)
{
uint32_t retval = 0;
if (NULL != aAlloc && 0 != aAlloc->mEventCount) {
uint32_t index = aAlloc->mEventCount;
/*
** Generally, the size is the last event's size.
*/
do {
index--;
retval = aAlloc->mEvents[index].mHeapSize;
}
while (0 == retval && 0 != index);
}
return actualByteSize(inOptions, retval);
}
/*
** recalculateAllocationCost
**
** Given an allocation, does a recalculation of Cost - weight, heapcount etc.
** and does the right thing to propagate the cost upwards.
*/
int
recalculateAllocationCost(STOptions * inOptions, STContext * inContext,
STRun * aRun, STAllocation * aAllocation,
PRBool updateParent)
{
/*
** Now, see if they desire a callsite update.
** As mentioned previously, we decide if the run desires us to
** manipulate the callsite data only if its stamp is set.
** We change all callsites and parent callsites to have that
** stamp as well, so as to mark them as being relevant to
** the current run in question.
*/
if (NULL != inContext && 0 != aRun->mStats[inContext->mIndex].mStamp) {
uint32_t timeval =
aAllocation->mMaxTimeval - aAllocation->mMinTimeval;
uint32_t size = byteSize(inOptions, aAllocation);
uint32_t heapCost = aAllocation->mHeapRuntimeCost;
uint64_t timeval64 = timeval;
uint64_t size64 = size;
uint64_t weight64 = timeval64 * size64;
/*
** First, update this run.
*/
aRun->mStats[inContext->mIndex].mCompositeCount++;
aRun->mStats[inContext->mIndex].mHeapRuntimeCost += heapCost;
aRun->mStats[inContext->mIndex].mSize += size;
aRun->mStats[inContext->mIndex].mTimeval64 += timeval64;
aRun->mStats[inContext->mIndex].mWeight64 += weight64;
/*
** Use the first event of the allocation to update the parent
** callsites.
** This has positive effect of not updating realloc callsites
** with the same data over and over again.
*/
if (updateParent && 0 < aAllocation->mEventCount) {
tmcallsite *callsite = aAllocation->mEvents[0].mCallsite;
STRun *callsiteRun = NULL;
/*
** Go up parents till we drop.
*/
while (NULL != callsite && NULL != callsite->method) {
callsiteRun = CALLSITE_RUN(callsite);
if (NULL != callsiteRun) {
/*
** Do we init it?
*/
if (callsiteRun->mStats[inContext->mIndex].mStamp !=
aRun->mStats[inContext->mIndex].mStamp) {
memset(&callsiteRun->mStats[inContext->mIndex], 0,
sizeof(STCallsiteStats));
callsiteRun->mStats[inContext->mIndex].mStamp =
aRun->mStats[inContext->mIndex].mStamp;
}
/*
** Add the values.
** Note that if the allocation was ever realloced,
** we are actually recording the final size.
** Also, the composite count does not include
** calls to realloc (or free for that matter),
** but rather is simply a count of actual heap
** allocation objects, from which someone will
** draw conclusions regarding number of malloc
** and free calls.
** It is possible to generate the exact number
** of calls to free/malloc/realloc should the
** absolute need arise to count them individually,
** but I fear it will take mucho memory and this
** is perhaps good enough for now.
*/
callsiteRun->mStats[inContext->mIndex].mCompositeCount++;
callsiteRun->mStats[inContext->mIndex].mHeapRuntimeCost +=
heapCost;
callsiteRun->mStats[inContext->mIndex].mSize += size;
callsiteRun->mStats[inContext->mIndex].mTimeval64 +=
timeval64;
callsiteRun->mStats[inContext->mIndex].mWeight64 +=
weight64;
}
callsite = callsite->parent;
}
}
}
return 0;
}
/*
** appendAllocation
**
** Given a run, append the allocation to it.
** No DUP checks are done.
** Also, we might want to update the parent callsites with stats.
** We decide to do this heavy duty work only if the run we are appending
** to has a non ZERO mStats[].mStamp, meaning that it is asking to track
** such information when it was created.
** Returns !0 on success.
*/
int
appendAllocation(STOptions * inOptions, STContext * inContext,
STRun * aRun, STAllocation * aAllocation)
{
int retval = 0;
if (NULL != aRun && NULL != aAllocation && NULL != inOptions) {
STAllocation **expand = NULL;
/*
** Expand the size of the array if needed.
*/
expand = (STAllocation **) realloc(aRun->mAllocations,
sizeof(STAllocation *) *
(aRun->mAllocationCount + 1));
if (NULL != expand) {
/*
** Reassign in case of pointer move.
*/
aRun->mAllocations = expand;
/*
** Stick the allocation in.
*/
aRun->mAllocations[aRun->mAllocationCount] = aAllocation;
/*
** If this is the global run, we need to let the allocation
** track the index back to us.
*/
if (&globals.mRun == aRun) {
aAllocation->mRunIndex = aRun->mAllocationCount;
}
/*
** Increase the count.
*/
aRun->mAllocationCount++;
/*
** We're good.
*/
retval = __LINE__;
/*
** update allocation cost
*/
recalculateAllocationCost(inOptions, inContext, aRun, aAllocation,
PR_TRUE);
}
else {
REPORT_ERROR(__LINE__, appendAllocation);
}
}
else {
REPORT_ERROR(__LINE__, appendAllocation);
}
return retval;
}
/*
** hasCallsiteMatch
**
** Determine if the callsite or the other callsites has the matching text.
**
** Returns 0 if there is no match.
*/
int
hasCallsiteMatch(tmcallsite * aCallsite, const char *aMatch, int aDirection)
{
int retval = 0;
if (NULL != aCallsite && NULL != aCallsite->method &&
NULL != aMatch && '\0' != *aMatch) {
const char *methodName = NULL;
do {
methodName = tmmethodnode_name(aCallsite->method);
if (NULL != methodName && NULL != strstr(methodName, aMatch)) {
/*
** Contains the text.
*/
retval = __LINE__;
break;
}
else {
switch (aDirection) {
case ST_FOLLOW_SIBLINGS:
aCallsite = aCallsite->siblings;
break;
case ST_FOLLOW_PARENTS:
aCallsite = aCallsite->parent;
break;
default:
aCallsite = NULL;
REPORT_ERROR(__LINE__, hasCallsiteMatch);
break;
}
}
}
while (NULL != aCallsite && NULL != aCallsite->method);
}
else {
REPORT_ERROR(__LINE__, hasCallsiteMatch);
}
return retval;
}
/*
** harvestRun
**
** Provide a simply way to go over a run, and yield the relevant allocations.
** The restrictions are easily set via the options page or the command
** line switches.
**
** On any match, add the allocation to the provided run.
**
** This makes it much easier for all the code to respect the options in
** force.
**
** Returns !0 on error, though aOutRun may contain a partial data set.
*/
int
harvestRun(const STRun * aInRun, STRun * aOutRun,
STOptions * aOptions, STContext * inContext)
{
int retval = 0;
#if defined(DEBUG_dp)
PRIntervalTime start = PR_IntervalNow();
fprintf(stderr, "DEBUG: harvesting run...\n");
#endif
if (NULL != aInRun && NULL != aOutRun && aInRun != aOutRun
&& NULL != aOptions && NULL != inContext) {
uint32_t traverse = 0;
STAllocation *current = NULL;
for (traverse = 0;
0 == retval && traverse < aInRun->mAllocationCount; traverse++) {
current = aInRun->mAllocations[traverse];
if (NULL != current) {
uint32_t lifetime = 0;
uint32_t bytesize = 0;
uint64_t weight64 = 0;
uint64_t bytesize64 = 0;
uint64_t lifetime64 = 0;
int appendRes = 0;
int looper = 0;
PRBool matched = PR_FALSE;
/*
** Use this as an opportune time to fixup a memory
** leaked timeval, so as to not completely skew
** the weights.
*/
if (ST_TIMEVAL_MAX == current->mMaxTimeval) {
current->mMaxTimeval = globals.mMaxTimeval;
}
/*
** Check allocation timeval restrictions.
** We have to slide the recorded timevals to be zero
** based, so that the comparisons make sense.
*/
if ((aOptions->mAllocationTimevalMin >
(current->mMinTimeval - globals.mMinTimeval)) ||
(aOptions->mAllocationTimevalMax <
(current->mMinTimeval - globals.mMinTimeval))) {
continue;
}
/*
** Check timeval restrictions.
** We have to slide the recorded timevals to be zero
** based, so that the comparisons make sense.
*/
if ((aOptions->mTimevalMin >
(current->mMinTimeval - globals.mMinTimeval)) ||
(aOptions->mTimevalMax <
(current->mMinTimeval - globals.mMinTimeval))) {
continue;
}
/*
** Check lifetime restrictions.
*/
lifetime = current->mMaxTimeval - current->mMinTimeval;
if ((lifetime < aOptions->mLifetimeMin) ||
(lifetime > aOptions->mLifetimeMax)) {
continue;
}
/*
** Check byte size restrictions.
*/
bytesize = byteSize(aOptions, current);
if ((bytesize < aOptions->mSizeMin) ||
(bytesize > aOptions->mSizeMax)) {
continue;
}
/*
** Check weight restrictions.
*/
weight64 = (uint64_t)(bytesize * lifetime);
if (weight64 < aOptions->mWeightMin64 ||
weight64 > aOptions->mWeightMax64) {
continue;
}
/*
** Possibly restrict the callsite by text.
** Do this last, as it is a heavier check.
**
** One day, we may need to expand the logic to check for
** events beyond the initial allocation event.
*/
for (looper = 0; ST_SUBSTRING_MATCH_MAX > looper; looper++) {
if ('\0' != aOptions->mRestrictText[looper][0]) {
if (0 ==
hasCallsiteMatch(current->mEvents[0].mCallsite,
aOptions->mRestrictText[looper],
ST_FOLLOW_PARENTS)) {
break;
}
}
else {
matched = PR_TRUE;
break;
}
}
if (ST_SUBSTRING_MATCH_MAX == looper) {
matched = PR_TRUE;
}
if (PR_FALSE == matched) {
continue;
}
/*
** You get here, we add to the run.
*/
appendRes =
appendAllocation(aOptions, inContext, aOutRun, current);
if (0 == appendRes) {
retval = __LINE__;
REPORT_ERROR(__LINE__, appendAllocation);
}
}
}
}
#if defined(DEBUG_dp)
fprintf(stderr, "DEBUG: harvesting ends: %dms [%d allocations]\n",
PR_IntervalToMilliseconds(PR_IntervalNow() - start),
aInRun->mAllocationCount);
#endif
return retval;