log.h 13.9 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_LOG_H_
#define V8_LOG_H_

8 9
#include <string>

10
#include "src/allocation.h"
jfb's avatar
jfb committed
11
#include "src/base/compiler-specific.h"
12 13
#include "src/base/platform/elapsed-timer.h"
#include "src/base/platform/platform.h"
14 15
#include "src/code-events.h"
#include "src/isolate.h"
16
#include "src/objects.h"
17

18
namespace v8 {
19

20
struct TickSample;
21

22 23 24 25
namespace sampler {
class Sampler;
}

26
namespace internal {
27 28 29 30 31 32 33 34 35 36 37

// Logger is used for collecting logging information from V8 during
// execution. The result is dumped to a file.
//
// Available command line flags:
//
//  --log
// Minimal logging (no API, code, or GC sample events), default is off.
//
// --log-all
// Log all events to the file, default is off.  This is the same as combining
38
// --log-api, --log-code, --log-gc, and --log-regexp.
39 40 41 42 43 44 45 46 47 48 49 50
//
// --log-api
// Log API events to the logfile, default is off.  --log-api implies --log.
//
// --log-code
// Log code (create, move, and delete) events to the logfile, default is off.
// --log-code implies --log.
//
// --log-gc
// Log GC heap samples after each GC that can be processed by hp2ps, default
// is off.  --log-gc implies --log.
//
51
// --log-regexp
52
// Log creation and use of regular expressions, Default is off.
53 54
// --log-regexp implies --log.
//
55 56 57 58 59 60 61 62
// --logfile <filename>
// Specify the name of the logfile, default is "v8.log".
//
// --prof
// Collect statistical profiling information (ticks), default is off.  The
// tick profiler requires code events, so --prof implies --log-code.

// Forward declarations.
63
class CodeEventListener;
64 65
class CpuProfiler;
class Isolate;
66
class JitLogger;
67
class Log;
68 69 70
class LowLevelLogger;
class PerfBasicLogger;
class PerfJitLogger;
71
class Profiler;
72
class ProfilerListener;
73
class RuntimeCallTimer;
74
class Ticker;
75 76

#undef LOG
77 78 79 80
#define LOG(isolate, Call)                              \
  do {                                                  \
    v8::internal::Logger* logger = (isolate)->logger(); \
    if (logger->is_logging()) logger->Call;             \
81
  } while (false)
82

83 84 85 86
#define LOG_CODE_EVENT(isolate, Call)                   \
  do {                                                  \
    v8::internal::Logger* logger = (isolate)->logger(); \
    if (logger->is_logging_code_events()) logger->Call; \
87 88
  } while (false)

89
class Logger : public CodeEventListener {
90
 public:
91 92
  enum StartEnd { START = 0, END = 1 };

93
  // Acquires resources for logging if the right flags are set.
94
  bool SetUp(Isolate* isolate);
95

96 97 98 99
  // Sets the current code event handler.
  void SetCodeEventHandler(uint32_t options,
                           JitCodeEventHandler event_handler);

lpy's avatar
lpy committed
100 101 102 103 104 105
  // Sets up ProfilerListener.
  void SetUpProfilerListener();

  // Tear down ProfilerListener if it has no observers.
  void TearDownProfilerListener();

106
  sampler::Sampler* sampler();
107

lpy's avatar
lpy committed
108 109
  ProfilerListener* profiler_listener() { return profiler_listener_.get(); }

110
  // Frees resources acquired in SetUp.
111 112 113
  // When a temporary file is used for the log, returns its stream descriptor,
  // leaving the file open.
  FILE* TearDown();
114 115

  // Emits an event with a string value -> (name, value).
116
  void StringEvent(const char* name, const char* value);
117 118

  // Emits an event with an int value -> (name, value).
119 120
  void IntEvent(const char* name, int value);
  void IntPtrTEvent(const char* name, intptr_t value);
121 122

  // Emits an event with an handle value -> (name, location).
123
  void HandleEvent(const char* name, Object** location);
124 125

  // Emits memory management events for C allocated structures.
126 127 128
  void NewEvent(const char* name, void* object, size_t size);
  void DeleteEvent(const char* name, void* object);

129 130 131 132
  // Emits an event with a tag, and some resource usage information.
  // -> (name, tag, <rusage information>).
  // Currently, the resource usage information is a process time stamp
  // and a real time timestamp.
133
  void ResourceEvent(const char* name, const char* tag);
134 135 136

  // Emits an event that an undefined property was read from an
  // object.
137
  void SuspectReadEvent(Name* name, Object* obj);
138

139 140
  // Emits an event when a message is put on or read from a debugging queue.
  // DebugTag lets us put a call-site specific label on the event.
141 142
  void DebugTag(const char* call_site_tag);
  void DebugEvent(const char* event_type, Vector<uint16_t> parameter);
143

144 145

