1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef V8_RUNTIME_PROFILER_H_
29 #define V8_RUNTIME_PROFILER_H_
31 #include "allocation.h"
32 #include "atomicops.h"
42 class RuntimeProfiler {
44 explicit RuntimeProfiler(Isolate* isolate);
46 static void GlobalSetUp();
48 static inline bool IsEnabled() {
49 ASSERT(has_been_globally_set_up_);
61 Object** SamplerWindowAddress();
62 int SamplerWindowSize();
64 void NotifyICChanged() { any_ic_changed_ = true; }
66 // Rate limiting support.
68 // VM thread interface.
70 // Called by isolates when their states change.
71 static inline void IsolateEnteredJS(Isolate* isolate);
72 static inline void IsolateExitedJS(Isolate* isolate);
74 // Profiler thread interface.
76 // IsSomeIsolateInJS():
77 // The profiler thread can query whether some isolate is currently
78 // running JavaScript code.
80 // WaitForSomeIsolateToEnterJS():
81 // When no isolates are running JavaScript code for some time the
82 // profiler thread suspends itself by calling the wait function. The
83 // wait function returns true after it waited or false immediately.
84 // While the function was waiting the profiler may have been
85 // disabled so it *must check* whether it is allowed to continue.
86 static bool IsSomeIsolateInJS();
87 static bool WaitForSomeIsolateToEnterJS();
89 // Stops the runtime profiler thread when profiling support is being
91 static void StopRuntimeProfilerThreadBeforeShutdown(Thread* thread);
93 void UpdateSamplesAfterScavenge();
94 void RemoveDeadSamples();
95 void UpdateSamplesAfterCompact(ObjectVisitor* visitor);
97 void AttemptOnStackReplacement(JSFunction* function);
100 static const int kSamplerWindowSize = 16;
102 static void HandleWakeUp(Isolate* isolate);
104 void Optimize(JSFunction* function, const char* reason);
106 void ClearSampleBuffer();
108 void ClearSampleBufferNewSpaceEntries();
110 int LookupSample(JSFunction* function);
112 void AddSample(JSFunction* function, int weight);
116 int sampler_threshold_;
117 int sampler_threshold_size_factor_;
118 int sampler_ticks_until_threshold_adjustment_;
120 Object* sampler_window_[kSamplerWindowSize];
121 int sampler_window_position_;
122 int sampler_window_weight_[kSamplerWindowSize];
124 bool any_ic_changed_;
125 bool code_generated_;
127 // Possible state values:
128 // -1 => the profiler thread is waiting on the semaphore
129 // 0 or positive => the number of isolates running JavaScript code.
130 static Atomic32 state_;
133 static bool has_been_globally_set_up_;
135 static bool enabled_;
139 // Rate limiter intended to be used in the profiler thread.
140 class RuntimeProfilerRateLimiter BASE_EMBEDDED {
142 RuntimeProfilerRateLimiter() {}
144 // Suspends the current thread (which must be the profiler thread)
145 // when not executing JavaScript to minimize CPU usage. Returns
146 // whether the thread was suspended (and so must check whether
147 // profiling is still active.)
149 // Does nothing when runtime profiling is not enabled.
150 bool SuspendIfNecessary();
153 DISALLOW_COPY_AND_ASSIGN(RuntimeProfilerRateLimiter);
157 // Implementation of RuntimeProfiler inline functions.
159 void RuntimeProfiler::IsolateEnteredJS(Isolate* isolate) {
160 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1);
161 if (new_state == 0) {
162 // Just incremented from -1 to 0. -1 can only be set by the
163 // profiler thread before it suspends itself and starts waiting on
165 HandleWakeUp(isolate);
167 ASSERT(new_state >= 0);
171 void RuntimeProfiler::IsolateExitedJS(Isolate* isolate) {
172 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, -1);
173 ASSERT(new_state >= 0);
177 } } // namespace v8::internal
179 #endif // V8_RUNTIME_PROFILER_H_