1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=78:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is SpiderMonkey JavaScript engine.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * the Mozilla Foundation.
21 : * Portions created by the Initial Developer are Copyright (C) 2011
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef jsgc_statistics_h___
41 : #define jsgc_statistics_h___
42 :
43 : #include <string.h>
44 :
45 : #include "jsfriendapi.h"
46 : #include "jspubtd.h"
47 : #include "jsutil.h"
48 :
49 : struct JSCompartment;
50 :
51 : namespace js {
52 : namespace gcstats {
53 :
54 : enum Phase {
55 : PHASE_MARK,
56 : PHASE_MARK_ROOTS,
57 : PHASE_MARK_DELAYED,
58 : PHASE_MARK_OTHER,
59 : PHASE_SWEEP,
60 : PHASE_SWEEP_OBJECT,
61 : PHASE_SWEEP_STRING,
62 : PHASE_SWEEP_SCRIPT,
63 : PHASE_SWEEP_SHAPE,
64 : PHASE_DISCARD_CODE,
65 : PHASE_DISCARD_ANALYSIS,
66 : PHASE_XPCONNECT,
67 : PHASE_DESTROY,
68 :
69 : PHASE_LIMIT
70 : };
71 :
72 : enum Stat {
73 : STAT_NEW_CHUNK,
74 : STAT_DESTROY_CHUNK,
75 :
76 : STAT_LIMIT
77 : };
78 :
79 : static const size_t BUFFER_SIZE = 8192;
80 :
81 : struct Statistics {
82 : Statistics(JSRuntime *rt);
83 : ~Statistics();
84 :
85 : void beginPhase(Phase phase);
86 : void endPhase(Phase phase);
87 :
88 : void beginSlice(JSCompartment *comp, gcreason::Reason reason);
89 : void endSlice();
90 :
91 0 : void reset(const char *reason) { slices.back().resetReason = reason; }
92 51092 : void nonincremental(const char *reason) { nonincrementalReason = reason; }
93 :
94 87236 : void count(Stat s) {
95 87236 : JS_ASSERT(s < STAT_LIMIT);
96 87236 : counts[s]++;
97 87236 : }
98 :
99 : private:
100 : JSRuntime *runtime;
101 :
102 : int64_t startupTime;
103 :
104 : FILE *fp;
105 : bool fullFormat;
106 :
107 : JSCompartment *compartment;
108 : const char *nonincrementalReason;
109 :
110 51091 : struct SliceData {
111 51092 : SliceData(gcreason::Reason reason, int64_t start)
112 51092 : : reason(reason), resetReason(NULL), start(start)
113 : {
114 51092 : PodArrayZero(phaseTimes);
115 51092 : }
116 :
117 : gcreason::Reason reason;
118 : const char *resetReason;
119 : int64_t start, end;
120 : int64_t phaseTimes[PHASE_LIMIT];
121 :
122 13338 : int64_t duration() const { return end - start; }
123 : };
124 :
125 : Vector<SliceData, 8, SystemAllocPolicy> slices;
126 :
127 : /* Most recent time when the given phase started. */
128 : int64_t phaseStarts[PHASE_LIMIT];
129 :
130 : /* Total time in a given phase for this GC. */
131 : int64_t phaseTimes[PHASE_LIMIT];
132 :
133 : /* Total time in a given phase over all GCs. */
134 : int64_t phaseTotals[PHASE_LIMIT];
135 :
136 : /* Number of events of this type for this GC. */
137 : unsigned int counts[STAT_LIMIT];
138 :
139 : char buffer[BUFFER_SIZE];
140 : bool needComma;
141 :
142 : void beginGC();
143 : void endGC();
144 :
145 : int64_t gcDuration();
146 : double t(int64_t t);
147 : void printStats();
148 : void fmt(const char *f, ...);
149 : void fmtIfNonzero(const char *name, double t);
150 : void formatPhases(int64_t *times);
151 : const char *formatData();
152 :
153 : double computeMMU(int64_t resolution);
154 : };
155 :
156 : struct AutoGCSlice {
157 51092 : AutoGCSlice(Statistics &stats, JSCompartment *comp, gcreason::Reason reason
158 : JS_GUARD_OBJECT_NOTIFIER_PARAM)
159 51092 : : stats(stats) { JS_GUARD_OBJECT_NOTIFIER_INIT; stats.beginSlice(comp, reason); }
160 51092 : ~AutoGCSlice() { stats.endSlice(); }
161 :
162 : Statistics &stats;
163 : JS_DECL_USE_GUARD_OBJECT_NOTIFIER
164 : };
165 :
166 : struct AutoPhase {
167 858024 : AutoPhase(Statistics &stats, Phase phase JS_GUARD_OBJECT_NOTIFIER_PARAM)
168 858024 : : stats(stats), phase(phase) { JS_GUARD_OBJECT_NOTIFIER_INIT; stats.beginPhase(phase); }
169 858024 : ~AutoPhase() { stats.endPhase(phase); }
170 :
171 : Statistics &stats;
172 : Phase phase;
173 : JS_DECL_USE_GUARD_OBJECT_NOTIFIER
174 : };
175 :
176 : } /* namespace gcstats */
177 : } /* namespace js */
178 :
179 : #endif /* jsgc_statistics_h___ */
|