  // ==== Events logged by --log-api. ====
146
  void ApiSecurityCheck();
147 148 149 150 151 152
  void ApiNamedPropertyAccess(const char* tag, JSObject* holder, Object* name);
  void ApiIndexedPropertyAccess(const char* tag,
                                JSObject* holder,
                                uint32_t index);
  void ApiObjectAccess(const char* tag, JSObject* obj);
  void ApiEntryCall(const char* name);
153 154

  // ==== Events logged by --log-code. ====
155 156 157
  void addCodeEventListener(CodeEventListener* listener);
  void removeCodeEventListener(CodeEventListener* listener);

158
  // Emits a code event for a callback function.
159 160 161
  void CallbackEvent(Name* name, Address entry_point);
  void GetterCallbackEvent(Name* name, Address entry_point);
  void SetterCallbackEvent(Name* name, Address entry_point);
162
  // Emits a code create event.
163 164 165 166 167 168 169 170 171 172 173 174
  void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
                       AbstractCode* code, const char* source);
  void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
                       AbstractCode* code, Name* name);
  void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
                       AbstractCode* code, SharedFunctionInfo* shared,
                       Name* name);
  void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
                       AbstractCode* code, SharedFunctionInfo* shared,
                       Name* source, int line, int column);
  void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
                       AbstractCode* code, int args_count);
175
  // Emits a code deoptimization event.
176
  void CodeDisableOptEvent(AbstractCode* code, SharedFunctionInfo* shared);
177
  void CodeMovingGCEvent();
178
  // Emits a code create event for a RegExp.
179
  void RegExpCodeCreateEvent(AbstractCode* code, String* source);
180
  // Emits a code move event.
181
  void CodeMoveEvent(AbstractCode* from, Address to);
182 183 184
  // Emits a code line info record event.
  void CodeLinePosInfoRecordEvent(AbstractCode* code,
                                  ByteArray* source_position_table);
185

186
  void SharedFunctionInfoMoveEvent(Address from, Address to);
187

188
  void CodeNameEvent(Address addr, int pos, const char* code_name);
189

190 191
  void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta);

192 193
  // ==== Events logged by --log-gc. ====
  // Heap sampling events: start, end, and individual types.
194 195 196 197 198 199
  void HeapSampleBeginEvent(const char* space, const char* kind);
  void HeapSampleEndEvent(const char* space, const char* kind);
  void HeapSampleItemEvent(const char* type, int number, int bytes);
  void HeapSampleJSConstructorEvent(const char* constructor,
                                    int number, int bytes);
  void HeapSampleJSRetainersEvent(const char* constructor,
200
                                         const char* event);
201 202 203 204 205
  void HeapSampleJSProducerEvent(const char* constructor,
                                 Address* stack);
  void HeapSampleStats(const char* space, const char* kind,
                       intptr_t capacity, intptr_t used);

206 207
  void SharedLibraryEvent(const std::string& library_path, uintptr_t start,
                          uintptr_t end, intptr_t aslr_slide);
208

209
  void CurrentTimeEvent();
210

211
  void TimerEvent(StartEnd se, const char* name);
212

213 214
  static void EnterExternal(Isolate* isolate);
  static void LeaveExternal(Isolate* isolate);
215

216 217 218 219
  static void DefaultEventLoggerSentinel(const char* name, int event) {}

  INLINE(static void CallEventLogger(Isolate* isolate, const char* name,
                                     StartEnd se, bool expose_to_api));
220

221
  bool is_logging() {
222
    return is_logging_;
223
  }
224

225
  bool is_logging_code_events() {
226
    return is_logging() || jit_logger_ != NULL;
227 228
  }

229 230 231
  // Stop collection of profiling data.
  // When data collection is paused, CPU Tick events are discarded.
  void StopProfiler();
232

233
  void LogExistingFunction(Handle<SharedFunctionInfo> shared,
234
                           Handle<AbstractCode> code);
235
  // Logs all compiled functions found in the heap.
236
  void LogCompiledFunctions();
237
  // Logs all accessor callbacks found in the heap.
238
  void LogAccessorCallbacks();
239
  // Used for logging stubs found in the snapshot.
240
  void LogCodeObjects();
241 242
  // Used for logging bytecode handlers found in the snapshot.
  void LogBytecodeHandlers();
243

244
  // Converts tag to a corresponding NATIVE_... if the script is native.
245 246
  INLINE(static CodeEventListener::LogEventsAndTags ToNativeByScript(
      CodeEventListener::LogEventsAndTags, Script*));
247

248
  // Profiler's sampling interval (in milliseconds).
249 250 251 252 253
#if defined(ANDROID)
  // Phones and tablets have processors that are much slower than desktop
  // and laptop computers for which current heuristics are tuned.
  static const int kSamplingIntervalMs = 5;
#else
254
  static const int kSamplingIntervalMs = 1;
255
#endif
256

257 258 259
  // Callback from Log, stops profiling in case of insufficient resources.
  void LogFailure();

260
 private:
261
  explicit Logger(Isolate* isolate);
262
  ~Logger();
263

264
  // Emits the profiler's first message.
265
  void ProfilerBeginEvent();
266

267
  // Emits callback event messages.
