blob: 6eb3c9b0ba6ca6b36c911195aa98abf0ff1a95e6 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 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 <stdarg.h>
29
30#include "v8.h"
31
32#include "bootstrapper.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010033#include "code-stubs.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010034#include "deoptimizer.h"
Steve Blockd0582a62009-12-15 09:54:21 +000035#include "global-handles.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036#include "log.h"
37#include "macro-assembler.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010038#include "runtime-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039#include "serialize.h"
40#include "string-stream.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010041#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000042
43namespace v8 {
44namespace internal {
45
46#ifdef ENABLE_LOGGING_AND_PROFILING
47
48//
49// Sliding state window. Updates counters to keep track of the last
50// window of kBufferSize states. This is useful to track where we
51// spent our time.
52//
53class SlidingStateWindow {
54 public:
55 SlidingStateWindow();
56 ~SlidingStateWindow();
57 void AddState(StateTag state);
58
59 private:
60 static const int kBufferSize = 256;
61 int current_index_;
62 bool is_full_;
63 byte buffer_[kBufferSize];
64
65
66 void IncrementStateCounter(StateTag state) {
67 Counters::state_counters[state].Increment();
68 }
69
70
71 void DecrementStateCounter(StateTag state) {
72 Counters::state_counters[state].Decrement();
73 }
74};
75
76
77//
78// The Profiler samples pc and sp values for the main thread.
79// Each sample is appended to a circular buffer.
80// An independent thread removes data and writes it to the log.
81// This design minimizes the time spent in the sampler.
82//
83class Profiler: public Thread {
84 public:
85 Profiler();
86 void Engage();
87 void Disengage();
88
89 // Inserts collected profiling data into buffer.
90 void Insert(TickSample* sample) {
91 if (paused_)
92 return;
93
94 if (Succ(head_) == tail_) {
95 overflow_ = true;
96 } else {
97 buffer_[head_] = *sample;
98 head_ = Succ(head_);
99 buffer_semaphore_->Signal(); // Tell we have an element.
100 }
101 }
102
103 // Waits for a signal and removes profiling data.
104 bool Remove(TickSample* sample) {
105 buffer_semaphore_->Wait(); // Wait for an element.
106 *sample = buffer_[tail_];
107 bool result = overflow_;
108 tail_ = Succ(tail_);
109 overflow_ = false;
110 return result;
111 }
112
113 void Run();
114
115 // Pause and Resume TickSample data collection.
116 static bool paused() { return paused_; }
117 static void pause() { paused_ = true; }
118 static void resume() { paused_ = false; }
119
120 private:
121 // Returns the next index in the cyclic buffer.
122 int Succ(int index) { return (index + 1) % kBufferSize; }
123
124 // Cyclic buffer for communicating profiling samples
125 // between the signal handler and the worker thread.
126 static const int kBufferSize = 128;
127 TickSample buffer_[kBufferSize]; // Buffer storage.
128 int head_; // Index to the buffer head.
129 int tail_; // Index to the buffer tail.
130 bool overflow_; // Tell whether a buffer overflow has occurred.
131 Semaphore* buffer_semaphore_; // Sempahore used for buffer synchronization.
132
Steve Blockd0582a62009-12-15 09:54:21 +0000133 // Tells whether profiler is engaged, that is, processing thread is stated.
134 bool engaged_;
135
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 // Tells whether worker thread should continue running.
137 bool running_;
138
139 // Tells whether we are currently recording tick samples.
140 static bool paused_;
141};
142
143bool Profiler::paused_ = false;
144
145
146//
147// StackTracer implementation
148//
149void StackTracer::Trace(TickSample* sample) {
Steve Block6ded16b2010-05-10 14:33:55 +0100150 sample->function = NULL;
151 sample->frames_count = 0;
152
Ben Murdochb0fe1622011-05-05 13:52:32 +0100153 // Avoid collecting traces while doing GC.
Steve Block6ded16b2010-05-10 14:33:55 +0100154 if (sample->state == GC) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000155
156 const Address js_entry_sp = Top::js_entry_sp(Top::GetCurrentThread());
157 if (js_entry_sp == 0) {
158 // Not executing JS now.
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 return;
160 }
161
Ben Murdochb0fe1622011-05-05 13:52:32 +0100162 const Address function_address =
Leon Clarked91b9f72010-01-27 17:25:45 +0000163 sample->fp + JavaScriptFrameConstants::kFunctionOffset;
164 if (SafeStackFrameIterator::IsWithinBounds(sample->sp, js_entry_sp,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100165 function_address)) {
166 Object* object = Memory::Object_at(function_address);
167 if (object->IsHeapObject()) {
168 sample->function = HeapObject::cast(object)->address();
169 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000170 }
171
Steve Blockd0582a62009-12-15 09:54:21 +0000172 int i = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100173 const Address callback = Top::external_callback();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800174 // Surprisingly, PC can point _exactly_ to callback start, with good
175 // probability, and this will result in reporting fake nested
176 // callback call.
177 if (callback != NULL && callback != sample->pc) {
Steve Blockd0582a62009-12-15 09:54:21 +0000178 sample->stack[i++] = callback;
179 }
180
Leon Clarked91b9f72010-01-27 17:25:45 +0000181 SafeStackTraceFrameIterator it(sample->fp, sample->sp,
182 sample->sp, js_entry_sp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 while (!it.done() && i < TickSample::kMaxFramesCount) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100184 Object* object = it.frame()->function_slot_object();
185 if (object->IsHeapObject()) {
186 sample->stack[i++] = HeapObject::cast(object)->address();
187 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 it.Advance();
189 }
190 sample->frames_count = i;
191}
192
193
194//
195// Ticker used to provide ticks to the profiler and the sliding state
196// window.
197//
198class Ticker: public Sampler {
199 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100200 explicit Ticker(int interval) :
201 Sampler(interval),
202 window_(NULL),
203 profiler_(NULL) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000204
205 ~Ticker() { if (IsActive()) Stop(); }
206
Ben Murdochf87a2032010-10-22 12:50:53 +0100207 virtual void Tick(TickSample* sample) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 if (profiler_) profiler_->Insert(sample);
209 if (window_) window_->AddState(sample->state);
210 }
211
212 void SetWindow(SlidingStateWindow* window) {
213 window_ = window;
214 if (!IsActive()) Start();
215 }
216
217 void ClearWindow() {
218 window_ = NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100219 if (!profiler_ && IsActive() && !RuntimeProfiler::IsEnabled()) Stop();
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 }
221
222 void SetProfiler(Profiler* profiler) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100223 ASSERT(profiler_ == NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 profiler_ = profiler;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100225 IncreaseProfilingDepth();
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 if (!FLAG_prof_lazy && !IsActive()) Start();
227 }
228
229 void ClearProfiler() {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100230 DecreaseProfilingDepth();
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 profiler_ = NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100232 if (!window_ && IsActive() && !RuntimeProfiler::IsEnabled()) Stop();
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 }
234
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800235 protected:
236 virtual void DoSampleStack(TickSample* sample) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800237 StackTracer::Trace(sample);
238 }
239
Steve Blocka7e24c12009-10-30 11:49:00 +0000240 private:
241 SlidingStateWindow* window_;
242 Profiler* profiler_;
243};
244
245
246//
247// SlidingStateWindow implementation.
248//
249SlidingStateWindow::SlidingStateWindow(): current_index_(0), is_full_(false) {
250 for (int i = 0; i < kBufferSize; i++) {
251 buffer_[i] = static_cast<byte>(OTHER);
252 }
253 Logger::ticker_->SetWindow(this);
254}
255
256
257SlidingStateWindow::~SlidingStateWindow() {
258 Logger::ticker_->ClearWindow();
259}
260
261
262void SlidingStateWindow::AddState(StateTag state) {
263 if (is_full_) {
264 DecrementStateCounter(static_cast<StateTag>(buffer_[current_index_]));
265 } else if (current_index_ == kBufferSize - 1) {
266 is_full_ = true;
267 }
268 buffer_[current_index_] = static_cast<byte>(state);
269 IncrementStateCounter(state);
270 ASSERT(IsPowerOf2(kBufferSize));
271 current_index_ = (current_index_ + 1) & (kBufferSize - 1);
272}
273
274
275//
276// Profiler implementation.
277//
Steve Blockd0582a62009-12-15 09:54:21 +0000278Profiler::Profiler()
Steve Block9fac8402011-05-12 15:51:54 +0100279 : Thread("v8:Profiler"),
280 head_(0),
Steve Blockd0582a62009-12-15 09:54:21 +0000281 tail_(0),
282 overflow_(false),
283 buffer_semaphore_(OS::CreateSemaphore(0)),
284 engaged_(false),
285 running_(false) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000286}
287
288
289void Profiler::Engage() {
Steve Blockd0582a62009-12-15 09:54:21 +0000290 if (engaged_) return;
291 engaged_ = true;
292
293 // TODO(mnaganov): This is actually "Chromium" mode. Flags need to be revised.
294 // http://code.google.com/p/v8/issues/detail?id=487
295 if (!FLAG_prof_lazy) {
296 OS::LogSharedLibraryAddresses();
297 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000298
299 // Start thread processing the profiler buffer.
300 running_ = true;
301 Start();
302
303 // Register to get ticks.
304 Logger::ticker_->SetProfiler(this);
305
306 Logger::ProfilerBeginEvent();
Steve Blocka7e24c12009-10-30 11:49:00 +0000307}
308
309
310void Profiler::Disengage() {
Steve Blockd0582a62009-12-15 09:54:21 +0000311 if (!engaged_) return;
312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 // Stop receiving ticks.
314 Logger::ticker_->ClearProfiler();
315
316 // Terminate the worker thread by setting running_ to false,
317 // inserting a fake element in the queue and then wait for
318 // the thread to terminate.
319 running_ = false;
320 TickSample sample;
321 // Reset 'paused_' flag, otherwise semaphore may not be signalled.
322 resume();
323 Insert(&sample);
324 Join();
325
326 LOG(UncheckedStringEvent("profiler", "end"));
327}
328
329
330void Profiler::Run() {
331 TickSample sample;
Steve Block8defd9f2010-07-08 12:39:36 +0100332 bool overflow = Remove(&sample);
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 while (running_) {
334 LOG(TickEvent(&sample, overflow));
Steve Block8defd9f2010-07-08 12:39:36 +0100335 overflow = Remove(&sample);
Steve Blocka7e24c12009-10-30 11:49:00 +0000336 }
337}
338
339
340//
341// Logger class implementation.
342//
343Ticker* Logger::ticker_ = NULL;
344Profiler* Logger::profiler_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000345SlidingStateWindow* Logger::sliding_state_window_ = NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100346int Logger::logging_nesting_ = 0;
Andrei Popescu402d9372010-02-26 13:31:12 +0000347int Logger::cpu_profiler_nesting_ = 0;
348int Logger::heap_profiler_nesting_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000349
Ben Murdochb0fe1622011-05-05 13:52:32 +0100350#define DECLARE_EVENT(ignore1, name) name,
351const char* kLogEventsNames[Logger::NUMBER_OF_LOG_EVENTS] = {
352 LOG_EVENTS_AND_TAGS_LIST(DECLARE_EVENT)
Steve Blocka7e24c12009-10-30 11:49:00 +0000353};
Ben Murdochb0fe1622011-05-05 13:52:32 +0100354#undef DECLARE_EVENT
Steve Blocka7e24c12009-10-30 11:49:00 +0000355
356
357void Logger::ProfilerBeginEvent() {
358 if (!Log::IsEnabled()) return;
359 LogMessageBuilder msg;
360 msg.Append("profiler,\"begin\",%d\n", kSamplingIntervalMs);
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 msg.WriteToLogFile();
362}
363
364#endif // ENABLE_LOGGING_AND_PROFILING
365
366
Steve Blocka7e24c12009-10-30 11:49:00 +0000367void Logger::StringEvent(const char* name, const char* value) {
368#ifdef ENABLE_LOGGING_AND_PROFILING
369 if (FLAG_log) UncheckedStringEvent(name, value);
370#endif
371}
372
373
374#ifdef ENABLE_LOGGING_AND_PROFILING
375void Logger::UncheckedStringEvent(const char* name, const char* value) {
376 if (!Log::IsEnabled()) return;
377 LogMessageBuilder msg;
378 msg.Append("%s,\"%s\"\n", name, value);
379 msg.WriteToLogFile();
380}
381#endif
382
383
384void Logger::IntEvent(const char* name, int value) {
385#ifdef ENABLE_LOGGING_AND_PROFILING
Steve Block6ded16b2010-05-10 14:33:55 +0100386 if (FLAG_log) UncheckedIntEvent(name, value);
387#endif
388}
389
390
Ben Murdochf87a2032010-10-22 12:50:53 +0100391void Logger::IntPtrTEvent(const char* name, intptr_t value) {
392#ifdef ENABLE_LOGGING_AND_PROFILING
393 if (FLAG_log) UncheckedIntPtrTEvent(name, value);
394#endif
395}
396
397
Steve Block6ded16b2010-05-10 14:33:55 +0100398#ifdef ENABLE_LOGGING_AND_PROFILING
399void Logger::UncheckedIntEvent(const char* name, int value) {
400 if (!Log::IsEnabled()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000401 LogMessageBuilder msg;
402 msg.Append("%s,%d\n", name, value);
403 msg.WriteToLogFile();
Steve Blocka7e24c12009-10-30 11:49:00 +0000404}
Steve Block6ded16b2010-05-10 14:33:55 +0100405#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000406
407
Ben Murdochf87a2032010-10-22 12:50:53 +0100408#ifdef ENABLE_LOGGING_AND_PROFILING
409void Logger::UncheckedIntPtrTEvent(const char* name, intptr_t value) {
410 if (!Log::IsEnabled()) return;
411 LogMessageBuilder msg;
412 msg.Append("%s,%" V8_PTR_PREFIX "d\n", name, value);
413 msg.WriteToLogFile();
414}
415#endif
416
417
Steve Blocka7e24c12009-10-30 11:49:00 +0000418void Logger::HandleEvent(const char* name, Object** location) {
419#ifdef ENABLE_LOGGING_AND_PROFILING
420 if (!Log::IsEnabled() || !FLAG_log_handles) return;
421 LogMessageBuilder msg;
422 msg.Append("%s,0x%" V8PRIxPTR "\n", name, location);
423 msg.WriteToLogFile();
424#endif
425}
426
427
428#ifdef ENABLE_LOGGING_AND_PROFILING
429// ApiEvent is private so all the calls come from the Logger class. It is the
430// caller's responsibility to ensure that log is enabled and that
431// FLAG_log_api is true.
432void Logger::ApiEvent(const char* format, ...) {
433 ASSERT(Log::IsEnabled() && FLAG_log_api);
434 LogMessageBuilder msg;
435 va_list ap;
436 va_start(ap, format);
437 msg.AppendVA(format, ap);
438 va_end(ap);
439 msg.WriteToLogFile();
440}
441#endif
442
443
444void Logger::ApiNamedSecurityCheck(Object* key) {
445#ifdef ENABLE_LOGGING_AND_PROFILING
446 if (!Log::IsEnabled() || !FLAG_log_api) return;
447 if (key->IsString()) {
448 SmartPointer<char> str =
449 String::cast(key)->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
450 ApiEvent("api,check-security,\"%s\"\n", *str);
451 } else if (key->IsUndefined()) {
452 ApiEvent("api,check-security,undefined\n");
453 } else {
454 ApiEvent("api,check-security,['no-name']\n");
455 }
456#endif
457}
458
459
460void Logger::SharedLibraryEvent(const char* library_path,
461 uintptr_t start,
462 uintptr_t end) {
463#ifdef ENABLE_LOGGING_AND_PROFILING
464 if (!Log::IsEnabled() || !FLAG_prof) return;
465 LogMessageBuilder msg;
466 msg.Append("shared-library,\"%s\",0x%08" V8PRIxPTR ",0x%08" V8PRIxPTR "\n",
467 library_path,
468 start,
469 end);
470 msg.WriteToLogFile();
471#endif
472}
473
474
475void Logger::SharedLibraryEvent(const wchar_t* library_path,
476 uintptr_t start,
477 uintptr_t end) {
478#ifdef ENABLE_LOGGING_AND_PROFILING
479 if (!Log::IsEnabled() || !FLAG_prof) return;
480 LogMessageBuilder msg;
481 msg.Append("shared-library,\"%ls\",0x%08" V8PRIxPTR ",0x%08" V8PRIxPTR "\n",
482 library_path,
483 start,
484 end);
485 msg.WriteToLogFile();
486#endif
487}
488
489
490#ifdef ENABLE_LOGGING_AND_PROFILING
491void Logger::LogRegExpSource(Handle<JSRegExp> regexp) {
492 // Prints "/" + re.source + "/" +
493 // (re.global?"g":"") + (re.ignorecase?"i":"") + (re.multiline?"m":"")
494 LogMessageBuilder msg;
495
496 Handle<Object> source = GetProperty(regexp, "source");
497 if (!source->IsString()) {
498 msg.Append("no source");
499 return;
500 }
501
502 switch (regexp->TypeTag()) {
503 case JSRegExp::ATOM:
504 msg.Append('a');
505 break;
506 default:
507 break;
508 }
509 msg.Append('/');
510 msg.AppendDetailed(*Handle<String>::cast(source), false);
511 msg.Append('/');
512
513 // global flag
514 Handle<Object> global = GetProperty(regexp, "global");
515 if (global->IsTrue()) {
516 msg.Append('g');
517 }
518 // ignorecase flag
519 Handle<Object> ignorecase = GetProperty(regexp, "ignoreCase");
520 if (ignorecase->IsTrue()) {
521 msg.Append('i');
522 }
523 // multiline flag
524 Handle<Object> multiline = GetProperty(regexp, "multiline");
525 if (multiline->IsTrue()) {
526 msg.Append('m');
527 }
528
529 msg.WriteToLogFile();
530}
531#endif // ENABLE_LOGGING_AND_PROFILING
532
533
534void Logger::RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache) {
535#ifdef ENABLE_LOGGING_AND_PROFILING
536 if (!Log::IsEnabled() || !FLAG_log_regexp) return;
537 LogMessageBuilder msg;
538 msg.Append("regexp-compile,");
539 LogRegExpSource(regexp);
540 msg.Append(in_cache ? ",hit\n" : ",miss\n");
541 msg.WriteToLogFile();
542#endif
543}
544
545
546void Logger::LogRuntime(Vector<const char> format, JSArray* args) {
547#ifdef ENABLE_LOGGING_AND_PROFILING
548 if (!Log::IsEnabled() || !FLAG_log_runtime) return;
549 HandleScope scope;
550 LogMessageBuilder msg;
551 for (int i = 0; i < format.length(); i++) {
552 char c = format[i];
553 if (c == '%' && i <= format.length() - 2) {
554 i++;
555 ASSERT('0' <= format[i] && format[i] <= '9');
John Reck59135872010-11-02 12:39:01 -0700556 MaybeObject* maybe = args->GetElement(format[i] - '0');
557 Object* obj;
558 if (!maybe->ToObject(&obj)) {
559 msg.Append("<exception>");
560 continue;
561 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000562 i++;
563 switch (format[i]) {
564 case 's':
565 msg.AppendDetailed(String::cast(obj), false);
566 break;
567 case 'S':
568 msg.AppendDetailed(String::cast(obj), true);
569 break;
570 case 'r':
571 Logger::LogRegExpSource(Handle<JSRegExp>(JSRegExp::cast(obj)));
572 break;
573 case 'x':
574 msg.Append("0x%x", Smi::cast(obj)->value());
575 break;
576 case 'i':
577 msg.Append("%i", Smi::cast(obj)->value());
578 break;
579 default:
580 UNREACHABLE();
581 }
582 } else {
583 msg.Append(c);
584 }
585 }
586 msg.Append('\n');
587 msg.WriteToLogFile();
588#endif
589}
590
591
592void Logger::ApiIndexedSecurityCheck(uint32_t index) {
593#ifdef ENABLE_LOGGING_AND_PROFILING
594 if (!Log::IsEnabled() || !FLAG_log_api) return;
595 ApiEvent("api,check-security,%u\n", index);
596#endif
597}
598
599
600void Logger::ApiNamedPropertyAccess(const char* tag,
601 JSObject* holder,
602 Object* name) {
603#ifdef ENABLE_LOGGING_AND_PROFILING
604 ASSERT(name->IsString());
605 if (!Log::IsEnabled() || !FLAG_log_api) return;
606 String* class_name_obj = holder->class_name();
607 SmartPointer<char> class_name =
608 class_name_obj->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
609 SmartPointer<char> property_name =
610 String::cast(name)->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
611 Logger::ApiEvent("api,%s,\"%s\",\"%s\"\n", tag, *class_name, *property_name);
612#endif
613}
614
615void Logger::ApiIndexedPropertyAccess(const char* tag,
616 JSObject* holder,
617 uint32_t index) {
618#ifdef ENABLE_LOGGING_AND_PROFILING
619 if (!Log::IsEnabled() || !FLAG_log_api) return;
620 String* class_name_obj = holder->class_name();
621 SmartPointer<char> class_name =
622 class_name_obj->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
623 Logger::ApiEvent("api,%s,\"%s\",%u\n", tag, *class_name, index);
624#endif
625}
626
627void Logger::ApiObjectAccess(const char* tag, JSObject* object) {
628#ifdef ENABLE_LOGGING_AND_PROFILING
629 if (!Log::IsEnabled() || !FLAG_log_api) return;
630 String* class_name_obj = object->class_name();
631 SmartPointer<char> class_name =
632 class_name_obj->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
633 Logger::ApiEvent("api,%s,\"%s\"\n", tag, *class_name);
634#endif
635}
636
637
638void Logger::ApiEntryCall(const char* name) {
639#ifdef ENABLE_LOGGING_AND_PROFILING
640 if (!Log::IsEnabled() || !FLAG_log_api) return;
641 Logger::ApiEvent("api,%s\n", name);
642#endif
643}
644
645
646void Logger::NewEvent(const char* name, void* object, size_t size) {
647#ifdef ENABLE_LOGGING_AND_PROFILING
648 if (!Log::IsEnabled() || !FLAG_log) return;
649 LogMessageBuilder msg;
650 msg.Append("new,%s,0x%" V8PRIxPTR ",%u\n", name, object,
651 static_cast<unsigned int>(size));
652 msg.WriteToLogFile();
653#endif
654}
655
656
657void Logger::DeleteEvent(const char* name, void* object) {
658#ifdef ENABLE_LOGGING_AND_PROFILING
659 if (!Log::IsEnabled() || !FLAG_log) return;
660 LogMessageBuilder msg;
661 msg.Append("delete,%s,0x%" V8PRIxPTR "\n", name, object);
662 msg.WriteToLogFile();
663#endif
664}
665
666
667#ifdef ENABLE_LOGGING_AND_PROFILING
Steve Blockd0582a62009-12-15 09:54:21 +0000668void Logger::CallbackEventInternal(const char* prefix, const char* name,
669 Address entry_point) {
670 if (!Log::IsEnabled() || !FLAG_log_code) return;
671 LogMessageBuilder msg;
672 msg.Append("%s,%s,",
Ben Murdochb0fe1622011-05-05 13:52:32 +0100673 kLogEventsNames[CODE_CREATION_EVENT],
674 kLogEventsNames[CALLBACK_TAG]);
Steve Blockd0582a62009-12-15 09:54:21 +0000675 msg.AppendAddress(entry_point);
676 msg.Append(",1,\"%s%s\"", prefix, name);
Steve Blockd0582a62009-12-15 09:54:21 +0000677 msg.Append('\n');
678 msg.WriteToLogFile();
679}
680#endif
681
682
683void Logger::CallbackEvent(String* name, Address entry_point) {
684#ifdef ENABLE_LOGGING_AND_PROFILING
685 if (!Log::IsEnabled() || !FLAG_log_code) return;
686 SmartPointer<char> str =
687 name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
688 CallbackEventInternal("", *str, entry_point);
689#endif
690}
691
692
693void Logger::GetterCallbackEvent(String* name, Address entry_point) {
694#ifdef ENABLE_LOGGING_AND_PROFILING
695 if (!Log::IsEnabled() || !FLAG_log_code) return;
696 SmartPointer<char> str =
697 name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
698 CallbackEventInternal("get ", *str, entry_point);
699#endif
700}
701
702
703void Logger::SetterCallbackEvent(String* name, Address entry_point) {
704#ifdef ENABLE_LOGGING_AND_PROFILING
705 if (!Log::IsEnabled() || !FLAG_log_code) return;
706 SmartPointer<char> str =
707 name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
708 CallbackEventInternal("set ", *str, entry_point);
709#endif
710}
711
712
Ben Murdochb0fe1622011-05-05 13:52:32 +0100713#ifdef ENABLE_LOGGING_AND_PROFILING
714static const char* ComputeMarker(Code* code) {
715 switch (code->kind()) {
716 case Code::FUNCTION: return code->optimizable() ? "~" : "";
717 case Code::OPTIMIZED_FUNCTION: return "*";
718 default: return "";
719 }
720}
721#endif
722
723
Steve Blocka7e24c12009-10-30 11:49:00 +0000724void Logger::CodeCreateEvent(LogEventsAndTags tag,
725 Code* code,
726 const char* comment) {
727#ifdef ENABLE_LOGGING_AND_PROFILING
728 if (!Log::IsEnabled() || !FLAG_log_code) return;
729 LogMessageBuilder msg;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100730 msg.Append("%s,%s,",
731 kLogEventsNames[CODE_CREATION_EVENT],
732 kLogEventsNames[tag]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000733 msg.AppendAddress(code->address());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100734 msg.Append(",%d,\"%s", code->ExecutableSize(), ComputeMarker(code));
Steve Blocka7e24c12009-10-30 11:49:00 +0000735 for (const char* p = comment; *p != '\0'; p++) {
736 if (*p == '"') {
737 msg.Append('\\');
738 }
739 msg.Append(*p);
740 }
741 msg.Append('"');
Ben Murdochf87a2032010-10-22 12:50:53 +0100742 LowLevelCodeCreateEvent(code, &msg);
Steve Blocka7e24c12009-10-30 11:49:00 +0000743 msg.Append('\n');
744 msg.WriteToLogFile();
745#endif
746}
747
748
749void Logger::CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name) {
750#ifdef ENABLE_LOGGING_AND_PROFILING
751 if (!Log::IsEnabled() || !FLAG_log_code) return;
752 LogMessageBuilder msg;
753 SmartPointer<char> str =
754 name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100755 msg.Append("%s,%s,",
756 kLogEventsNames[CODE_CREATION_EVENT],
757 kLogEventsNames[tag]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000758 msg.AppendAddress(code->address());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100759 msg.Append(",%d,\"%s%s\"", code->ExecutableSize(), ComputeMarker(code), *str);
Ben Murdochf87a2032010-10-22 12:50:53 +0100760 LowLevelCodeCreateEvent(code, &msg);
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 msg.Append('\n');
762 msg.WriteToLogFile();
763#endif
764}
765
766
767void Logger::CodeCreateEvent(LogEventsAndTags tag,
768 Code* code, String* name,
769 String* source, int line) {
770#ifdef ENABLE_LOGGING_AND_PROFILING
771 if (!Log::IsEnabled() || !FLAG_log_code) return;
772 LogMessageBuilder msg;
773 SmartPointer<char> str =
774 name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
775 SmartPointer<char> sourcestr =
776 source->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100777 msg.Append("%s,%s,",
778 kLogEventsNames[CODE_CREATION_EVENT],
779 kLogEventsNames[tag]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000780 msg.AppendAddress(code->address());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100781 msg.Append(",%d,\"%s%s %s:%d\"",
782 code->ExecutableSize(),
783 ComputeMarker(code),
784 *str,
785 *sourcestr,
786 line);
Ben Murdochf87a2032010-10-22 12:50:53 +0100787 LowLevelCodeCreateEvent(code, &msg);
Steve Blocka7e24c12009-10-30 11:49:00 +0000788 msg.Append('\n');
789 msg.WriteToLogFile();
790#endif
791}
792
793
794void Logger::CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count) {
795#ifdef ENABLE_LOGGING_AND_PROFILING
796 if (!Log::IsEnabled() || !FLAG_log_code) return;
797 LogMessageBuilder msg;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100798 msg.Append("%s,%s,",
799 kLogEventsNames[CODE_CREATION_EVENT],
800 kLogEventsNames[tag]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000801 msg.AppendAddress(code->address());
802 msg.Append(",%d,\"args_count: %d\"", code->ExecutableSize(), args_count);
Ben Murdochf87a2032010-10-22 12:50:53 +0100803 LowLevelCodeCreateEvent(code, &msg);
Steve Blocka7e24c12009-10-30 11:49:00 +0000804 msg.Append('\n');
805 msg.WriteToLogFile();
806#endif
807}
808
809
Ben Murdochf87a2032010-10-22 12:50:53 +0100810void Logger::CodeMovingGCEvent() {
811#ifdef ENABLE_LOGGING_AND_PROFILING
812 if (!Log::IsEnabled() || !FLAG_log_code || !FLAG_ll_prof) return;
813 LogMessageBuilder msg;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100814 msg.Append("%s\n", kLogEventsNames[CODE_MOVING_GC]);
Ben Murdochf87a2032010-10-22 12:50:53 +0100815 msg.WriteToLogFile();
816 OS::SignalCodeMovingGC();
817#endif
818}
819
820
Steve Blocka7e24c12009-10-30 11:49:00 +0000821void Logger::RegExpCodeCreateEvent(Code* code, String* source) {
822#ifdef ENABLE_LOGGING_AND_PROFILING
823 if (!Log::IsEnabled() || !FLAG_log_code) return;
824 LogMessageBuilder msg;
825 msg.Append("%s,%s,",
Ben Murdochb0fe1622011-05-05 13:52:32 +0100826 kLogEventsNames[CODE_CREATION_EVENT],
827 kLogEventsNames[REG_EXP_TAG]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000828 msg.AppendAddress(code->address());
829 msg.Append(",%d,\"", code->ExecutableSize());
830 msg.AppendDetailed(source, false);
831 msg.Append('\"');
Ben Murdochf87a2032010-10-22 12:50:53 +0100832 LowLevelCodeCreateEvent(code, &msg);
Steve Blocka7e24c12009-10-30 11:49:00 +0000833 msg.Append('\n');
834 msg.WriteToLogFile();
835#endif
836}
837
838
839void Logger::CodeMoveEvent(Address from, Address to) {
840#ifdef ENABLE_LOGGING_AND_PROFILING
Leon Clarked91b9f72010-01-27 17:25:45 +0000841 MoveEventInternal(CODE_MOVE_EVENT, from, to);
Steve Blocka7e24c12009-10-30 11:49:00 +0000842#endif
843}
844
845
846void Logger::CodeDeleteEvent(Address from) {
847#ifdef ENABLE_LOGGING_AND_PROFILING
Leon Clarked91b9f72010-01-27 17:25:45 +0000848 DeleteEventInternal(CODE_DELETE_EVENT, from);
Steve Blocka7e24c12009-10-30 11:49:00 +0000849#endif
850}
851
852
Leon Clarkee46be812010-01-19 14:06:41 +0000853void Logger::SnapshotPositionEvent(Address addr, int pos) {
854#ifdef ENABLE_LOGGING_AND_PROFILING
855 if (!Log::IsEnabled() || !FLAG_log_snapshot_positions) return;
856 LogMessageBuilder msg;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100857 msg.Append("%s,", kLogEventsNames[SNAPSHOT_POSITION_EVENT]);
Leon Clarkee46be812010-01-19 14:06:41 +0000858 msg.AppendAddress(addr);
859 msg.Append(",%d", pos);
Leon Clarkee46be812010-01-19 14:06:41 +0000860 msg.Append('\n');
861 msg.WriteToLogFile();
862#endif
863}
864
865
Leon Clarked91b9f72010-01-27 17:25:45 +0000866void Logger::FunctionCreateEvent(JSFunction* function) {
867#ifdef ENABLE_LOGGING_AND_PROFILING
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100868 // This function can be called from GC iterators (during Scavenge,
869 // MC, and MS), so marking bits can be set on objects. That's
870 // why unchecked accessors are used here.
Leon Clarked91b9f72010-01-27 17:25:45 +0000871 if (!Log::IsEnabled() || !FLAG_log_code) return;
872 LogMessageBuilder msg;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100873 msg.Append("%s,", kLogEventsNames[FUNCTION_CREATION_EVENT]);
Leon Clarked91b9f72010-01-27 17:25:45 +0000874 msg.AppendAddress(function->address());
875 msg.Append(',');
Ben Murdochb0fe1622011-05-05 13:52:32 +0100876 msg.AppendAddress(function->unchecked_code()->address());
Leon Clarked91b9f72010-01-27 17:25:45 +0000877 msg.Append('\n');
878 msg.WriteToLogFile();
879#endif
880}
881
882
Ben Murdochf87a2032010-10-22 12:50:53 +0100883void Logger::FunctionCreateEventFromMove(JSFunction* function) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100884#ifdef ENABLE_LOGGING_AND_PROFILING
885 if (function->unchecked_code() != Builtins::builtin(Builtins::LazyCompile)) {
886 FunctionCreateEvent(function);
887 }
888#endif
889}
890
891
Leon Clarked91b9f72010-01-27 17:25:45 +0000892void Logger::FunctionMoveEvent(Address from, Address to) {
893#ifdef ENABLE_LOGGING_AND_PROFILING
894 MoveEventInternal(FUNCTION_MOVE_EVENT, from, to);
895#endif
896}
897
898
899void Logger::FunctionDeleteEvent(Address from) {
900#ifdef ENABLE_LOGGING_AND_PROFILING
901 DeleteEventInternal(FUNCTION_DELETE_EVENT, from);
902#endif
903}
904
905
906#ifdef ENABLE_LOGGING_AND_PROFILING
907void Logger::MoveEventInternal(LogEventsAndTags event,
908 Address from,
909 Address to) {
Leon Clarked91b9f72010-01-27 17:25:45 +0000910 if (!Log::IsEnabled() || !FLAG_log_code) return;
911 LogMessageBuilder msg;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100912 msg.Append("%s,", kLogEventsNames[event]);
Leon Clarked91b9f72010-01-27 17:25:45 +0000913 msg.AppendAddress(from);
914 msg.Append(',');
Ben Murdochb0fe1622011-05-05 13:52:32 +0100915 msg.AppendAddress(to);
Leon Clarked91b9f72010-01-27 17:25:45 +0000916 msg.Append('\n');
917 msg.WriteToLogFile();
918}
919#endif
920
921
922#ifdef ENABLE_LOGGING_AND_PROFILING
923void Logger::DeleteEventInternal(LogEventsAndTags event, Address from) {
924 if (!Log::IsEnabled() || !FLAG_log_code) return;
925 LogMessageBuilder msg;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100926 msg.Append("%s,", kLogEventsNames[event]);
Leon Clarked91b9f72010-01-27 17:25:45 +0000927 msg.AppendAddress(from);
Leon Clarked91b9f72010-01-27 17:25:45 +0000928 msg.Append('\n');
929 msg.WriteToLogFile();
930}
931#endif
932
933
Steve Blocka7e24c12009-10-30 11:49:00 +0000934void Logger::ResourceEvent(const char* name, const char* tag) {
935#ifdef ENABLE_LOGGING_AND_PROFILING
936 if (!Log::IsEnabled() || !FLAG_log) return;
937 LogMessageBuilder msg;
938 msg.Append("%s,%s,", name, tag);
939
940 uint32_t sec, usec;
941 if (OS::GetUserTime(&sec, &usec) != -1) {
942 msg.Append("%d,%d,", sec, usec);
943 }
944 msg.Append("%.0f", OS::TimeCurrentMillis());
945
946 msg.Append('\n');
947 msg.WriteToLogFile();
948#endif
949}
950
951
952void Logger::SuspectReadEvent(String* name, Object* obj) {
953#ifdef ENABLE_LOGGING_AND_PROFILING
954 if (!Log::IsEnabled() || !FLAG_log_suspect) return;
955 LogMessageBuilder msg;
956 String* class_name = obj->IsJSObject()
957 ? JSObject::cast(obj)->class_name()
958 : Heap::empty_string();
959 msg.Append("suspect-read,");
960 msg.Append(class_name);
961 msg.Append(',');
962 msg.Append('"');
963 msg.Append(name);
964 msg.Append('"');
965 msg.Append('\n');
966 msg.WriteToLogFile();
967#endif
968}
969
970
971void Logger::HeapSampleBeginEvent(const char* space, const char* kind) {
972#ifdef ENABLE_LOGGING_AND_PROFILING
973 if (!Log::IsEnabled() || !FLAG_log_gc) return;
974 LogMessageBuilder msg;
975 // Using non-relative system time in order to be able to synchronize with
976 // external memory profiling events (e.g. DOM memory size).
977 msg.Append("heap-sample-begin,\"%s\",\"%s\",%.0f\n",
978 space, kind, OS::TimeCurrentMillis());
979 msg.WriteToLogFile();
980#endif
981}
982
983
984void Logger::HeapSampleStats(const char* space, const char* kind,
Ben Murdochf87a2032010-10-22 12:50:53 +0100985 intptr_t capacity, intptr_t used) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000986#ifdef ENABLE_LOGGING_AND_PROFILING
987 if (!Log::IsEnabled() || !FLAG_log_gc) return;
988 LogMessageBuilder msg;
Ben Murdochf87a2032010-10-22 12:50:53 +0100989 msg.Append("heap-sample-stats,\"%s\",\"%s\","
990 "%" V8_PTR_PREFIX "d,%" V8_PTR_PREFIX "d\n",
Steve Blocka7e24c12009-10-30 11:49:00 +0000991 space, kind, capacity, used);
992 msg.WriteToLogFile();
993#endif
994}
995
996
997void Logger::HeapSampleEndEvent(const char* space, const char* kind) {
998#ifdef ENABLE_LOGGING_AND_PROFILING
999 if (!Log::IsEnabled() || !FLAG_log_gc) return;
1000 LogMessageBuilder msg;
1001 msg.Append("heap-sample-end,\"%s\",\"%s\"\n", space, kind);
1002 msg.WriteToLogFile();
1003#endif
1004}
1005
1006
1007void Logger::HeapSampleItemEvent(const char* type, int number, int bytes) {
1008#ifdef ENABLE_LOGGING_AND_PROFILING
1009 if (!Log::IsEnabled() || !FLAG_log_gc) return;
1010 LogMessageBuilder msg;
1011 msg.Append("heap-sample-item,%s,%d,%d\n", type, number, bytes);
1012 msg.WriteToLogFile();
1013#endif
1014}
1015
1016
1017void Logger::HeapSampleJSConstructorEvent(const char* constructor,
1018 int number, int bytes) {
1019#ifdef ENABLE_LOGGING_AND_PROFILING
1020 if (!Log::IsEnabled() || !FLAG_log_gc) return;
1021 LogMessageBuilder msg;
1022 msg.Append("heap-js-cons-item,%s,%d,%d\n", constructor, number, bytes);
1023 msg.WriteToLogFile();
1024#endif
1025}
1026
1027
1028void Logger::HeapSampleJSRetainersEvent(
1029 const char* constructor, const char* event) {
1030#ifdef ENABLE_LOGGING_AND_PROFILING
1031 if (!Log::IsEnabled() || !FLAG_log_gc) return;
1032 // Event starts with comma, so we don't have it in the format string.
1033 static const char* event_text = "heap-js-ret-item,%s";
1034 // We take placeholder strings into account, but it's OK to be conservative.
Steve Blockd0582a62009-12-15 09:54:21 +00001035 static const int event_text_len = StrLength(event_text);
1036 const int cons_len = StrLength(constructor);
1037 const int event_len = StrLength(event);
Steve Blocka7e24c12009-10-30 11:49:00 +00001038 int pos = 0;
1039 // Retainer lists can be long. We may need to split them into multiple events.
1040 do {
1041 LogMessageBuilder msg;
1042 msg.Append(event_text, constructor);
1043 int to_write = event_len - pos;
1044 if (to_write > Log::kMessageBufferSize - (cons_len + event_text_len)) {
1045 int cut_pos = pos + Log::kMessageBufferSize - (cons_len + event_text_len);
1046 ASSERT(cut_pos < event_len);
1047 while (cut_pos > pos && event[cut_pos] != ',') --cut_pos;
1048 if (event[cut_pos] != ',') {
1049 // Crash in debug mode, skip in release mode.
1050 ASSERT(false);
1051 return;
1052 }
1053 // Append a piece of event that fits, without trailing comma.
1054 msg.AppendStringPart(event + pos, cut_pos - pos);
1055 // Start next piece with comma.
1056 pos = cut_pos;
1057 } else {
1058 msg.Append("%s", event + pos);
1059 pos += event_len;
1060 }
1061 msg.Append('\n');
1062 msg.WriteToLogFile();
1063 } while (pos < event_len);
1064#endif
1065}
1066
1067
Steve Block3ce2e202009-11-05 08:53:23 +00001068void Logger::HeapSampleJSProducerEvent(const char* constructor,
1069 Address* stack) {
1070#ifdef ENABLE_LOGGING_AND_PROFILING
1071 if (!Log::IsEnabled() || !FLAG_log_gc) return;
1072 LogMessageBuilder msg;
1073 msg.Append("heap-js-prod-item,%s", constructor);
1074 while (*stack != NULL) {
1075 msg.Append(",0x%" V8PRIxPTR, *stack++);
1076 }
1077 msg.Append("\n");
1078 msg.WriteToLogFile();
1079#endif
1080}
1081
1082
Steve Blocka7e24c12009-10-30 11:49:00 +00001083void Logger::DebugTag(const char* call_site_tag) {
1084#ifdef ENABLE_LOGGING_AND_PROFILING
1085 if (!Log::IsEnabled() || !FLAG_log) return;
1086 LogMessageBuilder msg;
1087 msg.Append("debug-tag,%s\n", call_site_tag);
1088 msg.WriteToLogFile();
1089#endif
1090}
1091
1092
1093void Logger::DebugEvent(const char* event_type, Vector<uint16_t> parameter) {
1094#ifdef ENABLE_LOGGING_AND_PROFILING
1095 if (!Log::IsEnabled() || !FLAG_log) return;
1096 StringBuilder s(parameter.length() + 1);
1097 for (int i = 0; i < parameter.length(); ++i) {
1098 s.AddCharacter(static_cast<char>(parameter[i]));
1099 }
1100 char* parameter_string = s.Finalize();
1101 LogMessageBuilder msg;
1102 msg.Append("debug-queue-event,%s,%15.3f,%s\n",
1103 event_type,
1104 OS::TimeCurrentMillis(),
1105 parameter_string);
1106 DeleteArray(parameter_string);
1107 msg.WriteToLogFile();
1108#endif
1109}
1110
1111
1112#ifdef ENABLE_LOGGING_AND_PROFILING
1113void Logger::TickEvent(TickSample* sample, bool overflow) {
1114 if (!Log::IsEnabled() || !FLAG_prof) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001115 LogMessageBuilder msg;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001116 msg.Append("%s,", kLogEventsNames[TICK_EVENT]);
1117 msg.AppendAddress(sample->pc);
Steve Blocka7e24c12009-10-30 11:49:00 +00001118 msg.Append(',');
Ben Murdochb0fe1622011-05-05 13:52:32 +01001119 msg.AppendAddress(sample->sp);
Leon Clarked91b9f72010-01-27 17:25:45 +00001120 msg.Append(',');
Ben Murdochb0fe1622011-05-05 13:52:32 +01001121 msg.AppendAddress(sample->function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001122 msg.Append(",%d", static_cast<int>(sample->state));
1123 if (overflow) {
1124 msg.Append(",overflow");
1125 }
1126 for (int i = 0; i < sample->frames_count; ++i) {
1127 msg.Append(',');
Ben Murdochb0fe1622011-05-05 13:52:32 +01001128 msg.AppendAddress(sample->stack[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +00001129 }
1130 msg.Append('\n');
1131 msg.WriteToLogFile();
1132}
1133
1134
1135int Logger::GetActiveProfilerModules() {
1136 int result = PROFILER_MODULE_NONE;
Steve Block8defd9f2010-07-08 12:39:36 +01001137 if (profiler_ != NULL && !profiler_->paused()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001138 result |= PROFILER_MODULE_CPU;
1139 }
1140 if (FLAG_log_gc) {
1141 result |= PROFILER_MODULE_HEAP_STATS | PROFILER_MODULE_JS_CONSTRUCTORS;
1142 }
1143 return result;
1144}
1145
1146
Andrei Popescu402d9372010-02-26 13:31:12 +00001147void Logger::PauseProfiler(int flags, int tag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001148 if (!Log::IsEnabled()) return;
Steve Block8defd9f2010-07-08 12:39:36 +01001149 if (profiler_ != NULL && (flags & PROFILER_MODULE_CPU)) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001150 // It is OK to have negative nesting.
1151 if (--cpu_profiler_nesting_ == 0) {
1152 profiler_->pause();
1153 if (FLAG_prof_lazy) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001154 if (!FLAG_sliding_state_window && !RuntimeProfiler::IsEnabled()) {
1155 ticker_->Stop();
1156 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001157 FLAG_log_code = false;
1158 // Must be the same message as Log::kDynamicBufferSeal.
1159 LOG(UncheckedStringEvent("profiler", "pause"));
1160 }
Steve Block6ded16b2010-05-10 14:33:55 +01001161 --logging_nesting_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001162 }
1163 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001164 if (flags &
Steve Blocka7e24c12009-10-30 11:49:00 +00001165 (PROFILER_MODULE_HEAP_STATS | PROFILER_MODULE_JS_CONSTRUCTORS)) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001166 if (--heap_profiler_nesting_ == 0) {
1167 FLAG_log_gc = false;
Steve Block6ded16b2010-05-10 14:33:55 +01001168 --logging_nesting_;
Andrei Popescu402d9372010-02-26 13:31:12 +00001169 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001170 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001171 if (tag != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001172 UncheckedIntEvent("close-tag", tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00001173 }
1174}
1175
1176
Andrei Popescu402d9372010-02-26 13:31:12 +00001177void Logger::ResumeProfiler(int flags, int tag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001178 if (!Log::IsEnabled()) return;
Andrei Popescu402d9372010-02-26 13:31:12 +00001179 if (tag != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001180 UncheckedIntEvent("open-tag", tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00001181 }
Steve Block8defd9f2010-07-08 12:39:36 +01001182 if (profiler_ != NULL && (flags & PROFILER_MODULE_CPU)) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001183 if (cpu_profiler_nesting_++ == 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001184 ++logging_nesting_;
Andrei Popescu402d9372010-02-26 13:31:12 +00001185 if (FLAG_prof_lazy) {
1186 profiler_->Engage();
1187 LOG(UncheckedStringEvent("profiler", "resume"));
1188 FLAG_log_code = true;
1189 LogCompiledFunctions();
1190 LogFunctionObjects();
1191 LogAccessorCallbacks();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001192 if (!FLAG_sliding_state_window && !ticker_->IsActive()) {
1193 ticker_->Start();
1194 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001195 }
1196 profiler_->resume();
Steve Blocka7e24c12009-10-30 11:49:00 +00001197 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001198 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001199 if (flags &
Steve Blocka7e24c12009-10-30 11:49:00 +00001200 (PROFILER_MODULE_HEAP_STATS | PROFILER_MODULE_JS_CONSTRUCTORS)) {
Andrei Popescu402d9372010-02-26 13:31:12 +00001201 if (heap_profiler_nesting_++ == 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001202 ++logging_nesting_;
Andrei Popescu402d9372010-02-26 13:31:12 +00001203 FLAG_log_gc = true;
1204 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001205 }
1206}
1207
1208
1209// This function can be called when Log's mutex is acquired,
1210// either from main or Profiler's thread.
1211void Logger::StopLoggingAndProfiling() {
1212 Log::stop();
Andrei Popescu402d9372010-02-26 13:31:12 +00001213 PauseProfiler(PROFILER_MODULE_CPU, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001214}
1215
1216
1217bool Logger::IsProfilerSamplerActive() {
1218 return ticker_->IsActive();
1219}
1220
1221
1222int Logger::GetLogLines(int from_pos, char* dest_buf, int max_size) {
1223 return Log::GetLogLines(from_pos, dest_buf, max_size);
1224}
1225
1226
Ben Murdochb0fe1622011-05-05 13:52:32 +01001227class EnumerateOptimizedFunctionsVisitor: public OptimizedFunctionVisitor {
1228 public:
1229 EnumerateOptimizedFunctionsVisitor(Handle<SharedFunctionInfo>* sfis,
1230 Handle<Code>* code_objects,
1231 int* count)
1232 : sfis_(sfis), code_objects_(code_objects), count_(count) { }
1233
1234 virtual void EnterContext(Context* context) {}
1235 virtual void LeaveContext(Context* context) {}
1236
1237 virtual void VisitFunction(JSFunction* function) {
1238 if (sfis_ != NULL) {
1239 sfis_[*count_] = Handle<SharedFunctionInfo>(function->shared());
1240 }
1241 if (code_objects_ != NULL) {
1242 ASSERT(function->code()->kind() == Code::OPTIMIZED_FUNCTION);
1243 code_objects_[*count_] = Handle<Code>(function->code());
1244 }
1245 *count_ = *count_ + 1;
1246 }
1247
1248 private:
1249 Handle<SharedFunctionInfo>* sfis_;
1250 Handle<Code>* code_objects_;
1251 int* count_;
1252};
1253
1254
1255static int EnumerateCompiledFunctions(Handle<SharedFunctionInfo>* sfis,
1256 Handle<Code>* code_objects) {
Steve Block3ce2e202009-11-05 08:53:23 +00001257 AssertNoAllocation no_alloc;
Steve Blocka7e24c12009-10-30 11:49:00 +00001258 int compiled_funcs_count = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001259
1260 // Iterate the heap to find shared function info objects and record
1261 // the unoptimized code for them.
Steve Block3ce2e202009-11-05 08:53:23 +00001262 HeapIterator iterator;
Leon Clarked91b9f72010-01-27 17:25:45 +00001263 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
Steve Block3ce2e202009-11-05 08:53:23 +00001264 if (!obj->IsSharedFunctionInfo()) continue;
1265 SharedFunctionInfo* sfi = SharedFunctionInfo::cast(obj);
1266 if (sfi->is_compiled()
1267 && (!sfi->script()->IsScript()
1268 || Script::cast(sfi->script())->HasValidSource())) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001269 if (sfis != NULL) {
Steve Block3ce2e202009-11-05 08:53:23 +00001270 sfis[compiled_funcs_count] = Handle<SharedFunctionInfo>(sfi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001271 }
1272 if (code_objects != NULL) {
1273 code_objects[compiled_funcs_count] = Handle<Code>(sfi->code());
1274 }
Steve Block3ce2e202009-11-05 08:53:23 +00001275 ++compiled_funcs_count;
Steve Blocka7e24c12009-10-30 11:49:00 +00001276 }
1277 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001278
1279 // Iterate all optimized functions in all contexts.
1280 EnumerateOptimizedFunctionsVisitor visitor(sfis,
1281 code_objects,
1282 &compiled_funcs_count);
1283 Deoptimizer::VisitAllOptimizedFunctions(&visitor);
1284
Steve Block3ce2e202009-11-05 08:53:23 +00001285 return compiled_funcs_count;
1286}
1287
1288
Steve Blockd0582a62009-12-15 09:54:21 +00001289void Logger::LogCodeObject(Object* object) {
1290 if (FLAG_log_code) {
1291 Code* code_object = Code::cast(object);
1292 LogEventsAndTags tag = Logger::STUB_TAG;
1293 const char* description = "Unknown code from the snapshot";
1294 switch (code_object->kind()) {
1295 case Code::FUNCTION:
Ben Murdochb0fe1622011-05-05 13:52:32 +01001296 case Code::OPTIMIZED_FUNCTION:
Steve Blockd0582a62009-12-15 09:54:21 +00001297 return; // We log this later using LogCompiledFunctions.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001298 case Code::BINARY_OP_IC: // fall through
1299 case Code::TYPE_RECORDING_BINARY_OP_IC: // fall through
1300 case Code::COMPARE_IC: // fall through
Steve Blockd0582a62009-12-15 09:54:21 +00001301 case Code::STUB:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001302 description =
1303 CodeStub::MajorName(CodeStub::GetMajorKey(code_object), true);
Andrei Popescu31002712010-02-23 13:46:05 +00001304 if (description == NULL)
1305 description = "A stub from the snapshot";
Steve Blockd0582a62009-12-15 09:54:21 +00001306 tag = Logger::STUB_TAG;
1307 break;
1308 case Code::BUILTIN:
1309 description = "A builtin from the snapshot";
1310 tag = Logger::BUILTIN_TAG;
1311 break;
1312 case Code::KEYED_LOAD_IC:
1313 description = "A keyed load IC from the snapshot";
1314 tag = Logger::KEYED_LOAD_IC_TAG;
1315 break;
1316 case Code::LOAD_IC:
1317 description = "A load IC from the snapshot";
1318 tag = Logger::LOAD_IC_TAG;
1319 break;
1320 case Code::STORE_IC:
1321 description = "A store IC from the snapshot";
1322 tag = Logger::STORE_IC_TAG;
1323 break;
1324 case Code::KEYED_STORE_IC:
1325 description = "A keyed store IC from the snapshot";
1326 tag = Logger::KEYED_STORE_IC_TAG;
1327 break;
1328 case Code::CALL_IC:
1329 description = "A call IC from the snapshot";
1330 tag = Logger::CALL_IC_TAG;
1331 break;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001332 case Code::KEYED_CALL_IC:
1333 description = "A keyed call IC from the snapshot";
1334 tag = Logger::KEYED_CALL_IC_TAG;
1335 break;
Steve Blockd0582a62009-12-15 09:54:21 +00001336 }
Steve Block6ded16b2010-05-10 14:33:55 +01001337 PROFILE(CodeCreateEvent(tag, code_object, description));
Steve Blockd0582a62009-12-15 09:54:21 +00001338 }
1339}
1340
1341
Ben Murdochf87a2032010-10-22 12:50:53 +01001342void Logger::LogCodeInfo() {
1343#ifdef ENABLE_LOGGING_AND_PROFILING
1344 if (!Log::IsEnabled() || !FLAG_log_code || !FLAG_ll_prof) return;
1345#if V8_TARGET_ARCH_IA32
1346 const char arch[] = "ia32";
1347#elif V8_TARGET_ARCH_X64
1348 const char arch[] = "x64";
1349#elif V8_TARGET_ARCH_ARM
1350 const char arch[] = "arm";
1351#else
1352 const char arch[] = "unknown";
1353#endif
1354 LogMessageBuilder msg;
1355 msg.Append("code-info,%s,%d\n", arch, Code::kHeaderSize);
1356 msg.WriteToLogFile();
1357#endif // ENABLE_LOGGING_AND_PROFILING
1358}
1359
1360
1361void Logger::LowLevelCodeCreateEvent(Code* code, LogMessageBuilder* msg) {
1362 if (!FLAG_ll_prof || Log::output_code_handle_ == NULL) return;
1363 int pos = static_cast<int>(ftell(Log::output_code_handle_));
John Reck59135872010-11-02 12:39:01 -07001364 size_t rv = fwrite(code->instruction_start(), 1, code->instruction_size(),
1365 Log::output_code_handle_);
1366 ASSERT(static_cast<size_t>(code->instruction_size()) == rv);
1367 USE(rv);
Ben Murdochf87a2032010-10-22 12:50:53 +01001368 msg->Append(",%d", pos);
1369}
1370
1371
Andrei Popescu31002712010-02-23 13:46:05 +00001372void Logger::LogCodeObjects() {
1373 AssertNoAllocation no_alloc;
1374 HeapIterator iterator;
1375 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
1376 if (obj->IsCode()) LogCodeObject(obj);
1377 }
1378}
1379
1380
Steve Block3ce2e202009-11-05 08:53:23 +00001381void Logger::LogCompiledFunctions() {
1382 HandleScope scope;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001383 const int compiled_funcs_count = EnumerateCompiledFunctions(NULL, NULL);
Kristian Monsen25f61362010-05-21 11:50:48 +01001384 ScopedVector< Handle<SharedFunctionInfo> > sfis(compiled_funcs_count);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001385 ScopedVector< Handle<Code> > code_objects(compiled_funcs_count);
1386 EnumerateCompiledFunctions(sfis.start(), code_objects.start());
Steve Blocka7e24c12009-10-30 11:49:00 +00001387
1388 // During iteration, there can be heap allocation due to
1389 // GetScriptLineNumber call.
1390 for (int i = 0; i < compiled_funcs_count; ++i) {
1391 Handle<SharedFunctionInfo> shared = sfis[i];
1392 Handle<String> name(String::cast(shared->name()));
1393 Handle<String> func_name(name->length() > 0 ?
1394 *name : shared->inferred_name());
1395 if (shared->script()->IsScript()) {
1396 Handle<Script> script(Script::cast(shared->script()));
1397 if (script->name()->IsString()) {
1398 Handle<String> script_name(String::cast(script->name()));
1399 int line_num = GetScriptLineNumber(script, shared->start_position());
1400 if (line_num > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001401 PROFILE(CodeCreateEvent(
1402 Logger::ToNativeByScript(Logger::LAZY_COMPILE_TAG, *script),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001403 *code_objects[i], *func_name,
Steve Block6ded16b2010-05-10 14:33:55 +01001404 *script_name, line_num + 1));
Steve Blocka7e24c12009-10-30 11:49:00 +00001405 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01001406 // Can't distinguish eval and script here, so always use Script.
1407 PROFILE(CodeCreateEvent(
1408 Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001409 *code_objects[i], *script_name));
Steve Blocka7e24c12009-10-30 11:49:00 +00001410 }
Steve Blockd0582a62009-12-15 09:54:21 +00001411 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01001412 PROFILE(CodeCreateEvent(
1413 Logger::ToNativeByScript(Logger::LAZY_COMPILE_TAG, *script),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001414 *code_objects[i], *func_name));
Steve Blocka7e24c12009-10-30 11:49:00 +00001415 }
Steve Block6ded16b2010-05-10 14:33:55 +01001416 } else if (shared->IsApiFunction()) {
Steve Blockd0582a62009-12-15 09:54:21 +00001417 // API function.
Steve Block6ded16b2010-05-10 14:33:55 +01001418 FunctionTemplateInfo* fun_data = shared->get_api_func_data();
Steve Blockd0582a62009-12-15 09:54:21 +00001419 Object* raw_call_data = fun_data->call_code();
1420 if (!raw_call_data->IsUndefined()) {
1421 CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data);
1422 Object* callback_obj = call_data->callback();
1423 Address entry_point = v8::ToCData<Address>(callback_obj);
Steve Block6ded16b2010-05-10 14:33:55 +01001424 PROFILE(CallbackEvent(*func_name, entry_point));
Steve Blockd0582a62009-12-15 09:54:21 +00001425 }
1426 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01001427 PROFILE(CodeCreateEvent(
Ben Murdochb0fe1622011-05-05 13:52:32 +01001428 Logger::LAZY_COMPILE_TAG, *code_objects[i], *func_name));
Steve Blocka7e24c12009-10-30 11:49:00 +00001429 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001430 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001431}
1432
Steve Blockd0582a62009-12-15 09:54:21 +00001433
Leon Clarked91b9f72010-01-27 17:25:45 +00001434void Logger::LogFunctionObjects() {
1435 AssertNoAllocation no_alloc;
1436 HeapIterator iterator;
1437 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
1438 if (!obj->IsJSFunction()) continue;
1439 JSFunction* jsf = JSFunction::cast(obj);
1440 if (!jsf->is_compiled()) continue;
Steve Block6ded16b2010-05-10 14:33:55 +01001441 PROFILE(FunctionCreateEvent(jsf));
Leon Clarked91b9f72010-01-27 17:25:45 +00001442 }
1443}
1444
1445
Steve Blockd0582a62009-12-15 09:54:21 +00001446void Logger::LogAccessorCallbacks() {
1447 AssertNoAllocation no_alloc;
1448 HeapIterator iterator;
Leon Clarked91b9f72010-01-27 17:25:45 +00001449 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
Steve Blockd0582a62009-12-15 09:54:21 +00001450 if (!obj->IsAccessorInfo()) continue;
1451 AccessorInfo* ai = AccessorInfo::cast(obj);
1452 if (!ai->name()->IsString()) continue;
1453 String* name = String::cast(ai->name());
1454 Address getter_entry = v8::ToCData<Address>(ai->getter());
1455 if (getter_entry != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001456 PROFILE(GetterCallbackEvent(name, getter_entry));
Steve Blockd0582a62009-12-15 09:54:21 +00001457 }
1458 Address setter_entry = v8::ToCData<Address>(ai->setter());
1459 if (setter_entry != 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01001460 PROFILE(SetterCallbackEvent(name, setter_entry));
Steve Blockd0582a62009-12-15 09:54:21 +00001461 }
1462 }
1463}
1464
Steve Blocka7e24c12009-10-30 11:49:00 +00001465#endif
1466
1467
1468bool Logger::Setup() {
1469#ifdef ENABLE_LOGGING_AND_PROFILING
1470 // --log-all enables all the log flags.
1471 if (FLAG_log_all) {
1472 FLAG_log_runtime = true;
1473 FLAG_log_api = true;
1474 FLAG_log_code = true;
1475 FLAG_log_gc = true;
1476 FLAG_log_suspect = true;
1477 FLAG_log_handles = true;
1478 FLAG_log_regexp = true;
1479 }
1480
1481 // --prof implies --log-code.
1482 if (FLAG_prof) FLAG_log_code = true;
1483
Ben Murdochf87a2032010-10-22 12:50:53 +01001484 // --ll-prof implies --log-code and --log-snapshot-positions.
1485 if (FLAG_ll_prof) {
1486 FLAG_log_code = true;
1487 FLAG_log_snapshot_positions = true;
1488 }
1489
Steve Blocka7e24c12009-10-30 11:49:00 +00001490 // --prof_lazy controls --log-code, implies --noprof_auto.
1491 if (FLAG_prof_lazy) {
1492 FLAG_log_code = false;
1493 FLAG_prof_auto = false;
1494 }
1495
1496 bool start_logging = FLAG_log || FLAG_log_runtime || FLAG_log_api
1497 || FLAG_log_code || FLAG_log_gc || FLAG_log_handles || FLAG_log_suspect
1498 || FLAG_log_regexp || FLAG_log_state_changes;
1499
1500 bool open_log_file = start_logging || FLAG_prof_lazy;
1501
1502 // If we're logging anything, we need to open the log file.
1503 if (open_log_file) {
1504 if (strcmp(FLAG_logfile, "-") == 0) {
1505 Log::OpenStdout();
1506 } else if (strcmp(FLAG_logfile, "*") == 0) {
1507 Log::OpenMemoryBuffer();
1508 } else if (strchr(FLAG_logfile, '%') != NULL) {
1509 // If there's a '%' in the log file name we have to expand
1510 // placeholders.
1511 HeapStringAllocator allocator;
1512 StringStream stream(&allocator);
1513 for (const char* p = FLAG_logfile; *p; p++) {
1514 if (*p == '%') {
1515 p++;
1516 switch (*p) {
1517 case '\0':
1518 // If there's a % at the end of the string we back up
1519 // one character so we can escape the loop properly.
1520 p--;
1521 break;
1522 case 't': {
1523 // %t expands to the current time in milliseconds.
1524 double time = OS::TimeCurrentMillis();
1525 stream.Add("%.0f", FmtElm(time));
1526 break;
1527 }
1528 case '%':
1529 // %% expands (contracts really) to %.
1530 stream.Put('%');
1531 break;
1532 default:
1533 // All other %'s expand to themselves.
1534 stream.Put('%');
1535 stream.Put(*p);
1536 break;
1537 }
1538 } else {
1539 stream.Put(*p);
1540 }
1541 }
1542 SmartPointer<const char> expanded = stream.ToCString();
1543 Log::OpenFile(*expanded);
1544 } else {
1545 Log::OpenFile(FLAG_logfile);
1546 }
1547 }
1548
Ben Murdochf87a2032010-10-22 12:50:53 +01001549 if (FLAG_ll_prof) LogCodeInfo();
1550
Steve Blocka7e24c12009-10-30 11:49:00 +00001551 ticker_ = new Ticker(kSamplingIntervalMs);
1552
1553 if (FLAG_sliding_state_window && sliding_state_window_ == NULL) {
1554 sliding_state_window_ = new SlidingStateWindow();
1555 }
1556
Steve Block6ded16b2010-05-10 14:33:55 +01001557 if (start_logging) {
1558 logging_nesting_ = 1;
1559 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001560
1561 if (FLAG_prof) {
1562 profiler_ = new Profiler();
1563 if (!FLAG_prof_auto) {
1564 profiler_->pause();
1565 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01001566 logging_nesting_ = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00001567 }
Steve Blockd0582a62009-12-15 09:54:21 +00001568 if (!FLAG_prof_lazy) {
1569 profiler_->Engage();
1570 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001571 }
1572
1573 LogMessageBuilder::set_write_failure_handler(StopLoggingAndProfiling);
Steve Blocka7e24c12009-10-30 11:49:00 +00001574 return true;
1575
1576#else
1577 return false;
1578#endif
1579}
1580
1581
Ben Murdochb0fe1622011-05-05 13:52:32 +01001582void Logger::EnsureTickerStarted() {
1583#ifdef ENABLE_LOGGING_AND_PROFILING
1584 ASSERT(ticker_ != NULL);
1585 if (!ticker_->IsActive()) ticker_->Start();
1586#endif
1587}
1588
1589
1590void Logger::EnsureTickerStopped() {
1591#ifdef ENABLE_LOGGING_AND_PROFILING
1592 if (ticker_ != NULL && ticker_->IsActive()) ticker_->Stop();
1593#endif
1594}
1595
1596
Steve Blocka7e24c12009-10-30 11:49:00 +00001597void Logger::TearDown() {
1598#ifdef ENABLE_LOGGING_AND_PROFILING
1599 LogMessageBuilder::set_write_failure_handler(NULL);
1600
1601 // Stop the profiler before closing the file.
1602 if (profiler_ != NULL) {
1603 profiler_->Disengage();
1604 delete profiler_;
1605 profiler_ = NULL;
1606 }
1607
Steve Blocka7e24c12009-10-30 11:49:00 +00001608 delete sliding_state_window_;
1609 sliding_state_window_ = NULL;
1610
1611 delete ticker_;
1612 ticker_ = NULL;
1613
1614 Log::Close();
1615#endif
1616}
1617
1618
1619void Logger::EnableSlidingStateWindow() {
1620#ifdef ENABLE_LOGGING_AND_PROFILING
1621 // If the ticker is NULL, Logger::Setup has not been called yet. In
1622 // that case, we set the sliding_state_window flag so that the
1623 // sliding window computation will be started when Logger::Setup is
1624 // called.
1625 if (ticker_ == NULL) {
1626 FLAG_sliding_state_window = true;
1627 return;
1628 }
1629 // Otherwise, if the sliding state window computation has not been
1630 // started we do it now.
1631 if (sliding_state_window_ == NULL) {
1632 sliding_state_window_ = new SlidingStateWindow();
1633 }
1634#endif
1635}
1636
Steve Blocka7e24c12009-10-30 11:49:00 +00001637} } // namespace v8::internal