blob: 3cbac77858d5560feb3cab420f08c9e461f2422e [file] [log] [blame]
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001// Copyright 2012 the V8 project authors. All rights reserved.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00002// 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
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000042static const int kEventsBufferSize = 256 * KB;
43static const int kTickSamplesBufferChunkSize = 64 * KB;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000044static const int kTickSamplesBufferChunksCount = 16;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +000045static const int kProfilerStackSize = 64 * KB;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000046
47
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000048ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator)
yangguo@chromium.org659ceec2012-01-26 07:37:54 +000049 : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)),
lrn@chromium.org5d00b602011-01-05 09:51:43 +000050 generator_(generator),
erik.corry@gmail.com145eff52010-08-23 11:36:18 +000051 running_(true),
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000052 ticks_buffer_(sizeof(TickSampleEventRecord),
53 kTickSamplesBufferChunkSize,
54 kTickSamplesBufferChunksCount),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +000055 enqueue_order_(0) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000056}
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000057
58
lrn@chromium.org25156de2010-04-06 13:10:27 +000059void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag,
60 const char* prefix,
61 String* name,
62 Address start) {
ager@chromium.org357bf652010-04-12 11:30:10 +000063 if (FilterOutCodeCreateEvent(tag)) return;
lrn@chromium.org25156de2010-04-06 13:10:27 +000064 CodeEventsContainer evt_rec;
65 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
66 rec->type = CodeEventRecord::CODE_CREATION;
67 rec->order = ++enqueue_order_;
68 rec->start = start;
69 rec->entry = generator_->NewCodeEntry(tag, prefix, name);
70 rec->size = 1;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000071 rec->shared = NULL;
lrn@chromium.org25156de2010-04-06 13:10:27 +000072 events_buffer_.Enqueue(evt_rec);
73}
74
75
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000076void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag,
77 String* name,
78 String* resource_name,
79 int line_number,
80 Address start,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +000081 unsigned size,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000082 Address shared) {
ager@chromium.org357bf652010-04-12 11:30:10 +000083 if (FilterOutCodeCreateEvent(tag)) return;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000084 CodeEventsContainer evt_rec;
85 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
86 rec->type = CodeEventRecord::CODE_CREATION;
87 rec->order = ++enqueue_order_;
88 rec->start = start;
89 rec->entry = generator_->NewCodeEntry(tag, name, resource_name, line_number);
90 rec->size = size;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000091 rec->shared = shared;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000092 events_buffer_.Enqueue(evt_rec);
93}
94
95
96void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag,
97 const char* name,
98 Address start,
99 unsigned size) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000100 if (FilterOutCodeCreateEvent(tag)) return;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000101 CodeEventsContainer evt_rec;
102 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
103 rec->type = CodeEventRecord::CODE_CREATION;
104 rec->order = ++enqueue_order_;
105 rec->start = start;
106 rec->entry = generator_->NewCodeEntry(tag, name);
107 rec->size = size;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000108 rec->shared = NULL;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000109 events_buffer_.Enqueue(evt_rec);
110}
111
112
113void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag,
114 int args_count,
115 Address start,
116 unsigned size) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000117 if (FilterOutCodeCreateEvent(tag)) return;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000118 CodeEventsContainer evt_rec;
119 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
120 rec->type = CodeEventRecord::CODE_CREATION;
121 rec->order = ++enqueue_order_;
122 rec->start = start;
123 rec->entry = generator_->NewCodeEntry(tag, args_count);
124 rec->size = size;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000125 rec->shared = NULL;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000126 events_buffer_.Enqueue(evt_rec);
127}
128
129
130void ProfilerEventsProcessor::CodeMoveEvent(Address from, Address to) {
131 CodeEventsContainer evt_rec;
132 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_;
133 rec->type = CodeEventRecord::CODE_MOVE;
134 rec->order = ++enqueue_order_;
135 rec->from = from;
136 rec->to = to;
137 events_buffer_.Enqueue(evt_rec);
138}
139
140
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000141void ProfilerEventsProcessor::SharedFunctionInfoMoveEvent(Address from,
142 Address to) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000143 CodeEventsContainer evt_rec;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000144 SharedFunctionInfoMoveEventRecord* rec =
145 &evt_rec.SharedFunctionInfoMoveEventRecord_;
146 rec->type = CodeEventRecord::SHARED_FUNC_MOVE;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000147 rec->order = ++enqueue_order_;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000148 rec->from = from;
149 rec->to = to;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000150 events_buffer_.Enqueue(evt_rec);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000151}
152
153
ager@chromium.org357bf652010-04-12 11:30:10 +0000154void ProfilerEventsProcessor::RegExpCodeCreateEvent(
155 Logger::LogEventsAndTags tag,
156 const char* prefix,
157 String* name,
158 Address start,
159 unsigned size) {
160 if (FilterOutCodeCreateEvent(tag)) return;
161 CodeEventsContainer evt_rec;
162 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
163 rec->type = CodeEventRecord::CODE_CREATION;
164 rec->order = ++enqueue_order_;
165 rec->start = start;
166 rec->entry = generator_->NewCodeEntry(tag, prefix, name);
167 rec->size = size;
168 events_buffer_.Enqueue(evt_rec);
169}
170
171
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000172void ProfilerEventsProcessor::AddCurrentStack() {
ager@chromium.org04921a82011-06-27 13:21:41 +0000173 TickSampleEventRecord record(enqueue_order_);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000174 TickSample* sample = &record.sample;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000175 Isolate* isolate = Isolate::Current();
176 sample->state = isolate->current_vm_state();
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000177 sample->pc = reinterpret_cast<Address>(sample); // Not NULL.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000178 for (StackTraceFrameIterator it(isolate);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000179 !it.done() && sample->frames_count < TickSample::kMaxFramesCount;
180 it.Advance()) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000181 sample->stack[sample->frames_count++] = it.frame()->pc();
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000182 }
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000183 ticks_from_vm_buffer_.Enqueue(record);
184}
185
186
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000187bool ProfilerEventsProcessor::ProcessCodeEvent(unsigned* dequeue_order) {
188 if (!events_buffer_.IsEmpty()) {
189 CodeEventsContainer record;
190 events_buffer_.Dequeue(&record);
191 switch (record.generic.type) {
192#define PROFILER_TYPE_CASE(type, clss) \
193 case CodeEventRecord::type: \
194 record.clss##_.UpdateCodeMap(generator_->code_map()); \
195 break;
196
197 CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE)
198
199#undef PROFILER_TYPE_CASE
200 default: return true; // Skip record.
201 }
202 *dequeue_order = record.generic.order;
203 return true;
204 }
205 return false;
206}
207
208
209bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) {
210 while (true) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000211 if (!ticks_from_vm_buffer_.IsEmpty()
212 && ticks_from_vm_buffer_.Peek()->order == dequeue_order) {
213 TickSampleEventRecord record;
214 ticks_from_vm_buffer_.Dequeue(&record);
215 generator_->RecordTickSample(record.sample);
216 }
217
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000218 const TickSampleEventRecord* rec =
lrn@chromium.org25156de2010-04-06 13:10:27 +0000219 TickSampleEventRecord::cast(ticks_buffer_.StartDequeue());
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000220 if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty();
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000221 // 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) {
228 // 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 if (record.sample.frames_count < 0
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000231 || record.sample.frames_count > TickSample::kMaxFramesCount)
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000232 record.sample.frames_count = 0;
233 generator_->RecordTickSample(record.sample);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000234 ticks_buffer_.FinishDequeue();
235 } else {
236 return true;
237 }
238 }
239}
240
241
242void ProfilerEventsProcessor::Run() {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000243 unsigned dequeue_order = 0;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000244
245 while (running_) {
246 // Process ticks until we have any.
247 if (ProcessTicks(dequeue_order)) {
248 // All ticks of the current dequeue_order are processed,
249 // proceed to the next code event.
250 ProcessCodeEvent(&dequeue_order);
251 }
252 YieldCPU();
253 }
254
255 // Process remaining tick events.
256 ticks_buffer_.FlushResidualRecords();
257 // Perform processing until we have tick events, skip remaining code events.
258 while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000259}
260
261
lrn@chromium.org25156de2010-04-06 13:10:27 +0000262void CpuProfiler::StartProfiling(const char* title) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000263 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
264 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000265}
266
267
268void CpuProfiler::StartProfiling(String* title) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000269 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
270 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000271}
272
273
274CpuProfile* CpuProfiler::StopProfiling(const char* title) {
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000275 Isolate* isolate = Isolate::Current();
276 return is_profiling(isolate) ?
277 isolate->cpu_profiler()->StopCollectingProfile(title) : NULL;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000278}
279
280
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000281CpuProfile* CpuProfiler::StopProfiling(Object* security_token, String* title) {
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000282 Isolate* isolate = Isolate::Current();
283 return is_profiling(isolate) ?
284 isolate->cpu_profiler()->StopCollectingProfile(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000285 security_token, title) : NULL;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000286}
287
288
289int CpuProfiler::GetProfilesCount() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000291 // The count of profiles doesn't depend on a security token.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000292 return Isolate::Current()->cpu_profiler()->profiles_->Profiles(
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000293 TokenEnumerator::kNoSecurityToken)->length();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000294}
295
296
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000297CpuProfile* CpuProfiler::GetProfile(Object* security_token, int index) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000298 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
299 CpuProfiler* profiler = Isolate::Current()->cpu_profiler();
300 const int token = profiler->token_enumerator_->GetTokenId(security_token);
301 return profiler->profiles_->Profiles(token)->at(index);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000302}
303
304
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000305CpuProfile* CpuProfiler::FindProfile(Object* security_token, unsigned uid) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000306 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
307 CpuProfiler* profiler = Isolate::Current()->cpu_profiler();
308 const int token = profiler->token_enumerator_->GetTokenId(security_token);
309 return profiler->profiles_->GetProfile(token, uid);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000310}
311
312
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000313TickSample* CpuProfiler::TickSampleEvent(Isolate* isolate) {
314 if (CpuProfiler::is_profiling(isolate)) {
315 return isolate->cpu_profiler()->processor_->TickSampleEvent();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000316 } else {
317 return NULL;
318 }
319}
320
321
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322void CpuProfiler::DeleteAllProfiles() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000323 Isolate* isolate = Isolate::Current();
324 ASSERT(isolate->cpu_profiler() != NULL);
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000325 if (is_profiling(isolate)) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000326 isolate->cpu_profiler()->StopProcessor();
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000327 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000328 isolate->cpu_profiler()->ResetProfiles();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000329}
330
331
332void CpuProfiler::DeleteProfile(CpuProfile* profile) {
333 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
334 Isolate::Current()->cpu_profiler()->profiles_->RemoveProfile(profile);
335 delete profile;
336}
337
338
339bool CpuProfiler::HasDetachedProfiles() {
340 ASSERT(Isolate::Current()->cpu_profiler() != NULL);
341 return Isolate::Current()->cpu_profiler()->profiles_->HasDetachedProfiles();
342}
343
344
lrn@chromium.org25156de2010-04-06 13:10:27 +0000345void CpuProfiler::CallbackEvent(String* name, Address entry_point) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000346 Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000347 Logger::CALLBACK_TAG, CodeEntry::kEmptyNamePrefix, name, entry_point);
348}
349
350
351void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
352 Code* code, const char* comment) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000353 Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000354 tag, comment, code->address(), code->ExecutableSize());
355}
356
357
358void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
359 Code* code, String* name) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000360 Isolate* isolate = Isolate::Current();
361 isolate->cpu_profiler()->processor_->CodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000362 tag,
363 name,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000364 isolate->heap()->empty_string(),
ager@chromium.org357bf652010-04-12 11:30:10 +0000365 v8::CpuProfileNode::kNoLineNumberInfo,
lrn@chromium.org25156de2010-04-06 13:10:27 +0000366 code->address(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000367 code->ExecutableSize(),
368 NULL);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000369}
370
371
372void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000373 Code* code,
374 SharedFunctionInfo* shared,
375 String* name) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000376 Isolate* isolate = Isolate::Current();
377 isolate->cpu_profiler()->processor_->CodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000378 tag,
379 name,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000380 isolate->heap()->empty_string(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000381 v8::CpuProfileNode::kNoLineNumberInfo,
382 code->address(),
383 code->ExecutableSize(),
384 shared->address());
385}
386
387
388void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
389 Code* code,
390 SharedFunctionInfo* shared,
391 String* source, int line) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000392 Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent(
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000393 tag,
394 shared->DebugName(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000395 source,
396 line,
397 code->address(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000398 code->ExecutableSize(),
399 shared->address());
lrn@chromium.org25156de2010-04-06 13:10:27 +0000400}
401
402
403void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
404 Code* code, int args_count) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405 Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000406 tag,
407 args_count,
408 code->address(),
409 code->ExecutableSize());
410}
411
412
413void CpuProfiler::CodeMoveEvent(Address from, Address to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000414 Isolate::Current()->cpu_profiler()->processor_->CodeMoveEvent(from, to);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000415}
416
417
418void CpuProfiler::CodeDeleteEvent(Address from) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000419}
420
421
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000422void CpuProfiler::SharedFunctionInfoMoveEvent(Address from, Address to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000423 CpuProfiler* profiler = Isolate::Current()->cpu_profiler();
424 profiler->processor_->SharedFunctionInfoMoveEvent(from, to);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000425}
426
427
428void CpuProfiler::GetterCallbackEvent(String* name, Address entry_point) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000429 Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000430 Logger::CALLBACK_TAG, "get ", name, entry_point);
431}
432
433
434void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000435 Isolate::Current()->cpu_profiler()->processor_->RegExpCodeCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000436 Logger::REG_EXP_TAG,
ager@chromium.org357bf652010-04-12 11:30:10 +0000437 "RegExp: ",
lrn@chromium.org25156de2010-04-06 13:10:27 +0000438 source,
lrn@chromium.org25156de2010-04-06 13:10:27 +0000439 code->address(),
440 code->ExecutableSize());
441}
442
443
444void CpuProfiler::SetterCallbackEvent(String* name, Address entry_point) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000445 Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent(
lrn@chromium.org25156de2010-04-06 13:10:27 +0000446 Logger::CALLBACK_TAG, "set ", name, entry_point);
447}
448
449
450CpuProfiler::CpuProfiler()
451 : profiles_(new CpuProfilesCollection()),
452 next_profile_uid_(1),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000453 token_enumerator_(new TokenEnumerator()),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000454 generator_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000455 processor_(NULL),
456 need_to_stop_sampler_(false),
457 is_profiling_(false) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000458}
459
460
461CpuProfiler::~CpuProfiler() {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000462 delete token_enumerator_;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000463 delete profiles_;
464}
465
466
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000467void CpuProfiler::ResetProfiles() {
468 delete profiles_;
469 profiles_ = new CpuProfilesCollection();
470}
471
lrn@chromium.org25156de2010-04-06 13:10:27 +0000472void CpuProfiler::StartCollectingProfile(const char* title) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000473 if (profiles_->StartProfiling(title, next_profile_uid_++)) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000474 StartProcessorIfNotStarted();
475 }
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000476 processor_->AddCurrentStack();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000477}
478
479
480void CpuProfiler::StartCollectingProfile(String* title) {
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000481 StartCollectingProfile(profiles_->GetName(title));
lrn@chromium.org25156de2010-04-06 13:10:27 +0000482}
483
484
485void CpuProfiler::StartProcessorIfNotStarted() {
486 if (processor_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000487 Isolate* isolate = Isolate::Current();
488
ager@chromium.org357bf652010-04-12 11:30:10 +0000489 // Disable logging when using the new implementation.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000490 saved_logging_nesting_ = isolate->logger()->logging_nesting_;
491 isolate->logger()->logging_nesting_ = 0;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000492 generator_ = new ProfileGenerator(profiles_);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000493 processor_ = new ProfilerEventsProcessor(generator_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000494 NoBarrier_Store(&is_profiling_, true);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000495 processor_->Start();
496 // Enumerate stuff we already have in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000497 if (isolate->heap()->HasBeenSetUp()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000498 if (!FLAG_prof_browser_mode) {
499 bool saved_log_code_flag = FLAG_log_code;
500 FLAG_log_code = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000501 isolate->logger()->LogCodeObjects();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000502 FLAG_log_code = saved_log_code_flag;
503 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000504 isolate->logger()->LogCompiledFunctions();
505 isolate->logger()->LogAccessorCallbacks();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000506 }
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000507 // Enable stack sampling.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000508 Sampler* sampler = reinterpret_cast<Sampler*>(isolate->logger()->ticker_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000509 if (!sampler->IsActive()) {
510 sampler->Start();
511 need_to_stop_sampler_ = true;
512 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000513 sampler->IncreaseProfilingDepth();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000514 }
515}
516
517
518CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000519 const double actual_sampling_rate = generator_->actual_sampling_rate();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000520 StopProcessorIfLastProfile(title);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000521 CpuProfile* result =
522 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken,
523 title,
524 actual_sampling_rate);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000525 if (result != NULL) {
526 result->Print();
527 }
528 return result;
529}
530
531
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000532CpuProfile* CpuProfiler::StopCollectingProfile(Object* security_token,
533 String* title) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000534 const double actual_sampling_rate = generator_->actual_sampling_rate();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000535 const char* profile_title = profiles_->GetName(title);
536 StopProcessorIfLastProfile(profile_title);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000537 int token = token_enumerator_->GetTokenId(security_token);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000538 return profiles_->StopProfiling(token, profile_title, actual_sampling_rate);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000539}
540
541
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000542void CpuProfiler::StopProcessorIfLastProfile(const char* title) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000543 if (profiles_->IsLastProfile(title)) StopProcessor();
544}
545
546
547void CpuProfiler::StopProcessor() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000548 Logger* logger = Isolate::Current()->logger();
549 Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000550 sampler->DecreaseProfilingDepth();
551 if (need_to_stop_sampler_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000552 sampler->Stop();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000553 need_to_stop_sampler_ = false;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000554 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000555 NoBarrier_Store(&is_profiling_, false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000556 processor_->Stop();
557 processor_->Join();
558 delete processor_;
559 delete generator_;
560 processor_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000561 generator_ = NULL;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000562 logger->logging_nesting_ = saved_logging_nesting_;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000563}
564
lrn@chromium.org25156de2010-04-06 13:10:27 +0000565
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000566void CpuProfiler::SetUp() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000567 Isolate* isolate = Isolate::Current();
568 if (isolate->cpu_profiler() == NULL) {
569 isolate->set_cpu_profiler(new CpuProfiler());
lrn@chromium.org25156de2010-04-06 13:10:27 +0000570 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000571}
572
573
574void CpuProfiler::TearDown() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000575 Isolate* isolate = Isolate::Current();
576 if (isolate->cpu_profiler() != NULL) {
577 delete isolate->cpu_profiler();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000578 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000579 isolate->set_cpu_profiler(NULL);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000580}
581
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000582} } // namespace v8::internal