blob: d7792aceaede3d5d72f6b3dc821d3845b758a3f1 [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();
196 // Iterate the unoptimized code and patch every stack check except at
197 // the function entry. This code assumes the function entry stack
198 // check appears first i.e., is not deferred or otherwise reordered.
199 bool first = true;
200 for (RelocIterator it(unoptimized_code, RelocInfo::kCodeTargetMask);
201 !it.done();
202 it.next()) {
203 RelocInfo* rinfo = it.rinfo();
204 if (rinfo->target_address() == Code::cast(check_code)->entry()) {
205 if (first) {
206 first = false;
207 } else {
208 Deoptimizer::PatchStackCheckCode(rinfo, replacement_code);
209 }
210 }
211 }
212 }
213}
214
215
216static void ClearSampleBuffer() {
217 for (int i = 0; i < kSamplerWindowSize; i++) {
218 sampler_window[i] = NULL;
219 sampler_window_weight[i] = 0;
220 }
221}
222
223
224static void ClearSampleBufferNewSpaceEntries() {
225 for (int i = 0; i < kSamplerWindowSize; i++) {
226 if (Heap::InNewSpace(sampler_window[i])) {
227 sampler_window[i] = NULL;
228 sampler_window_weight[i] = 0;
229 }
230 }
231}
232
233
234static int LookupSample(JSFunction* function) {
235 int weight = 0;
236 for (int i = 0; i < kSamplerWindowSize; i++) {
237 Object* sample = sampler_window[i];
238 if (sample != NULL) {
239 if (function == sample) {
240 weight += sampler_window_weight[i];
241 }
242 }
243 }
244 return weight;
245}
246
247
248static void AddSample(JSFunction* function, int weight) {
249 ASSERT(IsPowerOf2(kSamplerWindowSize));
250 sampler_window[sampler_window_position] = function;
251 sampler_window_weight[sampler_window_position] = weight;
252 sampler_window_position = (sampler_window_position + 1) &
253 (kSamplerWindowSize - 1);
254}
255
256
257void RuntimeProfiler::OptimizeNow() {
258 HandleScope scope;
259 PendingListNode* current = optimize_soon_list;
260 while (current != NULL) {
261 PendingListNode* next = current->next();
262 if (current->IsValid()) {
263 Handle<JSFunction> function = current->function();
264 int delay = current->Delay();
265 if (IsOptimizable(*function)) {
266 Optimize(*function, true, delay);
267 }
268 }
269 delete current;
270 current = next;
271 }
272 optimize_soon_list = NULL;
273
274 // Run through the JavaScript frames and collect them. If we already
275 // have a sample of the function, we mark it for optimizations
276 // (eagerly or lazily).
277 JSFunction* samples[kSamplerFrameCount];
278 int sample_count = 0;
279 int frame_count = 0;
280 for (JavaScriptFrameIterator it;
281 frame_count++ < kSamplerFrameCount && !it.done();
282 it.Advance()) {
283 JavaScriptFrame* frame = it.frame();
284 JSFunction* function = JSFunction::cast(frame->function());
285
286 // Adjust threshold each time we have processed
287 // a certain number of ticks.
288 if (sampler_ticks_until_threshold_adjustment > 0) {
289 sampler_ticks_until_threshold_adjustment--;
290 if (sampler_ticks_until_threshold_adjustment <= 0) {
291 // If the threshold is not already at the minimum
292 // modify and reset the ticks until next adjustment.
293 if (sampler_threshold > kSamplerThresholdMin) {
294 sampler_threshold -= kSamplerThresholdDelta;
295 sampler_ticks_until_threshold_adjustment =
296 kSamplerTicksBetweenThresholdAdjustment;
297 }
298 }
299 }
300
301 if (function->IsMarkedForLazyRecompilation()) {
302 Code* unoptimized = function->shared()->code();
303 int nesting = unoptimized->allow_osr_at_loop_nesting_level();
304 if (nesting == 0) AttemptOnStackReplacement(function);
305 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
306 unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting);
307 }
308
309 // Do not record non-optimizable functions.
310 if (!IsOptimizable(function)) continue;
311 samples[sample_count++] = function;
312
313 int function_size = function->shared()->SourceSize();
314 int threshold_size_factor = (function_size > kSizeLimit)
315 ? sampler_threshold_size_factor
316 : 1;
317
318 int threshold = sampler_threshold * threshold_size_factor;
319 int current_js_ratio = NoBarrier_Load(&js_ratio);
320
321 // Adjust threshold depending on the ratio of time spent
322 // in JS code.
323 if (current_js_ratio < 20) {
324 // If we spend less than 20% of the time in JS code,
325 // do not optimize.
326 continue;
327 } else if (current_js_ratio < 75) {
328 // Below 75% of time spent in JS code, only optimize very
329 // frequently used functions.
330 threshold *= 3;
331 }
332
333 if (LookupSample(function) >= threshold) {
334 Optimize(function, false, 0);
335 CompilationCache::MarkForEagerOptimizing(Handle<JSFunction>(function));
336 }
337 }
338
339 // Add the collected functions as samples. It's important not to do
340 // this as part of collecting them because this will interfere with
341 // the sample lookup in case of recursive functions.
342 for (int i = 0; i < sample_count; i++) {
343 AddSample(samples[i], kSamplerFrameWeight[i]);
344 }
345}
346
347
348void RuntimeProfiler::OptimizeSoon(JSFunction* function) {
349 if (!IsOptimizable(function)) return;
350 PendingListNode* node = new PendingListNode(function);
351 node->set_next(optimize_soon_list);
352 optimize_soon_list = node;
353}
354
355
356#ifdef ENABLE_LOGGING_AND_PROFILING
357static void UpdateStateRatio(SamplerState current_state) {
358 static const int kStateWindowSize = 128;
359 static SamplerState state_window[kStateWindowSize];
360 static int state_window_position = 0;
361 static int state_counts[2] = { kStateWindowSize, 0 };
362
363 SamplerState old_state = state_window[state_window_position];
364 state_counts[old_state]--;
365 state_window[state_window_position] = current_state;
366 state_counts[current_state]++;
367 ASSERT(IsPowerOf2(kStateWindowSize));
368 state_window_position = (state_window_position + 1) &
369 (kStateWindowSize - 1);
370 NoBarrier_Store(&js_ratio, state_counts[IN_JS_STATE] * 100 /
371 kStateWindowSize);
372}
373#endif
374
375
376void RuntimeProfiler::NotifyTick() {
377#ifdef ENABLE_LOGGING_AND_PROFILING
378 // Record state sample.
379 SamplerState state = Top::IsInJSState()
380 ? IN_JS_STATE
381 : IN_NON_JS_STATE;
382 UpdateStateRatio(state);
383 StackGuard::RequestRuntimeProfilerTick();
384#endif
385}
386
387
388void RuntimeProfiler::MarkCompactPrologue(bool is_compacting) {
389 if (is_compacting) {
390 // Clear all samples before mark-sweep-compact because every
391 // function might move.
392 ClearSampleBuffer();
393 } else {
394 // Clear only new space entries on mark-sweep since none of the
395 // old-space functions will move.
396 ClearSampleBufferNewSpaceEntries();
397 }
398}
399
400
401bool IsEqual(void* first, void* second) {
402 return first == second;
403}
404
405
406void RuntimeProfiler::Setup() {
407 ClearSampleBuffer();
408 // If the ticker hasn't already started, make sure to do so to get
409 // the ticks for the runtime profiler.
410 if (IsEnabled()) Logger::EnsureTickerStarted();
411}
412
413
414void RuntimeProfiler::Reset() {
415 sampler_threshold = kSamplerThresholdInit;
416 sampler_ticks_until_threshold_adjustment =
417 kSamplerTicksBetweenThresholdAdjustment;
418 sampler_threshold_size_factor = kSamplerThresholdSizeFactorInit;
419}
420
421
422void RuntimeProfiler::TearDown() {
423 // Nothing to do.
424}
425
426
427Object** RuntimeProfiler::SamplerWindowAddress() {
428 return sampler_window;
429}
430
431
432int RuntimeProfiler::SamplerWindowSize() {
433 return kSamplerWindowSize;
434}
435
436
437bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
438#ifdef ENABLE_LOGGING_AND_PROFILING
439 static const int kNonJSTicksThreshold = 100;
440 // We suspend the runtime profiler thread when not running
441 // JavaScript. If the CPU profiler is active we must not do this
442 // because it samples both JavaScript and C++ code.
443 if (RuntimeProfiler::IsEnabled() &&
444 !CpuProfiler::is_profiling() &&
445 !(FLAG_prof && FLAG_prof_auto)) {
446 if (Top::IsInJSState()) {
447 non_js_ticks_ = 0;
448 } else {
449 if (non_js_ticks_ < kNonJSTicksThreshold) {
450 ++non_js_ticks_;
451 } else {
452 if (Top::WaitForJSState()) return true;
453 }
454 }
455 }
456#endif
457 return false;
458}
459
460
461} } // namespace v8::internal