1/*
2 * Copyright (C) 2012 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef StackStats_h
27#define StackStats_h
28
29#include "ExportMacros.h"
30#include <mutex>
31#include <wtf/Lock.h>
32
33
34// Define this flag to enable Stack stats collection. This feature is useful
35// for getting a sample of native stack usage sizes.
36//
37// Enabling this will cause stats to be collected and written to a log file at
38// various instrumented points in the code. It will result in noticeable
39// performance loss. Hence, this should only be enable when you want to do
40// some stats location in your local build. This code is provided here as a
41// convenience for collecting that data. It is not meant to be enabled by
42// default on release or debug builds.
43
44#define ENABLE_STACK_STATS 0
45
46
47namespace WTF {
48
49#if !ENABLE(STACK_STATS)
50
51class StackStats {
52public:
53 // The CheckPoint class is for marking check points corresponding
54 // each location in code where a stack recursion check is being done.
55
56 class CheckPoint {
57 public:
58 CheckPoint() { }
59 };
60
61 class PerThreadStats {
62 public:
63 PerThreadStats() { }
64 };
65
66 class LayoutCheckPoint {
67 public:
68 LayoutCheckPoint() { }
69 };
70
71 static void probe() { }
72};
73
74#else // ENABLE(STACK_STATS)
75
76class StackStats {
77public:
78 // The CheckPoint class is for marking check points corresponding
79 // each location in code where a stack recursion check is being done.
80
81 class CheckPoint {
82 public:
83 CheckPoint();
84 ~CheckPoint();
85 private:
86 CheckPoint* m_prev;
87 };
88
89 class PerThreadStats {
90 public:
91 PerThreadStats();
92
93 private:
94 int m_reentryDepth;
95 char* m_stackStart;
96 CheckPoint* m_currentCheckPoint;
97
98 friend class CheckPoint;
99 friend class StackStats;
100 };
101
102 class LayoutCheckPoint {
103 public:
104 WTF_EXPORT_PRIVATE LayoutCheckPoint();
105 WTF_EXPORT_PRIVATE ~LayoutCheckPoint();
106
107 private:
108 LayoutCheckPoint* m_prev;
109 int m_depth;
110 };
111
112 // Used for probing the stack at places where we suspect to be high
113 // points of stack usage but are NOT check points where stack recursion
114 // is checked.
115 //
116 // The more places where we add this probe, the more accurate our
117 // stats data will be. However, adding too many probes will also
118 // result in unnecessary performance loss. So, only add these probes
119 // judiciously where appropriate.
120 static void probe();
121
122private:
123 // CheckPoint management:
124 static StaticLock s_sharedMutex;
125 static CheckPoint* s_topCheckPoint;
126 static LayoutCheckPoint* s_firstLayoutCheckPoint;
127 static LayoutCheckPoint* s_topLayoutCheckPoint;
128
129 // High watermark stats:
130 static int s_maxCheckPointDiff;
131 static int s_maxStackHeight;
132 static int s_maxReentryDepth;
133
134 static int s_maxLayoutCheckPointDiff;
135 static int s_maxTotalLayoutCheckPointDiff;
136 static int s_maxLayoutReentryDepth;
137
138 friend class CheckPoint;
139 friend class LayoutCheckPoint;
140};
141
142#endif // ENABLE(STACK_STATS)
143
144} // namespace WTF
145
146using WTF::StackStats;
147
148#endif // StackStats_h
149