blob: 3406cdc250cbc30cdf071ff34a72d9f84e8a410a [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// 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
71enum SamplerState {
72 IN_NON_JS_STATE = 0,
73 IN_JS_STATE = 1
74};
75
76
77// Optimization sampler constants.
78static const int kSamplerFrameCount = 2;
79static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
80static const int kSamplerWindowSize = 16;
81
82static const int kSamplerTicksBetweenThresholdAdjustment = 32;
83
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
97static int sampler_ticks_until_threshold_adjustment =
98 kSamplerTicksBetweenThresholdAdjustment;
99
100// The ratio of ticks spent in JS code in percent.
101static Atomic32 js_ratio;
102
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) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100137 if (Heap::InNewSpace(function)) return false;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100138 Code* code = function->code();
139 return code->kind() == Code::FUNCTION && code->optimizable();
140}
141
142
143static void Optimize(JSFunction* function, bool eager, int delay) {
144 ASSERT(IsOptimizable(function));
145 if (FLAG_trace_opt) {
146 PrintF("[marking (%s) ", eager ? "eagerly" : "lazily");
147 function->PrintName();
148 PrintF(" for recompilation");
149 if (delay > 0) {
150 PrintF(" (delayed %0.3f ms)", static_cast<double>(delay) / 1000);
151 }
152 PrintF("]\n");
153 }
154
155 // The next call to the function will trigger optimization.
156 function->MarkForLazyRecompilation();
157}
158
159
160static void AttemptOnStackReplacement(JSFunction* function) {
161 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
162 // Debug::has_break_points().
163 ASSERT(function->IsMarkedForLazyRecompilation());
164 if (!FLAG_use_osr || Debug::has_break_points() || function->IsBuiltin()) {
165 return;
166 }
167
168 SharedFunctionInfo* shared = function->shared();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100169 // If the code is not optimizable or references context slots, don't try OSR.
170 if (!shared->code()->optimizable() || !shared->allows_lazy_compilation()) {
171 return;
172 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100173
174 // We are not prepared to do OSR for a function that already has an
175 // allocated arguments object. The optimized code would bypass it for
176 // arguments accesses, which is unsound. Don't try OSR.
177 if (shared->scope_info()->HasArgumentsShadow()) return;
178
179 // We're using on-stack replacement: patch the unoptimized code so that
180 // any back edge in any unoptimized frame will trigger on-stack
181 // replacement for that frame.
182 if (FLAG_trace_osr) {
183 PrintF("[patching stack checks in ");
184 function->PrintName();
185 PrintF(" for on-stack replacement]\n");
186 }
187
188 // Get the stack check stub code object to match against. We aren't
189 // prepared to generate it, but we don't expect to have to.
190 StackCheckStub check_stub;
191 Object* check_code;
192 MaybeObject* maybe_check_code = check_stub.TryGetCode();
193 if (maybe_check_code->ToObject(&check_code)) {
194 Code* replacement_code = Builtins::builtin(Builtins::OnStackReplacement);
195 Code* unoptimized_code = shared->code();
Steve Block1e0659c2011-05-24 12:43:12 +0100196 Deoptimizer::PatchStackCheckCode(unoptimized_code,
197 Code::cast(check_code),
198 replacement_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100199 }
200}
201
202
203static void ClearSampleBuffer() {
204 for (int i = 0; i < kSamplerWindowSize; i++) {
205 sampler_window[i] = NULL;
206 sampler_window_weight[i] = 0;
207 }
208}
209
210
211static void ClearSampleBufferNewSpaceEntries() {
212 for (int i = 0; i < kSamplerWindowSize; i++) {
213 if (Heap::InNewSpace(sampler_window[i])) {
214 sampler_window[i] = NULL;
215 sampler_window_weight[i] = 0;
216 }
217 }
218}
219
220
221static int LookupSample(JSFunction* function) {
222 int weight = 0;
223 for (int i = 0; i < kSamplerWindowSize; i++) {
224 Object* sample = sampler_window[i];
225 if (sample != NULL) {
226 if (function == sample) {
227 weight += sampler_window_weight[i];
228 }
229 }
230 }
231 return weight;
232}
233
234
235static void AddSample(JSFunction* function, int weight) {
236 ASSERT(IsPowerOf2(kSamplerWindowSize));
237 sampler_window[sampler_window_position] = function;
238 sampler_window_weight[sampler_window_position] = weight;
239 sampler_window_position = (sampler_window_position + 1) &
240 (kSamplerWindowSize - 1);
241}
242
243
244void RuntimeProfiler::OptimizeNow() {
245 HandleScope scope;
246 PendingListNode* current = optimize_soon_list;
247 while (current != NULL) {
248 PendingListNode* next = current->next();
249 if (current->IsValid()) {
250 Handle<JSFunction> function = current->function();
251 int delay = current->Delay();
252 if (IsOptimizable(*function)) {
253 Optimize(*function, true, delay);
254 }
255 }
256 delete current;
257 current = next;
258 }
259 optimize_soon_list = NULL;
260
261 // Run through the JavaScript frames and collect them. If we already
262 // have a sample of the function, we mark it for optimizations
263 // (eagerly or lazily).
264 JSFunction* samples[kSamplerFrameCount];
265 int sample_count = 0;
266 int frame_count = 0;
267 for (JavaScriptFrameIterator it;
268 frame_count++ < kSamplerFrameCount && !it.done();
269 it.Advance()) {
270 JavaScriptFrame* frame = it.frame();
271 JSFunction* function = JSFunction::cast(frame->function());
272
273 // Adjust threshold each time we have processed
274 // a certain number of ticks.
275 if (sampler_ticks_until_threshold_adjustment > 0) {
276 sampler_ticks_until_threshold_adjustment--;
277 if (sampler_ticks_until_threshold_adjustment <= 0) {
278 // If the threshold is not already at the minimum
279 // modify and reset the ticks until next adjustment.
280 if (sampler_threshold > kSamplerThresholdMin) {
281 sampler_threshold -= kSamplerThresholdDelta;
282 sampler_ticks_until_threshold_adjustment =
283 kSamplerTicksBetweenThresholdAdjustment;
284 }
285 }
286 }
287
288 if (function->IsMarkedForLazyRecompilation()) {
289 Code* unoptimized = function->shared()->code();
290 int nesting = unoptimized->allow_osr_at_loop_nesting_level();
291 if (nesting == 0) AttemptOnStackReplacement(function);
292 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
293 unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting);
294 }
295
296 // Do not record non-optimizable functions.
297 if (!IsOptimizable(function)) continue;
298 samples[sample_count++] = function;
299
300 int function_size = function->shared()->SourceSize();
301 int threshold_size_factor = (function_size > kSizeLimit)
302 ? sampler_threshold_size_factor
303 : 1;
304
305 int threshold = sampler_threshold * threshold_size_factor;
306 int current_js_ratio = NoBarrier_Load(&js_ratio);
307
308 // Adjust threshold depending on the ratio of time spent
309 // in JS code.
310 if (current_js_ratio < 20) {
311 // If we spend less than 20% of the time in JS code,
312 // do not optimize.
313 continue;
314 } else if (current_js_ratio < 75) {
315 // Below 75% of time spent in JS code, only optimize very
316 // frequently used functions.
317 threshold *= 3;
318 }
319
320 if (LookupSample(function) >= threshold) {
321 Optimize(function, false, 0);
322 CompilationCache::MarkForEagerOptimizing(Handle<JSFunction>(function));
323 }
324 }
325
326 // Add the collected functions as samples. It's important not to do
327 // this as part of collecting them because this will interfere with
328 // the sample lookup in case of recursive functions.
329 for (int i = 0; i < sample_count; i++) {
330 AddSample(samples[i], kSamplerFrameWeight[i]);
331 }
332}
333
334
335void RuntimeProfiler::OptimizeSoon(JSFunction* function) {
336 if (!IsOptimizable(function)) return;
337 PendingListNode* node = new PendingListNode(function);
338 node->set_next(optimize_soon_list);
339 optimize_soon_list = node;
340}
341
342
343#ifdef ENABLE_LOGGING_AND_PROFILING
344static void UpdateStateRatio(SamplerState current_state) {
345 static const int kStateWindowSize = 128;
346 static SamplerState state_window[kStateWindowSize];
347 static int state_window_position = 0;
348 static int state_counts[2] = { kStateWindowSize, 0 };
349
350 SamplerState old_state = state_window[state_window_position];
351 state_counts[old_state]--;
352 state_window[state_window_position] = current_state;
353 state_counts[current_state]++;
354 ASSERT(IsPowerOf2(kStateWindowSize));
355 state_window_position = (state_window_position + 1) &
356 (kStateWindowSize - 1);
357 NoBarrier_Store(&js_ratio, state_counts[IN_JS_STATE] * 100 /
358 kStateWindowSize);
359}
360#endif
361
362
363void RuntimeProfiler::NotifyTick() {
364#ifdef ENABLE_LOGGING_AND_PROFILING
365 // Record state sample.
366 SamplerState state = Top::IsInJSState()
367 ? IN_JS_STATE
368 : IN_NON_JS_STATE;
369 UpdateStateRatio(state);
370 StackGuard::RequestRuntimeProfilerTick();
371#endif
372}
373
374
375void RuntimeProfiler::MarkCompactPrologue(bool is_compacting) {
376 if (is_compacting) {
377 // Clear all samples before mark-sweep-compact because every
378 // function might move.
379 ClearSampleBuffer();
380 } else {
381 // Clear only new space entries on mark-sweep since none of the
382 // old-space functions will move.
383 ClearSampleBufferNewSpaceEntries();
384 }
385}
386
387
388bool IsEqual(void* first, void* second) {
389 return first == second;
390}
391
392
393void RuntimeProfiler::Setup() {
394 ClearSampleBuffer();
395 // If the ticker hasn't already started, make sure to do so to get
396 // the ticks for the runtime profiler.
397 if (IsEnabled()) Logger::EnsureTickerStarted();
398}
399
400
401void RuntimeProfiler::Reset() {
402 sampler_threshold = kSamplerThresholdInit;
403 sampler_ticks_until_threshold_adjustment =
404 kSamplerTicksBetweenThresholdAdjustment;
405 sampler_threshold_size_factor = kSamplerThresholdSizeFactorInit;
406}
407
408
409void RuntimeProfiler::TearDown() {
410 // Nothing to do.
411}
412
413
414Object** RuntimeProfiler::SamplerWindowAddress() {
415 return sampler_window;
416}
417
418
419int RuntimeProfiler::SamplerWindowSize() {
420 return kSamplerWindowSize;
421}
422
423
424bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
425#ifdef ENABLE_LOGGING_AND_PROFILING
426 static const int kNonJSTicksThreshold = 100;
427 // We suspend the runtime profiler thread when not running
428 // JavaScript. If the CPU profiler is active we must not do this
429 // because it samples both JavaScript and C++ code.
430 if (RuntimeProfiler::IsEnabled() &&
431 !CpuProfiler::is_profiling() &&
432 !(FLAG_prof && FLAG_prof_auto)) {
433 if (Top::IsInJSState()) {
434 non_js_ticks_ = 0;
435 } else {
436 if (non_js_ticks_ < kNonJSTicksThreshold) {
437 ++non_js_ticks_;
438 } else {
439 if (Top::WaitForJSState()) return true;
440 }
441 }
442 }
443#endif
444 return false;
445}
446
447
448} } // namespace v8::internal