]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - runtime/SamplingCounter.h
JavaScriptCore-7600.1.4.11.8.tar.gz
[apple/javascriptcore.git] / runtime / SamplingCounter.h
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef SamplingCounter_h
30#define SamplingCounter_h
31
32#include <stdint.h>
33#include <wtf/Assertions.h>
34
35namespace JSC {
36
37// AbstractSamplingCounter:
38//
39// Implements a named set of counters, printed on exit if ENABLE(SAMPLING_COUNTERS).
40// See subclasses below, SamplingCounter, GlobalSamplingCounter and DeletableSamplingCounter.
41class AbstractSamplingCounter {
42 friend class DeletableSamplingCounter;
43public:
44 void count(uint32_t count = 1)
45 {
46 m_counter += count;
47 }
48
49 JS_EXPORT_PRIVATE static void dump();
50
51 int64_t* addressOfCounter() { return &m_counter; }
52
53protected:
54 // Effectively the contructor, however called lazily in the case of GlobalSamplingCounter.
55 void init(const char* name)
56 {
57 m_counter = 0;
58 m_name = name;
59
60 // Set m_next to point to the head of the chain, and inform whatever is
61 // currently at the head that this node will now hold the pointer to it.
62 m_next = s_abstractSamplingCounterChain;
63 s_abstractSamplingCounterChain->m_referer = &m_next;
64 // Add this node to the head of the list.
65 s_abstractSamplingCounterChain = this;
66 m_referer = &s_abstractSamplingCounterChain;
67 }
68
69 int64_t m_counter;
70 const char* m_name;
71 AbstractSamplingCounter* m_next;
72 // This is a pointer to the pointer to this node in the chain; used to
73 // allow fast linked list deletion.
74 AbstractSamplingCounter** m_referer;
75 // Null object used to detect end of static chain.
76 static AbstractSamplingCounter s_abstractSamplingCounterChainEnd;
77 JS_EXPORTDATA static AbstractSamplingCounter* s_abstractSamplingCounterChain;
78 static bool s_completed;
79};
80
81#if ENABLE(SAMPLING_COUNTERS)
82// SamplingCounter:
83//
84// This class is suitable and (hopefully!) convenient for cases where a counter is
85// required within the scope of a single function. It can be instantiated as a
86// static variable since it contains a constructor but not a destructor (static
87// variables in WebKit cannot have destructors).
88//
89// For example:
90//
91// void someFunction()
92// {
93// static SamplingCounter countMe("This is my counter. There are many like it, but this one is mine.");
94// countMe.count();
95// // ...
96// }
97//
98class SamplingCounter : public AbstractSamplingCounter {
99public:
100 SamplingCounter(const char* name) { init(name); }
101};
102
103// GlobalSamplingCounter:
104//
105// This class is suitable for use where a counter is to be declared globally,
106// since it contains neither a constructor nor destructor. Instead, ensure
107// that 'name()' is called to provide the counter with a name (and also to
108// allow it to be printed out on exit).
109//
110// GlobalSamplingCounter globalCounter;
111//
112// void firstFunction()
113// {
114// // Put this within a function that is definitely called!
115// // (Or alternatively alongside all calls to 'count()').
116// globalCounter.name("I Name You Destroyer.");
117// globalCounter.count();
118// // ...
119// }
120//
121// void secondFunction()
122// {
123// globalCounter.count();
124// // ...
125// }
126//
127class GlobalSamplingCounter : public AbstractSamplingCounter {
128public:
129 void name(const char* name)
130 {
131 // Global objects should be mapped in zero filled memory, so this should
132 // be a safe (albeit not necessarily threadsafe) check for 'first call'.
133 if (!m_next)
134 init(name);
135 }
136};
137
138// DeletableSamplingCounter:
139//
140// The above classes (SamplingCounter, GlobalSamplingCounter), are intended for
141// use within a global or static scope, and as such cannot have a destructor.
142// This means there is no convenient way for them to remove themselves from the
143// static list of counters, and should an instance of either class be freed
144// before 'dump()' has walked over the list it will potentially walk over an
145// invalid pointer.
146//
147// This class is intended for use where the counter may possibly be deleted before
148// the program exits. Should this occur, the counter will print it's value to
149// stderr, and remove itself from the static list. Example:
150//
151// DeletableSamplingCounter* counter = new DeletableSamplingCounter("The Counter With No Name");
152// counter->count();
153// delete counter;
154//
155class DeletableSamplingCounter : public AbstractSamplingCounter {
156public:
157 DeletableSamplingCounter(const char* name) { init(name); }
158
159 ~DeletableSamplingCounter()
160 {
161 if (!s_completed)
162 dataFile("DeletableSamplingCounter \"%s\" deleted early (with count %lld)\n", m_name, m_counter);
163 // Our m_referer pointer should know where the pointer to this node is,
164 // and m_next should know that this node is the previous node in the list.
165 ASSERT(*m_referer == this);
166 ASSERT(m_next->m_referer == &m_next);
167 // Remove this node from the list, and inform m_next that we have done so.
168 m_next->m_referer = m_referer;
169 *m_referer = m_next;
170 }
171};
172#endif
173
174} // namespace JSC
175
176#endif // SamplingCounter_h
177
178