diff --git a/src/d8.cc b/src/d8.cc index 934bcbd832..9648168b33 100644 --- a/src/d8.cc +++ b/src/d8.cc @@ -268,12 +268,19 @@ Handle Shell::DebugCommandToJSONRequest(Handle command) { } -int32_t* Counter::Bind(const char* name) { +int32_t* Counter::Bind(const char* name, bool is_histogram) { int i; for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) name_[i] = static_cast(name[i]); name_[i] = '\0'; - return &counter_; + is_histogram_ = is_histogram; + return ptr(); +} + + +void Counter::AddSample(int32_t sample) { + count_++; + sample_total_ += sample; } @@ -302,6 +309,8 @@ void Shell::MapCounters(const char* name) { } counters_ = static_cast(memory); V8::SetCounterFunction(LookupCounter); + V8::SetCreateHistogramFunction(CreateHistogram); + V8::SetAddHistogramSampleFunction(AddHistogramSample); } @@ -316,15 +325,44 @@ int CounterMap::Hash(const char* name) { } -int* Shell::LookupCounter(const char* name) { +Counter* Shell::GetCounter(const char* name, bool is_histogram) { Counter* counter = counter_map_->Lookup(name); + + if (counter == NULL) { + counter = counters_->GetNextCounter(); + if (counter != NULL) { + counter_map_->Set(name, counter); + counter->Bind(name, is_histogram); + } + } else { + ASSERT(counter->is_histogram() == is_histogram); + } + return counter; +} + + +int* Shell::LookupCounter(const char* name) { + Counter* counter = GetCounter(name, false); + if (counter != NULL) { return counter->ptr(); + } else { + return NULL; } - Counter* result = counters_->GetNextCounter(); - if (result == NULL) return NULL; - counter_map_->Set(name, result); - return result->Bind(name); +} + + +void* Shell::CreateHistogram(const char* name, + int min, + int max, + size_t buckets) { + return GetCounter(name, true); +} + + +void Shell::AddHistogramSample(void* histogram, int sample) { + Counter* counter = reinterpret_cast(histogram); + counter->AddSample(sample); } @@ -333,8 +371,12 @@ void Shell::Initialize() { // Set up counters if (i::FLAG_map_counters != NULL) MapCounters(i::FLAG_map_counters); - if (i::FLAG_dump_counters) + if (i::FLAG_dump_counters) { V8::SetCounterFunction(LookupCounter); + V8::SetCreateHistogramFunction(CreateHistogram); + V8::SetAddHistogramSampleFunction(AddHistogramSample); + } + // Initialize the global objects HandleScope scope; Handle global_template = ObjectTemplate::New(); @@ -406,7 +448,14 @@ void Shell::OnExit() { ::printf("+----------------------------------------+-------------+\n"); for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) { Counter* counter = i.CurrentValue(); - ::printf("| %-38s | %11i |\n", i.CurrentKey(), counter->value()); + if (counter->is_histogram()) { + ::printf("| c:%-36s | %11i |\n", i.CurrentKey(), counter->count()); + ::printf("| t:%-36s | %11i |\n", + i.CurrentKey(), + counter->sample_total()); + } else { + ::printf("| %-38s | %11i |\n", i.CurrentKey(), counter->count()); + } } ::printf("+----------------------------------------+-------------+\n"); } diff --git a/src/d8.h b/src/d8.h index 54ae6127c8..342a0d2c2e 100644 --- a/src/d8.h +++ b/src/d8.h @@ -42,11 +42,16 @@ namespace i = v8::internal; class Counter { public: static const int kMaxNameSize = 64; - int32_t* Bind(const char* name); - int32_t* ptr() { return &counter_; } - int32_t value() { return counter_; } + int32_t* Bind(const char* name, bool histogram); + int32_t* ptr() { return &count_; } + int32_t count() { return count_; } + int32_t sample_total() { return sample_total_; } + bool is_histogram() { return is_histogram_; } + void AddSample(int32_t sample); private: - int32_t counter_; + int32_t count_; + int32_t sample_total_; + bool is_histogram_; uint8_t name_[kMaxNameSize]; }; @@ -116,6 +121,11 @@ class Shell: public i::AllStatic { static void Initialize(); static void OnExit(); static int* LookupCounter(const char* name); + static void* CreateHistogram(const char* name, + int min, + int max, + size_t buckets); + static void AddHistogramSample(void* histogram, int sample); static void MapCounters(const char* name); static Handle ReadFile(const char* name); static void RunShell(); @@ -179,6 +189,7 @@ class Shell: public i::AllStatic { static CounterCollection local_counters_; static CounterCollection* counters_; static i::OS::MemoryMappedFile* counters_file_; + static Counter* GetCounter(const char* name, bool is_histogram); };