2012-02-23 09:12:57 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2010-03-19 09:46:53 +00:00
|
|
|
|
|
|
|
#ifndef V8_CPU_PROFILER_H_
|
|
|
|
#define V8_CPU_PROFILER_H_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/allocation.h"
|
|
|
|
#include "src/atomicops.h"
|
|
|
|
#include "src/circular-queue.h"
|
|
|
|
#include "src/platform/time.h"
|
|
|
|
#include "src/sampler.h"
|
|
|
|
#include "src/unbound-queue.h"
|
2010-03-19 09:46:53 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2010-04-06 10:36:38 +00:00
|
|
|
// Forward declarations.
|
|
|
|
class CodeEntry;
|
|
|
|
class CodeMap;
|
2013-05-14 22:51:33 +00:00
|
|
|
class CompilationInfo;
|
2010-04-06 10:36:38 +00:00
|
|
|
class CpuProfile;
|
|
|
|
class CpuProfilesCollection;
|
|
|
|
class ProfileGenerator;
|
|
|
|
|
2011-03-10 12:00:27 +00:00
|
|
|
#define CODE_EVENTS_TYPE_LIST(V) \
|
|
|
|
V(CODE_CREATION, CodeCreateEventRecord) \
|
|
|
|
V(CODE_MOVE, CodeMoveEventRecord) \
|
2014-05-22 05:36:27 +00:00
|
|
|
V(CODE_DISABLE_OPT, CodeDisableOptEventRecord) \
|
2013-07-02 07:51:09 +00:00
|
|
|
V(SHARED_FUNC_MOVE, SharedFunctionInfoMoveEventRecord) \
|
|
|
|
V(REPORT_BUILTIN, ReportBuiltinEventRecord)
|
2010-03-19 09:46:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
2013-07-01 10:12:03 +00:00
|
|
|
mutable unsigned order;
|
2010-03-19 09:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CodeCreateEventRecord : public CodeEventRecord {
|
|
|
|
public:
|
|
|
|
Address start;
|
|
|
|
CodeEntry* entry;
|
|
|
|
unsigned size;
|
2011-03-10 12:00:27 +00:00
|
|
|
Address shared;
|
2010-03-19 09:46:53 +00:00
|
|
|
|
2010-04-06 10:36:38 +00:00
|
|
|
INLINE(void UpdateCodeMap(CodeMap* code_map));
|
2010-03-19 09:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CodeMoveEventRecord : public CodeEventRecord {
|
|
|
|
public:
|
|
|
|
Address from;
|
|
|
|
Address to;
|
|
|
|
|
2010-04-06 10:36:38 +00:00
|
|
|
INLINE(void UpdateCodeMap(CodeMap* code_map));
|
2010-03-19 09:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-22 05:36:27 +00:00
|
|
|
class CodeDisableOptEventRecord : public CodeEventRecord {
|
|
|
|
public:
|
|
|
|
Address start;
|
|
|
|
const char* bailout_reason;
|
|
|
|
|
|
|
|
INLINE(void UpdateCodeMap(CodeMap* code_map));
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-03-10 12:00:27 +00:00
|
|
|
class SharedFunctionInfoMoveEventRecord : public CodeEventRecord {
|
2010-03-19 09:46:53 +00:00
|
|
|
public:
|
2011-02-22 16:31:24 +00:00
|
|
|
Address from;
|
|
|
|
Address to;
|
2010-03-19 09:46:53 +00:00
|
|
|
|
2010-04-06 10:36:38 +00:00
|
|
|
INLINE(void UpdateCodeMap(CodeMap* code_map));
|
2010-03-19 09:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-02 07:51:09 +00:00
|
|
|
class ReportBuiltinEventRecord : public CodeEventRecord {
|
|
|
|
public:
|
|
|
|
Address start;
|
|
|
|
Builtins::Name builtin_id;
|
|
|
|
|
|
|
|
INLINE(void UpdateCodeMap(CodeMap* code_map));
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-06-22 13:54:35 +00:00
|
|
|
class TickSampleEventRecord {
|
2010-03-19 09:46:53 +00:00
|
|
|
public:
|
2011-06-22 13:54:35 +00:00
|
|
|
// The parameterless constructor is used when we dequeue data from
|
|
|
|
// the ticks buffer.
|
|
|
|
TickSampleEventRecord() { }
|
2013-07-18 13:42:04 +00:00
|
|
|
explicit TickSampleEventRecord(unsigned order) : order(order) { }
|
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
unsigned order;
|
2010-04-12 07:23:43 +00:00
|
|
|
TickSample sample;
|
2010-03-19 09:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-01 10:12:03 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
// This class implements both the profile events processor thread and
|
|
|
|
// methods called by event producers: VM and stack sampler threads.
|
|
|
|
class ProfilerEventsProcessor : public Thread {
|
|
|
|
public:
|
2013-08-26 07:17:12 +00:00
|
|
|
ProfilerEventsProcessor(ProfileGenerator* generator,
|
|
|
|
Sampler* sampler,
|
2013-08-29 09:15:13 +00:00
|
|
|
TimeDelta period);
|
2011-02-22 16:31:24 +00:00
|
|
|
virtual ~ProfilerEventsProcessor() {}
|
2010-03-19 09:46:53 +00:00
|
|
|
|
|
|
|
// Thread control.
|
|
|
|
virtual void Run();
|
2013-07-07 11:42:30 +00:00
|
|
|
void StopSynchronously();
|
2010-03-19 13:51:01 +00:00
|
|
|
INLINE(bool running()) { return running_; }
|
2013-07-01 10:12:03 +00:00
|
|
|
void Enqueue(const CodeEventsContainer& event);
|
2010-03-19 09:46:53 +00:00
|
|
|
|
2010-06-01 13:52:49 +00:00
|
|
|
// Puts current stack into tick sample events buffer.
|
2013-07-02 15:44:30 +00:00
|
|
|
void AddCurrentStack(Isolate* isolate);
|
2010-03-19 09:46:53 +00:00
|
|
|
|
2012-11-30 10:26:21 +00:00
|
|
|
// 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.
|
2013-08-23 08:22:07 +00:00
|
|
|
inline TickSample* StartTickSample();
|
|
|
|
inline void FinishTickSample();
|
2010-03-19 09:46:53 +00:00
|
|
|
|
2014-01-10 10:39:47 +00:00
|
|
|
// 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);
|
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
private:
|
|
|
|
// Called from events processing thread (Run() method.)
|
2013-07-07 11:42:30 +00:00
|
|
|
bool ProcessCodeEvent();
|
2013-09-05 12:17:17 +00:00
|
|
|
|
2013-09-06 06:25:06 +00:00
|
|
|
enum SampleProcessingResult {
|
|
|
|
OneSampleProcessed,
|
|
|
|
FoundSampleForNextCodeEvent,
|
|
|
|
NoSamplesInQueue
|
|
|
|
};
|
|
|
|
SampleProcessingResult ProcessOneSample();
|
2013-08-26 07:17:12 +00:00
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
ProfileGenerator* generator_;
|
2013-08-26 07:17:12 +00:00
|
|
|
Sampler* sampler_;
|
2010-03-19 09:46:53 +00:00
|
|
|
bool running_;
|
2013-08-26 07:17:12 +00:00
|
|
|
// Sampling period in microseconds.
|
2013-08-29 09:15:13 +00:00
|
|
|
const TimeDelta period_;
|
2010-05-22 05:27:19 +00:00
|
|
|
UnboundQueue<CodeEventsContainer> events_buffer_;
|
2013-08-23 08:22:07 +00:00
|
|
|
static const size_t kTickSampleBufferSize = 1 * MB;
|
|
|
|
static const size_t kTickSampleQueueLength =
|
|
|
|
kTickSampleBufferSize / sizeof(TickSampleEventRecord);
|
|
|
|
SamplingCircularQueue<TickSampleEventRecord,
|
|
|
|
kTickSampleQueueLength> ticks_buffer_;
|
2010-06-01 13:52:49 +00:00
|
|
|
UnboundQueue<TickSampleEventRecord> ticks_from_vm_buffer_;
|
2013-07-07 11:42:30 +00:00
|
|
|
unsigned last_code_event_id_;
|
|
|
|
unsigned last_processed_code_event_id_;
|
2010-03-19 09:46:53 +00:00
|
|
|
};
|
|
|
|
|
2010-04-06 10:36:38 +00:00
|
|
|
|
2013-07-23 08:12:15 +00:00
|
|
|
#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; \
|
|
|
|
} \
|
2010-04-06 10:36:38 +00:00
|
|
|
} while (false)
|
|
|
|
|
|
|
|
|
2013-07-26 13:50:23 +00:00
|
|
|
class CpuProfiler : public CodeEventListener {
|
2010-04-06 10:36:38 +00:00
|
|
|
public:
|
2013-04-02 07:53:50 +00:00
|
|
|
explicit CpuProfiler(Isolate* isolate);
|
2013-07-01 10:12:03 +00:00
|
|
|
|
|
|
|
CpuProfiler(Isolate* isolate,
|
|
|
|
CpuProfilesCollection* test_collection,
|
|
|
|
ProfileGenerator* test_generator,
|
|
|
|
ProfilerEventsProcessor* test_processor);
|
|
|
|
|
2013-07-26 13:50:23 +00:00
|
|
|
virtual ~CpuProfiler();
|
2013-04-02 07:53:50 +00:00
|
|
|
|
2013-09-04 11:55:28 +00:00
|
|
|
void set_sampling_interval(TimeDelta value);
|
2013-04-02 07:53:50 +00:00
|
|
|
void StartProfiling(const char* title, bool record_samples = false);
|
|
|
|
void StartProfiling(String* title, bool record_samples);
|
|
|
|
CpuProfile* StopProfiling(const char* title);
|
2013-07-06 09:12:09 +00:00
|
|
|
CpuProfile* StopProfiling(String* title);
|
2013-04-02 07:53:50 +00:00
|
|
|
int GetProfilesCount();
|
2013-07-06 09:12:09 +00:00
|
|
|
CpuProfile* GetProfile(int index);
|
2013-04-02 07:53:50 +00:00
|
|
|
void DeleteAllProfiles();
|
|
|
|
void DeleteProfile(CpuProfile* profile);
|
2010-04-06 10:36:38 +00:00
|
|
|
|
|
|
|
// Invoked from stack sampler (thread or signal handler.)
|
2013-08-23 08:22:07 +00:00
|
|
|
inline TickSample* StartTickSample();
|
|
|
|
inline void FinishTickSample();
|
2010-04-06 10:36:38 +00:00
|
|
|
|
|
|
|
// Must be called via PROFILE macro, otherwise will crash when
|
|
|
|
// profiling is not enabled.
|
2013-07-26 13:50:23 +00:00
|
|
|
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);
|
|
|
|
virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
|
|
|
|
Code* code,
|
|
|
|
SharedFunctionInfo* shared,
|
|
|
|
CompilationInfo* info,
|
|
|
|
Name* name);
|
|
|
|
virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
|
|
|
|
Code* code,
|
|
|
|
SharedFunctionInfo* shared,
|
|
|
|
CompilationInfo* info,
|
2013-10-10 13:15:47 +00:00
|
|
|
Name* source, int line, int column);
|
2013-07-26 13:50:23 +00:00
|
|
|
virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
|
|
|
|
Code* code, int args_count);
|
|
|
|
virtual void CodeMovingGCEvent() {}
|
|
|
|
virtual void CodeMoveEvent(Address from, Address to);
|
2014-05-22 05:36:27 +00:00
|
|
|
virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared);
|
2013-07-26 13:50:23 +00:00
|
|
|
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);
|
2013-04-02 07:53:50 +00:00
|
|
|
|
|
|
|
INLINE(bool is_profiling() const) { return is_profiling_; }
|
2013-06-13 19:16:35 +00:00
|
|
|
bool* is_profiling_address() {
|
|
|
|
return &is_profiling_;
|
|
|
|
}
|
2010-04-06 10:36:38 +00:00
|
|
|
|
2013-07-07 11:42:30 +00:00
|
|
|
ProfileGenerator* generator() const { return generator_; }
|
|
|
|
ProfilerEventsProcessor* processor() const { return processor_; }
|
2013-08-07 17:04:27 +00:00
|
|
|
Isolate* isolate() const { return isolate_; }
|
2013-07-07 11:42:30 +00:00
|
|
|
|
2010-04-06 10:36:38 +00:00
|
|
|
private:
|
|
|
|
void StartProcessorIfNotStarted();
|
2010-08-10 12:06:42 +00:00
|
|
|
void StopProcessorIfLastProfile(const char* title);
|
2011-03-22 16:10:01 +00:00
|
|
|
void StopProcessor();
|
|
|
|
void ResetProfiles();
|
2013-07-02 07:51:09 +00:00
|
|
|
void LogBuiltins();
|
2010-04-06 10:36:38 +00:00
|
|
|
|
2013-04-02 07:53:50 +00:00
|
|
|
Isolate* isolate_;
|
2013-09-04 11:55:28 +00:00
|
|
|
TimeDelta sampling_interval_;
|
2010-04-06 10:36:38 +00:00
|
|
|
CpuProfilesCollection* profiles_;
|
|
|
|
ProfileGenerator* generator_;
|
|
|
|
ProfilerEventsProcessor* processor_;
|
2013-08-29 10:42:55 +00:00
|
|
|
bool saved_is_logging_;
|
2013-02-26 12:27:55 +00:00
|
|
|
bool is_profiling_;
|
2010-04-06 10:36:38 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CpuProfiler);
|
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
#endif // V8_CPU_PROFILER_H_
|