OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 YieldCPU(); | 251 YieldCPU(); |
252 } | 252 } |
253 | 253 |
254 // Process remaining tick events. | 254 // Process remaining tick events. |
255 ticks_buffer_.FlushResidualRecords(); | 255 ticks_buffer_.FlushResidualRecords(); |
256 // Perform processing until we have tick events, skip remaining code events. | 256 // Perform processing until we have tick events, skip remaining code events. |
257 while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { } | 257 while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { } |
258 } | 258 } |
259 | 259 |
260 | 260 |
261 void CpuProfiler::StartProfiling(const char* title) { | |
262 ASSERT(Isolate::Current()->cpu_profiler() != NULL); | |
263 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title, false); | |
264 } | |
265 | |
266 | |
267 void CpuProfiler::StartProfiling(String* title, bool record_samples) { | |
268 ASSERT(Isolate::Current()->cpu_profiler() != NULL); | |
269 Isolate::Current()->cpu_profiler()->StartCollectingProfile( | |
270 title, record_samples); | |
271 } | |
272 | |
273 | |
274 CpuProfile* CpuProfiler::StopProfiling(const char* title) { | |
275 Isolate* isolate = Isolate::Current(); | |
276 return is_profiling(isolate) ? | |
277 isolate->cpu_profiler()->StopCollectingProfile(title) : NULL; | |
278 } | |
279 | |
280 | |
281 CpuProfile* CpuProfiler::StopProfiling(Object* security_token, String* title) { | |
282 Isolate* isolate = Isolate::Current(); | |
283 return is_profiling(isolate) ? | |
284 isolate->cpu_profiler()->StopCollectingProfile( | |
285 security_token, title) : NULL; | |
286 } | |
287 | |
288 | |
289 int CpuProfiler::GetProfilesCount() { | 261 int CpuProfiler::GetProfilesCount() { |
290 ASSERT(Isolate::Current()->cpu_profiler() != NULL); | |
291 // The count of profiles doesn't depend on a security token. | 262 // The count of profiles doesn't depend on a security token. |
292 return Isolate::Current()->cpu_profiler()->profiles_->Profiles( | 263 return profiles_->Profiles(TokenEnumerator::kNoSecurityToken)->length(); |
293 TokenEnumerator::kNoSecurityToken)->length(); | |
294 } | 264 } |
295 | 265 |
296 | 266 |
297 CpuProfile* CpuProfiler::GetProfile(Object* security_token, int index) { | 267 CpuProfile* CpuProfiler::GetProfile(Object* security_token, int index) { |
298 ASSERT(Isolate::Current()->cpu_profiler() != NULL); | 268 const int token = token_enumerator_->GetTokenId(security_token); |
299 CpuProfiler* profiler = Isolate::Current()->cpu_profiler(); | 269 return profiles_->Profiles(token)->at(index); |
300 const int token = profiler->token_enumerator_->GetTokenId(security_token); | |
301 return profiler->profiles_->Profiles(token)->at(index); | |
302 } | 270 } |
303 | 271 |
304 | 272 |
305 CpuProfile* CpuProfiler::FindProfile(Object* security_token, unsigned uid) { | 273 CpuProfile* CpuProfiler::FindProfile(Object* security_token, unsigned uid) { |
306 ASSERT(Isolate::Current()->cpu_profiler() != NULL); | 274 const int token = token_enumerator_->GetTokenId(security_token); |
307 CpuProfiler* profiler = Isolate::Current()->cpu_profiler(); | 275 return profiles_->GetProfile(token, uid); |
308 const int token = profiler->token_enumerator_->GetTokenId(security_token); | |
309 return profiler->profiles_->GetProfile(token, uid); | |
310 } | 276 } |
311 | 277 |
312 | 278 |
313 TickSample* CpuProfiler::TickSampleEvent(Isolate* isolate) { | 279 TickSample* CpuProfiler::TickSampleEvent() { |
314 if (CpuProfiler::is_profiling(isolate)) { | 280 if (is_profiling_) return processor_->TickSampleEvent(); |
315 return isolate->cpu_profiler()->processor_->TickSampleEvent(); | 281 return NULL; |
316 } else { | |
317 return NULL; | |
318 } | |
319 } | 282 } |
320 | 283 |
321 | 284 |
322 void CpuProfiler::DeleteAllProfiles() { | 285 void CpuProfiler::DeleteAllProfiles() { |
323 Isolate* isolate = Isolate::Current(); | 286 if (is_profiling_) StopProcessor(); |
324 ASSERT(isolate->cpu_profiler() != NULL); | 287 ResetProfiles(); |
325 if (is_profiling(isolate)) { | |
326 isolate->cpu_profiler()->StopProcessor(); | |
327 } | |
328 isolate->cpu_profiler()->ResetProfiles(); | |
329 } | 288 } |
330 | 289 |
331 | 290 |
332 void CpuProfiler::DeleteProfile(CpuProfile* profile) { | 291 void CpuProfiler::DeleteProfile(CpuProfile* profile) { |
333 ASSERT(Isolate::Current()->cpu_profiler() != NULL); | 292 profiles_->RemoveProfile(profile); |
334 Isolate::Current()->cpu_profiler()->profiles_->RemoveProfile(profile); | |
335 delete profile; | 293 delete profile; |
336 } | 294 } |
337 | 295 |
338 | 296 |
339 bool CpuProfiler::HasDetachedProfiles() { | 297 bool CpuProfiler::HasDetachedProfiles() { |
340 ASSERT(Isolate::Current()->cpu_profiler() != NULL); | 298 return profiles_->HasDetachedProfiles(); |
341 return Isolate::Current()->cpu_profiler()->profiles_->HasDetachedProfiles(); | |
342 } | 299 } |
343 | 300 |
344 | 301 |
345 void CpuProfiler::CallbackEvent(Name* name, Address entry_point) { | 302 void CpuProfiler::CallbackEvent(Name* name, Address entry_point) { |
346 Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent( | 303 processor_->CallbackCreateEvent( |
347 Logger::CALLBACK_TAG, CodeEntry::kEmptyNamePrefix, name, entry_point); | 304 Logger::CALLBACK_TAG, CodeEntry::kEmptyNamePrefix, name, entry_point); |
348 } | 305 } |
349 | 306 |
350 | 307 |
351 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 308 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
352 Code* code, const char* comment) { | 309 Code* code, const char* comment) { |
353 Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent( | 310 processor_->CodeCreateEvent( |
354 tag, comment, code->address(), code->ExecutableSize()); | 311 tag, comment, code->address(), code->ExecutableSize()); |
355 } | 312 } |
356 | 313 |
357 | 314 |
358 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 315 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
359 Code* code, Name* name) { | 316 Code* code, Name* name) { |
360 Isolate* isolate = Isolate::Current(); | 317 processor_->CodeCreateEvent( |
361 isolate->cpu_profiler()->processor_->CodeCreateEvent( | |
362 tag, | 318 tag, |
363 name, | 319 name, |
364 isolate->heap()->empty_string(), | 320 isolate_->heap()->empty_string(), |
365 v8::CpuProfileNode::kNoLineNumberInfo, | 321 v8::CpuProfileNode::kNoLineNumberInfo, |
366 code->address(), | 322 code->address(), |
367 code->ExecutableSize(), | 323 code->ExecutableSize(), |
368 NULL); | 324 NULL); |
369 } | 325 } |
370 | 326 |
371 | 327 |
372 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 328 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
373 Code* code, | 329 Code* code, |
374 SharedFunctionInfo* shared, | 330 SharedFunctionInfo* shared, |
375 Name* name) { | 331 Name* name) { |
376 Isolate* isolate = Isolate::Current(); | 332 processor_->CodeCreateEvent( |
377 isolate->cpu_profiler()->processor_->CodeCreateEvent( | |
378 tag, | 333 tag, |
379 name, | 334 name, |
380 isolate->heap()->empty_string(), | 335 isolate_->heap()->empty_string(), |
381 v8::CpuProfileNode::kNoLineNumberInfo, | 336 v8::CpuProfileNode::kNoLineNumberInfo, |
382 code->address(), | 337 code->address(), |
383 code->ExecutableSize(), | 338 code->ExecutableSize(), |
384 shared->address()); | 339 shared->address()); |
385 } | 340 } |
386 | 341 |
387 | 342 |
388 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 343 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
389 Code* code, | 344 Code* code, |
390 SharedFunctionInfo* shared, | 345 SharedFunctionInfo* shared, |
391 String* source, int line) { | 346 String* source, int line) { |
392 Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent( | 347 processor_->CodeCreateEvent( |
393 tag, | 348 tag, |
394 shared->DebugName(), | 349 shared->DebugName(), |
395 source, | 350 source, |
396 line, | 351 line, |
397 code->address(), | 352 code->address(), |
398 code->ExecutableSize(), | 353 code->ExecutableSize(), |
399 shared->address()); | 354 shared->address()); |
400 } | 355 } |
401 | 356 |
402 | 357 |
403 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 358 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
404 Code* code, int args_count) { | 359 Code* code, int args_count) { |
405 Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent( | 360 processor_->CodeCreateEvent( |
406 tag, | 361 tag, |
407 args_count, | 362 args_count, |
408 code->address(), | 363 code->address(), |
409 code->ExecutableSize()); | 364 code->ExecutableSize()); |
410 } | 365 } |
411 | 366 |
412 | 367 |
413 void CpuProfiler::CodeMoveEvent(Address from, Address to) { | 368 void CpuProfiler::CodeMoveEvent(Address from, Address to) { |
414 Isolate::Current()->cpu_profiler()->processor_->CodeMoveEvent(from, to); | 369 processor_->CodeMoveEvent(from, to); |
415 } | 370 } |
416 | 371 |
417 | 372 |
418 void CpuProfiler::CodeDeleteEvent(Address from) { | 373 void CpuProfiler::CodeDeleteEvent(Address from) { |
419 } | 374 } |
420 | 375 |
421 | 376 |
422 void CpuProfiler::SharedFunctionInfoMoveEvent(Address from, Address to) { | 377 void CpuProfiler::SharedFunctionInfoMoveEvent(Address from, Address to) { |
423 CpuProfiler* profiler = Isolate::Current()->cpu_profiler(); | 378 processor_->SharedFunctionInfoMoveEvent(from, to); |
424 profiler->processor_->SharedFunctionInfoMoveEvent(from, to); | |
425 } | 379 } |
426 | 380 |
427 | 381 |
428 void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) { | 382 void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) { |
429 Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent( | 383 processor_->CallbackCreateEvent( |
430 Logger::CALLBACK_TAG, "get ", name, entry_point); | 384 Logger::CALLBACK_TAG, "get ", name, entry_point); |
431 } | 385 } |
432 | 386 |
433 | 387 |
434 void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) { | 388 void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) { |
435 Isolate::Current()->cpu_profiler()->processor_->RegExpCodeCreateEvent( | 389 processor_->RegExpCodeCreateEvent( |
436 Logger::REG_EXP_TAG, | 390 Logger::REG_EXP_TAG, |
437 "RegExp: ", | 391 "RegExp: ", |
438 source, | 392 source, |
439 code->address(), | 393 code->address(), |
440 code->ExecutableSize()); | 394 code->ExecutableSize()); |
441 } | 395 } |
442 | 396 |
443 | 397 |
444 void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) { | 398 void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) { |
445 Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent( | 399 processor_->CallbackCreateEvent( |
446 Logger::CALLBACK_TAG, "set ", name, entry_point); | 400 Logger::CALLBACK_TAG, "set ", name, entry_point); |
447 } | 401 } |
448 | 402 |
449 | 403 |
450 CpuProfiler::CpuProfiler() | 404 CpuProfiler::CpuProfiler(Isolate* isolate) |
451 : profiles_(new CpuProfilesCollection()), | 405 : isolate_(isolate), |
| 406 profiles_(new CpuProfilesCollection()), |
452 next_profile_uid_(1), | 407 next_profile_uid_(1), |
453 token_enumerator_(new TokenEnumerator()), | 408 token_enumerator_(new TokenEnumerator()), |
454 generator_(NULL), | 409 generator_(NULL), |
455 processor_(NULL), | 410 processor_(NULL), |
456 need_to_stop_sampler_(false), | 411 need_to_stop_sampler_(false), |
457 is_profiling_(false) { | 412 is_profiling_(false) { |
458 } | 413 } |
459 | 414 |
460 | 415 |
461 CpuProfiler::~CpuProfiler() { | 416 CpuProfiler::~CpuProfiler() { |
462 delete token_enumerator_; | 417 delete token_enumerator_; |
463 delete profiles_; | 418 delete profiles_; |
464 } | 419 } |
465 | 420 |
466 | 421 |
467 void CpuProfiler::ResetProfiles() { | 422 void CpuProfiler::ResetProfiles() { |
468 delete profiles_; | 423 delete profiles_; |
469 profiles_ = new CpuProfilesCollection(); | 424 profiles_ = new CpuProfilesCollection(); |
470 } | 425 } |
471 | 426 |
472 void CpuProfiler::StartCollectingProfile(const char* title, | 427 void CpuProfiler::StartProfiling(const char* title, bool record_samples) { |
473 bool record_samples) { | |
474 if (profiles_->StartProfiling(title, next_profile_uid_++, record_samples)) { | 428 if (profiles_->StartProfiling(title, next_profile_uid_++, record_samples)) { |
475 StartProcessorIfNotStarted(); | 429 StartProcessorIfNotStarted(); |
476 } | 430 } |
477 processor_->AddCurrentStack(); | 431 processor_->AddCurrentStack(); |
478 } | 432 } |
479 | 433 |
480 | 434 |
481 void CpuProfiler::StartCollectingProfile(String* title, bool record_samples) { | 435 void CpuProfiler::StartProfiling(String* title, bool record_samples) { |
482 StartCollectingProfile(profiles_->GetName(title), record_samples); | 436 StartProfiling(profiles_->GetName(title), record_samples); |
483 } | 437 } |
484 | 438 |
485 | 439 |
486 void CpuProfiler::StartProcessorIfNotStarted() { | 440 void CpuProfiler::StartProcessorIfNotStarted() { |
487 if (processor_ == NULL) { | 441 if (processor_ == NULL) { |
488 Isolate* isolate = Isolate::Current(); | |
489 | |
490 // Disable logging when using the new implementation. | 442 // Disable logging when using the new implementation. |
491 saved_logging_nesting_ = isolate->logger()->logging_nesting_; | 443 saved_logging_nesting_ = isolate_->logger()->logging_nesting_; |
492 isolate->logger()->logging_nesting_ = 0; | 444 isolate_->logger()->logging_nesting_ = 0; |
493 generator_ = new ProfileGenerator(profiles_); | 445 generator_ = new ProfileGenerator(profiles_); |
494 processor_ = new ProfilerEventsProcessor(generator_); | 446 processor_ = new ProfilerEventsProcessor(generator_); |
495 is_profiling_ = true; | 447 is_profiling_ = true; |
496 processor_->Start(); | 448 processor_->Start(); |
497 // Enumerate stuff we already have in the heap. | 449 // Enumerate stuff we already have in the heap. |
498 if (isolate->heap()->HasBeenSetUp()) { | 450 if (isolate_->heap()->HasBeenSetUp()) { |
499 if (!FLAG_prof_browser_mode) { | 451 if (!FLAG_prof_browser_mode) { |
500 bool saved_log_code_flag = FLAG_log_code; | 452 bool saved_log_code_flag = FLAG_log_code; |
501 FLAG_log_code = true; | 453 FLAG_log_code = true; |
502 isolate->logger()->LogCodeObjects(); | 454 isolate_->logger()->LogCodeObjects(); |
503 FLAG_log_code = saved_log_code_flag; | 455 FLAG_log_code = saved_log_code_flag; |
504 } | 456 } |
505 isolate->logger()->LogCompiledFunctions(); | 457 isolate_->logger()->LogCompiledFunctions(); |
506 isolate->logger()->LogAccessorCallbacks(); | 458 isolate_->logger()->LogAccessorCallbacks(); |
507 } | 459 } |
508 // Enable stack sampling. | 460 // Enable stack sampling. |
509 Sampler* sampler = reinterpret_cast<Sampler*>(isolate->logger()->ticker_); | 461 Sampler* sampler = reinterpret_cast<Sampler*>(isolate_->logger()->ticker_); |
510 if (!sampler->IsActive()) { | 462 if (!sampler->IsActive()) { |
511 sampler->Start(); | 463 sampler->Start(); |
512 need_to_stop_sampler_ = true; | 464 need_to_stop_sampler_ = true; |
513 } | 465 } |
514 sampler->IncreaseProfilingDepth(); | 466 sampler->IncreaseProfilingDepth(); |
515 } | 467 } |
516 } | 468 } |
517 | 469 |
518 | 470 |
519 CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) { | 471 CpuProfile* CpuProfiler::StopProfiling(const char* title) { |
| 472 if (!is_profiling_) return NULL; |
520 const double actual_sampling_rate = generator_->actual_sampling_rate(); | 473 const double actual_sampling_rate = generator_->actual_sampling_rate(); |
521 StopProcessorIfLastProfile(title); | 474 StopProcessorIfLastProfile(title); |
522 CpuProfile* result = | 475 CpuProfile* result = |
523 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken, | 476 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken, |
524 title, | 477 title, |
525 actual_sampling_rate); | 478 actual_sampling_rate); |
526 if (result != NULL) { | 479 if (result != NULL) { |
527 result->Print(); | 480 result->Print(); |
528 } | 481 } |
529 return result; | 482 return result; |
530 } | 483 } |
531 | 484 |
532 | 485 |
533 CpuProfile* CpuProfiler::StopCollectingProfile(Object* security_token, | 486 CpuProfile* CpuProfiler::StopProfiling(Object* security_token, String* title) { |
534 String* title) { | 487 if (!is_profiling_) return NULL; |
535 const double actual_sampling_rate = generator_->actual_sampling_rate(); | 488 const double actual_sampling_rate = generator_->actual_sampling_rate(); |
536 const char* profile_title = profiles_->GetName(title); | 489 const char* profile_title = profiles_->GetName(title); |
537 StopProcessorIfLastProfile(profile_title); | 490 StopProcessorIfLastProfile(profile_title); |
538 int token = token_enumerator_->GetTokenId(security_token); | 491 int token = token_enumerator_->GetTokenId(security_token); |
539 return profiles_->StopProfiling(token, profile_title, actual_sampling_rate); | 492 return profiles_->StopProfiling(token, profile_title, actual_sampling_rate); |
540 } | 493 } |
541 | 494 |
542 | 495 |
543 void CpuProfiler::StopProcessorIfLastProfile(const char* title) { | 496 void CpuProfiler::StopProcessorIfLastProfile(const char* title) { |
544 if (profiles_->IsLastProfile(title)) StopProcessor(); | 497 if (profiles_->IsLastProfile(title)) StopProcessor(); |
545 } | 498 } |
546 | 499 |
547 | 500 |
548 void CpuProfiler::StopProcessor() { | 501 void CpuProfiler::StopProcessor() { |
549 Logger* logger = Isolate::Current()->logger(); | 502 Logger* logger = isolate_->logger(); |
550 Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_); | 503 Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_); |
551 sampler->DecreaseProfilingDepth(); | 504 sampler->DecreaseProfilingDepth(); |
552 if (need_to_stop_sampler_) { | 505 if (need_to_stop_sampler_) { |
553 sampler->Stop(); | 506 sampler->Stop(); |
554 need_to_stop_sampler_ = false; | 507 need_to_stop_sampler_ = false; |
555 } | 508 } |
556 is_profiling_ = false; | 509 is_profiling_ = false; |
557 processor_->Stop(); | 510 processor_->Stop(); |
558 processor_->Join(); | 511 processor_->Join(); |
559 delete processor_; | 512 delete processor_; |
560 delete generator_; | 513 delete generator_; |
561 processor_ = NULL; | 514 processor_ = NULL; |
562 generator_ = NULL; | 515 generator_ = NULL; |
563 logger->logging_nesting_ = saved_logging_nesting_; | 516 logger->logging_nesting_ = saved_logging_nesting_; |
564 } | 517 } |
565 | 518 |
566 | 519 |
567 void CpuProfiler::SetUp() { | |
568 Isolate* isolate = Isolate::Current(); | |
569 if (isolate->cpu_profiler() == NULL) { | |
570 isolate->set_cpu_profiler(new CpuProfiler()); | |
571 } | |
572 } | |
573 | |
574 | |
575 void CpuProfiler::TearDown() { | |
576 Isolate* isolate = Isolate::Current(); | |
577 if (isolate->cpu_profiler() != NULL) { | |
578 delete isolate->cpu_profiler(); | |
579 } | |
580 isolate->set_cpu_profiler(NULL); | |
581 } | |
582 | |
583 } } // namespace v8::internal | 520 } } // namespace v8::internal |
OLD | NEW |