2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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 Computer, 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.
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.
29 #ifndef SamplingCounter_h
30 #define SamplingCounter_h
33 #include <wtf/Assertions.h>
37 // AbstractSamplingCounter:
39 // Implements a named set of counters, printed on exit if ENABLE(SAMPLING_COUNTERS).
40 // See subclasses below, SamplingCounter, GlobalSamplingCounter and DeletableSamplingCounter.
41 class AbstractSamplingCounter
{
42 friend class DeletableSamplingCounter
;
44 void count(uint32_t count
= 1)
49 JS_EXPORT_PRIVATE
static void dump();
51 int64_t* addressOfCounter() { return &m_counter
; }
54 // Effectively the contructor, however called lazily in the case of GlobalSamplingCounter.
55 void init(const char* name
)
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
;
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
;
81 #if ENABLE(SAMPLING_COUNTERS)
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).
91 // void someFunction()
93 // static SamplingCounter countMe("This is my counter. There are many like it, but this one is mine.");
98 class SamplingCounter
: public AbstractSamplingCounter
{
100 SamplingCounter(const char* name
) { init(name
); }
103 // GlobalSamplingCounter:
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).
110 // GlobalSamplingCounter globalCounter;
112 // void firstFunction()
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();
121 // void secondFunction()
123 // globalCounter.count();
127 class GlobalSamplingCounter
: public AbstractSamplingCounter
{
129 void name(const char* name
)
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'.
138 // DeletableSamplingCounter:
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
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:
151 // DeletableSamplingCounter* counter = new DeletableSamplingCounter("The Counter With No Name");
155 class DeletableSamplingCounter
: public AbstractSamplingCounter
{
157 DeletableSamplingCounter(const char* name
) { init(name
); }
159 ~DeletableSamplingCounter()
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
;
176 #endif // SamplingCounter_h