blob: c53ddd2b9714ba0ddb76479e41addda787177da9 [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 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
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#include "v8.h"
29
30#include "runtime-profiler.h"
31
32#include "assembler.h"
33#include "code-stubs.h"
34#include "compilation-cache.h"
35#include "deoptimizer.h"
36#include "execution.h"
37#include "global-handles.h"
38#include "scopeinfo.h"
39#include "top.h"
40
41namespace v8 {
42namespace internal {
43
44
45class PendingListNode : public Malloced {
46 public:
47 explicit PendingListNode(JSFunction* function);
48 ~PendingListNode() { Destroy(); }
49
50 PendingListNode* next() const { return next_; }
51 void set_next(PendingListNode* node) { next_ = node; }
52 Handle<JSFunction> function() { return Handle<JSFunction>::cast(function_); }
53
54 // If the function is garbage collected before we've had the chance
55 // to optimize it the weak handle will be null.
56 bool IsValid() { return !function_.is_null(); }
57
58 // Returns the number of microseconds this node has been pending.
59 int Delay() const { return static_cast<int>(OS::Ticks() - start_); }
60
61 private:
62 void Destroy();
63 static void WeakCallback(v8::Persistent<v8::Value> object, void* data);
64
65 PendingListNode* next_;
66 Handle<Object> function_; // Weak handle.
67 int64_t start_;
68};
69
70
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000071enum SamplerState {
72 IN_NON_JS_STATE = 0,
73 IN_JS_STATE = 1
74};
75
76
kasperl@chromium.orga5551262010-12-07 12:49:48 +000077// Optimization sampler constants.
78static const int kSamplerFrameCount = 2;
79static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
80static const int kSamplerWindowSize = 16;
81
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000082static const int kSamplerTicksBetweenThresholdAdjustment = 32;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000083
84static const int kSamplerThresholdInit = 3;
85static const int kSamplerThresholdMin = 1;
86static const int kSamplerThresholdDelta = 1;
87
88static const int kSamplerThresholdSizeFactorInit = 3;
89static const int kSamplerThresholdSizeFactorMin = 1;
90static const int kSamplerThresholdSizeFactorDelta = 1;
91
92static const int kSizeLimit = 1500;
93
94static int sampler_threshold = kSamplerThresholdInit;
95static int sampler_threshold_size_factor = kSamplerThresholdSizeFactorInit;
96
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000097static int sampler_ticks_until_threshold_adjustment =
98 kSamplerTicksBetweenThresholdAdjustment;
99
100// The ratio of ticks spent in JS code in percent.
101static Atomic32 js_ratio;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000102
103// The JSFunctions in the sampler window are not GC safe. Old-space
104// pointers are not cleared during mark-sweep collection and therefore
105// the window might contain stale pointers. The window is updated on
106// scavenges and (parts of it) cleared on mark-sweep and
107// mark-sweep-compact.
108static Object* sampler_window[kSamplerWindowSize] = { NULL, };
109static int sampler_window_position = 0;
110static int sampler_window_weight[kSamplerWindowSize] = { 0, };
111
112
113// Support for pending 'optimize soon' requests.
114static PendingListNode* optimize_soon_list = NULL;
115
116
117PendingListNode::PendingListNode(JSFunction* function) : next_(NULL) {
118 function_ = GlobalHandles::Create(function);
119 start_ = OS::Ticks();
120 GlobalHandles::MakeWeak(function_.location(), this, &WeakCallback);
121}
122
123
124void PendingListNode::Destroy() {
125 if (!IsValid()) return;
126 GlobalHandles::Destroy(function_.location());
127 function_= Handle<Object>::null();
128}
129
130
131void PendingListNode::WeakCallback(v8::Persistent<v8::Value>, void* data) {
132 reinterpret_cast<PendingListNode*>(data)->Destroy();
133}
134
135
136static bool IsOptimizable(JSFunction* function) {
137 Code* code = function->code();
138 return code->kind() == Code::FUNCTION && code->optimizable();
139}
140
141
142static void Optimize(JSFunction* function, bool eager, int delay) {
143 ASSERT(IsOptimizable(function));
144 if (FLAG_trace_opt) {
145 PrintF("[marking (%s) ", eager ? "eagerly" : "lazily");
146 function->PrintName();
147 PrintF(" for recompilation");
148 if (delay > 0) {
149 PrintF(" (delayed %0.3f ms)", static_cast<double>(delay) / 1000);
150 }
151 PrintF("]\n");
152 }
153
154 // The next call to the function will trigger optimization.
155 function->MarkForLazyRecompilation();
156}
157
158
159static void AttemptOnStackReplacement(JSFunction* function) {
160 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
161 // Debug::has_break_points().
162 ASSERT(function->IsMarkedForLazyRecompilation());
163 if (!FLAG_use_osr || Debug::has_break_points() || function->IsBuiltin()) {
164 return;
165 }
166
167 SharedFunctionInfo* shared = function->shared();
168 // If the code is not optimizable, don't try OSR.
169 if (!shared->code()->optimizable()) return;
170
171 // We are not prepared to do OSR for a function that already has an
172 // allocated arguments object. The optimized code would bypass it for
173 // arguments accesses, which is unsound. Don't try OSR.
174 if (shared->scope_info()->HasArgumentsShadow()) return;
175
176 // We're using on-stack replacement: patch the unoptimized code so that
177 // any back edge in any unoptimized frame will trigger on-stack
178 // replacement for that frame.
179 if (FLAG_trace_osr) {
180 PrintF("[patching stack checks in ");
181 function->PrintName();
182 PrintF(" for on-stack replacement]\n");
183 }
184
185 // Get the stack check stub code object to match against. We aren't
186 // prepared to generate it, but we don't expect to have to.
187 StackCheckStub check_stub;
188 Object* check_code;
189 MaybeObject* maybe_check_code = check_stub.TryGetCode();
190 if (maybe_check_code->ToObject(&check_code)) {
191 Code* replacement_code = Builtins::builtin(Builtins::OnStackReplacement);
192 Code* unoptimized_code = shared->code();
193 // Iterate the unoptimized code and patch every stack check except at
194 // the function entry. This code assumes the function entry stack
195 // check appears first i.e., is not deferred or otherwise reordered.
196 bool first = true;
197 for (RelocIterator it(unoptimized_code, RelocInfo::kCodeTargetMask);
198 !it.done();
199 it.next()) {
200 RelocInfo* rinfo = it.rinfo();
201 if (rinfo->target_address() == Code::cast(check_code)->entry()) {
202 if (first) {
203 first = false;
204 } else {
205 Deoptimizer::PatchStackCheckCode(rinfo, replacement_code);
206 }
207 }
208 }
209 }
210}
211
212
213static void ClearSampleBuffer() {
214 for (int i = 0; i < kSamplerWindowSize; i++) {
215 sampler_window[i] = NULL;
216 sampler_window_weight[i] = 0;
217 }
218}
219
220
221static void ClearSampleBufferNewSpaceEntries() {
222 for (int i = 0; i < kSamplerWindowSize; i++) {
223 if (Heap::InNewSpace(sampler_window[i])) {
224 sampler_window[i] = NULL;
225 sampler_window_weight[i] = 0;
226 }
227 }
228}
229
230
231static int LookupSample(JSFunction* function) {
232 int weight = 0;
233 for (int i = 0; i < kSamplerWindowSize; i++) {
234 Object* sample = sampler_window[i];
235 if (sample != NULL) {
236 if (function == sample) {
237 weight += sampler_window_weight[i];
238 }
239 }
240 }
241 return weight;
242}
243
244
245static void AddSample(JSFunction* function, int weight) {
246 ASSERT(IsPowerOf2(kSamplerWindowSize));
247 sampler_window[sampler_window_position] = function;
248 sampler_window_weight[sampler_window_position] = weight;
249 sampler_window_position = (sampler_window_position + 1) &
250 (kSamplerWindowSize - 1);
251}
252
253
254void RuntimeProfiler::OptimizeNow() {
255 HandleScope scope;
256 PendingListNode* current = optimize_soon_list;
257 while (current != NULL) {
258 PendingListNode* next = current->next();
259 if (current->IsValid()) {
260 Handle<JSFunction> function = current->function();
261 int delay = current->Delay();
262 if (IsOptimizable(*function)) {
263 Optimize(*function, true, delay);
264 }
265 }
266 delete current;
267 current = next;
268 }
269 optimize_soon_list = NULL;
270
271 // Run through the JavaScript frames and collect them. If we already
272 // have a sample of the function, we mark it for optimizations
273 // (eagerly or lazily).
274 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000275 int sample_count = 0;
276 int frame_count = 0;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000277 for (JavaScriptFrameIterator it;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000278 frame_count++ < kSamplerFrameCount && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000279 it.Advance()) {
280 JavaScriptFrame* frame = it.frame();
281 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000282
283 // Adjust threshold each time we have processed
284 // a certain number of ticks.
285 if (sampler_ticks_until_threshold_adjustment > 0) {
286 sampler_ticks_until_threshold_adjustment--;
287 if (sampler_ticks_until_threshold_adjustment <= 0) {
288 // If the threshold is not already at the minimum
289 // modify and reset the ticks until next adjustment.
290 if (sampler_threshold > kSamplerThresholdMin) {
291 sampler_threshold -= kSamplerThresholdDelta;
292 sampler_ticks_until_threshold_adjustment =
293 kSamplerTicksBetweenThresholdAdjustment;
294 }
295 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000296 }
297
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000298 if (function->IsMarkedForLazyRecompilation()) {
299 Code* unoptimized = function->shared()->code();
300 int nesting = unoptimized->allow_osr_at_loop_nesting_level();
301 if (nesting == 0) AttemptOnStackReplacement(function);
302 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
303 unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000304 }
305
306 // Do not record non-optimizable functions.
307 if (!IsOptimizable(function)) continue;
308 samples[sample_count++] = function;
309
310 int function_size = function->shared()->SourceSize();
311 int threshold_size_factor = (function_size > kSizeLimit)
312 ? sampler_threshold_size_factor
313 : 1;
314
315 int threshold = sampler_threshold * threshold_size_factor;
316 int current_js_ratio = NoBarrier_Load(&js_ratio);
317
318 // Adjust threshold depending on the ratio of time spent
319 // in JS code.
320 if (current_js_ratio < 20) {
321 // If we spend less than 20% of the time in JS code,
322 // do not optimize.
323 continue;
324 } else if (current_js_ratio < 75) {
325 // Below 75% of time spent in JS code, only optimize very
326 // frequently used functions.
327 threshold *= 3;
328 }
329
330 if (LookupSample(function) >= threshold) {
331 Optimize(function, false, 0);
332 CompilationCache::MarkForEagerOptimizing(Handle<JSFunction>(function));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000333 }
334 }
335
336 // Add the collected functions as samples. It's important not to do
337 // this as part of collecting them because this will interfere with
338 // the sample lookup in case of recursive functions.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000339 for (int i = 0; i < sample_count; i++) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000340 AddSample(samples[i], kSamplerFrameWeight[i]);
341 }
342}
343
344
345void RuntimeProfiler::OptimizeSoon(JSFunction* function) {
346 if (!IsOptimizable(function)) return;
347 PendingListNode* node = new PendingListNode(function);
348 node->set_next(optimize_soon_list);
349 optimize_soon_list = node;
350}
351
352
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000353#ifdef ENABLE_LOGGING_AND_PROFILING
354static void UpdateStateRatio(SamplerState current_state) {
355 static const int kStateWindowSize = 128;
356 static SamplerState state_window[kStateWindowSize];
357 static int state_window_position = 0;
358 static int state_counts[2] = { kStateWindowSize, 0 };
359
360 SamplerState old_state = state_window[state_window_position];
361 state_counts[old_state]--;
362 state_window[state_window_position] = current_state;
363 state_counts[current_state]++;
364 ASSERT(IsPowerOf2(kStateWindowSize));
365 state_window_position = (state_window_position + 1) &
366 (kStateWindowSize - 1);
367 NoBarrier_Store(&js_ratio, state_counts[IN_JS_STATE] * 100 /
368 kStateWindowSize);
369}
370#endif
371
372
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000373void RuntimeProfiler::NotifyTick() {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000374#ifdef ENABLE_LOGGING_AND_PROFILING
375 // Record state sample.
376 SamplerState state = Top::IsInJSState()
377 ? IN_JS_STATE
378 : IN_NON_JS_STATE;
379 UpdateStateRatio(state);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000380 StackGuard::RequestRuntimeProfilerTick();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000381#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000382}
383
384
385void RuntimeProfiler::MarkCompactPrologue(bool is_compacting) {
386 if (is_compacting) {
387 // Clear all samples before mark-sweep-compact because every
388 // function might move.
389 ClearSampleBuffer();
390 } else {
391 // Clear only new space entries on mark-sweep since none of the
392 // old-space functions will move.
393 ClearSampleBufferNewSpaceEntries();
394 }
395}
396
397
398bool IsEqual(void* first, void* second) {
399 return first == second;
400}
401
402
403void RuntimeProfiler::Setup() {
404 ClearSampleBuffer();
405 // If the ticker hasn't already started, make sure to do so to get
406 // the ticks for the runtime profiler.
407 if (IsEnabled()) Logger::EnsureTickerStarted();
408}
409
410
411void RuntimeProfiler::Reset() {
412 sampler_threshold = kSamplerThresholdInit;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000413 sampler_ticks_until_threshold_adjustment =
414 kSamplerTicksBetweenThresholdAdjustment;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000415 sampler_threshold_size_factor = kSamplerThresholdSizeFactorInit;
416}
417
418
419void RuntimeProfiler::TearDown() {
420 // Nothing to do.
421}
422
423
424Object** RuntimeProfiler::SamplerWindowAddress() {
425 return sampler_window;
426}
427
428
429int RuntimeProfiler::SamplerWindowSize() {
430 return kSamplerWindowSize;
431}
432
433
434bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000435#ifdef ENABLE_LOGGING_AND_PROFILING
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000436 static const int kNonJSTicksThreshold = 100;
437 // We suspend the runtime profiler thread when not running
438 // JavaScript. If the CPU profiler is active we must not do this
439 // because it samples both JavaScript and C++ code.
440 if (RuntimeProfiler::IsEnabled() &&
441 !CpuProfiler::is_profiling() &&
442 !(FLAG_prof && FLAG_prof_auto)) {
443 if (Top::IsInJSState()) {
444 non_js_ticks_ = 0;
445 } else {
446 if (non_js_ticks_ < kNonJSTicksThreshold) {
447 ++non_js_ticks_;
448 } else {
449 if (Top::WaitForJSState()) return true;
450 }
451 }
452 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000453#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000454 return false;
455}
456
457
458} } // namespace v8::internal