blob: 62c48c7a49bdfb49350eefb636c3d44371324c9b [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
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.
15//
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.
27
28#ifndef V8_RUNTIME_PROFILER_H_
29#define V8_RUNTIME_PROFILER_H_
30
kasperl@chromium.orga5551262010-12-07 12:49:48 +000031#include "allocation.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000032#include "atomicops.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000033
34namespace v8 {
35namespace internal {
36
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000037class Isolate;
38class JSFunction;
39class Object;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000040class Semaphore;
41
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000042class RuntimeProfiler {
kasperl@chromium.orga5551262010-12-07 12:49:48 +000043 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000044 explicit RuntimeProfiler(Isolate* isolate);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000045
fschneider@chromium.org7d10be52012-04-10 12:30:14 +000046 static void GlobalSetUp();
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000047
48 static inline bool IsEnabled() {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000049 ASSERT(has_been_globally_set_up_);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000050 return enabled_;
51 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +000052
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000053 void OptimizeNow();
kasperl@chromium.orga5551262010-12-07 12:49:48 +000054
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000055 void SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056 void Reset();
57 void TearDown();
58
59 Object** SamplerWindowAddress();
60 int SamplerWindowSize();
61
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000062 void NotifyICChanged() { any_ic_changed_ = true; }
63
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000064 // Rate limiting support.
65
66 // VM thread interface.
67 //
68 // Called by isolates when their states change.
69 static inline void IsolateEnteredJS(Isolate* isolate);
70 static inline void IsolateExitedJS(Isolate* isolate);
71
72 // Profiler thread interface.
73 //
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 // WaitForSomeIsolateToEnterJS():
75 // When no isolates are running JavaScript code for some time the
76 // profiler thread suspends itself by calling the wait function. The
77 // wait function returns true after it waited or false immediately.
78 // While the function was waiting the profiler may have been
79 // disabled so it *must check* whether it is allowed to continue.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000080 static bool WaitForSomeIsolateToEnterJS();
81
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +000082 // Stops the runtime profiler thread when profiling support is being
83 // turned off.
84 static void StopRuntimeProfilerThreadBeforeShutdown(Thread* thread);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000085
86 void UpdateSamplesAfterScavenge();
87 void RemoveDeadSamples();
88 void UpdateSamplesAfterCompact(ObjectVisitor* visitor);
89
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +000090 void AttemptOnStackReplacement(JSFunction* function);
91
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092 private:
93 static const int kSamplerWindowSize = 16;
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000094
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095 static void HandleWakeUp(Isolate* isolate);
96
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000097 void Optimize(JSFunction* function, const char* reason);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000098
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000099 void ClearSampleBuffer();
100
101 void ClearSampleBufferNewSpaceEntries();
102
103 int LookupSample(JSFunction* function);
104
105 void AddSample(JSFunction* function, int weight);
106
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000107 Isolate* isolate_;
108
109 int sampler_threshold_;
110 int sampler_threshold_size_factor_;
111 int sampler_ticks_until_threshold_adjustment_;
112
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000113 Object* sampler_window_[kSamplerWindowSize];
114 int sampler_window_position_;
115 int sampler_window_weight_[kSamplerWindowSize];
116
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000117 bool any_ic_changed_;
118 bool code_generated_;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000119
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000120 // Possible state values:
121 // -1 => the profiler thread is waiting on the semaphore
122 // 0 or positive => the number of isolates running JavaScript code.
123 static Atomic32 state_;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000124
125#ifdef DEBUG
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000126 static bool has_been_globally_set_up_;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000127#endif
128 static bool enabled_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000129};
130
131
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132// Implementation of RuntimeProfiler inline functions.
133
134void RuntimeProfiler::IsolateEnteredJS(Isolate* isolate) {
135 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1);
136 if (new_state == 0) {
137 // Just incremented from -1 to 0. -1 can only be set by the
138 // profiler thread before it suspends itself and starts waiting on
139 // the semaphore.
140 HandleWakeUp(isolate);
141 }
142 ASSERT(new_state >= 0);
143}
144
145
146void RuntimeProfiler::IsolateExitedJS(Isolate* isolate) {
147 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, -1);
148 ASSERT(new_state >= 0);
149 USE(new_state);
150}
151
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000152} } // namespace v8::internal
153
154#endif // V8_RUNTIME_PROFILER_H_