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 | #include "v8.h" |
| 29 | |
| 30 | #include "cpu-profiler-inl.h" |
| 31 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 32 | #ifdef ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 33 | |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 34 | #include "frames-inl.h" |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 35 | #include "log-inl.h" |
| 36 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 37 | #include "../include/v8-profiler.h" |
| 38 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 42 | static const int kEventsBufferSize = 256*KB; |
| 43 | static const int kTickSamplesBufferChunkSize = 64*KB; |
| 44 | static const int kTickSamplesBufferChunksCount = 16; |
| 45 | |
| 46 | |
| 47 | ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator) |
| 48 | : generator_(generator), |
| 49 | running_(false), |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 50 | ticks_buffer_(sizeof(TickSampleEventRecord), |
| 51 | kTickSamplesBufferChunkSize, |
| 52 | kTickSamplesBufferChunksCount), |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 53 | enqueue_order_(0) { |
| 54 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 55 | |
| 56 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 57 | void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag, |
| 58 | const char* prefix, |
| 59 | String* name, |
| 60 | Address start) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 61 | if (FilterOutCodeCreateEvent(tag)) return; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 62 | CodeEventsContainer evt_rec; |
| 63 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 64 | rec->type = CodeEventRecord::CODE_CREATION; |
| 65 | rec->order = ++enqueue_order_; |
| 66 | rec->start = start; |
| 67 | rec->entry = generator_->NewCodeEntry(tag, prefix, name); |
| 68 | rec->size = 1; |
| 69 | events_buffer_.Enqueue(evt_rec); |
| 70 | } |
| 71 | |
| 72 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 73 | void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 74 | String* name, |
| 75 | String* resource_name, |
| 76 | int line_number, |
| 77 | Address start, |
| 78 | unsigned size) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 79 | if (FilterOutCodeCreateEvent(tag)) return; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 80 | CodeEventsContainer evt_rec; |
| 81 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 82 | rec->type = CodeEventRecord::CODE_CREATION; |
| 83 | rec->order = ++enqueue_order_; |
| 84 | rec->start = start; |
| 85 | rec->entry = generator_->NewCodeEntry(tag, name, resource_name, line_number); |
| 86 | rec->size = size; |
| 87 | events_buffer_.Enqueue(evt_rec); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 92 | const char* name, |
| 93 | Address start, |
| 94 | unsigned size) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 95 | if (FilterOutCodeCreateEvent(tag)) return; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 96 | CodeEventsContainer evt_rec; |
| 97 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 98 | rec->type = CodeEventRecord::CODE_CREATION; |
| 99 | rec->order = ++enqueue_order_; |
| 100 | rec->start = start; |
| 101 | rec->entry = generator_->NewCodeEntry(tag, name); |
| 102 | rec->size = size; |
| 103 | events_buffer_.Enqueue(evt_rec); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 108 | int args_count, |
| 109 | Address start, |
| 110 | unsigned size) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 111 | if (FilterOutCodeCreateEvent(tag)) return; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 112 | CodeEventsContainer evt_rec; |
| 113 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 114 | rec->type = CodeEventRecord::CODE_CREATION; |
| 115 | rec->order = ++enqueue_order_; |
| 116 | rec->start = start; |
| 117 | rec->entry = generator_->NewCodeEntry(tag, args_count); |
| 118 | rec->size = size; |
| 119 | events_buffer_.Enqueue(evt_rec); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void ProfilerEventsProcessor::CodeMoveEvent(Address from, Address to) { |
| 124 | CodeEventsContainer evt_rec; |
| 125 | CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; |
| 126 | rec->type = CodeEventRecord::CODE_MOVE; |
| 127 | rec->order = ++enqueue_order_; |
| 128 | rec->from = from; |
| 129 | rec->to = to; |
| 130 | events_buffer_.Enqueue(evt_rec); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | void ProfilerEventsProcessor::CodeDeleteEvent(Address from) { |
| 135 | CodeEventsContainer evt_rec; |
| 136 | CodeDeleteEventRecord* rec = &evt_rec.CodeDeleteEventRecord_; |
| 137 | rec->type = CodeEventRecord::CODE_DELETE; |
| 138 | rec->order = ++enqueue_order_; |
| 139 | rec->start = from; |
| 140 | events_buffer_.Enqueue(evt_rec); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void ProfilerEventsProcessor::FunctionCreateEvent(Address alias, |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 145 | Address start, |
| 146 | int security_token_id) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 147 | CodeEventsContainer evt_rec; |
| 148 | CodeAliasEventRecord* rec = &evt_rec.CodeAliasEventRecord_; |
| 149 | rec->type = CodeEventRecord::CODE_ALIAS; |
| 150 | rec->order = ++enqueue_order_; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 151 | rec->start = alias; |
| 152 | rec->entry = generator_->NewCodeEntry(security_token_id); |
| 153 | rec->code_start = start; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 154 | events_buffer_.Enqueue(evt_rec); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | void ProfilerEventsProcessor::FunctionMoveEvent(Address from, Address to) { |
| 159 | CodeMoveEvent(from, to); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void ProfilerEventsProcessor::FunctionDeleteEvent(Address from) { |
| 164 | CodeDeleteEvent(from); |
| 165 | } |
| 166 | |
| 167 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 168 | void ProfilerEventsProcessor::RegExpCodeCreateEvent( |
| 169 | Logger::LogEventsAndTags tag, |
| 170 | const char* prefix, |
| 171 | String* name, |
| 172 | Address start, |
| 173 | unsigned size) { |
| 174 | if (FilterOutCodeCreateEvent(tag)) return; |
| 175 | CodeEventsContainer evt_rec; |
| 176 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 177 | rec->type = CodeEventRecord::CODE_CREATION; |
| 178 | rec->order = ++enqueue_order_; |
| 179 | rec->start = start; |
| 180 | rec->entry = generator_->NewCodeEntry(tag, prefix, name); |
| 181 | rec->size = size; |
| 182 | events_buffer_.Enqueue(evt_rec); |
| 183 | } |
| 184 | |
| 185 | |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 186 | void ProfilerEventsProcessor::AddCurrentStack() { |
| 187 | TickSampleEventRecord record; |
| 188 | TickSample* sample = &record.sample; |
| 189 | sample->state = VMState::current_state(); |
| 190 | sample->pc = reinterpret_cast<Address>(sample); // Not NULL. |
| 191 | sample->frames_count = 0; |
| 192 | for (StackTraceFrameIterator it; |
| 193 | !it.done() && sample->frames_count < TickSample::kMaxFramesCount; |
| 194 | it.Advance()) { |
| 195 | JavaScriptFrame* frame = it.frame(); |
| 196 | sample->stack[sample->frames_count++] = |
| 197 | reinterpret_cast<Address>(frame->function()); |
| 198 | } |
| 199 | record.order = enqueue_order_; |
| 200 | ticks_from_vm_buffer_.Enqueue(record); |
| 201 | } |
| 202 | |
| 203 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 204 | bool ProfilerEventsProcessor::ProcessCodeEvent(unsigned* dequeue_order) { |
| 205 | if (!events_buffer_.IsEmpty()) { |
| 206 | CodeEventsContainer record; |
| 207 | events_buffer_.Dequeue(&record); |
| 208 | switch (record.generic.type) { |
| 209 | #define PROFILER_TYPE_CASE(type, clss) \ |
| 210 | case CodeEventRecord::type: \ |
| 211 | record.clss##_.UpdateCodeMap(generator_->code_map()); \ |
| 212 | break; |
| 213 | |
| 214 | CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE) |
| 215 | |
| 216 | #undef PROFILER_TYPE_CASE |
| 217 | default: return true; // Skip record. |
| 218 | } |
| 219 | *dequeue_order = record.generic.order; |
| 220 | return true; |
| 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) { |
| 227 | while (true) { |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 228 | if (!ticks_from_vm_buffer_.IsEmpty() |
| 229 | && ticks_from_vm_buffer_.Peek()->order == dequeue_order) { |
| 230 | TickSampleEventRecord record; |
| 231 | ticks_from_vm_buffer_.Dequeue(&record); |
| 232 | generator_->RecordTickSample(record.sample); |
| 233 | } |
| 234 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 235 | const TickSampleEventRecord* rec = |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 236 | TickSampleEventRecord::cast(ticks_buffer_.StartDequeue()); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 237 | if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 238 | if (rec->order == dequeue_order) { |
| 239 | generator_->RecordTickSample(rec->sample); |
| 240 | ticks_buffer_.FinishDequeue(); |
| 241 | } else { |
| 242 | return true; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | |
| 248 | void ProfilerEventsProcessor::Run() { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 249 | unsigned dequeue_order = 0; |
| 250 | running_ = true; |
| 251 | |
| 252 | while (running_) { |
| 253 | // Process ticks until we have any. |
| 254 | if (ProcessTicks(dequeue_order)) { |
| 255 | // All ticks of the current dequeue_order are processed, |
| 256 | // proceed to the next code event. |
| 257 | ProcessCodeEvent(&dequeue_order); |
| 258 | } |
| 259 | YieldCPU(); |
| 260 | } |
| 261 | |
| 262 | // Process remaining tick events. |
| 263 | ticks_buffer_.FlushResidualRecords(); |
| 264 | // Perform processing until we have tick events, skip remaining code events. |
| 265 | while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 269 | CpuProfiler* CpuProfiler::singleton_ = NULL; |
| 270 | |
| 271 | void CpuProfiler::StartProfiling(const char* title) { |
| 272 | ASSERT(singleton_ != NULL); |
| 273 | singleton_->StartCollectingProfile(title); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | void CpuProfiler::StartProfiling(String* title) { |
| 278 | ASSERT(singleton_ != NULL); |
| 279 | singleton_->StartCollectingProfile(title); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | CpuProfile* CpuProfiler::StopProfiling(const char* title) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 284 | return is_profiling() ? singleton_->StopCollectingProfile(title) : NULL; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 288 | CpuProfile* CpuProfiler::StopProfiling(Object* security_token, String* title) { |
| 289 | return is_profiling() ? |
| 290 | singleton_->StopCollectingProfile(security_token, title) : NULL; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
| 294 | int CpuProfiler::GetProfilesCount() { |
| 295 | ASSERT(singleton_ != NULL); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 296 | // The count of profiles doesn't depend on a security token. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 297 | return singleton_->profiles_->Profiles( |
| 298 | TokenEnumerator::kNoSecurityToken)->length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 302 | CpuProfile* CpuProfiler::GetProfile(Object* security_token, int index) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 303 | ASSERT(singleton_ != NULL); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 304 | const int token = singleton_->token_enumerator_->GetTokenId(security_token); |
| 305 | return singleton_->profiles_->Profiles(token)->at(index); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 309 | CpuProfile* CpuProfiler::FindProfile(Object* security_token, unsigned uid) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 310 | ASSERT(singleton_ != NULL); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 311 | const int token = singleton_->token_enumerator_->GetTokenId(security_token); |
| 312 | return singleton_->profiles_->GetProfile(token, uid); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
| 316 | TickSample* CpuProfiler::TickSampleEvent() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 317 | if (CpuProfiler::is_profiling()) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 318 | return singleton_->processor_->TickSampleEvent(); |
| 319 | } else { |
| 320 | return NULL; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | |
| 325 | void CpuProfiler::CallbackEvent(String* name, Address entry_point) { |
| 326 | singleton_->processor_->CallbackCreateEvent( |
| 327 | Logger::CALLBACK_TAG, CodeEntry::kEmptyNamePrefix, name, entry_point); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 332 | Code* code, const char* comment) { |
| 333 | singleton_->processor_->CodeCreateEvent( |
| 334 | tag, comment, code->address(), code->ExecutableSize()); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 339 | Code* code, String* name) { |
| 340 | singleton_->processor_->CodeCreateEvent( |
| 341 | tag, |
| 342 | name, |
| 343 | Heap::empty_string(), |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 344 | v8::CpuProfileNode::kNoLineNumberInfo, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 345 | code->address(), |
| 346 | code->ExecutableSize()); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 351 | Code* code, String* name, |
| 352 | String* source, int line) { |
| 353 | singleton_->processor_->CodeCreateEvent( |
| 354 | tag, |
| 355 | name, |
| 356 | source, |
| 357 | line, |
| 358 | code->address(), |
| 359 | code->ExecutableSize()); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 364 | Code* code, int args_count) { |
| 365 | singleton_->processor_->CodeCreateEvent( |
| 366 | tag, |
| 367 | args_count, |
| 368 | code->address(), |
| 369 | code->ExecutableSize()); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | void CpuProfiler::CodeMoveEvent(Address from, Address to) { |
| 374 | singleton_->processor_->CodeMoveEvent(from, to); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | void CpuProfiler::CodeDeleteEvent(Address from) { |
| 379 | singleton_->processor_->CodeDeleteEvent(from); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | void CpuProfiler::FunctionCreateEvent(JSFunction* function) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 384 | int security_token_id = TokenEnumerator::kNoSecurityToken; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 385 | if (function->unchecked_context()->IsContext()) { |
| 386 | security_token_id = singleton_->token_enumerator_->GetTokenId( |
| 387 | function->context()->global_context()->security_token()); |
| 388 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 389 | singleton_->processor_->FunctionCreateEvent( |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 390 | function->address(), |
| 391 | function->code()->address(), |
| 392 | security_token_id); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
| 396 | void CpuProfiler::FunctionMoveEvent(Address from, Address to) { |
| 397 | singleton_->processor_->FunctionMoveEvent(from, to); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | void CpuProfiler::FunctionDeleteEvent(Address from) { |
| 402 | singleton_->processor_->FunctionDeleteEvent(from); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | void CpuProfiler::GetterCallbackEvent(String* name, Address entry_point) { |
| 407 | singleton_->processor_->CallbackCreateEvent( |
| 408 | Logger::CALLBACK_TAG, "get ", name, entry_point); |
| 409 | } |
| 410 | |
| 411 | |
| 412 | void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 413 | singleton_->processor_->RegExpCodeCreateEvent( |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 414 | Logger::REG_EXP_TAG, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 415 | "RegExp: ", |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 416 | source, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 417 | code->address(), |
| 418 | code->ExecutableSize()); |
| 419 | } |
| 420 | |
| 421 | |
| 422 | void CpuProfiler::SetterCallbackEvent(String* name, Address entry_point) { |
| 423 | singleton_->processor_->CallbackCreateEvent( |
| 424 | Logger::CALLBACK_TAG, "set ", name, entry_point); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | CpuProfiler::CpuProfiler() |
| 429 | : profiles_(new CpuProfilesCollection()), |
| 430 | next_profile_uid_(1), |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 431 | token_enumerator_(new TokenEnumerator()), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 432 | generator_(NULL), |
| 433 | processor_(NULL) { |
| 434 | } |
| 435 | |
| 436 | |
| 437 | CpuProfiler::~CpuProfiler() { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 438 | delete token_enumerator_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 439 | delete profiles_; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | void CpuProfiler::StartCollectingProfile(const char* title) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 444 | if (profiles_->StartProfiling(title, next_profile_uid_++)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 445 | StartProcessorIfNotStarted(); |
| 446 | } |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 447 | processor_->AddCurrentStack(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | |
| 451 | void CpuProfiler::StartCollectingProfile(String* title) { |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 452 | StartCollectingProfile(profiles_->GetName(title)); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
| 456 | void CpuProfiler::StartProcessorIfNotStarted() { |
| 457 | if (processor_ == NULL) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 458 | // Disable logging when using the new implementation. |
| 459 | saved_logging_nesting_ = Logger::logging_nesting_; |
| 460 | Logger::logging_nesting_ = 0; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 461 | generator_ = new ProfileGenerator(profiles_); |
| 462 | processor_ = new ProfilerEventsProcessor(generator_); |
| 463 | processor_->Start(); |
| 464 | // Enumerate stuff we already have in the heap. |
| 465 | if (Heap::HasBeenSetup()) { |
| 466 | Logger::LogCodeObjects(); |
| 467 | Logger::LogCompiledFunctions(); |
| 468 | Logger::LogFunctionObjects(); |
| 469 | Logger::LogAccessorCallbacks(); |
| 470 | } |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 471 | // Enable stack sampling. |
| 472 | reinterpret_cast<Sampler*>(Logger::ticker_)->Start(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | |
| 477 | CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 478 | const double actual_sampling_rate = generator_->actual_sampling_rate(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 479 | StopProcessorIfLastProfile(); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 480 | CpuProfile* result = |
| 481 | profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken, |
| 482 | title, |
| 483 | actual_sampling_rate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 484 | if (result != NULL) { |
| 485 | result->Print(); |
| 486 | } |
| 487 | return result; |
| 488 | } |
| 489 | |
| 490 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 491 | CpuProfile* CpuProfiler::StopCollectingProfile(Object* security_token, |
| 492 | String* title) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 493 | const double actual_sampling_rate = generator_->actual_sampling_rate(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 494 | StopProcessorIfLastProfile(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 495 | int token = token_enumerator_->GetTokenId(security_token); |
| 496 | return profiles_->StopProfiling(token, title, actual_sampling_rate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | |
| 500 | void CpuProfiler::StopProcessorIfLastProfile() { |
| 501 | if (profiles_->is_last_profile()) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 502 | reinterpret_cast<Sampler*>(Logger::ticker_)->Stop(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 503 | processor_->Stop(); |
| 504 | processor_->Join(); |
| 505 | delete processor_; |
| 506 | delete generator_; |
| 507 | processor_ = NULL; |
| 508 | generator_ = NULL; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 509 | Logger::logging_nesting_ = saved_logging_nesting_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | } } // namespace v8::internal |
| 514 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 515 | #endif // ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 516 | |
| 517 | namespace v8 { |
| 518 | namespace internal { |
| 519 | |
| 520 | void CpuProfiler::Setup() { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 521 | #ifdef ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 522 | if (singleton_ == NULL) { |
| 523 | singleton_ = new CpuProfiler(); |
| 524 | } |
| 525 | #endif |
| 526 | } |
| 527 | |
| 528 | |
| 529 | void CpuProfiler::TearDown() { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 530 | #ifdef ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 531 | if (singleton_ != NULL) { |
| 532 | delete singleton_; |
| 533 | } |
| 534 | singleton_ = NULL; |
| 535 | #endif |
| 536 | } |
| 537 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 538 | } } // namespace v8::internal |