- Added ability to call histograms from within v8

- Changed the StatsRates to use the new HistogramTimers

Review URL: http://codereview.chromium.org/42020

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1510 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 8dffcb9e
...@@ -839,7 +839,7 @@ class EXPORT String : public Primitive { ...@@ -839,7 +839,7 @@ class EXPORT String : public Primitive {
* external string resource. * external string resource.
*/ */
static Local<String> NewExternal(ExternalStringResource* resource); static Local<String> NewExternal(ExternalStringResource* resource);
/** /**
* Associate an external string resource with this string by transforming it * Associate an external string resource with this string by transforming it
* in place so that existing references to this string in the JavaScript heap * in place so that existing references to this string in the JavaScript heap
...@@ -859,7 +859,7 @@ class EXPORT String : public Primitive { ...@@ -859,7 +859,7 @@ class EXPORT String : public Primitive {
* external string resource. * external string resource.
*/ */
static Local<String> NewExternal(ExternalAsciiStringResource* resource); static Local<String> NewExternal(ExternalAsciiStringResource* resource);
/** /**
* Associate an external string resource with this string by transforming it * Associate an external string resource with this string by transforming it
* in place so that existing references to this string in the JavaScript heap * in place so that existing references to this string in the JavaScript heap
...@@ -1816,6 +1816,13 @@ class EXPORT Exception { ...@@ -1816,6 +1816,13 @@ class EXPORT Exception {
typedef int* (*CounterLookupCallback)(const char* name); typedef int* (*CounterLookupCallback)(const char* name);
typedef void* (*CreateHistogramCallback)(const char* name,
int min,
int max,
size_t buckets);
typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
// --- F a i l e d A c c e s s C h e c k C a l l b a c k --- // --- F a i l e d A c c e s s C h e c k C a l l b a c k ---
typedef void (*FailedAccessCheckCallback)(Local<Object> target, typedef void (*FailedAccessCheckCallback)(Local<Object> target,
AccessType type, AccessType type,
...@@ -1905,6 +1912,15 @@ class EXPORT V8 { ...@@ -1905,6 +1912,15 @@ class EXPORT V8 {
*/ */
static void SetCounterFunction(CounterLookupCallback); static void SetCounterFunction(CounterLookupCallback);
/**
* Enables the host application to provide a mechanism for recording
* histograms. The CreateHistogram function returns a
* histogram which will later be passed to the AddHistogramSample
* function.
*/
static void SetCreateHistogramFunction(CreateHistogramCallback);
static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
/** /**
* Enables the computation of a sliding window of states. The sliding * Enables the computation of a sliding window of states. The sliding
* window information is recorded in statistics counters. * window information is recorded in statistics counters.
......
...@@ -2700,6 +2700,15 @@ void V8::SetCounterFunction(CounterLookupCallback callback) { ...@@ -2700,6 +2700,15 @@ void V8::SetCounterFunction(CounterLookupCallback callback) {
i::StatsTable::SetCounterFunction(callback); i::StatsTable::SetCounterFunction(callback);
} }
void V8::SetCreateHistogramFunction(CreateHistogramCallback callback) {
if (IsDeadCheck("v8::V8::SetCreateHistogramFunction()")) return;
i::StatsTable::SetCreateHistogramFunction(callback);
}
void V8::SetAddHistogramSampleFunction(AddHistogramSampleCallback callback) {
if (IsDeadCheck("v8::V8::SetAddHistogramSampleFunction()")) return;
i::StatsTable::SetAddHistogramSampleFunction(callback);
}
void V8::EnableSlidingStateWindow() { void V8::EnableSlidingStateWindow() {
if (IsDeadCheck("v8::V8::EnableSlidingStateWindow()")) return; if (IsDeadCheck("v8::V8::EnableSlidingStateWindow()")) return;
......
...@@ -110,10 +110,10 @@ static Handle<JSFunction> MakeFunction(bool is_global, ...@@ -110,10 +110,10 @@ static Handle<JSFunction> MakeFunction(bool is_global,
// Measure how long it takes to do the compilation; only take the // Measure how long it takes to do the compilation; only take the
// rest of the function into account to avoid overlap with the // rest of the function into account to avoid overlap with the
// parsing statistics. // parsing statistics.
StatsRate* rate = is_eval HistogramTimer* rate = is_eval
? &Counters::compile_eval ? &Counters::compile_eval
: &Counters::compile; : &Counters::compile;
StatsRateScope timer(rate); HistogramTimerScope timer(rate);
// Compile the code. // Compile the code.
Handle<Code> code = MakeCode(lit, script, context, is_eval); Handle<Code> code = MakeCode(lit, script, context, is_eval);
...@@ -300,7 +300,7 @@ bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared, ...@@ -300,7 +300,7 @@ bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared,
// Measure how long it takes to do the lazy compilation; only take // Measure how long it takes to do the lazy compilation; only take
// the rest of the function into account to avoid overlap with the // the rest of the function into account to avoid overlap with the
// lazy parsing statistics. // lazy parsing statistics.
StatsRateScope timer(&Counters::compile_lazy); HistogramTimerScope timer(&Counters::compile_lazy);
// Compile the code. // Compile the code.
Handle<Code> code = MakeCode(lit, script, Handle<Context>::null(), false); Handle<Code> code = MakeCode(lit, script, Handle<Context>::null(), false);
......
...@@ -33,6 +33,8 @@ ...@@ -33,6 +33,8 @@
namespace v8 { namespace internal { namespace v8 { namespace internal {
CounterLookupCallback StatsTable::lookup_function_ = NULL; CounterLookupCallback StatsTable::lookup_function_ = NULL;
CreateHistogramCallback StatsTable::create_histogram_function_ = NULL;
AddHistogramSampleCallback StatsTable::add_histogram_sample_function_ = NULL;
// Start the timer. // Start the timer.
void StatsCounterTimer::Start() { void StatsCounterTimer::Start() {
...@@ -53,4 +55,23 @@ void StatsCounterTimer::Stop() { ...@@ -53,4 +55,23 @@ void StatsCounterTimer::Stop() {
counter_.Increment(milliseconds); counter_.Increment(milliseconds);
} }
// Start the timer.
void HistogramTimer::Start() {
if (GetHistogram() != NULL) {
stop_time_ = 0;
start_time_ = OS::Ticks();
}
}
// Stop the timer and record the results.
void HistogramTimer::Stop() {
if (histogram_ != NULL) {
stop_time_ = OS::Ticks();
// Compute the delta between start and stop, in milliseconds.
int milliseconds = static_cast<int>(stop_time_ - start_time_) / 1000;
StatsTable::AddHistogramSample(histogram_, milliseconds);
}
}
} } // namespace v8::internal } } // namespace v8::internal
...@@ -42,6 +42,18 @@ class StatsTable : public AllStatic { ...@@ -42,6 +42,18 @@ class StatsTable : public AllStatic {
lookup_function_ = f; lookup_function_ = f;
} }
// Register an application-defined function to create
// a histogram for passing to the AddHistogramSample function
static void SetCreateHistogramFunction(CreateHistogramCallback f) {
create_histogram_function_ = f;
}
// Register an application-defined function to add a sample
// to a histogram created with CreateHistogram function
static void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) {
add_histogram_sample_function_ = f;
}
static bool HasCounterFunction() { static bool HasCounterFunction() {
return lookup_function_ != NULL; return lookup_function_ != NULL;
} }
...@@ -57,8 +69,30 @@ class StatsTable : public AllStatic { ...@@ -57,8 +69,30 @@ class StatsTable : public AllStatic {
return lookup_function_(name); return lookup_function_(name);
} }
// Create a histogram by name. If the create is successful,
// returns a non-NULL pointer for use with AddHistogramSample
// function. min and max define the expected minimum and maximum
// sample values. buckets is the maximum number of buckets
// that the samples will be grouped into.
static void *CreateHistogram(const char* name,
int min,
int max,
size_t buckets) {
if (!create_histogram_function_) return NULL;
return create_histogram_function_(name, min, max, buckets);
}
// Add a sample to a histogram created with the CreateHistogram
// function.
static void AddHistogramSample(void* histogram, int sample) {
if (!add_histogram_sample_function_) return;
return add_histogram_sample_function_(histogram, sample);
}
private: private:
static CounterLookupCallback lookup_function_; static CounterLookupCallback lookup_function_;
static CreateHistogramCallback create_histogram_function_;
static AddHistogramSampleCallback add_histogram_sample_function_;
}; };
// StatsCounters are dynamically created values which can be tracked in // StatsCounters are dynamically created values which can be tracked in
...@@ -152,44 +186,50 @@ struct StatsCounterTimer { ...@@ -152,44 +186,50 @@ struct StatsCounterTimer {
} }
}; };
// A StatsRate is a combination of both a timer and a counter so that // A HistogramTimer allows distributions of results to be created
// several statistics can be produced: // HistogramTimer t = { L"foo", NULL, false, 0, 0 };
// min, max, avg, count, total struct HistogramTimer {
// const char* name_;
// For example: void* histogram_;
// StatsCounter c = { { { L"t:myrate", NULL, false }, 0, 0 }, bool lookup_done_;
// { L"c:myrate", NULL, false } };
struct StatsRate {
StatsCounterTimer timer_;
StatsCounter counter_;
// Starts the rate timer. int64_t start_time_;
void Start() { int64_t stop_time_;
timer_.Start();
// Start the timer.
void Start();
// Stop the timer and record the results.
void Stop();
// Returns true if the timer is running.
bool Running() {
return (histogram_ != NULL) && (start_time_ != 0) && (stop_time_ == 0);
} }
// Stops the rate and records the time. protected:
void Stop() { // Returns the handle to the histogram.
if (timer_.Running()) { void* GetHistogram() {
timer_.Stop(); if (!lookup_done_) {
counter_.Increment(); lookup_done_ = true;
histogram_ = StatsTable::CreateHistogram(name_, 0, 10000, 50);
} }
return histogram_;
} }
}; };
// Helper class for scoping a HistogramTimer.
// Helper class for scoping a rate. class HistogramTimerScope BASE_EMBEDDED {
class StatsRateScope BASE_EMBEDDED {
public: public:
explicit StatsRateScope(StatsRate* rate) : explicit HistogramTimerScope(HistogramTimer* timer) :
rate_(rate) { timer_(timer) {
rate_->Start(); timer_->Start();
} }
~StatsRateScope() { ~HistogramTimerScope() {
rate_->Stop(); timer_->Stop();
} }
private: private:
StatsRate* rate_; HistogramTimer* timer_;
}; };
......
...@@ -310,7 +310,7 @@ void Heap::CollectAllGarbageIfContextDisposed() { ...@@ -310,7 +310,7 @@ void Heap::CollectAllGarbageIfContextDisposed() {
// contexts are disposed and leave it to the embedder to make // contexts are disposed and leave it to the embedder to make
// informed decisions about when to force a collection. // informed decisions about when to force a collection.
if (!FLAG_expose_gc && context_disposed_pending_) { if (!FLAG_expose_gc && context_disposed_pending_) {
StatsRateScope scope(&Counters::gc_context); HistogramTimerScope scope(&Counters::gc_context);
CollectAllGarbage(); CollectAllGarbage();
} }
context_disposed_pending_ = false; context_disposed_pending_ = false;
...@@ -345,7 +345,7 @@ bool Heap::CollectGarbage(int requested_size, AllocationSpace space) { ...@@ -345,7 +345,7 @@ bool Heap::CollectGarbage(int requested_size, AllocationSpace space) {
// Tell the tracer which collector we've selected. // Tell the tracer which collector we've selected.
tracer.set_collector(collector); tracer.set_collector(collector);
StatsRate* rate = (collector == SCAVENGER) HistogramTimer* rate = (collector == SCAVENGER)
? &Counters::gc_scavenger ? &Counters::gc_scavenger
: &Counters::gc_compactor; : &Counters::gc_compactor;
rate->Start(); rate->Start();
......
...@@ -1075,7 +1075,7 @@ Parser::Parser(Handle<Script> script, ...@@ -1075,7 +1075,7 @@ Parser::Parser(Handle<Script> script,
bool Parser::PreParseProgram(unibrow::CharacterStream* stream) { bool Parser::PreParseProgram(unibrow::CharacterStream* stream) {
StatsRateScope timer(&Counters::pre_parse); HistogramTimerScope timer(&Counters::pre_parse);
StackGuard guard; StackGuard guard;
AssertNoZoneAllocation assert_no_zone_allocation; AssertNoZoneAllocation assert_no_zone_allocation;
AssertNoAllocation assert_no_allocation; AssertNoAllocation assert_no_allocation;
...@@ -1098,7 +1098,7 @@ FunctionLiteral* Parser::ParseProgram(Handle<String> source, ...@@ -1098,7 +1098,7 @@ FunctionLiteral* Parser::ParseProgram(Handle<String> source,
bool in_global_context) { bool in_global_context) {
ZoneScope zone_scope(DONT_DELETE_ON_EXIT); ZoneScope zone_scope(DONT_DELETE_ON_EXIT);
StatsRateScope timer(&Counters::parse); HistogramTimerScope timer(&Counters::parse);
StringShape shape(*source); StringShape shape(*source);
Counters::total_parse_size.Increment(source->length(shape)); Counters::total_parse_size.Increment(source->length(shape));
...@@ -1151,7 +1151,7 @@ FunctionLiteral* Parser::ParseLazy(Handle<String> source, ...@@ -1151,7 +1151,7 @@ FunctionLiteral* Parser::ParseLazy(Handle<String> source,
int start_position, int start_position,
bool is_expression) { bool is_expression) {
ZoneScope zone_scope(DONT_DELETE_ON_EXIT); ZoneScope zone_scope(DONT_DELETE_ON_EXIT);
StatsRateScope timer(&Counters::parse_lazy); HistogramTimerScope timer(&Counters::parse_lazy);
source->TryFlattenIfNotFlat(StringShape(*source)); source->TryFlattenIfNotFlat(StringShape(*source));
StringShape shape(*source); StringShape shape(*source);
Counters::total_parse_size.Increment(source->length(shape)); Counters::total_parse_size.Increment(source->length(shape));
......
...@@ -31,12 +31,10 @@ ...@@ -31,12 +31,10 @@
namespace v8 { namespace internal { namespace v8 { namespace internal {
#define SR(name, caption) \ #define HT(name, caption) \
StatsRate Counters::name = { \ HistogramTimer Counters::name = { #caption, NULL, false, 0, 0 }; \
{ { "t:" #caption, NULL, false }, 0, 0 }, \
{ "c:" #caption, NULL, false } };
STATS_RATE_LIST(SR) HISTOGRAM_TIMER_LIST(HT)
#undef SR #undef SR
#define SC(name, caption) \ #define SC(name, caption) \
......
...@@ -32,16 +32,16 @@ ...@@ -32,16 +32,16 @@
namespace v8 { namespace internal { namespace v8 { namespace internal {
#define STATS_RATE_LIST(SR) \ #define HISTOGRAM_TIMER_LIST(HT) \
SR(gc_compactor, V8.GCCompactor) /* GC Compactor time */ \ HT(gc_compactor, V8.GCCompactor) /* GC Compactor time */ \
SR(gc_scavenger, V8.GCScavenger) /* GC Scavenger time */ \ HT(gc_scavenger, V8.GCScavenger) /* GC Scavenger time */ \
SR(gc_context, V8.GCContext) /* GC context cleanup time */ \ HT(gc_context, V8.GCContext) /* GC context cleanup time */ \
SR(compile, V8.Compile) /* Compile time*/ \ HT(compile, V8.Compile) /* Compile time*/ \
SR(compile_eval, V8.CompileEval) /* Eval compile time */ \ HT(compile_eval, V8.CompileEval) /* Eval compile time */ \
SR(compile_lazy, V8.CompileLazy) /* Lazy compile time */ \ HT(compile_lazy, V8.CompileLazy) /* Lazy compile time */ \
SR(parse, V8.Parse) /* Parse time */ \ HT(parse, V8.Parse) /* Parse time */ \
SR(parse_lazy, V8.ParseLazy) /* Lazy parse time */ \ HT(parse_lazy, V8.ParseLazy) /* Lazy parse time */ \
SR(pre_parse, V8.PreParse) /* Pre-parse time */ HT(pre_parse, V8.PreParse) /* Pre-parse time */
// WARNING: STATS_COUNTER_LIST_* is a very large macro that is causing MSVC // WARNING: STATS_COUNTER_LIST_* is a very large macro that is causing MSVC
// Intellisense to crash. It was broken into two macros (each of length 40 // Intellisense to crash. It was broken into two macros (each of length 40
...@@ -129,10 +129,10 @@ namespace v8 { namespace internal { ...@@ -129,10 +129,10 @@ namespace v8 { namespace internal {
// This file contains all the v8 counters that are in use. // This file contains all the v8 counters that are in use.
class Counters : AllStatic { class Counters : AllStatic {
public: public:
#define SR(name, caption) \ #define HT(name, caption) \
static StatsRate name; static HistogramTimer name;
STATS_RATE_LIST(SR) HISTOGRAM_TIMER_LIST(HT)
#undef SR #undef HT
#define SC(name, caption) \ #define SC(name, caption) \
static StatsCounter name; static StatsCounter name;
...@@ -142,7 +142,7 @@ class Counters : AllStatic { ...@@ -142,7 +142,7 @@ class Counters : AllStatic {
enum Id { enum Id {
#define RATE_ID(name, caption) k_##name, #define RATE_ID(name, caption) k_##name,
STATS_RATE_LIST(RATE_ID) HISTOGRAM_TIMER_LIST(RATE_ID)
#undef RATE_ID #undef RATE_ID
#define COUNTER_ID(name, caption) k_##name, #define COUNTER_ID(name, caption) k_##name,
STATS_COUNTER_LIST_1(COUNTER_ID) STATS_COUNTER_LIST_1(COUNTER_ID)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment