OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 #include <stdio.h> |
| 5 |
| 6 #include "include/libplatform/v8-tracing.h" |
| 7 #include "src/tracing/trace-event.h" |
| 8 #include "test/cctest/cctest.h" |
| 9 |
| 10 namespace v8 { |
| 11 namespace platform { |
| 12 namespace tracing { |
| 13 |
| 14 TEST(TestTraceConfig) { |
| 15 LocalContext env; |
| 16 TraceConfig* trace_config = new TraceConfig(); |
| 17 trace_config->EnableSampling(); |
| 18 trace_config->AddIncludedCategory("v8"); |
| 19 trace_config->AddIncludedCategory(TRACE_DISABLED_BY_DEFAULT("v8.runtime")); |
| 20 trace_config->AddExcludedCategory("v8.cpu_profile"); |
| 21 |
| 22 CHECK_EQ(trace_config->IsSamplingEnabled(), true); |
| 23 CHECK_EQ(trace_config->IsSystraceEnabled(), false); |
| 24 CHECK_EQ(trace_config->IsArgumentFilterEnabled(), false); |
| 25 CHECK_EQ(trace_config->IsCategoryGroupEnabled("v8"), true); |
| 26 CHECK_EQ(trace_config->IsCategoryGroupEnabled("v8.cpu_profile"), false); |
| 27 CHECK_EQ(trace_config->IsCategoryGroupEnabled("v8.cpu_profile.hires"), false); |
| 28 CHECK_EQ(trace_config->IsCategoryGroupEnabled( |
| 29 TRACE_DISABLED_BY_DEFAULT("v8.runtime")), |
| 30 true); |
| 31 delete trace_config; |
| 32 } |
| 33 |
| 34 TEST(TestTraceObject) { |
| 35 TraceObject trace_object; |
| 36 uint8_t category_enabled_flag = 41; |
| 37 trace_object.Initialize('X', &category_enabled_flag, "Test.Trace", |
| 38 "Test.Scope", 42, 123, 0, NULL, NULL, NULL, 0); |
| 39 CHECK_EQ('X', trace_object.phase()); |
| 40 CHECK_EQ(category_enabled_flag, *trace_object.category_enabled_flag()); |
| 41 CHECK_EQ(std::string("Test.Trace"), std::string(trace_object.name())); |
| 42 CHECK_EQ(std::string("Test.Scope"), std::string(trace_object.scope())); |
| 43 CHECK_EQ(0, trace_object.duration()); |
| 44 CHECK_EQ(0, trace_object.cpu_duration()); |
| 45 } |
| 46 |
| 47 class MockTraceWriter : public TraceWriter { |
| 48 public: |
| 49 void AppendTraceEvent(TraceObject* trace_event) override { |
| 50 events_.push_back(trace_event->name()); |
| 51 } |
| 52 |
| 53 void Flush() override {} |
| 54 |
| 55 std::vector<std::string> events() { return events_; } |
| 56 |
| 57 private: |
| 58 std::vector<std::string> events_; |
| 59 }; |
| 60 |
| 61 TEST(TestTraceBufferRingBuffer) { |
| 62 // We should be able to add kChunkSize * 2 + 1 trace events. |
| 63 const int HANDLES_COUNT = TraceBufferChunk::kChunkSize * 2 + 1; |
| 64 MockTraceWriter* writer = new MockTraceWriter(); |
| 65 TraceBuffer* ring_buffer = |
| 66 TraceBuffer::CreateTraceBufferRingBuffer(2, writer); |
| 67 std::string names[HANDLES_COUNT]; |
| 68 for (int i = 0; i < HANDLES_COUNT; ++i) { |
| 69 names[i] = "Test.EventNo" + std::to_string(i); |
| 70 } |
| 71 |
| 72 std::vector<uint64_t> handles(HANDLES_COUNT); |
| 73 uint8_t category_enabled_flag = 41; |
| 74 for (size_t i = 0; i < handles.size(); ++i) { |
| 75 TraceObject* trace_object = ring_buffer->AddTraceEvent(&handles[i]); |
| 76 CHECK_NOT_NULL(trace_object); |
| 77 trace_object->Initialize('X', &category_enabled_flag, names[i].c_str(), |
| 78 "Test.Scope", 42, 123, 0, NULL, NULL, NULL, 0); |
| 79 trace_object = ring_buffer->GetEventByHandle(handles[i]); |
| 80 CHECK_NOT_NULL(trace_object); |
| 81 CHECK_EQ('X', trace_object->phase()); |
| 82 CHECK_EQ(names[i], std::string(trace_object->name())); |
| 83 CHECK_EQ(category_enabled_flag, *trace_object->category_enabled_flag()); |
| 84 } |
| 85 |
| 86 // We should only be able to retrieve the last kChunkSize + 1. |
| 87 for (size_t i = 0; i < TraceBufferChunk::kChunkSize; ++i) { |
| 88 CHECK_NULL(ring_buffer->GetEventByHandle(handles[i])); |
| 89 } |
| 90 |
| 91 for (size_t i = TraceBufferChunk::kChunkSize; i < handles.size(); ++i) { |
| 92 TraceObject* trace_object = ring_buffer->GetEventByHandle(handles[i]); |
| 93 CHECK_NOT_NULL(trace_object); |
| 94 // The object properties should be correct. |
| 95 CHECK_EQ('X', trace_object->phase()); |
| 96 CHECK_EQ(names[i], std::string(trace_object->name())); |
| 97 CHECK_EQ(category_enabled_flag, *trace_object->category_enabled_flag()); |
| 98 } |
| 99 |
| 100 // Check Flush(), that the writer wrote the last kChunkSize 1 event names. |
| 101 ring_buffer->Flush(); |
| 102 auto events = writer->events(); |
| 103 CHECK_EQ(TraceBufferChunk::kChunkSize + 1, events.size()); |
| 104 for (size_t i = TraceBufferChunk::kChunkSize; i < handles.size(); ++i) { |
| 105 CHECK_EQ(names[i], events[i - TraceBufferChunk::kChunkSize]); |
| 106 } |
| 107 delete ring_buffer; |
| 108 } |
| 109 |
| 110 TEST(TestJSONTraceWriter) { |
| 111 std::ostringstream stream; |
| 112 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| 113 v8::Platform* default_platform = v8::platform::CreateDefaultPlatform(); |
| 114 i::V8::SetPlatformForTesting(default_platform); |
| 115 // Create a scope for the tracing controller to terminate the trace writer. |
| 116 { |
| 117 TracingController tracing_controller; |
| 118 platform::SetTracingController(default_platform, &tracing_controller); |
| 119 TraceWriter* writer = TraceWriter::CreateJSONTraceWriter(stream); |
| 120 |
| 121 TraceBuffer* ring_buffer = |
| 122 TraceBuffer::CreateTraceBufferRingBuffer(1, writer); |
| 123 tracing_controller.Initialize(ring_buffer); |
| 124 TraceConfig* trace_config = new TraceConfig(); |
| 125 trace_config->AddIncludedCategory("v8-cat"); |
| 126 tracing_controller.StartTracing(trace_config); |
| 127 |
| 128 TraceObject trace_object; |
| 129 trace_object.InitializeForTesting( |
| 130 'X', tracing_controller.GetCategoryGroupEnabled("v8-cat"), "Test0", |
| 131 v8::internal::tracing::kGlobalScope, 42, 123, 0, NULL, NULL, NULL, 0, |
| 132 11, 22, 100, 50, 33, 44); |
| 133 writer->AppendTraceEvent(&trace_object); |
| 134 trace_object.InitializeForTesting( |
| 135 'Y', tracing_controller.GetCategoryGroupEnabled("v8-cat"), "Test1", |
| 136 v8::internal::tracing::kGlobalScope, 43, 456, 0, NULL, NULL, NULL, 0, |
| 137 55, 66, 110, 55, 77, 88); |
| 138 writer->AppendTraceEvent(&trace_object); |
| 139 tracing_controller.StopTracing(); |
| 140 } |
| 141 |
| 142 std::string trace_str = stream.str(); |
| 143 std::string expected_trace_str = |
| 144 "{\"traceEvents\":[{\"pid\":11,\"tid\":22,\"ts\":100,\"tts\":50," |
| 145 "\"ph\":\"X\",\"cat\":\"v8-cat\",\"name\":\"Test0\",\"args\":{}," |
| 146 "\"dur\":33,\"tdur\":44},{\"pid\":55,\"tid\":66,\"ts\":110,\"tts\":55," |
| 147 "\"ph\":\"Y\",\"cat\":\"v8-cat\",\"name\":\"Test1\",\"args\":{},\"dur\":" |
| 148 "77,\"tdur\":88}]}"; |
| 149 |
| 150 CHECK_EQ(expected_trace_str, trace_str); |
| 151 |
| 152 i::V8::SetPlatformForTesting(old_platform); |
| 153 } |
| 154 |
| 155 TEST(TestTracingController) { |
| 156 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); |
| 157 v8::Platform* default_platform = v8::platform::CreateDefaultPlatform(); |
| 158 i::V8::SetPlatformForTesting(default_platform); |
| 159 |
| 160 TracingController tracing_controller; |
| 161 platform::SetTracingController(default_platform, &tracing_controller); |
| 162 |
| 163 MockTraceWriter* writer = new MockTraceWriter(); |
| 164 TraceBuffer* ring_buffer = |
| 165 TraceBuffer::CreateTraceBufferRingBuffer(1, writer); |
| 166 tracing_controller.Initialize(ring_buffer); |
| 167 TraceConfig* trace_config = new TraceConfig(); |
| 168 trace_config->AddIncludedCategory("v8"); |
| 169 tracing_controller.StartTracing(trace_config); |
| 170 |
| 171 TRACE_EVENT0("v8", "v8.Test"); |
| 172 // cat category is not included in default config |
| 173 TRACE_EVENT0("cat", "v8.Test2"); |
| 174 TRACE_EVENT0("v8", "v8.Test3"); |
| 175 tracing_controller.StopTracing(); |
| 176 |
| 177 CHECK_EQ(2, writer->events().size()); |
| 178 CHECK_EQ(std::string("v8.Test"), writer->events()[0]); |
| 179 CHECK_EQ(std::string("v8.Test3"), writer->events()[1]); |
| 180 |
| 181 i::V8::SetPlatformForTesting(old_platform); |
| 182 } |
| 183 |
| 184 } // namespace tracing |
| 185 } // namespace platform |
| 186 } // namespace v8 |
OLD | NEW |