| OLD | NEW |
| (Empty) | |
| 1 // 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 #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 PendingListNode; |
| 41 class Semaphore; |
| 42 |
| 43 class RuntimeProfiler { |
| 44 public: |
| 45 explicit RuntimeProfiler(Isolate* isolate); |
| 46 |
| 47 static bool IsEnabled(); |
| 48 |
| 49 void OptimizeNow(); |
| 50 void OptimizeSoon(JSFunction* function); |
| 51 |
| 52 void NotifyTick(); |
| 53 |
| 54 void Setup(); |
| 55 void Reset(); |
| 56 void TearDown(); |
| 57 |
| 58 void MarkCompactPrologue(bool is_compacting); |
| 59 Object** SamplerWindowAddress(); |
| 60 int SamplerWindowSize(); |
| 61 |
| 62 // Rate limiting support. |
| 63 |
| 64 // VM thread interface. |
| 65 // |
| 66 // Called by isolates when their states change. |
| 67 static inline void IsolateEnteredJS(Isolate* isolate); |
| 68 static inline void IsolateExitedJS(Isolate* isolate); |
| 69 |
| 70 // Profiler thread interface. |
| 71 // |
| 72 // IsSomeIsolateInJS(): |
| 73 // The profiler thread can query whether some isolate is currently |
| 74 // running JavaScript code. |
| 75 // |
| 76 // WaitForSomeIsolateToEnterJS(): |
| 77 // When no isolates are running JavaScript code for some time the |
| 78 // profiler thread suspends itself by calling the wait function. The |
| 79 // wait function returns true after it waited or false immediately. |
| 80 // While the function was waiting the profiler may have been |
| 81 // disabled so it *must check* whether it is allowed to continue. |
| 82 static bool IsSomeIsolateInJS(); |
| 83 static bool WaitForSomeIsolateToEnterJS(); |
| 84 |
| 85 // When shutting down we join the profiler thread. Doing so while |
| 86 // it's waiting on a semaphore will cause a deadlock, so we have to |
| 87 // wake it up first. |
| 88 static void WakeUpRuntimeProfilerThreadBeforeShutdown(); |
| 89 |
| 90 private: |
| 91 static const int kSamplerWindowSize = 16; |
| 92 |
| 93 static void HandleWakeUp(Isolate* isolate); |
| 94 |
| 95 void Optimize(JSFunction* function, bool eager, int delay); |
| 96 |
| 97 void AttemptOnStackReplacement(JSFunction* function); |
| 98 |
| 99 void ClearSampleBuffer(); |
| 100 |
| 101 void ClearSampleBufferNewSpaceEntries(); |
| 102 |
| 103 int LookupSample(JSFunction* function); |
| 104 |
| 105 void AddSample(JSFunction* function, int weight); |
| 106 |
| 107 Isolate* isolate_; |
| 108 |
| 109 int sampler_threshold_; |
| 110 int sampler_threshold_size_factor_; |
| 111 |
| 112 // The JSFunctions in the sampler window are not GC safe. Old-space |
| 113 // pointers are not cleared during mark-sweep collection and therefore |
| 114 // the window might contain stale pointers. The window is updated on |
| 115 // scavenges and (parts of it) cleared on mark-sweep and |
| 116 // mark-sweep-compact. |
| 117 JSFunction* sampler_window_[kSamplerWindowSize]; |
| 118 int sampler_window_position_; |
| 119 int sampler_window_weight_[kSamplerWindowSize]; |
| 120 |
| 121 // Support for pending 'optimize soon' requests. |
| 122 PendingListNode* optimize_soon_list_; |
| 123 |
| 124 // Possible state values: |
| 125 // -1 => the profiler thread is waiting on the semaphore |
| 126 // 0 or positive => the number of isolates running JavaScript code. |
| 127 static Atomic32 state_; |
| 128 static Semaphore* semaphore_; |
| 129 }; |
| 130 |
| 131 |
| 132 // Rate limiter intended to be used in the profiler thread. |
| 133 class RuntimeProfilerRateLimiter BASE_EMBEDDED { |
| 134 public: |
| 135 RuntimeProfilerRateLimiter() : non_js_ticks_(0) { } |
| 136 |
| 137 // Suspends the current thread (which must be the profiler thread) |
| 138 // when not executing JavaScript to minimize CPU usage. Returns |
| 139 // whether the thread was suspended (and so must check whether |
| 140 // profiling is still active.) |
| 141 // |
| 142 // Does nothing when runtime profiling is not enabled. |
| 143 bool SuspendIfNecessary(); |
| 144 |
| 145 private: |
| 146 int non_js_ticks_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(RuntimeProfilerRateLimiter); |
| 149 }; |
| 150 |
| 151 |
| 152 // Implementation of RuntimeProfiler inline functions. |
| 153 |
| 154 void RuntimeProfiler::IsolateEnteredJS(Isolate* isolate) { |
| 155 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1); |
| 156 if (new_state == 0) { |
| 157 // Just incremented from -1 to 0. -1 can only be set by the |
| 158 // profiler thread before it suspends itself and starts waiting on |
| 159 // the semaphore. |
| 160 HandleWakeUp(isolate); |
| 161 } |
| 162 ASSERT(new_state >= 0); |
| 163 } |
| 164 |
| 165 |
| 166 void RuntimeProfiler::IsolateExitedJS(Isolate* isolate) { |
| 167 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, -1); |
| 168 ASSERT(new_state >= 0); |
| 169 USE(new_state); |
| 170 } |
| 171 |
| 172 } } // namespace v8::internal |
| 173 |
| 174 #endif // V8_RUNTIME_PROFILER_H_ |
| OLD | NEW |