cpu-profiler.h 8.31 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_CPU_PROFILER_H_
#define V8_CPU_PROFILER_H_

8
#include "src/allocation.h"
9
#include "src/base/atomicops.h"
10
#include "src/base/platform/time.h"
11 12 13
#include "src/circular-queue.h"
#include "src/sampler.h"
#include "src/unbound-queue.h"
14 15 16 17

namespace v8 {
namespace internal {

18 19 20
// Forward declarations.
class CodeEntry;
class CodeMap;
21
class CompilationInfo;
22 23 24 25
class CpuProfile;
class CpuProfilesCollection;
class ProfileGenerator;

26 27 28
#define CODE_EVENTS_TYPE_LIST(V)                                   \
  V(CODE_CREATION,    CodeCreateEventRecord)                       \
  V(CODE_MOVE,        CodeMoveEventRecord)                         \
29
  V(CODE_DISABLE_OPT, CodeDisableOptEventRecord)                   \
30 31
  V(SHARED_FUNC_MOVE, SharedFunctionInfoMoveEventRecord)           \
  V(REPORT_BUILTIN,   ReportBuiltinEventRecord)
32 33 34 35 36 37 38 39 40 41 42 43 44


class CodeEventRecord {
 public:
#define DECLARE_TYPE(type, ignore) type,
  enum Type {
    NONE = 0,
    CODE_EVENTS_TYPE_LIST(DECLARE_TYPE)
    NUMBER_OF_TYPES
  };
#undef DECLARE_TYPE

  Type type;
45
  mutable unsigned order;
46 47 48 49 50 51 52 53
};


class CodeCreateEventRecord : public CodeEventRecord {
 public:
  Address start;
  CodeEntry* entry;
  unsigned size;
54
  Address shared;
55

56
  INLINE(void UpdateCodeMap(CodeMap* code_map));
57 58 59 60 61 62 63 64
};


class CodeMoveEventRecord : public CodeEventRecord {
 public:
  Address from;
  Address to;

65
  INLINE(void UpdateCodeMap(CodeMap* code_map));
66 67 68
};


69 70 71 72 73 74 75 76 77
class CodeDisableOptEventRecord : public CodeEventRecord {
 public:
  Address start;
  const char* bailout_reason;

  INLINE(void UpdateCodeMap(CodeMap* code_map));
};


78
class SharedFunctionInfoMoveEventRecord : public CodeEventRecord {
79
 public:
80 81
  Address from;
  Address to;
82

83
  INLINE(void UpdateCodeMap(CodeMap* code_map));
84 85 86
};


87 88 89 90 91 92 93 94 95
class ReportBuiltinEventRecord : public CodeEventRecord {
 public:
  Address start;
  Builtins::Name builtin_id;

  INLINE(void UpdateCodeMap(CodeMap* code_map));
};


96
class TickSampleEventRecord {
97
 public:
98 99 100
  // The parameterless constructor is used when we dequeue data from
  // the ticks buffer.
  TickSampleEventRecord() { }
101 102
  explicit TickSampleEventRecord(unsigned order) : order(order) { }

103
  unsigned order;
104
  TickSample sample;
105 106 107
};


108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
class CodeEventsContainer {
 public:
  explicit CodeEventsContainer(
      CodeEventRecord::Type type = CodeEventRecord::NONE) {
    generic.type = type;
  }
  union  {
    CodeEventRecord generic;
#define DECLARE_CLASS(ignore, type) type type##_;
    CODE_EVENTS_TYPE_LIST(DECLARE_CLASS)
#undef DECLARE_TYPE
  };
};


123 124
// This class implements both the profile events processor thread and
// methods called by event producers: VM and stack sampler threads.
125
class ProfilerEventsProcessor : public base::Thread {
126
 public:
127 128
  ProfilerEventsProcessor(ProfileGenerator* generator,
                          Sampler* sampler,
129
                          base::TimeDelta period);
130
  virtual ~ProfilerEventsProcessor() {}
131 132 133

