From 65b3af466e9aea7bc451558d671fcd9866b6cbe3 Mon Sep 17 00:00:00 2001 From: lpy Date: Wed, 19 Oct 2016 07:56:18 -0700 Subject: [PATCH] Use TracedValue in runtime statistics. We introduced TracedValue into V8 tracing previously, this patch uses it to build JSON string of runtime statistics instead of using stringstream as buffer. BUG=v8:5089 LOG=N Review-Url: https://chromiumcodereview.appspot.com/2418303002 Cr-Commit-Position: refs/heads/master@{#40443} --- src/counters.cc | 26 ++++++++++++-------------- src/counters.h | 6 +++--- src/tracing/trace-event.cc | 8 ++++---- src/tracing/traced-value.cc | 6 ++++++ src/tracing/traced-value.h | 1 + 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/counters.cc b/src/counters.cc index df75e21279..8fcfd54141 100644 --- a/src/counters.cc +++ b/src/counters.cc @@ -277,9 +277,11 @@ void RuntimeCallCounter::Reset() { time = base::TimeDelta(); } -void RuntimeCallCounter::Dump(std::stringstream& out) { - out << "\"" << name << "\":[" << count << "," << time.InMicroseconds() - << "],"; +void RuntimeCallCounter::Dump(v8::tracing::TracedValue* value) { + value->BeginArray(name); + value->AppendLongInteger(count); + value->AppendLongInteger(time.InMicroseconds()); + value->EndArray(); } // static @@ -365,37 +367,33 @@ void RuntimeCallStats::Reset() { in_use_ = true; } -std::string RuntimeCallStats::Dump() { - buffer_.str(std::string()); - buffer_.clear(); - buffer_ << "{"; +void RuntimeCallStats::Dump(v8::tracing::TracedValue* value) { #define DUMP_COUNTER(name) \ - if (this->name.count > 0) this->name.Dump(buffer_); + if (this->name.count > 0) this->name.Dump(value); FOR_EACH_MANUAL_COUNTER(DUMP_COUNTER) #undef DUMP_COUNTER #define DUMP_COUNTER(name, nargs, result_size) \ - if (this->Runtime_##name.count > 0) this->Runtime_##name.Dump(buffer_); + if (this->Runtime_##name.count > 0) this->Runtime_##name.Dump(value); FOR_EACH_INTRINSIC(DUMP_COUNTER) #undef DUMP_COUNTER #define DUMP_COUNTER(name) \ - if (this->Builtin_##name.count > 0) this->Builtin_##name.Dump(buffer_); + if (this->Builtin_##name.count > 0) this->Builtin_##name.Dump(value); BUILTIN_LIST_C(DUMP_COUNTER) #undef DUMP_COUNTER #define DUMP_COUNTER(name) \ - if (this->API_##name.count > 0) this->API_##name.Dump(buffer_); + if (this->API_##name.count > 0) this->API_##name.Dump(value); FOR_EACH_API_COUNTER(DUMP_COUNTER) #undef DUMP_COUNTER #define DUMP_COUNTER(name) \ - if (this->Handler_##name.count > 0) this->Handler_##name.Dump(buffer_); + if (this->Handler_##name.count > 0) this->Handler_##name.Dump(value); FOR_EACH_HANDLER_COUNTER(DUMP_COUNTER) #undef DUMP_COUNTER - buffer_ << "\"END\":[]}"; + in_use_ = false; - return buffer_.str(); } } // namespace internal diff --git a/src/counters.h b/src/counters.h index 8967c500c5..c4a20dc30c 100644 --- a/src/counters.h +++ b/src/counters.h @@ -15,6 +15,7 @@ #include "src/objects.h" #include "src/runtime/runtime.h" #include "src/tracing/trace-event.h" +#include "src/tracing/traced-value.h" namespace v8 { namespace internal { @@ -484,7 +485,7 @@ double AggregatedMemoryHistogram::Aggregate(double current_ms, struct RuntimeCallCounter { explicit RuntimeCallCounter(const char* name) : name(name) {} V8_NOINLINE void Reset(); - V8_NOINLINE void Dump(std::stringstream& out); + V8_NOINLINE void Dump(v8::tracing::TracedValue* value); const char* name; int64_t count = 0; @@ -799,7 +800,7 @@ class RuntimeCallStats { void Reset(); void Print(std::ostream& os); - std::string Dump(); + V8_NOINLINE void Dump(v8::tracing::TracedValue* value); RuntimeCallStats() { Reset(); @@ -810,7 +811,6 @@ class RuntimeCallStats { bool InUse() { return in_use_; } private: - std::stringstream buffer_; // Counter to track recursive time events. RuntimeCallTimer* current_timer_ = NULL; // Used to track nested tracing scopes. diff --git a/src/tracing/trace-event.cc b/src/tracing/trace-event.cc index 440af193e9..a24ebc0d97 100644 --- a/src/tracing/trace-event.cc +++ b/src/tracing/trace-event.cc @@ -8,6 +8,7 @@ #include "src/counters.h" #include "src/isolate.h" +#include "src/tracing/traced-value.h" #include "src/v8.h" namespace v8 { @@ -24,14 +25,13 @@ v8::Platform* TraceEventHelper::GetCurrentPlatform() { void CallStatsScopedTracer::AddEndTraceEvent() { if (!has_parent_scope_ && p_data_->isolate) { + auto value = v8::tracing::TracedValue::Create(); + p_data_->isolate->counters()->runtime_call_stats()->Dump(value.get()); v8::internal::tracing::AddTraceEvent( TRACE_EVENT_PHASE_END, p_data_->category_group_enabled, p_data_->name, v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId, v8::internal::tracing::kNoId, TRACE_EVENT_FLAG_NONE, - "runtime-call-stats", TRACE_STR_COPY(p_data_->isolate->counters() - ->runtime_call_stats() - ->Dump() - .c_str())); + "runtime-call-stats", std::move(value)); } else { v8::internal::tracing::AddTraceEvent( TRACE_EVENT_PHASE_END, p_data_->category_group_enabled, p_data_->name, diff --git a/src/tracing/traced-value.cc b/src/tracing/traced-value.cc index 3406bbf6fe..0c73b04295 100644 --- a/src/tracing/traced-value.cc +++ b/src/tracing/traced-value.cc @@ -121,6 +121,12 @@ void TracedValue::AppendInteger(int value) { data_ += std::to_string(value); } +void TracedValue::AppendLongInteger(int64_t value) { + DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray); + WriteComma(); + data_ += std::to_string(value); +} + void TracedValue::AppendDouble(double value) { DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray); WriteComma(); diff --git a/src/tracing/traced-value.h b/src/tracing/traced-value.h index befa387bc8..b5c265cd48 100644 --- a/src/tracing/traced-value.h +++ b/src/tracing/traced-value.h @@ -34,6 +34,7 @@ class TracedValue : public ConvertableToTraceFormat { void BeginArray(const char* name); void AppendInteger(int); + void AppendLongInteger(int64_t); void AppendDouble(double); void AppendBoolean(bool); void AppendString(const std::string&);