whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 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_CPU_PROFILER_H_ |
| 29 | #define V8_CPU_PROFILER_H_ |
| 30 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 31 | #ifdef ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 32 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 33 | #include "atomicops.h" |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 34 | #include "circular-queue.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; |
| 43 | class CpuProfile; |
| 44 | class CpuProfilesCollection; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 45 | class HashMap; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 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 | |
| 49 | #define CODE_EVENTS_TYPE_LIST(V) \ |
| 50 | V(CODE_CREATION, CodeCreateEventRecord) \ |
| 51 | V(CODE_MOVE, CodeMoveEventRecord) \ |
| 52 | V(CODE_DELETE, CodeDeleteEventRecord) \ |
| 53 | V(CODE_ALIAS, CodeAliasEventRecord) |
| 54 | |
| 55 | |
| 56 | class CodeEventRecord { |
| 57 | public: |
| 58 | #define DECLARE_TYPE(type, ignore) type, |
| 59 | enum Type { |
| 60 | NONE = 0, |
| 61 | CODE_EVENTS_TYPE_LIST(DECLARE_TYPE) |
| 62 | NUMBER_OF_TYPES |
| 63 | }; |
| 64 | #undef DECLARE_TYPE |
| 65 | |
| 66 | Type type; |
| 67 | unsigned order; |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | class CodeCreateEventRecord : public CodeEventRecord { |
| 72 | public: |
| 73 | Address start; |
| 74 | CodeEntry* entry; |
| 75 | unsigned size; |
| 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 | |
| 90 | class CodeDeleteEventRecord : public CodeEventRecord { |
| 91 | public: |
| 92 | Address start; |
| 93 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 94 | INLINE(void UpdateCodeMap(CodeMap* code_map)); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | |
| 98 | class CodeAliasEventRecord : public CodeEventRecord { |
| 99 | public: |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 100 | Address start; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 101 | CodeEntry* entry; |
| 102 | Address code_start; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 103 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 104 | INLINE(void UpdateCodeMap(CodeMap* code_map)); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 108 | class TickSampleEventRecord BASE_EMBEDDED { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 109 | public: |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 110 | TickSampleEventRecord() |
| 111 | : filler(1) { |
| 112 | ASSERT(filler != SamplingCircularQueue::kClear); |
| 113 | } |
| 114 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 115 | // The first machine word of a TickSampleEventRecord must not ever |
| 116 | // become equal to SamplingCircularQueue::kClear. As both order and |
| 117 | // TickSample's first field are not reliable in this sense (order |
| 118 | // can overflow, TickSample can have all fields reset), we are |
| 119 | // forced to use an artificial filler field. |
| 120 | int filler; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 121 | unsigned order; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 122 | TickSample sample; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 123 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 124 | static TickSampleEventRecord* cast(void* value) { |
| 125 | return reinterpret_cast<TickSampleEventRecord*>(value); |
| 126 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 127 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 128 | INLINE(static TickSampleEventRecord* init(void* value)); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | |
| 132 | // This class implements both the profile events processor thread and |
| 133 | // methods called by event producers: VM and stack sampler threads. |
| 134 | class ProfilerEventsProcessor : public Thread { |
| 135 | public: |
| 136 | explicit ProfilerEventsProcessor(ProfileGenerator* generator); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 137 | virtual ~ProfilerEventsProcessor(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 138 | |
| 139 | // Thread control. |
| 140 | virtual void Run(); |
| 141 | inline void Stop() { running_ = false; } |
| 142 | INLINE(bool running()) { return running_; } |
| 143 | |
| 144 | // Events adding methods. Called by VM threads. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 145 | void CallbackCreateEvent(Logger::LogEventsAndTags tag, |
| 146 | const char* prefix, String* name, |
| 147 | Address start); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 148 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 149 | String* name, |
| 150 | String* resource_name, int line_number, |
| 151 | Address start, unsigned size); |
| 152 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 153 | const char* name, |
| 154 | Address start, unsigned size); |
| 155 | void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 156 | int args_count, |
| 157 | Address start, unsigned size); |
| 158 | void CodeMoveEvent(Address from, Address to); |
| 159 | void CodeDeleteEvent(Address from); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 160 | void FunctionCreateEvent(Address alias, Address start, int security_token_id); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 161 | void FunctionMoveEvent(Address from, Address to); |
| 162 | void FunctionDeleteEvent(Address from); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 163 | void RegExpCodeCreateEvent(Logger::LogEventsAndTags tag, |
| 164 | const char* prefix, String* name, |
| 165 | Address start, unsigned size); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 166 | // Puts current stack into tick sample events buffer. |
| 167 | void AddCurrentStack(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 168 | bool IsKnownFunction(Address start); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 169 | void ProcessMovedFunctions(); |
| 170 | void RememberMovedFunction(JSFunction* function); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 171 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 172 | // Tick sample events are filled directly in the buffer of the circular |
| 173 | // queue (because the structure is of fixed width, but usually not all |
| 174 | // stack frame entries are filled.) This method returns a pointer to the |
| 175 | // next record of the buffer. |
| 176 | INLINE(TickSample* TickSampleEvent()); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 177 | |
| 178 | private: |
| 179 | union CodeEventsContainer { |
| 180 | CodeEventRecord generic; |
| 181 | #define DECLARE_CLASS(ignore, type) type type##_; |
| 182 | CODE_EVENTS_TYPE_LIST(DECLARE_CLASS) |
| 183 | #undef DECLARE_TYPE |
| 184 | }; |
| 185 | |
| 186 | // Called from events processing thread (Run() method.) |
| 187 | bool ProcessCodeEvent(unsigned* dequeue_order); |
| 188 | bool ProcessTicks(unsigned dequeue_order); |
| 189 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 190 | INLINE(static bool FilterOutCodeCreateEvent(Logger::LogEventsAndTags tag)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 191 | INLINE(static bool AddressesMatch(void* key1, void* key2)) { |
| 192 | return key1 == key2; |
| 193 | } |
| 194 | INLINE(static uint32_t AddressHash(Address addr)) { |
| 195 | return ComputeIntegerHash( |
| 196 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(addr))); |
| 197 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 198 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 199 | ProfileGenerator* generator_; |
| 200 | bool running_; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 201 | UnboundQueue<CodeEventsContainer> events_buffer_; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 202 | SamplingCircularQueue ticks_buffer_; |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 203 | UnboundQueue<TickSampleEventRecord> ticks_from_vm_buffer_; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 204 | unsigned enqueue_order_; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 205 | |
| 206 | // Used from the VM thread. |
| 207 | HashMap* known_functions_; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 208 | List<JSFunction*> moved_functions_; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 211 | } } // namespace v8::internal |
| 212 | |
| 213 | |
| 214 | #define PROFILE(Call) \ |
| 215 | LOG(Call); \ |
| 216 | do { \ |
| 217 | if (v8::internal::CpuProfiler::is_profiling()) { \ |
| 218 | v8::internal::CpuProfiler::Call; \ |
| 219 | } \ |
| 220 | } while (false) |
| 221 | #else |
| 222 | #define PROFILE(Call) LOG(Call) |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 223 | #endif // ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 224 | |
| 225 | |
| 226 | namespace v8 { |
| 227 | namespace internal { |
| 228 | |
| 229 | class CpuProfiler { |
| 230 | public: |
| 231 | static void Setup(); |
| 232 | static void TearDown(); |
| 233 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 234 | #ifdef ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 235 | static void StartProfiling(const char* title); |
| 236 | static void StartProfiling(String* title); |
| 237 | static CpuProfile* StopProfiling(const char* title); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 238 | static CpuProfile* StopProfiling(Object* security_token, String* title); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 239 | static int GetProfilesCount(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 240 | static CpuProfile* GetProfile(Object* security_token, int index); |
| 241 | static CpuProfile* FindProfile(Object* security_token, unsigned uid); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 242 | |
| 243 | // Invoked from stack sampler (thread or signal handler.) |
| 244 | static TickSample* TickSampleEvent(); |
| 245 | |
| 246 | // Must be called via PROFILE macro, otherwise will crash when |
| 247 | // profiling is not enabled. |
| 248 | static void CallbackEvent(String* name, Address entry_point); |
| 249 | static void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 250 | Code* code, const char* comment); |
| 251 | static void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 252 | Code* code, String* name); |
| 253 | static void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 254 | Code* code, String* name, |
| 255 | String* source, int line); |
| 256 | static void CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 257 | Code* code, int args_count); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 258 | static void CodeMovingGCEvent() {} |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 259 | static void CodeMoveEvent(Address from, Address to); |
| 260 | static void CodeDeleteEvent(Address from); |
| 261 | static void FunctionCreateEvent(JSFunction* function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 262 | // Reports function creation in case we had missed it (e.g. |
| 263 | // if it was created from compiled code). |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 264 | static void FunctionCreateEventFromMove(JSFunction* function); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 265 | static void FunctionMoveEvent(Address from, Address to); |
| 266 | static void FunctionDeleteEvent(Address from); |
| 267 | static void GetterCallbackEvent(String* name, Address entry_point); |
| 268 | static void RegExpCodeCreateEvent(Code* code, String* source); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 269 | static void ProcessMovedFunctions(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 270 | static void SetterCallbackEvent(String* name, Address entry_point); |
| 271 | |
| 272 | static INLINE(bool is_profiling()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 273 | return NoBarrier_Load(&is_profiling_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | private: |
| 277 | CpuProfiler(); |
| 278 | ~CpuProfiler(); |
| 279 | void StartCollectingProfile(const char* title); |
| 280 | void StartCollectingProfile(String* title); |
| 281 | void StartProcessorIfNotStarted(); |
| 282 | CpuProfile* StopCollectingProfile(const char* title); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 283 | CpuProfile* StopCollectingProfile(Object* security_token, String* title); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 284 | void StopProcessorIfLastProfile(const char* title); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 285 | |
| 286 | CpuProfilesCollection* profiles_; |
| 287 | unsigned next_profile_uid_; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 288 | TokenEnumerator* token_enumerator_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 289 | ProfileGenerator* generator_; |
| 290 | ProfilerEventsProcessor* processor_; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 291 | int saved_logging_nesting_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 292 | |
| 293 | static CpuProfiler* singleton_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 294 | static Atomic32 is_profiling_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 295 | |
| 296 | #else |
| 297 | static INLINE(bool is_profiling()) { return false; } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 298 | #endif // ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 299 | |
| 300 | private: |
| 301 | DISALLOW_COPY_AND_ASSIGN(CpuProfiler); |
| 302 | }; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 303 | |
| 304 | } } // namespace v8::internal |
| 305 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 306 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 307 | #endif // V8_CPU_PROFILER_H_ |