Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1 | // 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 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 32 | #include "frames-inl.h" |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 33 | #include "hashmap.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 34 | #include "log-inl.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 35 | #include "vm-state-inl.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 36 | |
| 37 | #include "../include/v8-profiler.h" |
| 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
| 42 | static const int kEventsBufferSize = 256*KB; |
| 43 | static const int kTickSamplesBufferChunkSize = 64*KB; |
| 44 | static const int kTickSamplesBufferChunksCount = 16; |
| 45 | |
| 46 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 47 | ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator) |
| 48 | : Thread("v8:ProfEvntProc"), |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 49 | generator_(generator), |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 50 | running_(true), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 51 | ticks_buffer_(sizeof(TickSampleEventRecord), |
| 52 | kTickSamplesBufferChunkSize, |
| 53 | kTickSamplesBufferChunksCount), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 54 | enqueue_order_(0) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 55 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 56 | |
| 57 | |
| 58 | void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag, |
| 59 | const char* prefix, |
| 60 | String* name, |
| 61 | Address start) { |
| 62 | if (FilterOutCodeCreateEvent(tag)) return; |
| 63 | 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; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 70 | rec->shared = NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 71 | events_buffer_.Enqueue(evt_rec); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 76 | String* name, |
| 77 | String* resource_name, |
| 78 | int line_number, |
| 79 | Address start, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 80 | unsigned size, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 81 | Address shared) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 82 | if (FilterOutCodeCreateEvent(tag)) return; |
| 83 | 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; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 90 | rec->shared = shared; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 91 | events_buffer_.Enqueue(evt_rec); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 96 | const char* name, |
| 97 | Address start, |
| 98 | unsigned size) { |
| 99 | if (FilterOutCodeCreateEvent(tag)) return; |
| 100 | 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; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 107 | rec->shared = NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 108 | events_buffer_.Enqueue(evt_rec); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 113 | int args_count, |
| 114 | Address start, |
| 115 | unsigned size) { |
| 116 | if (FilterOutCodeCreateEvent(tag)) return; |
| 117 | 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; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 124 | rec->shared = NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 125 | events_buffer_.Enqueue(evt_rec); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void 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 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 140 | void ProfilerEventsProcessor::SharedFunctionInfoMoveEvent(Address from, |
| 141 | Address to) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 142 | CodeEventsContainer evt_rec; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 143 | SharedFunctionInfoMoveEventRecord* rec = |
| 144 | &evt_rec.SharedFunctionInfoMoveEventRecord_; |
| 145 | rec->type = CodeEventRecord::SHARED_FUNC_MOVE; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 146 | rec->order = ++enqueue_order_; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 147 | rec->from = from; |
| 148 | rec->to = to; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 149 | events_buffer_.Enqueue(evt_rec); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 153 | void ProfilerEventsProcessor::RegExpCodeCreateEvent( |
| 154 | Logger::LogEventsAndTags tag, |
| 155 | const char* prefix, |
| 156 | String* name, |
| 157 | Address start, |
| 158 | unsigned size) { |
| 159 | if (FilterOutCodeCreateEvent(tag)) return; |
| 160 | CodeEventsContainer evt_rec; |
| 161 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 162 | rec->type = CodeEventRecord::CODE_CREATION; |
| 163 | rec->order = ++enqueue_order_; |
| 164 | rec->start = start; |
| 165 | rec->entry = generator_->NewCodeEntry(tag, prefix, name); |
| 166 | rec->size = size; |
| 167 | events_buffer_.Enqueue(evt_rec); |
| 168 | } |
| 169 | |
| 170 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 171 | void ProfilerEventsProcessor::AddCurrentStack() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 172 | TickSampleEventRecord record(enqueue_order_); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 173 | TickSample* sample = &record.sample; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 174 | Isolate* isolate = Isolate::Current(); |
| 175 | sample->state = isolate->current_vm_state(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 176 | sample->pc = reinterpret_cast<Address>(sample); // Not NULL. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 177 | for (StackTraceFrameIterator it(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 178 | !it.done() && sample->frames_count < TickSample::kMaxFramesCount; |
| 179 | it.Advance()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 180 | sample->stack[sample->frames_count++] = it.frame()->pc(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 181 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 182 | ticks_from_vm_buffer_.Enqueue(record); |
| 183 | } |
| 184 | |
| 185 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 186 | bool ProfilerEventsProcessor::ProcessCodeEvent(unsigned* dequeue_order) { |
| 187 | if (!events_buffer_.IsEmpty()) { |
| 188 | CodeEventsContainer record; |
| 189 | events_buffer_.Dequeue(&record); |
| 190 | switch (record.generic.type) { |
| 191 | #define PROFILER_TYPE_CASE(type, clss) \ |
| 192 | case CodeEventRecord::type: \ |
| 193 | record.clss##_.UpdateCodeMap(generator_->code_map()); \ |
| 194 | break; |
| 195 | |
| 196 | CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE) |
| 197 | |
| 198 | #undef PROFILER_TYPE_CASE |
| 199 | default: return true; // Skip record. |
| 200 | } |
| 201 | *dequeue_order = record.generic.order; |
| 202 | return true; |
| 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) { |
| 209 | while (true) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 210 | if (!ticks_from_vm_buffer_.IsEmpty() |
| 211 | && ticks_from_vm_buffer_.Peek()->order == dequeue_order) { |
| 212 | TickSampleEventRecord record; |
| 213 | ticks_from_vm_buffer_.Dequeue(&record); |
| 214 | generator_->RecordTickSample(record.sample); |
| 215 | } |
| 216 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 217 | const TickSampleEventRecord* rec = |
| 218 | TickSampleEventRecord::cast(ticks_buffer_.StartDequeue()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 219 | if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty(); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 220 | // Make a local copy of tick sample record to ensure that it won't |
| 221 | // be modified as we are processing it. This is possible as the |
| 222 | // sampler writes w/o any sync to the queue, so if the processor |
| 223 | // will get far behind, a record may be modified right under its |
| 224 | // feet. |
| 225 | TickSampleEventRecord record = *rec; |
| 226 | if (record.order == dequeue_order) { |
| 227 | // A paranoid check to make sure that we don't get a memory overrun |
| 228 | // in case of frames_count having a wild value. |
| 229 | if (record.sample.frames_count < 0 |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 230 | || record.sample.frames_count > TickSample::kMaxFramesCount) |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 231 | record.sample.frames_count = 0; |
| 232 | generator_->RecordTickSample(record.sample); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 233 | ticks_buffer_.FinishDequeue(); |
| 234 | } else { |
| 235 | return true; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | void ProfilerEventsProcessor::Run() { |
| 242 | unsigned dequeue_order = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 243 | |
| 244 | while (running_) { |
| 245 | // Process ticks until we have any. |
| 246 | if (ProcessTicks(dequeue_order)) { |
| 247 | // All ticks of the current dequeue_order are processed, |
| 248 | // proceed to the next code event. |
| 249 | ProcessCodeEvent(&dequeue_order); |
| 250 | } |
| 251 | YieldCPU(); |
| 252 | } |
| 253 | |
| 254 | // Process remaining tick events. |
| 255 | ticks_buffer_.FlushResidualRecords(); |
| 256 | // Perform processing until we have tick events, skip remaining code events. |
| 257 | while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { } |
| 258 | } |
| 259 | |
| 260 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 261 | void CpuProfiler::StartProfiling(const char* title) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 262 | ASSERT(Isolate::Current()->cpu_profiler() != NULL); |
| 263 | Isolate::Current()->cpu_profiler()->StartCollectingProfile(title); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
| 267 | void CpuProfiler::StartProfiling(String* title) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 268 | ASSERT(Isolate::Current()->cpu_profiler() != NULL); |
| 269 | Isolate::Current()->cpu_profiler()->StartCollectingProfile(title); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
| 273 | CpuProfile* CpuProfiler::StopProfiling(const char* title) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 274 | Isolate* isolate = Isolate::Current(); |
| 275 | return is_profiling(isolate) ? |
| 276 | isolate->cpu_profiler()->StopCollectingProfile(title) : NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 280 | CpuProfile* CpuProfiler::StopProfiling(Object* security_token, String* title) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 281 | Isolate* isolate = Isolate::Current(); |
| 282 | return is_profiling(isolate) ? |
| 283 | isolate->cpu_profiler()->StopCollectingProfile( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 284 | security_token, title) : NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | int CpuProfiler::GetProfilesCount() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 289 | ASSERT(Isolate::Current()->cpu_profiler() != NULL); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 290 | // The count of profiles doesn't depend on a security token. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 291 | return Isolate::Current()->cpu_profiler()->profiles_->Profiles( |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 292 | TokenEnumerator::kNoSecurityToken)->length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 296 | CpuProfile* CpuProfiler::GetProfile(Object* security_token, int index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 297 | ASSERT(Isolate::Current()->cpu_profiler() != NULL); |
| 298 | CpuProfiler* profiler = Isolate::Current()->cpu_profiler(); |
| 299 | const int token = profiler->token_enumerator_->GetTokenId(security_token); |
| 300 | return profiler->profiles_->Profiles(token)->at(index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 304 | CpuProfile* CpuProfiler::FindProfile(Object* security_token, unsigned uid) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 305 | ASSERT(Isolate::Current()->cpu_profiler() != NULL); |
| 306 | CpuProfiler* profiler = Isolate::Current()->cpu_profiler(); |
| 307 | const int token = profiler->token_enumerator_->GetTokenId(security_token); |
| 308 | return profiler->profiles_->GetProfile(token, uid); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 312 | TickSample* CpuProfiler::TickSampleEvent(Isolate* isolate) { |
| 313 | if (CpuProfiler::is_profiling(isolate)) { |
| 314 | return isolate->cpu_profiler()->processor_->TickSampleEvent(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 315 | } else { |
| 316 | return NULL; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 321 | void CpuProfiler::DeleteAllProfiles() { |
| 322 | Isolate* isolate = Isolate::Current(); |
| 323 | ASSERT(isolate->cpu_profiler() != NULL); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 324 | if (is_profiling(isolate)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 325 | isolate->cpu_profiler()->StopProcessor(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 326 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 327 | isolate->cpu_profiler()->ResetProfiles(); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | void CpuProfiler::DeleteProfile(CpuProfile* profile) { |
| 332 | ASSERT(Isolate::Current()->cpu_profiler() != NULL); |
| 333 | Isolate::Current()->cpu_profiler()->profiles_->RemoveProfile(profile); |
| 334 | delete profile; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | bool CpuProfiler::HasDetachedProfiles() { |
| 339 | ASSERT(Isolate::Current()->cpu_profiler() != NULL); |
| 340 | return Isolate::Current()->cpu_profiler()->profiles_->HasDetachedProfiles(); |
| 341 | } |
| 342 | |
| 343 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 344 | void CpuProfiler::CallbackEvent(String* name, Address entry_point) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 345 | Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 346 | Logger::CALLBACK_TAG, CodeEntry::kEmptyNamePrefix, name, entry_point); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 351 | Code* code, const char* comment) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 352 | Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 353 | tag, comment, code->address(), code->ExecutableSize()); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 358 | Code* code, String* name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 359 | Isolate* isolate = Isolate::Current(); |
| 360 | isolate->cpu_profiler()->processor_->CodeCreateEvent( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 361 | tag, |
| 362 | name, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 363 | isolate->heap()->empty_string(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 364 | v8::CpuProfileNode::kNoLineNumberInfo, |
| 365 | code->address(), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 366 | code->ExecutableSize(), |
| 367 | NULL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
| 371 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 372 | Code* code, |
| 373 | SharedFunctionInfo* shared, |
| 374 | String* name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 375 | Isolate* isolate = Isolate::Current(); |
| 376 | isolate->cpu_profiler()->processor_->CodeCreateEvent( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 377 | tag, |
| 378 | name, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 379 | isolate->heap()->empty_string(), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 380 | v8::CpuProfileNode::kNoLineNumberInfo, |
| 381 | code->address(), |
| 382 | code->ExecutableSize(), |
| 383 | shared->address()); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 388 | Code* code, |
| 389 | SharedFunctionInfo* shared, |
| 390 | String* source, int line) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 391 | Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent( |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 392 | tag, |
| 393 | shared->DebugName(), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 394 | source, |
| 395 | line, |
| 396 | code->address(), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 397 | code->ExecutableSize(), |
| 398 | shared->address()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
| 402 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 403 | Code* code, int args_count) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 404 | Isolate::Current()->cpu_profiler()->processor_->CodeCreateEvent( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 405 | tag, |
| 406 | args_count, |
| 407 | code->address(), |
| 408 | code->ExecutableSize()); |
| 409 | } |
| 410 | |
| 411 | |
| 412 | void CpuProfiler::CodeMoveEvent(Address from, Address to) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 413 | Isolate::Current()->cpu_profiler()->processor_->CodeMoveEvent(from, to); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
| 417 | void CpuProfiler::CodeDeleteEvent(Address from) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 421 | void CpuProfiler::SharedFunctionInfoMoveEvent(Address from, Address to) { |
| 422 | CpuProfiler* profiler = Isolate::Current()->cpu_profiler(); |
| 423 | profiler->processor_->SharedFunctionInfoMoveEvent(from, to); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
| 427 | void CpuProfiler::GetterCallbackEvent(String* name, Address entry_point) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 428 | Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 429 | Logger::CALLBACK_TAG, "get ", name, entry_point); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 434 | Isolate::Current()->cpu_profiler()->processor_->RegExpCodeCreateEvent( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 435 | Logger::REG_EXP_TAG, |
| 436 | "RegExp: ", |
| 437 | source, |
| 438 | code->address(), |
| 439 | code->ExecutableSize()); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | void CpuProfiler::SetterCallbackEvent(String* name, Address entry_point) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 444 | Isolate::Current()->cpu_profiler()->processor_->CallbackCreateEvent( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 445 | Logger::CALLBACK_TAG, "set ", name, entry_point); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | CpuProfiler::CpuProfiler() |
| 450 | : profiles_(new CpuProfilesCollection()), |
| 451 | next_profile_uid_(1), |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 452 | token_enumerator_(new TokenEnumerator()), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 453 | generator_(NULL), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 454 | processor_(NULL), |
| 455 | need_to_stop_sampler_(false), |
| 456 | is_profiling_(false) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
| 460 | CpuProfiler::~CpuProfiler() { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 461 | delete token_enumerator_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 462 | delete profiles_; |
| 463 | } |
| 464 | |
| 465 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 466 | void CpuProfiler::ResetProfiles() { |
| 467 | delete profiles_; |
| 468 | profiles_ = new CpuProfilesCollection(); |
| 469 | } |
| 470 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 471 | void CpuProfiler::StartCollectingProfile(const char* title) { |
| 472 | if (profiles_->StartProfiling(title, next_profile_uid_++)) { |
| 473 | StartProcessorIfNotStarted(); |
| 474 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 475 | processor_->AddCurrentStack(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | |
| 479 | void CpuProfiler::StartCollectingProfile(String* title) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 480 | StartCollectingProfile(profiles_->GetName(title)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
| 484 | void CpuProfiler::StartProcessorIfNotStarted() { |
| 485 | if (processor_ == NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 486 | Isolate* isolate = Isolate::Current(); |
| 487 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 488 | // Disable logging when using the new implementation. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 489 | saved_logging_nesting_ = isolate->logger()->logging_nesting_; |
| 490 | isolate->logger()->logging_nesting_ = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 491 | generator_ = new ProfileGenerator(profiles_); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 492 | processor_ = new ProfilerEventsProcessor(generator_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 493 | NoBarrier_Store(&is_profiling_, true); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 494 | processor_->Start(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 495 | // Enumerate stuff we already have in the heap. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 496 | if (isolate->heap()->HasBeenSetup()) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 497 | if (!FLAG_prof_browser_mode) { |
| 498 | bool saved_log_code_flag = FLAG_log_code; |
| 499 | FLAG_log_code = true; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 500 | isolate->logger()->LogCodeObjects(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 501 | FLAG_log_code = saved_log_code_flag; |
| 502 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 503 | isolate->logger()->LogCompiledFunctions(); |
| 504 | isolate->logger()->LogAccessorCallbacks(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 505 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 506 | // Enable stack sampling. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 507 | Sampler* sampler = reinterpret_cast<Sampler*>(isolate->logger()->ticker_); |
| 508 | if (!sampler->IsActive()) { |
| 509 | sampler->Start(); |
| 510 | need_to_stop_sampler_ = true; |
| 511 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 512 | sampler->IncreaseProfilingDepth(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | |
| 517 | CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) { |
| 518 | const double actual_sampling_rate = generator_->actual_sampling_rate(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 519 | StopProcessorIfLastProfile(title); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 520 | CpuProfile* result = |
| 521 | profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken, |
| 522 | title, |
| 523 | actual_sampling_rate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 524 | if (result != NULL) { |
| 525 | result->Print(); |
| 526 | } |
| 527 | return result; |
| 528 | } |
| 529 | |
| 530 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 531 | CpuProfile* CpuProfiler::StopCollectingProfile(Object* security_token, |
| 532 | String* title) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 533 | const double actual_sampling_rate = generator_->actual_sampling_rate(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 534 | const char* profile_title = profiles_->GetName(title); |
| 535 | StopProcessorIfLastProfile(profile_title); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 536 | int token = token_enumerator_->GetTokenId(security_token); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 537 | return profiles_->StopProfiling(token, profile_title, actual_sampling_rate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 541 | void CpuProfiler::StopProcessorIfLastProfile(const char* title) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 542 | if (profiles_->IsLastProfile(title)) StopProcessor(); |
| 543 | } |
| 544 | |
| 545 | |
| 546 | void CpuProfiler::StopProcessor() { |
| 547 | Logger* logger = Isolate::Current()->logger(); |
| 548 | Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_); |
| 549 | sampler->DecreaseProfilingDepth(); |
| 550 | if (need_to_stop_sampler_) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 551 | sampler->Stop(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 552 | need_to_stop_sampler_ = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 553 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 554 | NoBarrier_Store(&is_profiling_, false); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 555 | processor_->Stop(); |
| 556 | processor_->Join(); |
| 557 | delete processor_; |
| 558 | delete generator_; |
| 559 | processor_ = NULL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 560 | generator_ = NULL; |
| 561 | logger->logging_nesting_ = saved_logging_nesting_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 562 | } |
| 563 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 564 | |
| 565 | void CpuProfiler::Setup() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 566 | Isolate* isolate = Isolate::Current(); |
| 567 | if (isolate->cpu_profiler() == NULL) { |
| 568 | isolate->set_cpu_profiler(new CpuProfiler()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 569 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | |
| 573 | void CpuProfiler::TearDown() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 574 | Isolate* isolate = Isolate::Current(); |
| 575 | if (isolate->cpu_profiler() != NULL) { |
| 576 | delete isolate->cpu_profiler(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 577 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 578 | isolate->set_cpu_profiler(NULL); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | } } // namespace v8::internal |