OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/tracing/trace-event.h" | 5 #include "src/tracing/trace-event.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "src/counters.h" | |
10 #include "src/isolate.h" | 9 #include "src/isolate.h" |
11 #include "src/v8.h" | 10 #include "src/v8.h" |
12 | 11 |
13 namespace v8 { | 12 namespace v8 { |
14 namespace internal { | 13 namespace internal { |
15 namespace tracing { | 14 namespace tracing { |
16 | 15 |
17 // A global flag used as a shortcut to check for the | 16 // A global flag used as a shortcut to check for the |
18 // v8.runtime-call-stats category due to its high frequency use. | 17 // v8.runtime-call-stats category due to its high frequency use. |
19 base::Atomic32 kRuntimeCallStatsTracingEnabled = false; | 18 base::Atomic32 kRuntimeCallStatsTracingEnabled = false; |
20 | 19 |
21 v8::Platform* TraceEventHelper::GetCurrentPlatform() { | 20 v8::Platform* TraceEventHelper::GetCurrentPlatform() { |
22 return v8::internal::V8::GetCurrentPlatform(); | 21 return v8::internal::V8::GetCurrentPlatform(); |
23 } | 22 } |
24 | 23 |
25 void CallStatsScopedTracer::AddEndTraceEvent() { | 24 void CallStatsScopedTracer::AddEndTraceEvent() { |
26 if (!has_parent_scope_ && p_data_->isolate) { | 25 if (!has_parent_scope_ && p_data_->isolate) { |
27 v8::internal::tracing::AddTraceEvent( | 26 v8::internal::tracing::AddTraceEvent( |
28 TRACE_EVENT_PHASE_END, p_data_->category_group_enabled, p_data_->name, | 27 TRACE_EVENT_PHASE_END, p_data_->category_group_enabled, p_data_->name, |
29 v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId, | 28 v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId, |
30 v8::internal::tracing::kNoId, TRACE_EVENT_FLAG_NONE, | 29 v8::internal::tracing::kNoId, TRACE_EVENT_FLAG_NONE, |
31 "runtime-call-stats", TRACE_STR_COPY(p_data_->isolate->counters() | 30 "runtime-call-stats", |
32 ->tracing_runtime_call_stats() | 31 TRACE_STR_COPY(p_data_->isolate->trace_event_stats_table()->Dump())); |
33 ->Dump())); | |
34 } else { | 32 } else { |
35 v8::internal::tracing::AddTraceEvent( | 33 v8::internal::tracing::AddTraceEvent( |
36 TRACE_EVENT_PHASE_END, p_data_->category_group_enabled, p_data_->name, | 34 TRACE_EVENT_PHASE_END, p_data_->category_group_enabled, p_data_->name, |
37 v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId, | 35 v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId, |
38 v8::internal::tracing::kNoId, TRACE_EVENT_FLAG_NONE); | 36 v8::internal::tracing::kNoId, TRACE_EVENT_FLAG_NONE); |
39 } | 37 } |
40 } | 38 } |
41 | 39 |
42 void CallStatsScopedTracer::Initialize(v8::internal::Isolate* isolate, | 40 void CallStatsScopedTracer::Initialize(Isolate* isolate, |
43 const uint8_t* category_group_enabled, | 41 const uint8_t* category_group_enabled, |
44 const char* name) { | 42 const char* name) { |
45 data_.isolate = isolate; | 43 data_.isolate = isolate; |
46 data_.category_group_enabled = category_group_enabled; | 44 data_.category_group_enabled = category_group_enabled; |
47 data_.name = name; | 45 data_.name = name; |
48 p_data_ = &data_; | 46 p_data_ = &data_; |
49 RuntimeCallStats* table = isolate->counters()->tracing_runtime_call_stats(); | 47 TraceEventStatsTable* table = isolate->trace_event_stats_table(); |
50 has_parent_scope_ = table->InUse(); | 48 has_parent_scope_ = table->InUse(); |
51 if (!has_parent_scope_) table->Reset(); | 49 if (!has_parent_scope_) table->Reset(); |
52 v8::internal::tracing::AddTraceEvent( | 50 v8::internal::tracing::AddTraceEvent( |
53 TRACE_EVENT_PHASE_BEGIN, category_group_enabled, name, | 51 TRACE_EVENT_PHASE_BEGIN, category_group_enabled, name, |
54 v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId, | 52 v8::internal::tracing::kGlobalScope, v8::internal::tracing::kNoId, |
55 TRACE_EVENT_FLAG_NONE, v8::internal::tracing::kNoId); | 53 TRACE_EVENT_FLAG_NONE, v8::internal::tracing::kNoId); |
56 } | 54 } |
57 | 55 |
| 56 void TraceEventStatsTable::Enter(Isolate* isolate, |
| 57 TraceEventCallStatsTimer* timer, |
| 58 CounterId counter_id) { |
| 59 TraceEventStatsTable* table = isolate->trace_event_stats_table(); |
| 60 RuntimeCallCounter* counter = &(table->*counter_id); |
| 61 timer->Start(counter, table->current_timer_); |
| 62 table->current_timer_ = timer; |
| 63 } |
| 64 |
| 65 void TraceEventStatsTable::Leave(Isolate* isolate, |
| 66 TraceEventCallStatsTimer* timer) { |
| 67 TraceEventStatsTable* table = isolate->trace_event_stats_table(); |
| 68 if (table->current_timer_ == timer) { |
| 69 table->current_timer_ = timer->Stop(); |
| 70 } |
| 71 } |
| 72 |
| 73 void TraceEventStatsTable::Reset() { |
| 74 in_use_ = true; |
| 75 current_timer_ = nullptr; |
| 76 |
| 77 #define RESET_COUNTER(name) this->name.Reset(); |
| 78 FOR_EACH_MANUAL_COUNTER(RESET_COUNTER) |
| 79 #undef RESET_COUNTER |
| 80 |
| 81 #define RESET_COUNTER(name, nargs, result_size) this->Runtime_##name.Reset(); |
| 82 FOR_EACH_INTRINSIC(RESET_COUNTER) |
| 83 #undef RESET_COUNTER |
| 84 |
| 85 #define RESET_COUNTER(name) this->Builtin_##name.Reset(); |
| 86 BUILTIN_LIST_C(RESET_COUNTER) |
| 87 #undef RESET_COUNTER |
| 88 |
| 89 #define RESET_COUNTER(name) this->API_##name.Reset(); |
| 90 FOR_EACH_API_COUNTER(RESET_COUNTER) |
| 91 #undef RESET_COUNTER |
| 92 |
| 93 #define RESET_COUNTER(name) this->Handler_##name.Reset(); |
| 94 FOR_EACH_HANDLER_COUNTER(RESET_COUNTER) |
| 95 #undef RESET_COUNTER |
| 96 } |
| 97 |
| 98 const char* TraceEventStatsTable::Dump() { |
| 99 buffer_.str(std::string()); |
| 100 buffer_.clear(); |
| 101 buffer_ << "{"; |
| 102 #define DUMP_COUNTER(name) \ |
| 103 if (this->name.count > 0) this->name.Dump(buffer_); |
| 104 FOR_EACH_MANUAL_COUNTER(DUMP_COUNTER) |
| 105 #undef DUMP_COUNTER |
| 106 |
| 107 #define DUMP_COUNTER(name, nargs, result_size) \ |
| 108 if (this->Runtime_##name.count > 0) this->Runtime_##name.Dump(buffer_); |
| 109 FOR_EACH_INTRINSIC(DUMP_COUNTER) |
| 110 #undef DUMP_COUNTER |
| 111 |
| 112 #define DUMP_COUNTER(name) \ |
| 113 if (this->Builtin_##name.count > 0) this->Builtin_##name.Dump(buffer_); |
| 114 BUILTIN_LIST_C(DUMP_COUNTER) |
| 115 #undef DUMP_COUNTER |
| 116 |
| 117 #define DUMP_COUNTER(name) \ |
| 118 if (this->API_##name.count > 0) this->API_##name.Dump(buffer_); |
| 119 FOR_EACH_API_COUNTER(DUMP_COUNTER) |
| 120 #undef DUMP_COUNTER |
| 121 |
| 122 #define DUMP_COUNTER(name) \ |
| 123 if (this->Handler_##name.count > 0) this->Handler_##name.Dump(buffer_); |
| 124 FOR_EACH_HANDLER_COUNTER(DUMP_COUNTER) |
| 125 #undef DUMP_COUNTER |
| 126 buffer_ << "\"END\":[]}"; |
| 127 const std::string& buffer_str = buffer_.str(); |
| 128 size_t length = buffer_str.size(); |
| 129 if (length > len_) { |
| 130 buffer_c_str_.reset(new char[length + 1]); |
| 131 len_ = length; |
| 132 } |
| 133 strncpy(buffer_c_str_.get(), buffer_str.c_str(), length + 1); |
| 134 in_use_ = false; |
| 135 return buffer_c_str_.get(); |
| 136 } |
| 137 |
58 } // namespace tracing | 138 } // namespace tracing |
59 } // namespace internal | 139 } // namespace internal |
60 } // namespace v8 | 140 } // namespace v8 |
OLD | NEW |