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

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

Issue 10857035: Moving cpu profiling into its own thread. (Closed) Base URL: http://git.chromium.org/external/v8.git@master
Patch Set: Created 8 years, 4 months 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
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 27 matching lines...) Expand all
38 38
39 namespace v8 { 39 namespace v8 {
40 namespace internal { 40 namespace internal {
41 41
42 static const int kEventsBufferSize = 256 * KB; 42 static const int kEventsBufferSize = 256 * KB;
43 static const int kTickSamplesBufferChunkSize = 64 * KB; 43 static const int kTickSamplesBufferChunkSize = 64 * KB;
44 static const int kTickSamplesBufferChunksCount = 16; 44 static const int kTickSamplesBufferChunksCount = 16;
45 static const int kProfilerStackSize = 64 * KB; 45 static const int kProfilerStackSize = 64 * KB;
46 46
47 47
48 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator) 48 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator, Sa mpler* sampler, int interval)
49 : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), 49 : CpuProfilerThread(sampler),
50 generator_(generator), 50 generator_(generator),
51 running_(true), 51 running_(true),
52 interval_(interval),
52 ticks_buffer_(sizeof(TickSampleEventRecord), 53 ticks_buffer_(sizeof(TickSampleEventRecord),
53 kTickSamplesBufferChunkSize, 54 kTickSamplesBufferChunkSize,
54 kTickSamplesBufferChunksCount), 55 kTickSamplesBufferChunksCount),
55 enqueue_order_(0) { 56 enqueue_order_(0) {
56 } 57 }
57 58
58 59
59 void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag, 60 void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag,
60 const char* prefix, 61 const char* prefix,
61 String* name, 62 String* name,
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 sample->pc = reinterpret_cast<Address>(sample); // Not NULL. 178 sample->pc = reinterpret_cast<Address>(sample); // Not NULL.
178 for (StackTraceFrameIterator it(isolate); 179 for (StackTraceFrameIterator it(isolate);
179 !it.done() && sample->frames_count < TickSample::kMaxFramesCount; 180 !it.done() && sample->frames_count < TickSample::kMaxFramesCount;
180 it.Advance()) { 181 it.Advance()) {
181 sample->stack[sample->frames_count++] = it.frame()->pc(); 182 sample->stack[sample->frames_count++] = it.frame()->pc();
182 } 183 }
183 ticks_from_vm_buffer_.Enqueue(record); 184 ticks_from_vm_buffer_.Enqueue(record);
184 } 185 }
185 186
186 187
187 bool ProfilerEventsProcessor::ProcessCodeEvent(unsigned* dequeue_order) { 188 bool ProfilerEventsProcessor::ProcessCodeEvent(unsigned& dequeue_order) {
188 if (!events_buffer_.IsEmpty()) { 189 if (!events_buffer_.IsEmpty()) {
189 CodeEventsContainer record; 190 CodeEventsContainer record;
190 events_buffer_.Dequeue(&record); 191 events_buffer_.Dequeue(&record);
191 switch (record.generic.type) { 192 switch (record.generic.type) {
192 #define PROFILER_TYPE_CASE(type, clss) \ 193 #define PROFILER_TYPE_CASE(type, clss) \
193 case CodeEventRecord::type: \ 194 case CodeEventRecord::type: \
194 record.clss##_.UpdateCodeMap(generator_->code_map()); \ 195 record.clss##_.UpdateCodeMap(generator_->code_map()); \
195 break; 196 break;
196 197
197 CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE) 198 CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE)
198 199
199 #undef PROFILER_TYPE_CASE 200 #undef PROFILER_TYPE_CASE
200 default: return true; // Skip record. 201 default: return true; // Skip record.
201 } 202 }
202 *dequeue_order = record.generic.order; 203 dequeue_order = record.generic.order;
203 return true; 204 return true;
204 } 205 }
205 return false; 206 return false;
206 } 207 }
207 208
208 209
209 bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) { 210 bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order, int64_t start , int64_t time_limit) {
210 while (true) { 211 while (time_limit == -1 || OS::Ticks() - start < time_limit) {
211 if (!ticks_from_vm_buffer_.IsEmpty() 212 if (!ticks_from_vm_buffer_.IsEmpty()
212 && ticks_from_vm_buffer_.Peek()->order == dequeue_order) { 213 && ticks_from_vm_buffer_.Peek()->order == dequeue_order) {
213 TickSampleEventRecord record; 214 TickSampleEventRecord record;
214 ticks_from_vm_buffer_.Dequeue(&record); 215 ticks_from_vm_buffer_.Dequeue(&record);
215 generator_->RecordTickSample(record.sample); 216 generator_->RecordTickSample(record.sample);
216 } 217 }
217 218
218 const TickSampleEventRecord* rec = 219 const TickSampleEventRecord* rec =
219 TickSampleEventRecord::cast(ticks_buffer_.StartDequeue()); 220 TickSampleEventRecord::cast(ticks_buffer_.StartDequeue());
220 if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty(); 221 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 // 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 // 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 // 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 // will get far behind, a record may be modified right under its
225 // feet. 226 // feet.
226 TickSampleEventRecord record = *rec; 227 TickSampleEventRecord record = *rec;
227 if (record.order == dequeue_order) { 228 if (record.order == dequeue_order) {
228 // A paranoid check to make sure that we don't get a memory overrun 229 // A paranoid check to make sure that we don't get a memory overrun
229 // in case of frames_count having a wild value. 230 // in case of frames_count having a wild value.
230 if (record.sample.frames_count < 0 231 if (record.sample.frames_count < 0
231 || record.sample.frames_count > TickSample::kMaxFramesCount) 232 || record.sample.frames_count > TickSample::kMaxFramesCount)
232 record.sample.frames_count = 0; 233 record.sample.frames_count = 0;
233 generator_->RecordTickSample(record.sample); 234 generator_->RecordTickSample(record.sample);
234 ticks_buffer_.FinishDequeue(); 235 ticks_buffer_.FinishDequeue();
235 } else { 236 } else {
236 return true; 237 return true;
237 } 238 }
238 } 239 }
240 return false;
241 }
242
243
244 void ProfilerEventsProcessor::ProcessEventsQueue(unsigned& dequeue_order, int64_ t time_limit) {
245 int64_t start = OS::Ticks();
246 while (OS::Ticks() - start < time_limit)
caseq 2012/08/16 14:39:22 I would just use absolute time limit, so that we c
247 // Process ticks until we have any.
248 if (ProcessTicks(dequeue_order, start, time_limit)) {
249 // All ticks of the current dequeue_order are processed,
250 // proceed to the next code event.
251 ProcessCodeEvent(dequeue_order);
252 }
253 YieldCPU();
239 } 254 }
240 255
241 256
242 void ProfilerEventsProcessor::Run() { 257 void ProfilerEventsProcessor::Run() {
243 unsigned dequeue_order = 0; 258 unsigned dequeue_order = 0;
244 259
245 while (running_) { 260 while (running_) {
246 // Process ticks until we have any. 261 int64_t start = OS::Ticks();
247 if (ProcessTicks(dequeue_order)) { 262 DoCpuProfile();
248 // All ticks of the current dequeue_order are processed, 263 ProcessEventsQueue(dequeue_order, static_cast<int64_t>(interval_) - (OS::Tic ks() - start));
249 // proceed to the next code event.
250 ProcessCodeEvent(&dequeue_order);
251 }
252 YieldCPU();
caseq 2012/08/16 14:39:22 So we're running the loop with 100% usage until it
253 } 264 }
254 265
255 // Process remaining tick events. 266 // Process remaining tick events.
256 ticks_buffer_.FlushResidualRecords(); 267 ticks_buffer_.FlushResidualRecords();
257 // Perform processing until we have tick events, skip remaining code events. 268 // Perform processing until we have tick events, skip remaining code events.
258 while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { } 269 while (ProcessTicks(dequeue_order, -1, -1) && ProcessCodeEvent(dequeue_order)) { }
259 } 270 }
260 271
261 272
262 void CpuProfiler::StartProfiling(const char* title) { 273 void CpuProfiler::StartProfiling(const char* title) {
263 ASSERT(Isolate::Current()->cpu_profiler() != NULL); 274 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
264 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title); 275 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title);
265 } 276 }
266 277
267 278
268 void CpuProfiler::StartProfiling(String* title) { 279 void CpuProfiler::StartProfiling(String* title) {
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 490
480 void CpuProfiler::StartCollectingProfile(String* title) { 491 void CpuProfiler::StartCollectingProfile(String* title) {
481 StartCollectingProfile(profiles_->GetName(title)); 492 StartCollectingProfile(profiles_->GetName(title));
482 } 493 }
483 494
484 495
485 void CpuProfiler::StartProcessorIfNotStarted() { 496 void CpuProfiler::StartProcessorIfNotStarted() {
486 if (processor_ == NULL) { 497 if (processor_ == NULL) {
487 Isolate* isolate = Isolate::Current(); 498 Isolate* isolate = Isolate::Current();
488 499
500 // Enable stack sampling.
501 Sampler* sampler = reinterpret_cast<Sampler*>(isolate->logger()->ticker_);
502 if (!sampler->IsActive()) {
503 sampler->Start();
504 need_to_stop_sampler_ = true;
505 }
506 sampler->IncreaseProfilingDepth();
489 // Disable logging when using the new implementation. 507 // Disable logging when using the new implementation.
490 saved_logging_nesting_ = isolate->logger()->logging_nesting_; 508 saved_logging_nesting_ = isolate->logger()->logging_nesting_;
491 isolate->logger()->logging_nesting_ = 0; 509 isolate->logger()->logging_nesting_ = 0;
492 generator_ = new ProfileGenerator(profiles_); 510 generator_ = new ProfileGenerator(profiles_);
493 processor_ = new ProfilerEventsProcessor(generator_); 511 processor_ = new ProfilerEventsProcessor(generator_, sampler, sampler->inter val() * 1000);
494 NoBarrier_Store(&is_profiling_, true); 512 NoBarrier_Store(&is_profiling_, true);
495 processor_->Start(); 513 processor_->Start();
496 // Enumerate stuff we already have in the heap. 514 // Enumerate stuff we already have in the heap.
497 if (isolate->heap()->HasBeenSetUp()) { 515 if (isolate->heap()->HasBeenSetUp()) {
498 if (!FLAG_prof_browser_mode) { 516 if (!FLAG_prof_browser_mode) {
499 bool saved_log_code_flag = FLAG_log_code; 517 bool saved_log_code_flag = FLAG_log_code;
500 FLAG_log_code = true; 518 FLAG_log_code = true;
501 isolate->logger()->LogCodeObjects(); 519 isolate->logger()->LogCodeObjects();
502 FLAG_log_code = saved_log_code_flag; 520 FLAG_log_code = saved_log_code_flag;
503 } 521 }
504 isolate->logger()->LogCompiledFunctions(); 522 isolate->logger()->LogCompiledFunctions();
505 isolate->logger()->LogAccessorCallbacks(); 523 isolate->logger()->LogAccessorCallbacks();
506 } 524 }
507 // Enable stack sampling.
508 Sampler* sampler = reinterpret_cast<Sampler*>(isolate->logger()->ticker_);
509 if (!sampler->IsActive()) {
510 sampler->Start();
511 need_to_stop_sampler_ = true;
512 }
513 sampler->IncreaseProfilingDepth();
514 } 525 }
515 } 526 }
516 527
517 528
518 CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) { 529 CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) {
519 const double actual_sampling_rate = generator_->actual_sampling_rate(); 530 const double actual_sampling_rate = generator_->actual_sampling_rate();
520 StopProcessorIfLastProfile(title); 531 StopProcessorIfLastProfile(title);
521 CpuProfile* result = 532 CpuProfile* result =
522 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken, 533 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken,
523 title, 534 title,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 584
574 void CpuProfiler::TearDown() { 585 void CpuProfiler::TearDown() {
575 Isolate* isolate = Isolate::Current(); 586 Isolate* isolate = Isolate::Current();
576 if (isolate->cpu_profiler() != NULL) { 587 if (isolate->cpu_profiler() != NULL) {
577 delete isolate->cpu_profiler(); 588 delete isolate->cpu_profiler();
578 } 589 }
579 isolate->set_cpu_profiler(NULL); 590 isolate->set_cpu_profiler(NULL);
580 } 591 }
581 592
582 } } // namespace v8::internal 593 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/cpu-profiler.h ('k') | src/platform.h » ('j') | src/platform.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698