yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +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_CPU_PROFILER_H_ |
| 29 | #define V8_CPU_PROFILER_H_ |
| 30 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 32 | #include "atomicops.h" |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 33 | #include "circular-queue.h" |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 34 | #include "sampler.h" |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 35 | #include "unbound-queue.h" |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 40 | // Forward declarations. |
| 41 | class CodeEntry; |
| 42 | class CodeMap; |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 43 | class CompilationInfo; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 44 | class CpuProfile; |
| 45 | class CpuProfilesCollection; |
| 46 | class ProfileGenerator; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 47 | class TokenEnumerator; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 48 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 49 | #define CODE_EVENTS_TYPE_LIST(V) \ |
| 50 | V(CODE_CREATION, CodeCreateEventRecord) \ |
| 51 | V(CODE_MOVE, CodeMoveEventRecord) \ |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 52 | V(SHARED_FUNC_MOVE, SharedFunctionInfoMoveEventRecord) |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | class CodeEventRecord { |
| 56 | public: |
| 57 | #define DECLARE_TYPE(type, ignore) type, |
| 58 | enum Type { |
| 59 | NONE = 0, |
| 60 | CODE_EVENTS_TYPE_LIST(DECLARE_TYPE) |
| 61 | NUMBER_OF_TYPES |
| 62 | }; |
| 63 | #undef DECLARE_TYPE |
| 64 | |
| 65 | Type type; |
| 66 | unsigned order; |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | class CodeCreateEventRecord : public CodeEventRecord { |
| 71 | public: |
| 72 | Address start; |
| 73 | CodeEntry* entry; |
| 74 | unsigned size; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 75 | Address shared; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 76 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 77 | INLINE(void UpdateCodeMap(CodeMap* code_map)); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | |
| 81 | class CodeMoveEventRecord : public CodeEventRecord { |
| 82 | public: |
| 83 | Address from; |
| 84 | Address to; |
| 85 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 86 | INLINE(void UpdateCodeMap(CodeMap* code_map)); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 90 | class SharedFunctionInfoMoveEventRecord : public CodeEventRecord { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 91 | public: |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 92 | Address from; |
| 93 | Address to; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 94 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 95 | INLINE(void UpdateCodeMap(CodeMap* code_map)); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 99 | class TickSampleEventRecord { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 100 | public: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 101 | // The parameterless constructor is used when we dequeue data from |
| 102 | // the ticks buffer. |
| 103 | TickSampleEventRecord() { } |
| 104 | explicit TickSampleEventRecord(unsigned order) |
| 105 | : filler(1), |
| 106 | order(order) { |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 107 | ASSERT(filler != SamplingCircularQueue::kClear); |
| 108 | } |
| 109 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 110 | // The first machine word of a TickSampleEventRecord must not ever |
| 111 | // become equal to SamplingCircularQueue::kClear. As both order and |
| 112 | // TickSample's first field are not reliable in this sense (order |
| 113 | // can overflow, TickSample can have all fields reset), we are |
| 114 | // forced to use an artificial filler field. |
| 115 | int filler; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 116 | unsigned order; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 117 | TickSample sample; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 118 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 119 | static TickSampleEventRecord* cast(void* value) { |
| 120 | return reinterpret_cast<TickSampleEventRecord*>(value); |
| 121 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | |
| 125 | // This class implements both the profile events processor thread and |
| 126 | // methods called by event producers: VM and stack sampler threads. |
| 127 | class ProfilerEventsProcessor : public Thread { |
| 128 | public: |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 129 | ProfilerEventsProcessor(ProfileGenerator* generator, |
| 130 | CpuProfilesCollection* profiles); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 131 | virtual ~ProfilerEventsProcessor() {} |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 132 | |
| 133 | // Thread control. |
| 134 | virtual void Run(); |
| 135 | inline void Stop() { running_ = false; } |
| 136 | INLINE(bool running()) { return running_; } |
| 137 | |
| 138 | // Events adding methods. Called by VM threads. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 139 | void CallbackCreateEvent(Logger::LogEventsAndTags tag, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 140 | const char* prefix, Name* name, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 141 | Address start); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 142 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 143 | Name* name, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 144 | String* resource_name, int line_number, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 145 | Address start, unsigned size, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 146 | Address shared, |
| 147 | CompilationInfo* info); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 148 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 149 | const char* name, |
| 150 | Address start, unsigned size); |
| 151 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 152 | int args_count, |
| 153 | Address start, unsigned size); |
| 154 | void CodeMoveEvent(Address from, Address to); |
| 155 | void CodeDeleteEvent(Address from); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 156 | void SharedFunctionInfoMoveEvent(Address from, Address to); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 157 | void RegExpCodeCreateEvent(Logger::LogEventsAndTags tag, |
| 158 | const char* prefix, String* name, |
| 159 | Address start, unsigned size); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 160 | // Puts current stack into tick sample events buffer. |
| 161 | void AddCurrentStack(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 162 | |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 163 | // Tick sample events are filled directly in the buffer of the circular |
| 164 | // queue (because the structure is of fixed width, but usually not all |
| 165 | // stack frame entries are filled.) This method returns a pointer to the |
| 166 | // next record of the buffer. |
| 167 | INLINE(TickSample* TickSampleEvent()); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 168 | |
| 169 | private: |
| 170 | union CodeEventsContainer { |
| 171 | CodeEventRecord generic; |
| 172 | #define DECLARE_CLASS(ignore, type) type type##_; |
| 173 | CODE_EVENTS_TYPE_LIST(DECLARE_CLASS) |
| 174 | #undef DECLARE_TYPE |
| 175 | }; |
| 176 | |
| 177 | // Called from events processing thread (Run() method.) |
| 178 | bool ProcessCodeEvent(unsigned* dequeue_order); |
| 179 | bool ProcessTicks(unsigned dequeue_order); |
| 180 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 181 | INLINE(static bool FilterOutCodeCreateEvent(Logger::LogEventsAndTags tag)); |
| 182 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 183 | ProfileGenerator* generator_; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 184 | CpuProfilesCollection* profiles_; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 185 | bool running_; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 186 | UnboundQueue<CodeEventsContainer> events_buffer_; |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 187 | SamplingCircularQueue ticks_buffer_; |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 188 | UnboundQueue<TickSampleEventRecord> ticks_from_vm_buffer_; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 189 | unsigned enqueue_order_; |
| 190 | }; |
| 191 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 192 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 193 | #define PROFILE(IsolateGetter, Call) \ |
| 194 | do { \ |
| 195 | Isolate* cpu_profiler_isolate = (IsolateGetter); \ |
| 196 | LOG_CODE_EVENT(cpu_profiler_isolate, Call); \ |
| 197 | CpuProfiler* cpu_profiler = cpu_profiler_isolate->cpu_profiler(); \ |
| 198 | if (cpu_profiler->is_profiling()) { \ |
| 199 | cpu_profiler->Call; \ |
| 200 | } \ |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 201 | } while (false) |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 202 | |
| 203 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 204 | class CpuProfiler { |
| 205 | public: |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 206 | explicit CpuProfiler(Isolate* isolate); |
| 207 | ~CpuProfiler(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 208 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 209 | void StartProfiling(const char* title, bool record_samples = false); |
| 210 | void StartProfiling(String* title, bool record_samples); |
| 211 | CpuProfile* StopProfiling(const char* title); |
| 212 | CpuProfile* StopProfiling(Object* security_token, String* title); |
| 213 | int GetProfilesCount(); |
| 214 | CpuProfile* GetProfile(Object* security_token, int index); |
| 215 | CpuProfile* FindProfile(Object* security_token, unsigned uid); |
| 216 | void DeleteAllProfiles(); |
| 217 | void DeleteProfile(CpuProfile* profile); |
| 218 | bool HasDetachedProfiles(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 219 | |
| 220 | // Invoked from stack sampler (thread or signal handler.) |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 221 | TickSample* TickSampleEvent(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 222 | |
| 223 | // Must be called via PROFILE macro, otherwise will crash when |
| 224 | // profiling is not enabled. |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 225 | void CallbackEvent(Name* name, Address entry_point); |
| 226 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 227 | Code* code, const char* comment); |
| 228 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 229 | Code* code, Name* name); |
| 230 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 231 | Code* code, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 232 | SharedFunctionInfo* shared, |
| 233 | CompilationInfo* info, |
| 234 | Name* name); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 235 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 236 | Code* code, |
| 237 | SharedFunctionInfo* shared, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 238 | CompilationInfo* info, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 239 | String* source, int line); |
| 240 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 241 | Code* code, int args_count); |
| 242 | void CodeMovingGCEvent() {} |
| 243 | void CodeMoveEvent(Address from, Address to); |
| 244 | void CodeDeleteEvent(Address from); |
| 245 | void GetterCallbackEvent(Name* name, Address entry_point); |
| 246 | void RegExpCodeCreateEvent(Code* code, String* source); |
| 247 | void SetterCallbackEvent(Name* name, Address entry_point); |
| 248 | void SharedFunctionInfoMoveEvent(Address from, Address to); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 249 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 250 | INLINE(bool is_profiling() const) { return is_profiling_; } |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 251 | bool* is_profiling_address() { |
| 252 | return &is_profiling_; |
| 253 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 254 | |
| 255 | private: |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 256 | void StartProcessorIfNotStarted(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 257 | void StopProcessorIfLastProfile(const char* title); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 258 | void StopProcessor(); |
| 259 | void ResetProfiles(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 260 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 261 | Isolate* isolate_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 262 | CpuProfilesCollection* profiles_; |
| 263 | unsigned next_profile_uid_; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 264 | TokenEnumerator* token_enumerator_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 265 | ProfileGenerator* generator_; |
| 266 | ProfilerEventsProcessor* processor_; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 267 | int saved_logging_nesting_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 268 | bool need_to_stop_sampler_; |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 269 | bool is_profiling_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 270 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 271 | private: |
| 272 | DISALLOW_COPY_AND_ASSIGN(CpuProfiler); |
| 273 | }; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 274 | |
| 275 | } } // namespace v8::internal |
| 276 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 277 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 278 | #endif // V8_CPU_PROFILER_H_ |