  // Thread control.
  virtual void Run();
134
  void StopSynchronously();
135
  INLINE(bool running()) { return running_; }
136
  void Enqueue(const CodeEventsContainer& event);
137

138
  // Puts current stack into tick sample events buffer.
139
  void AddCurrentStack(Isolate* isolate);
140

141 142 143 144
  // Tick sample events are filled directly in the buffer of the circular
  // queue (because the structure is of fixed width, but usually not all
  // stack frame entries are filled.) This method returns a pointer to the
  // next record of the buffer.
145 146
  inline TickSample* StartTickSample();
  inline void FinishTickSample();
147

148 149 150 151 152
  // SamplingCircularQueue has stricter alignment requirements than a normal new
  // can fulfil, so we need to provide our own new/delete here.
  void* operator new(size_t size);
  void operator delete(void* ptr);

153 154
 private:
  // Called from events processing thread (Run() method.)
155
  bool ProcessCodeEvent();
156

157 158 159 160 161 162
  enum SampleProcessingResult {
    OneSampleProcessed,
    FoundSampleForNextCodeEvent,
    NoSamplesInQueue
  };
  SampleProcessingResult ProcessOneSample();
163

164
  ProfileGenerator* generator_;
165
  Sampler* sampler_;
166
  bool running_;
167
  // Sampling period in microseconds.
168
  const base::TimeDelta period_;
169
  UnboundQueue<CodeEventsContainer> events_buffer_;
170 171 172 173 174
  static const size_t kTickSampleBufferSize = 1 * MB;
  static const size_t kTickSampleQueueLength =
      kTickSampleBufferSize / sizeof(TickSampleEventRecord);
  SamplingCircularQueue<TickSampleEventRecord,
                        kTickSampleQueueLength> ticks_buffer_;
175
  UnboundQueue<TickSampleEventRecord> ticks_from_vm_buffer_;
176 177
  unsigned last_code_event_id_;
  unsigned last_processed_code_event_id_;
178 179
};

180

181 182 183 184 185 186 187 188
#define PROFILE(IsolateGetter, Call)                                        \
  do {                                                                      \
    Isolate* cpu_profiler_isolate = (IsolateGetter);                        \
    v8::internal::Logger* logger = cpu_profiler_isolate->logger();          \
    CpuProfiler* cpu_profiler = cpu_profiler_isolate->cpu_profiler();       \
    if (logger->is_logging_code_events() || cpu_profiler->is_profiling()) { \
      logger->Call;                                                         \
    }                                                                       \
189 190 191
  } while (false)


192
class CpuProfiler : public CodeEventListener {
193
 public:
194
  explicit CpuProfiler(Isolate* isolate);
195 196 197 198 199 200

  CpuProfiler(Isolate* isolate,
              CpuProfilesCollection* test_collection,
              ProfileGenerator* test_generator,
              ProfilerEventsProcessor* test_processor);

201
  virtual ~CpuProfiler();
202

203
  void set_sampling_interval(base::TimeDelta value);
204 205 206
  void StartProfiling(const char* title, bool record_samples = false);
  void StartProfiling(String* title, bool record_samples);
  CpuProfile* StopProfiling(const char* title);
207
  CpuProfile* StopProfiling(String* title);
208
  int GetProfilesCount();
209
  CpuProfile* GetProfile(int index);
210 211
  void DeleteAllProfiles();
  void DeleteProfile(CpuProfile* profile);
212 213

  // Invoked from stack sampler (thread or signal handler.)
214 215
  inline TickSample* StartTickSample();
  inline void FinishTickSample();
216 217 218

  // Must be called via PROFILE macro, otherwise will crash when
  // profiling is not enabled.
219 220 221 222 223
  virtual void CallbackEvent(Name* name, Address entry_point);
  virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
                               Code* code, const char* comment);
  virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
                               Code* code, Name* name);
224
  virtual void CodeCreateEvent(Logger::LogEventsAndTags tag, Code* code,
225
                               SharedFunctionInfo* shared,
226 227
                               CompilationInfo* info, Name* script_name);
  virtual void CodeCreateEvent(Logger::LogEventsAndTags tag, Code* code,
228
                               SharedFunctionInfo* shared,
229 230
                               CompilationInfo* info, Name* script_name,
                               int line, int column);
231 232 233 234
  virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
                               Code* code, int args_count);
  virtual void CodeMovingGCEvent() {}
  virtual void CodeMoveEvent(Address from, Address to);
235
  virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared);
236 237 238 239 240
  virtual void CodeDeleteEvent(Address from);
  virtual void GetterCallbackEvent(Name* name, Address entry_point);
  virtual void RegExpCodeCreateEvent(Code* code, String* source);
  virtual void SetterCallbackEvent(Name* name, Address entry_point);
  virtual void SharedFunctionInfoMoveEvent(Address from, Address to);
241 242

  INLINE(bool is_profiling() const) { return is_profiling_; }
243 244 245
  bool* is_profiling_address() {
    return &is_profiling_;
  }
246

247 248
  ProfileGenerator* generator() const { return generator_; }
  ProfilerEventsProcessor* processor() const { return processor_; }
249
  Isolate* isolate() const { return isolate_; }
250

251 252
 private:
  void StartProcessorIfNotStarted();
253
  void StopProcessorIfLastProfile(const char* title);
254 255
  void StopProcessor();
  void ResetProfiles();
256
  void LogBuiltins();
257

258
  Isolate* isolate_;
259
  base::TimeDelta sampling_interval_;
260 261 262
  CpuProfilesCollection* profiles_;
  ProfileGenerator* generator_;
  ProfilerEventsProcessor* processor_;
263
  bool saved_is_logging_;
264
  bool is_profiling_;
265 266 267 268 269 270 271

  DISALLOW_COPY_AND_ASSIGN(CpuProfiler);
};

} }  // namespace v8::internal


272
#endif  // V8_CPU_PROFILER_H_