v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
runtime-profiler.h
Go to the documentation of this file.
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
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 
31 #include "allocation.h"
32 #include "atomicops.h"
33 
34 namespace v8 {
35 namespace internal {
36 
37 class Isolate;
38 class JSFunction;
39 class Object;
40 class Semaphore;
41 
43  public:
44  explicit RuntimeProfiler(Isolate* isolate);
45 
46  static void GlobalSetUp();
47 
48  static inline bool IsEnabled() {
49  ASSERT(has_been_globally_set_up_);
50  return enabled_;
51  }
52 
53  void OptimizeNow();
54 
55  void NotifyTick();
56 
57  void SetUp();
58  void Reset();
59  void TearDown();
60 
62  int SamplerWindowSize();
63 
64  void NotifyICChanged() { any_ic_changed_ = true; }
65 
66  // Rate limiting support.
67 
68  // VM thread interface.
69  //
70  // Called by isolates when their states change.
71  static inline void IsolateEnteredJS(Isolate* isolate);
72  static inline void IsolateExitedJS(Isolate* isolate);
73 
74  // Profiler thread interface.
75  //
76  // IsSomeIsolateInJS():
77  // The profiler thread can query whether some isolate is currently
78  // running JavaScript code.
79  //
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();
88 
89  // Stops the runtime profiler thread when profiling support is being
90  // turned off.
92 
94  void RemoveDeadSamples();
95  void UpdateSamplesAfterCompact(ObjectVisitor* visitor);
96 
97  void AttemptOnStackReplacement(JSFunction* function);
98 
99  private:
100  static const int kSamplerWindowSize = 16;
101 
102  static void HandleWakeUp(Isolate* isolate);
103 
104  void Optimize(JSFunction* function, const char* reason);
105 
106  void ClearSampleBuffer();
107 
108  void ClearSampleBufferNewSpaceEntries();
109 
110  int LookupSample(JSFunction* function);
111 
112  void AddSample(JSFunction* function, int weight);
113 
114  Isolate* isolate_;
115 
116  int sampler_threshold_;
117  int sampler_threshold_size_factor_;
118  int sampler_ticks_until_threshold_adjustment_;
119 
120  Object* sampler_window_[kSamplerWindowSize];
121  int sampler_window_position_;
122  int sampler_window_weight_[kSamplerWindowSize];
123 
124  bool any_ic_changed_;
125  bool code_generated_;
126 
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_;
131 
132 #ifdef DEBUG
133  static bool has_been_globally_set_up_;
134 #endif
135  static bool enabled_;
136 };
137 
138 
139 // Rate limiter intended to be used in the profiler thread.
140 class RuntimeProfilerRateLimiter BASE_EMBEDDED {
141  public:
143 
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.)
148  //
149  // Does nothing when runtime profiling is not enabled.
150  bool SuspendIfNecessary();
151 
152  private:
153  DISALLOW_COPY_AND_ASSIGN(RuntimeProfilerRateLimiter);
154 };
155 
156 
157 // Implementation of RuntimeProfiler inline functions.
158 
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
164  // the semaphore.
165  HandleWakeUp(isolate);
166  }
167  ASSERT(new_state >= 0);
168 }
169 
170 
172  Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, -1);
173  ASSERT(new_state >= 0);
174  USE(new_state);
175 }
176 
177 } } // namespace v8::internal
178 
179 #endif // V8_RUNTIME_PROFILER_H_
static void IsolateEnteredJS(Isolate *isolate)
#define ASSERT(condition)
Definition: checks.h:270
static void StopRuntimeProfilerThreadBeforeShutdown(Thread *thread)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:307
Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32 *ptr, Atomic32 increment)
void UpdateSamplesAfterCompact(ObjectVisitor *visitor)
#define BASE_EMBEDDED
Definition: allocation.h:68
static void IsolateExitedJS(Isolate *isolate)
void USE(T)
Definition: globals.h:289
int32_t Atomic32
Definition: atomicops.h:57
void AttemptOnStackReplacement(JSFunction *function)