blob: bb480fc345bb0b5191620d0753c0431a035ca8d5 [file] [log] [blame]
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001// 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
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000032#include "frames-inl.h"
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +000033#include "hashmap.h"
lrn@chromium.org25156de2010-04-06 13:10:27 +000034#include "log-inl.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000035#include "vm-state-inl.h"
lrn@chromium.org25156de2010-04-06 13:10:27 +000036
ager@chromium.org357bf652010-04-12 11:30:10 +000037#include "../include/v8-profiler.h"
38
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000039namespace v8 {
40namespace internal {
41
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000042static const int kEventsBufferSize = 256*KB;
43static const int kTickSamplesBufferChunkSize = 64*KB;
44static const int kTickSamplesBufferChunksCount = 16;
45
46
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000047ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator)
48 : Thread("v8:ProfEvntProc"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +000049 generator_(generator),
erik.corry@gmail.com145eff52010-08-23 11:36:18 +000050 running_(true),
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000051 ticks_buffer_(sizeof(TickSampleEventRecord),
52 kTickSamplesBufferChunkSize,
53 kTickSamplesBufferChunksCount),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +000054 enqueue_order_(0) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000055}
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000056
57
lrn@chromium.org25156de2010-04-06 13:10:27 +000058void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag,
59 const char* prefix,
60 String* name,
61 Address start) {
ager@chromium.org357bf652010-04-12 11:30:10 +000062 if (FilterOutCodeCreateEvent(tag)) return;
lrn@chromium.org25156de2010-04-06 13:10:27 +000063 CodeEventsContainer evt_rec;
64 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
65 rec->type = CodeEventRecord::CODE_CREATION;
66 rec->order = ++enqueue_order_;
67 rec->start = start;
68 rec->entry = generator_->NewCodeEntry(tag, prefix, name);
69 rec->size = 1;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000070 rec->shared = NULL;
lrn@chromium.org25156de2010-04-06 13:10:27 +000071 events_buffer_.Enqueue(evt_rec);
72}
73
74
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000075void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag,
76 String* name,
77 String* resource_name,
78 int line_number,
79 Address start,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +000080 unsigned size,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000081 Address shared) {
ager@chromium.org357bf652010-04-12 11:30:10 +000082 if (FilterOutCodeCreateEvent(tag)) return;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000083 CodeEventsContainer evt_rec;
84 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
85 rec->type = CodeEventRecord::CODE_CREATION;
86 rec->order = ++enqueue_order_;
87 rec->start = start;
88 rec->entry = generator_->NewCodeEntry(tag, name, resource_name, line_number);
89 rec->size = size;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000090 rec->shared = shared;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000091 events_buffer_.Enqueue(evt_rec);
92}
93
94
95void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag,
96 const char* name,
97 Address start,
98 unsigned size) {
ager@chromium.org357bf652010-04-12 11:30:10 +000099 if (FilterOutCodeCreateEvent(tag)) return;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000100 CodeEventsContainer evt_rec;
101 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
102 rec->type = CodeEventRecord::CODE_CREATION;
103 rec->order = ++enqueue_order_;
104 rec->start = start;
105 rec->entry = generator_->NewCodeEntry(tag, name);
106 rec->size = size;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000107 rec->shared = NULL;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000108 events_buffer_.Enqueue(evt_rec);
109}
110
111
112void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag,
113 int args_count,
114 Address start,
115 unsigned size) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000116 if (FilterOutCodeCreateEvent(tag)) return;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000117 CodeEventsContainer evt_rec;
118 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
119 rec->type = CodeEventRecord::CODE_CREATION;
120 rec->order = ++enqueue_order_;
121 rec->start = start;
122 rec->entry = generator_->NewCodeEntry(tag, args_count);
123 rec->size = size;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000124 rec->shared = NULL;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000125 events_buffer_.Enqueue(evt_rec);
126}
127
128
129void ProfilerEventsProcessor::CodeMoveEvent(Address from, Address to) {
130 CodeEventsContainer evt_rec;
131 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_;
132 rec->type = CodeEventRecord::CODE_MOVE;
133 rec->order = ++enqueue_order_;
134 rec->from = from;
135 rec->to = to;
136 events_buffer_.Enqueue(evt_rec);
137}
138
139
140void ProfilerEventsProcessor::CodeDeleteEvent(Address from) {
141 CodeEventsContainer evt_rec;
142 CodeDeleteEventRecord* rec = &evt_rec.CodeDeleteEventRecord_;
143 rec->type = CodeEventRecord::CODE_DELETE;
144 rec->order = ++enqueue_order_;
145 rec->start = from;
146 events_buffer_.Enqueue(evt_rec);
147}
148
149
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000150void ProfilerEventsProcessor::SharedFunctionInfoMoveEvent(Address from,
151 Address to) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000152 CodeEventsContainer evt_rec;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000153 SharedFunctionInfoMoveEventRecord* rec =
154 &evt_rec.SharedFunctionInfoMoveEventRecord_;
155 rec->type = CodeEventRecord::SHARED_FUNC_MOVE;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000156 rec->order = ++enqueue_order_;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000157 rec->from = from;
158 rec->to = to;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000159 events_buffer_.Enqueue(evt_rec);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000160}
161
162
ager@chromium.org357bf652010-04-12 11:30:10 +0000163void ProfilerEventsProcessor::RegExpCodeCreateEvent(
164 Logger::LogEventsAndTags tag,
165 const char* prefix,
166 String* name,
167 Address start,
168 unsigned size) {
169 if (FilterOutCodeCreateEvent(tag)) return;
170 CodeEventsContainer evt_rec;
171 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
172 rec->type = CodeEventRecord::CODE_CREATION;
173 rec->order = ++enqueue_order_;
174 rec->start = start;
175 rec->entry = generator_->NewCodeEntry(tag, prefix, name);
176 rec->size = size;
177 events_buffer_.Enqueue(evt_rec);
178}
179
180
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000181void ProfilerEventsProcessor::AddCurrentStack() {
ager@chromium.org04921a82011-06-27 13:21:41 +0000182 TickSampleEventRecord record(enqueue_order_);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000183 TickSample* sample = &record.sample;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000184 Isolate* isolate = Isolate::Current();
185 sample->state = isolate->current_vm_state();
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000186 sample->pc = reinterpret_cast<Address>(sample); // Not NULL.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000187 for (StackTraceFrameIterator it(isolate);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000188 !it.done() && sample->frames_count < TickSample::kMaxFramesCount;
189 it.Advance()) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000190 sample->stack[sample->frames_count++] = it.frame()->pc();
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000191 }
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000192 ticks_from_vm_buffer_.Enqueue(record);
193}
194
195
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000196bool ProfilerEventsProcessor::ProcessCodeEvent(unsigned* dequeue_order) {
197 if (!events_buffer_.IsEmpty()) {
198 CodeEventsContainer record;
199 events_buffer_.Dequeue(&record);
200 switch (record.generic.type) {
201#define PROFILER_TYPE_CASE(type, clss) \
202 case CodeEventRecord::type: \
203 record.clss##_.UpdateCodeMap(generator_->code_map()); \
204 break;
205
206 CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE)
207
208#undef PROFILER_TYPE_CASE
209 default: return true; // Skip record.
210 }
211 *dequeue_order = record.generic.order;
212 return true;
213 }
214 return false;
215}
216
217
218bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) {
219 while (true) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000220 if (!ticks_from_vm_buffer_.IsEmpty()
221 && ticks_from_vm_buffer_.Peek()->order == dequeue_order) {
222 TickSampleEventRecord record;
223 ticks_from_vm_buffer_.Dequeue(&record);
224 generator_->RecordTickSample(record.sample);
225 }
226
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000227 const TickSampleEventRecord* rec =
lrn@chromium.org25156de2010-04-06 13:10:27 +0000228 TickSampleEventRecord::cast(ticks_buffer_.StartDequeue());
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000229 if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty();
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000230 // Make a local copy of tick sample record to ensure that it won't
231 // be modified as we are processing it. This is possible as the
232 // sampler writes w/o any sync to the queue, so if the processor
233 // will get far behind, a record may be modified right under its
234 // feet.
235 TickSampleEventRecord record = *rec;
236 if (record.order == dequeue_order) {
237 // A paranoid check to make sure that we don't get a memory overrun
238 // in case of frames_count having a wild value.
239 if (record.sample.frames_count < 0
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000240 || record.sample.frames_count > TickSample::kMaxFramesCount)
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000241 record.sample.frames_count = 0;
242 generator_->RecordTickSample(record.sample);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000243 ticks_buffer_.FinishDequeue();
244 } else {
245 return true;
246 }
247 }
248}
249
250
251void ProfilerEventsProcessor::Run() {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000252 unsigned dequeue_order = 0;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000253
254 while (running_) {
255 // Process ticks until we have any.
256 if (ProcessTicks(dequeue_order)) {
257 // All ticks of the current dequeue_order are processed,
258 // proceed to the next code event.
259 ProcessCodeEvent(&dequeue_order);
260 }
261 YieldCPU();
262 }
263
264 // Process remaining tick events.
265 ticks_buffer_.FlushResidualRecords();
266 // Perform processing until we have tick events, skip remaining code events.
267 while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000268}
269
270
lrn@chromium.org25156de2010-04-06 13:10:27 +0000271void CpuProfiler::StartProfiling(const char* title) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
273 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000274}
275
276
277void CpuProfiler::StartProfiling(String* title) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
279 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000280}
281
282
283CpuProfile* CpuProfiler::StopProfiling(const char* title) {
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000284 Isolate* isolate = Isolate::Current();
285 return is_profiling(isolate) ?
286 isolate->cpu_profiler()->StopCollectingProfile(title) : NULL;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000287}
288
289
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000290CpuProfile* CpuProfiler::StopProfiling(Object* security_token, String* title) {
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000291 Isolate* isolate = Isolate::Current();
292 return is_profiling(isolate) ?
293 isolate->cpu_profiler()->StopCollectingProfile(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000294 security_token, title) : NULL;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000295}
296
297
298int CpuProfiler::GetProfilesCount() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000299 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000300 // The count of profiles doesn't depend on a security token.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000301 return Isolate::Current()->cpu_profiler()->profiles_->Profiles(
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000302 TokenEnumerator::kNoSecurityToken)->length();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000303}
304
305
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000306CpuProfile* CpuProfiler::GetProfile(Object* security_token, int index) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000307 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
308 CpuProfiler* profiler = Isolate::Current()->cpu_profiler();
309 const int token = profiler->token_enumerator_->GetTokenId(security_token);
310 return profiler->profiles_->Profiles(token)->at(index);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000311}
312
313
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000314CpuProfile* CpuProfiler::FindProfile(Object* security_token, unsigned uid) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000315 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
316 CpuProfiler* profiler = Isolate::Current()->cpu_profiler();
317 const int token = profiler->token_enumerator_->GetTokenId(security_token);
318 return profiler->profiles_->GetProfile(token, uid);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000319}
320
321
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322TickSample* CpuProfiler::TickSampleEvent(Isolate* isolate) {
323 if (CpuProfiler::is_profiling(isolate)) {
324 return isolate->cpu_profiler()->processor_->TickSampleEvent();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000325 } else {
326 return NULL;
327 }
328}
329
330
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000331void CpuProfiler::DeleteAllProfiles() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000332 Isolate* isolate = Isolate::Current();
333 ASSERT(isolate->cpu_profiler() != NULL);
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000334 if (is_profiling(isolate)) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000335 isolate->cpu_profiler()->StopProcessor();
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000336 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000337 isolate->cpu_profiler()->ResetProfiles();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000338}
339
340
341void CpuProfiler::DeleteProfile(CpuProfile* profile) {
342 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
343 Isolate::Current()->cpu_profiler()->profiles_->RemoveProfile(profile);
344 delete profile;
345}
346
347
348bool CpuProfiler::HasDetachedProfiles() {
349 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
350 return Isolate::Current()->cpu_profiler()->profiles_->HasDetachedProfiles();
351}
352
353
lrn@chromium.org25156de2010-04-06 13:10:27 +0000354void CpuProfiler::CallbackEvent(String* name, Address entry_point) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000355 Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000356 Logger::CALLBACK_TAG, CodeEntry::kEmptyNamePrefix, name, entry_point);
357}
358
359
360void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
361 Code* code, const char* comment) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000362 Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000363 tag, comment, code->address(), code->ExecutableSize());
364}
365
366
367void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
368 Code* code, String* name) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000369 Isolate* isolate = Isolate::Current();
370 isolate->cpu_profiler()->processor_->CodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000371 tag,
372 name,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000373 isolate->heap()->empty_string(),
ager@chromium.org357bf652010-04-12 11:30:10 +0000374 v8::CpuProfileNode::kNoLineNumberInfo,
lrn@chromium.org25156de2010-04-06 13:10:27 +0000375 code->address(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000376 code->ExecutableSize(),
377 NULL);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000378}
379
380
381void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000382 Code* code,
383 SharedFunctionInfo* shared,
384 String* name) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000385 Isolate* isolate = Isolate::Current();
386 isolate->cpu_profiler()->processor_->CodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000387 tag,
388 name,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000389 isolate->heap()->empty_string(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000390 v8::CpuProfileNode::kNoLineNumberInfo,
391 code->address(),
392 code->ExecutableSize(),
393 shared->address());
394}
395
396
397void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
398 Code* code,
399 SharedFunctionInfo* shared,
400 String* source, int line) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000401 Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent(
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000402 tag,
403 shared->DebugName(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000404 source,
405 line,
406 code->address(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000407 code->ExecutableSize(),
408 shared->address());
lrn@chromium.org25156de2010-04-06 13:10:27 +0000409}
410
411
412void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
413 Code* code, int args_count) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000414 Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000415 tag,
416 args_count,
417 code->address(),
418 code->ExecutableSize());
419}
420
421
422void CpuProfiler::CodeMoveEvent(Address from, Address to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000423 Isolate::Current()->cpu_profiler()->processor_->CodeMoveEvent(from, to);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000424}
425
426
427void CpuProfiler::CodeDeleteEvent(Address from) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000428 Isolate::Current()->cpu_profiler()->processor_->CodeDeleteEvent(from);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000429}
430
431
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000432void CpuProfiler::SharedFunctionInfoMoveEvent(Address from, Address to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000433 CpuProfiler* profiler = Isolate::Current()->cpu_profiler();
434 profiler->processor_->SharedFunctionInfoMoveEvent(from, to);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000435}
436
437
438void CpuProfiler::GetterCallbackEvent(String* name, Address entry_point) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000439 Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000440 Logger::CALLBACK_TAG, "get ", name, entry_point);
441}
442
443
444void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000445 Isolate::Current()->cpu_profiler()->processor_->RegExpCodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000446 Logger::REG_EXP_TAG,
ager@chromium.org357bf652010-04-12 11:30:10 +0000447 "RegExp: ",
lrn@chromium.org25156de2010-04-06 13:10:27 +0000448 source,
lrn@chromium.org25156de2010-04-06 13:10:27 +0000449 code->address(),
450 code->ExecutableSize());
451}
452
453
454void CpuProfiler::SetterCallbackEvent(String* name, Address entry_point) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000455 Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000456 Logger::CALLBACK_TAG, "set ", name, entry_point);
457}
458
459
460CpuProfiler::CpuProfiler()
461 : profiles_(new CpuProfilesCollection()),
462 next_profile_uid_(1),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000463 token_enumerator_(new TokenEnumerator()),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000464 generator_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000465 processor_(NULL),
466 need_to_stop_sampler_(false),
467 is_profiling_(false) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000468}
469
470
471CpuProfiler::~CpuProfiler() {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000472 delete token_enumerator_;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000473 delete profiles_;
474}
475
476
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000477void CpuProfiler::ResetProfiles() {
478 delete profiles_;
479 profiles_ = new CpuProfilesCollection();
480}
481
lrn@chromium.org25156de2010-04-06 13:10:27 +0000482void CpuProfiler::StartCollectingProfile(const char* title) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000483 if (profiles_->StartProfiling(title, next_profile_uid_++)) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000484 StartProcessorIfNotStarted();
485 }
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000486 processor_->AddCurrentStack();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000487}
488
489
490void CpuProfiler::StartCollectingProfile(String* title) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000491 StartCollectingProfile(profiles_->GetName(title));
lrn@chromium.org25156de2010-04-06 13:10:27 +0000492}
493
494
495void CpuProfiler::StartProcessorIfNotStarted() {
496 if (processor_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000497 Isolate* isolate = Isolate::Current();
498
ager@chromium.org357bf652010-04-12 11:30:10 +0000499 // Disable logging when using the new implementation.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000500 saved_logging_nesting_ = isolate->logger()->logging_nesting_;
501 isolate->logger()->logging_nesting_ = 0;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000502 generator_ = new ProfileGenerator(profiles_);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000503 processor_ = new ProfilerEventsProcessor(generator_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000504 NoBarrier_Store(&is_profiling_, true);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000505 processor_->Start();
506 // Enumerate stuff we already have in the heap.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000507 if (isolate->heap()->HasBeenSetup()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000508 if (!FLAG_prof_browser_mode) {
509 bool saved_log_code_flag = FLAG_log_code;
510 FLAG_log_code = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000511 isolate->logger()->LogCodeObjects();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000512 FLAG_log_code = saved_log_code_flag;
513 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000514 isolate->logger()->LogCompiledFunctions();
515 isolate->logger()->LogAccessorCallbacks();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000516 }
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000517 // Enable stack sampling.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000518 Sampler* sampler = reinterpret_cast<Sampler*>(isolate->logger()->ticker_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000519 if (!sampler->IsActive()) {
520 sampler->Start();
521 need_to_stop_sampler_ = true;
522 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000523 sampler->IncreaseProfilingDepth();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000524 }
525}
526
527
528CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000529 const double actual_sampling_rate = generator_->actual_sampling_rate();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000530 StopProcessorIfLastProfile(title);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000531 CpuProfile* result =
532 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken,
533 title,
534 actual_sampling_rate);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000535 if (result != NULL) {
536 result->Print();
537 }
538 return result;
539}
540
541
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000542CpuProfile* CpuProfiler::StopCollectingProfile(Object* security_token,
543 String* title) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000544 const double actual_sampling_rate = generator_->actual_sampling_rate();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000545 const char* profile_title = profiles_->GetName(title);
546 StopProcessorIfLastProfile(profile_title);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000547 int token = token_enumerator_->GetTokenId(security_token);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000548 return profiles_->StopProfiling(token, profile_title, actual_sampling_rate);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000549}
550
551
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000552void CpuProfiler::StopProcessorIfLastProfile(const char* title) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000553 if (profiles_->IsLastProfile(title)) StopProcessor();
554}
555
556
557void CpuProfiler::StopProcessor() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000558 Logger* logger = Isolate::Current()->logger();
559 Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000560 sampler->DecreaseProfilingDepth();
561 if (need_to_stop_sampler_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000562 sampler->Stop();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000563 need_to_stop_sampler_ = false;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000564 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000565 processor_->Stop();
566 processor_->Join();
567 delete processor_;
568 delete generator_;
569 processor_ = NULL;
570 NoBarrier_Store(&is_profiling_, false);
571 generator_ = NULL;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000572 logger->logging_nesting_ = saved_logging_nesting_;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000573}
574
lrn@chromium.org25156de2010-04-06 13:10:27 +0000575
576void CpuProfiler::Setup() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000577 Isolate* isolate = Isolate::Current();
578 if (isolate->cpu_profiler() == NULL) {
579 isolate->set_cpu_profiler(new CpuProfiler());
lrn@chromium.org25156de2010-04-06 13:10:27 +0000580 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000581}
582
583
584void CpuProfiler::TearDown() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000585 Isolate* isolate = Isolate::Current();
586 if (isolate->cpu_profiler() != NULL) {
587 delete isolate->cpu_profiler();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000588 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000589 isolate->set_cpu_profiler(NULL);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000590}
591
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000592} } // namespace v8::internal