yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 32 | #include "atomicops.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 37 | class Isolate; |
| 38 | class JSFunction; |
| 39 | class Object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 40 | class Semaphore; |
| 41 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 42 | class RuntimeProfiler { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 43 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 44 | explicit RuntimeProfiler(Isolate* isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 45 | |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 46 | static void GlobalSetUp(); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 47 | |
| 48 | static inline bool IsEnabled() { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 49 | ASSERT(has_been_globally_set_up_); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 50 | return enabled_; |
| 51 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 52 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 53 | void OptimizeNow(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 54 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 55 | void SetUp(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 56 | void Reset(); |
| 57 | void TearDown(); |
| 58 | |
| 59 | Object** SamplerWindowAddress(); |
| 60 | int SamplerWindowSize(); |
| 61 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 62 | void NotifyICChanged() { any_ic_changed_ = true; } |
| 63 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 64 | // Rate limiting support. |
| 65 | |
| 66 | // VM thread interface. |
| 67 | // |
| 68 | // Called by isolates when their states change. |
| 69 | static inline void IsolateEnteredJS(Isolate* isolate); |
| 70 | static inline void IsolateExitedJS(Isolate* isolate); |
| 71 | |
| 72 | // Profiler thread interface. |
| 73 | // |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 74 | // WaitForSomeIsolateToEnterJS(): |
| 75 | // When no isolates are running JavaScript code for some time the |
| 76 | // profiler thread suspends itself by calling the wait function. The |
| 77 | // wait function returns true after it waited or false immediately. |
| 78 | // While the function was waiting the profiler may have been |
| 79 | // disabled so it *must check* whether it is allowed to continue. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 80 | static bool WaitForSomeIsolateToEnterJS(); |
| 81 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 82 | // Stops the runtime profiler thread when profiling support is being |
| 83 | // turned off. |
| 84 | static void StopRuntimeProfilerThreadBeforeShutdown(Thread* thread); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 85 | |
| 86 | void UpdateSamplesAfterScavenge(); |
| 87 | void RemoveDeadSamples(); |
| 88 | void UpdateSamplesAfterCompact(ObjectVisitor* visitor); |
| 89 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 90 | void AttemptOnStackReplacement(JSFunction* function); |
| 91 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 92 | private: |
| 93 | static const int kSamplerWindowSize = 16; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 94 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 95 | static void HandleWakeUp(Isolate* isolate); |
| 96 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 97 | void Optimize(JSFunction* function, const char* reason); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 98 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 99 | void ClearSampleBuffer(); |
| 100 | |
| 101 | void ClearSampleBufferNewSpaceEntries(); |
| 102 | |
| 103 | int LookupSample(JSFunction* function); |
| 104 | |
| 105 | void AddSample(JSFunction* function, int weight); |
| 106 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 107 | Isolate* isolate_; |
| 108 | |
| 109 | int sampler_threshold_; |
| 110 | int sampler_threshold_size_factor_; |
| 111 | int sampler_ticks_until_threshold_adjustment_; |
| 112 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 113 | Object* sampler_window_[kSamplerWindowSize]; |
| 114 | int sampler_window_position_; |
| 115 | int sampler_window_weight_[kSamplerWindowSize]; |
| 116 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 117 | bool any_ic_changed_; |
| 118 | bool code_generated_; |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 119 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 120 | // Possible state values: |
| 121 | // -1 => the profiler thread is waiting on the semaphore |
| 122 | // 0 or positive => the number of isolates running JavaScript code. |
| 123 | static Atomic32 state_; |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 124 | |
| 125 | #ifdef DEBUG |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 126 | static bool has_been_globally_set_up_; |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 127 | #endif |
| 128 | static bool enabled_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 132 | // Implementation of RuntimeProfiler inline functions. |
| 133 | |
| 134 | void RuntimeProfiler::IsolateEnteredJS(Isolate* isolate) { |
| 135 | Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1); |
| 136 | if (new_state == 0) { |
| 137 | // Just incremented from -1 to 0. -1 can only be set by the |
| 138 | // profiler thread before it suspends itself and starts waiting on |
| 139 | // the semaphore. |
| 140 | HandleWakeUp(isolate); |
| 141 | } |
| 142 | ASSERT(new_state >= 0); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | void RuntimeProfiler::IsolateExitedJS(Isolate* isolate) { |
| 147 | Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, -1); |
| 148 | ASSERT(new_state >= 0); |
| 149 | USE(new_state); |
| 150 | } |
| 151 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 152 | } } // namespace v8::internal |
| 153 | |
| 154 | #endif // V8_RUNTIME_PROFILER_H_ |