268
  void CallbackEventInternal(const char* prefix,
269
                             Name* name,
270
                             Address entry_point);
271

272
  // Internal configurable move event.
273 274
  void MoveEventInternal(CodeEventListener::LogEventsAndTags event,
                         Address from, Address to);
275

276
  // Used for logging stubs found in the snapshot.
277
  void LogCodeObject(Object* code_object);
278

279
  // Helper method. It resets name_buffer_ and add tag name into it.
280
  void InitNameBuffer(CodeEventListener::LogEventsAndTags tag);
281

282
  // Emits a profiler tick event. Used by the profiler thread.
283
  void TickEvent(TickSample* sample, bool overflow);
284
  void RuntimeCallTimerEvent();
285

jfb's avatar
jfb committed
286
  PRINTF_FORMAT(2, 3) void ApiEvent(const char* format, ...);
287

288
  // Logs a StringEvent regardless of whether FLAG_log is true.
289
  void UncheckedStringEvent(const char* name, const char* value);
290

291
  // Logs an IntEvent regardless of whether FLAG_log is true.
292 293
  void UncheckedIntEvent(const char* name, int value);
  void UncheckedIntPtrTEvent(const char* name, intptr_t value);
294

295 296
  Isolate* isolate_;

297
  // The sampler used by the profiler and the sliding state window.
298
  Ticker* ticker_;
299 300 301 302

  // When the statistical profile is active, profiler_
  // points to a Profiler, that handles collection
  // of samples.
303
  Profiler* profiler_;
304

305 306
  // An array of log events names.
  const char* const* log_events_;
307 308 309 310

  // Internal implementation classes with access to
  // private members.
  friend class EventLog;
311
  friend class Isolate;
312 313
  friend class TimeLog;
  friend class Profiler;
314
  template <StateTag Tag> friend class VMState;
315
  friend class LoggerTestHelper;
316

317
  bool is_logging_;
318
  Log* log_;
319
  PerfBasicLogger* perf_basic_logger_;
320
  PerfJitLogger* perf_jit_logger_;
321
  LowLevelLogger* ll_logger_;
322
  JitLogger* jit_logger_;
lpy's avatar
lpy committed
323
  std::unique_ptr<ProfilerListener> profiler_listener_;
324
  List<CodeEventListener*> listeners_;
325

326
  // Guards against multiple calls to TearDown() that can happen in some tests.
327
  // 'true' between SetUp() and TearDown().
328 329
  bool is_initialized_;

330
  base::ElapsedTimer timer_;
331

332
  friend class CpuProfiler;
333 334
};

335 336 337
#define TIMER_EVENTS_LIST(V)    \
  V(RecompileSynchronous, true) \
  V(RecompileConcurrent, true)  \
338
  V(CompileIgnition, true)      \
339
  V(CompileFullCode, true)      \
cbruni's avatar
cbruni committed
340
  V(OptimizeCode, true)         \
341 342
  V(CompileCode, true)          \
  V(DeoptimizeCode, true)       \
343
  V(Execute, true)              \
344
  V(External, true)
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

#define V(TimerName, expose)                                                  \
  class TimerEvent##TimerName : public AllStatic {                            \
   public:                                                                    \
    static const char* name(void* unused = NULL) { return "V8." #TimerName; } \
    static bool expose_to_api() { return expose; }                            \
  };
TIMER_EVENTS_LIST(V)
#undef V


template <class TimerEvent>
class TimerEventScope {
 public:
  explicit TimerEventScope(Isolate* isolate) : isolate_(isolate) {
    LogTimerEvent(Logger::START);
  }

  ~TimerEventScope() { LogTimerEvent(Logger::END); }

 private:
366
  void LogTimerEvent(Logger::StartEnd se);
367 368 369
  Isolate* isolate_;
};

370 371 372
class CodeEventLogger : public CodeEventListener {
 public:
  CodeEventLogger();
373 374
  ~CodeEventLogger() override;

375
  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
376
                       const char* comment) override;
377
  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
378
                       Name* name) override;
379
  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
380
                       int args_count) override;
381
  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
382
                       SharedFunctionInfo* shared, Name* name) override;
383
  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
384 385
                       SharedFunctionInfo* shared, Name* source, int line,
                       int column) override;
386 387 388 389 390 391 392
  void RegExpCodeCreateEvent(AbstractCode* code, String* source) override;

  void CallbackEvent(Name* name, Address entry_point) override {}
  void GetterCallbackEvent(Name* name, Address entry_point) override {}
  void SetterCallbackEvent(Name* name, Address entry_point) override {}
  void SharedFunctionInfoMoveEvent(Address from, Address to) override {}
  void CodeMovingGCEvent() override {}
393
  void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) override {}
394 395 396 397

 private:
  class NameBuffer;

398 399
  virtual void LogRecordedBuffer(AbstractCode* code, SharedFunctionInfo* shared,
                                 const char* name, int length) = 0;
400 401 402 403 404

  NameBuffer* name_buffer_;
};


405 406
}  // namespace internal
}  // namespace v8
407

408

409
#endif  // V8_LOG_H_