Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: src/cpu-profiler.cc

Issue 11428103: Revert "Perform CPU sampling by CPU sampling thread only iff processing thread is not running." (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/cpu-profiler.h ('k') | src/cpu-profiler-inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 21 matching lines...) Expand all
32 #include "frames-inl.h" 32 #include "frames-inl.h"
33 #include "hashmap.h" 33 #include "hashmap.h"
34 #include "log-inl.h" 34 #include "log-inl.h"
35 #include "vm-state-inl.h" 35 #include "vm-state-inl.h"
36 36
37 #include "../include/v8-profiler.h" 37 #include "../include/v8-profiler.h"
38 38
39 namespace v8 { 39 namespace v8 {
40 namespace internal { 40 namespace internal {
41 41
42 static const int kEventsBufferSize = 256 * KB;
43 static const int kTickSamplesBufferChunkSize = 64 * KB;
44 static const int kTickSamplesBufferChunksCount = 16;
42 static const int kProfilerStackSize = 64 * KB; 45 static const int kProfilerStackSize = 64 * KB;
43 46
44 47
45 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator, 48 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator)
46 Sampler* sampler,
47 int period_in_useconds)
48 : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), 49 : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)),
49 generator_(generator), 50 generator_(generator),
50 sampler_(sampler),
51 running_(true), 51 running_(true),
52 period_in_useconds_(period_in_useconds), 52 ticks_buffer_(sizeof(TickSampleEventRecord),
53 ticks_buffer_is_empty_(true), 53 kTickSamplesBufferChunkSize,
54 ticks_buffer_is_initialized_(false), 54 kTickSamplesBufferChunksCount),
55 enqueue_order_(0) { 55 enqueue_order_(0) {
56 } 56 }
57 57
58 58
59 void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag, 59 void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag,
60 const char* prefix, 60 const char* prefix,
61 String* name, 61 String* name,
62 Address start) { 62 Address start) {
63 if (FilterOutCodeCreateEvent(tag)) return; 63 if (FilterOutCodeCreateEvent(tag)) return;
64 CodeEventsContainer evt_rec; 64 CodeEventsContainer evt_rec;
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 208
209 bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) { 209 bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) {
210 while (true) { 210 while (true) {
211 if (!ticks_from_vm_buffer_.IsEmpty() 211 if (!ticks_from_vm_buffer_.IsEmpty()
212 && ticks_from_vm_buffer_.Peek()->order == dequeue_order) { 212 && ticks_from_vm_buffer_.Peek()->order == dequeue_order) {
213 TickSampleEventRecord record; 213 TickSampleEventRecord record;
214 ticks_from_vm_buffer_.Dequeue(&record); 214 ticks_from_vm_buffer_.Dequeue(&record);
215 generator_->RecordTickSample(record.sample); 215 generator_->RecordTickSample(record.sample);
216 } 216 }
217 217
218 if (ticks_buffer_is_empty_) return !ticks_from_vm_buffer_.IsEmpty(); 218 const TickSampleEventRecord* rec =
219 if (ticks_buffer_.order == dequeue_order) { 219 TickSampleEventRecord::cast(ticks_buffer_.StartDequeue());
220 if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty();
221 // Make a local copy of tick sample record to ensure that it won't
222 // be modified as we are processing it. This is possible as the
223 // sampler writes w/o any sync to the queue, so if the processor
224 // will get far behind, a record may be modified right under its
225 // feet.
226 TickSampleEventRecord record = *rec;
227 if (record.order == dequeue_order) {
220 // A paranoid check to make sure that we don't get a memory overrun 228 // A paranoid check to make sure that we don't get a memory overrun
221 // in case of frames_count having a wild value. 229 // in case of frames_count having a wild value.
222 if (ticks_buffer_.sample.frames_count < 0 230 if (record.sample.frames_count < 0
223 || ticks_buffer_.sample.frames_count > TickSample::kMaxFramesCount) { 231 || record.sample.frames_count > TickSample::kMaxFramesCount)
224 ticks_buffer_.sample.frames_count = 0; 232 record.sample.frames_count = 0;
225 } 233 generator_->RecordTickSample(record.sample);
226 generator_->RecordTickSample(ticks_buffer_.sample); 234 ticks_buffer_.FinishDequeue();
227 ticks_buffer_is_empty_ = true;
228 ticks_buffer_is_initialized_ = false;
229 } else { 235 } else {
230 return true; 236 return true;
231 } 237 }
232 } 238 }
233 } 239 }
234 240
235 241
236 void ProfilerEventsProcessor::ProcessEventsQueue(int64_t stop_time,
237 unsigned* dequeue_order) {
238 while (OS::Ticks() < stop_time) {
239 if (ProcessTicks(*dequeue_order)) {
240 // All ticks of the current dequeue_order are processed,
241 // proceed to the next code event.
242 ProcessCodeEvent(dequeue_order);
243 }
244 }
245 }
246
247
248 void ProfilerEventsProcessor::Run() { 242 void ProfilerEventsProcessor::Run() {
249 unsigned dequeue_order = 0; 243 unsigned dequeue_order = 0;
250 244
251 while (running_) { 245 while (running_) {
252 int64_t stop_time = OS::Ticks() + period_in_useconds_; 246 // Process ticks until we have any.
253 if (sampler_ != NULL) { 247 if (ProcessTicks(dequeue_order)) {
254 sampler_->DoSample(); 248 // All ticks of the current dequeue_order are processed,
249 // proceed to the next code event.
250 ProcessCodeEvent(&dequeue_order);
255 } 251 }
256 ProcessEventsQueue(stop_time, &dequeue_order); 252 YieldCPU();
257 } 253 }
258 254
255 // Process remaining tick events.
256 ticks_buffer_.FlushResidualRecords();
257 // Perform processing until we have tick events, skip remaining code events.
259 while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { } 258 while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { }
260 } 259 }
261 260
262 261
263 void CpuProfiler::StartProfiling(const char* title) { 262 void CpuProfiler::StartProfiling(const char* title) {
264 ASSERT(Isolate::Current()->cpu_profiler() != NULL); 263 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
265 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title); 264 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title);
266 } 265 }
267 266
268 267
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 303
305 304
306 CpuProfile* CpuProfiler::FindProfile(Object* security_token, unsigned uid) { 305 CpuProfile* CpuProfiler::FindProfile(Object* security_token, unsigned uid) {
307 ASSERT(Isolate::Current()->cpu_profiler() != NULL); 306 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
308 CpuProfiler* profiler = Isolate::Current()->cpu_profiler(); 307 CpuProfiler* profiler = Isolate::Current()->cpu_profiler();
309 const int token = profiler->token_enumerator_->GetTokenId(security_token); 308 const int token = profiler->token_enumerator_->GetTokenId(security_token);
310 return profiler->profiles_->GetProfile(token, uid); 309 return profiler->profiles_->GetProfile(token, uid);
311 } 310 }
312 311
313 312
314 TickSample* CpuProfiler::StartTickSampleEvent(Isolate* isolate) { 313 TickSample* CpuProfiler::TickSampleEvent(Isolate* isolate) {
315 if (CpuProfiler::is_profiling(isolate)) { 314 if (CpuProfiler::is_profiling(isolate)) {
316 return isolate->cpu_profiler()->processor_->StartTickSampleEvent(); 315 return isolate->cpu_profiler()->processor_->TickSampleEvent();
317 } else { 316 } else {
318 return NULL; 317 return NULL;
319 } 318 }
320 } 319 }
321 320
322 321
323 void CpuProfiler::FinishTickSampleEvent(Isolate* isolate) {
324 if (CpuProfiler::is_profiling(isolate)) {
325 isolate->cpu_profiler()->processor_->FinishTickSampleEvent();
326 }
327 }
328
329
330 void CpuProfiler::DeleteAllProfiles() { 322 void CpuProfiler::DeleteAllProfiles() {
331 Isolate* isolate = Isolate::Current(); 323 Isolate* isolate = Isolate::Current();
332 ASSERT(isolate->cpu_profiler() != NULL); 324 ASSERT(isolate->cpu_profiler() != NULL);
333 if (is_profiling(isolate)) { 325 if (is_profiling(isolate)) {
334 isolate->cpu_profiler()->StopProcessor(); 326 isolate->cpu_profiler()->StopProcessor();
335 } 327 }
336 isolate->cpu_profiler()->ResetProfiles(); 328 isolate->cpu_profiler()->ResetProfiles();
337 } 329 }
338 330
339 331
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 479
488 void CpuProfiler::StartCollectingProfile(String* title) { 480 void CpuProfiler::StartCollectingProfile(String* title) {
489 StartCollectingProfile(profiles_->GetName(title)); 481 StartCollectingProfile(profiles_->GetName(title));
490 } 482 }
491 483
492 484
493 void CpuProfiler::StartProcessorIfNotStarted() { 485 void CpuProfiler::StartProcessorIfNotStarted() {
494 if (processor_ == NULL) { 486 if (processor_ == NULL) {
495 Isolate* isolate = Isolate::Current(); 487 Isolate* isolate = Isolate::Current();
496 488
497 Sampler* sampler = isolate->logger()->sampler();
498 // Disable logging when using the new implementation. 489 // Disable logging when using the new implementation.
499 saved_logging_nesting_ = isolate->logger()->logging_nesting_; 490 saved_logging_nesting_ = isolate->logger()->logging_nesting_;
500 isolate->logger()->logging_nesting_ = 0; 491 isolate->logger()->logging_nesting_ = 0;
501 generator_ = new ProfileGenerator(profiles_); 492 generator_ = new ProfileGenerator(profiles_);
502 processor_ = new ProfilerEventsProcessor(generator_, 493 processor_ = new ProfilerEventsProcessor(generator_);
503 sampler,
504 FLAG_cpu_profiler_sampling_period);
505 NoBarrier_Store(&is_profiling_, true); 494 NoBarrier_Store(&is_profiling_, true);
495 processor_->Start();
506 // Enumerate stuff we already have in the heap. 496 // Enumerate stuff we already have in the heap.
507 if (isolate->heap()->HasBeenSetUp()) { 497 if (isolate->heap()->HasBeenSetUp()) {
508 if (!FLAG_prof_browser_mode) { 498 if (!FLAG_prof_browser_mode) {
509 bool saved_log_code_flag = FLAG_log_code; 499 bool saved_log_code_flag = FLAG_log_code;
510 FLAG_log_code = true; 500 FLAG_log_code = true;
511 isolate->logger()->LogCodeObjects(); 501 isolate->logger()->LogCodeObjects();
512 FLAG_log_code = saved_log_code_flag; 502 FLAG_log_code = saved_log_code_flag;
513 } 503 }
514 isolate->logger()->LogCompiledFunctions(); 504 isolate->logger()->LogCompiledFunctions();
515 isolate->logger()->LogAccessorCallbacks(); 505 isolate->logger()->LogAccessorCallbacks();
516 } 506 }
517 // Enable stack sampling. 507 // Enable stack sampling.
508 Sampler* sampler = reinterpret_cast<Sampler*>(isolate->logger()->ticker_);
518 if (!sampler->IsActive()) { 509 if (!sampler->IsActive()) {
519 sampler->Start(); 510 sampler->Start();
520 need_to_stop_sampler_ = true; 511 need_to_stop_sampler_ = true;
521 } 512 }
522 sampler->SetHasProcessingThread(true);
523 sampler->IncreaseProfilingDepth(); 513 sampler->IncreaseProfilingDepth();
524 processor_->Start();
525 } 514 }
526 } 515 }
527 516
528 517
529 CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) { 518 CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) {
530 const double actual_sampling_rate = generator_->actual_sampling_rate(); 519 const double actual_sampling_rate = generator_->actual_sampling_rate();
531 StopProcessorIfLastProfile(title); 520 StopProcessorIfLastProfile(title);
532 CpuProfile* result = 521 CpuProfile* result =
533 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken, 522 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken,
534 title, 523 title,
(...skipping 14 matching lines...) Expand all
549 return profiles_->StopProfiling(token, profile_title, actual_sampling_rate); 538 return profiles_->StopProfiling(token, profile_title, actual_sampling_rate);
550 } 539 }
551 540
552 541
553 void CpuProfiler::StopProcessorIfLastProfile(const char* title) { 542 void CpuProfiler::StopProcessorIfLastProfile(const char* title) {
554 if (profiles_->IsLastProfile(title)) StopProcessor(); 543 if (profiles_->IsLastProfile(title)) StopProcessor();
555 } 544 }
556 545
557 546
558 void CpuProfiler::StopProcessor() { 547 void CpuProfiler::StopProcessor() {
559 NoBarrier_Store(&is_profiling_, false);
560 processor_->Stop();
561 processor_->Join();
562 Logger* logger = Isolate::Current()->logger(); 548 Logger* logger = Isolate::Current()->logger();
563 Sampler* sampler = logger->sampler(); 549 Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_);
564 sampler->DecreaseProfilingDepth(); 550 sampler->DecreaseProfilingDepth();
565 sampler->SetHasProcessingThread(false);
566 if (need_to_stop_sampler_) { 551 if (need_to_stop_sampler_) {
567 sampler->Stop(); 552 sampler->Stop();
568 need_to_stop_sampler_ = false; 553 need_to_stop_sampler_ = false;
569 } 554 }
555 NoBarrier_Store(&is_profiling_, false);
556 processor_->Stop();
557 processor_->Join();
570 delete processor_; 558 delete processor_;
571 delete generator_; 559 delete generator_;
572 processor_ = NULL; 560 processor_ = NULL;
573 generator_ = NULL; 561 generator_ = NULL;
574 logger->logging_nesting_ = saved_logging_nesting_; 562 logger->logging_nesting_ = saved_logging_nesting_;
575 } 563 }
576 564
577 565
578 void CpuProfiler::SetUp() { 566 void CpuProfiler::SetUp() {
579 Isolate* isolate = Isolate::Current(); 567 Isolate* isolate = Isolate::Current();
580 if (isolate->cpu_profiler() == NULL) { 568 if (isolate->cpu_profiler() == NULL) {
581 isolate->set_cpu_profiler(new CpuProfiler()); 569 isolate->set_cpu_profiler(new CpuProfiler());
582 } 570 }
583 } 571 }
584 572
585 573
586 void CpuProfiler::TearDown() { 574 void CpuProfiler::TearDown() {
587 Isolate* isolate = Isolate::Current(); 575 Isolate* isolate = Isolate::Current();
588 if (isolate->cpu_profiler() != NULL) { 576 if (isolate->cpu_profiler() != NULL) {
589 delete isolate->cpu_profiler(); 577 delete isolate->cpu_profiler();
590 } 578 }
591 isolate->set_cpu_profiler(NULL); 579 isolate->set_cpu_profiler(NULL);
592 } 580 }
593 581
594 } } // namespace v8::internal 582 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/cpu-profiler.h ('k') | src/cpu-profiler-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698