OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 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 #include <limits> | 4 #include <limits> |
5 | 5 |
6 #include "include/libplatform/v8-tracing.h" | 6 #include "include/libplatform/v8-tracing.h" |
7 #include "src/tracing/trace-event.h" | 7 #include "src/tracing/trace-event.h" |
8 #include "test/cctest/cctest.h" | 8 #include "test/cctest/cctest.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
(...skipping 17 matching lines...) Expand all Loading... |
28 CHECK_EQ(trace_config->IsCategoryGroupEnabled( | 28 CHECK_EQ(trace_config->IsCategoryGroupEnabled( |
29 TRACE_DISABLED_BY_DEFAULT("v8.runtime")), | 29 TRACE_DISABLED_BY_DEFAULT("v8.runtime")), |
30 true); | 30 true); |
31 delete trace_config; | 31 delete trace_config; |
32 } | 32 } |
33 | 33 |
34 TEST(TestTraceObject) { | 34 TEST(TestTraceObject) { |
35 TraceObject trace_object; | 35 TraceObject trace_object; |
36 uint8_t category_enabled_flag = 41; | 36 uint8_t category_enabled_flag = 41; |
37 trace_object.Initialize('X', &category_enabled_flag, "Test.Trace", | 37 trace_object.Initialize('X', &category_enabled_flag, "Test.Trace", |
38 "Test.Scope", 42, 123, 0, nullptr, nullptr, nullptr, | 38 "Test.Scope", 42, 123, 0, NULL, NULL, NULL, 0); |
39 nullptr, 0); | |
40 CHECK_EQ('X', trace_object.phase()); | 39 CHECK_EQ('X', trace_object.phase()); |
41 CHECK_EQ(category_enabled_flag, *trace_object.category_enabled_flag()); | 40 CHECK_EQ(category_enabled_flag, *trace_object.category_enabled_flag()); |
42 CHECK_EQ(std::string("Test.Trace"), std::string(trace_object.name())); | 41 CHECK_EQ(std::string("Test.Trace"), std::string(trace_object.name())); |
43 CHECK_EQ(std::string("Test.Scope"), std::string(trace_object.scope())); | 42 CHECK_EQ(std::string("Test.Scope"), std::string(trace_object.scope())); |
44 CHECK_EQ(0, trace_object.duration()); | 43 CHECK_EQ(0, trace_object.duration()); |
45 CHECK_EQ(0, trace_object.cpu_duration()); | 44 CHECK_EQ(0, trace_object.cpu_duration()); |
46 } | 45 } |
47 | 46 |
48 class ConvertableToTraceFormatMock : public v8::ConvertableToTraceFormat { | |
49 public: | |
50 explicit ConvertableToTraceFormatMock(int value) : value_(value) {} | |
51 void AppendAsTraceFormat(std::string* out) const override { | |
52 *out += "[" + std::to_string(value_) + "," + std::to_string(value_) + "]"; | |
53 } | |
54 | |
55 private: | |
56 int value_; | |
57 | |
58 DISALLOW_COPY_AND_ASSIGN(ConvertableToTraceFormatMock); | |
59 }; | |
60 | |
61 class MockTraceWriter : public TraceWriter { | 47 class MockTraceWriter : public TraceWriter { |
62 public: | 48 public: |
63 void AppendTraceEvent(TraceObject* trace_event) override { | 49 void AppendTraceEvent(TraceObject* trace_event) override { |
64 events_.push_back(trace_event->name()); | 50 events_.push_back(trace_event->name()); |
65 } | 51 } |
66 | 52 |
67 void Flush() override {} | 53 void Flush() override {} |
68 | 54 |
69 std::vector<std::string> events() { return events_; } | 55 std::vector<std::string> events() { return events_; } |
70 | 56 |
(...skipping 11 matching lines...) Expand all Loading... |
82 for (int i = 0; i < HANDLES_COUNT; ++i) { | 68 for (int i = 0; i < HANDLES_COUNT; ++i) { |
83 names[i] = "Test.EventNo" + std::to_string(i); | 69 names[i] = "Test.EventNo" + std::to_string(i); |
84 } | 70 } |
85 | 71 |
86 std::vector<uint64_t> handles(HANDLES_COUNT); | 72 std::vector<uint64_t> handles(HANDLES_COUNT); |
87 uint8_t category_enabled_flag = 41; | 73 uint8_t category_enabled_flag = 41; |
88 for (size_t i = 0; i < handles.size(); ++i) { | 74 for (size_t i = 0; i < handles.size(); ++i) { |
89 TraceObject* trace_object = ring_buffer->AddTraceEvent(&handles[i]); | 75 TraceObject* trace_object = ring_buffer->AddTraceEvent(&handles[i]); |
90 CHECK_NOT_NULL(trace_object); | 76 CHECK_NOT_NULL(trace_object); |
91 trace_object->Initialize('X', &category_enabled_flag, names[i].c_str(), | 77 trace_object->Initialize('X', &category_enabled_flag, names[i].c_str(), |
92 "Test.Scope", 42, 123, 0, nullptr, nullptr, | 78 "Test.Scope", 42, 123, 0, NULL, NULL, NULL, 0); |
93 nullptr, nullptr, 0); | |
94 trace_object = ring_buffer->GetEventByHandle(handles[i]); | 79 trace_object = ring_buffer->GetEventByHandle(handles[i]); |
95 CHECK_NOT_NULL(trace_object); | 80 CHECK_NOT_NULL(trace_object); |
96 CHECK_EQ('X', trace_object->phase()); | 81 CHECK_EQ('X', trace_object->phase()); |
97 CHECK_EQ(names[i], std::string(trace_object->name())); | 82 CHECK_EQ(names[i], std::string(trace_object->name())); |
98 CHECK_EQ(category_enabled_flag, *trace_object->category_enabled_flag()); | 83 CHECK_EQ(category_enabled_flag, *trace_object->category_enabled_flag()); |
99 } | 84 } |
100 | 85 |
101 // We should only be able to retrieve the last kChunkSize + 1. | 86 // We should only be able to retrieve the last kChunkSize + 1. |
102 for (size_t i = 0; i < TraceBufferChunk::kChunkSize; ++i) { | 87 for (size_t i = 0; i < TraceBufferChunk::kChunkSize; ++i) { |
103 CHECK_NULL(ring_buffer->GetEventByHandle(handles[i])); | 88 CHECK_NULL(ring_buffer->GetEventByHandle(handles[i])); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 TraceBuffer* ring_buffer = | 121 TraceBuffer* ring_buffer = |
137 TraceBuffer::CreateTraceBufferRingBuffer(1, writer); | 122 TraceBuffer::CreateTraceBufferRingBuffer(1, writer); |
138 tracing_controller.Initialize(ring_buffer); | 123 tracing_controller.Initialize(ring_buffer); |
139 TraceConfig* trace_config = new TraceConfig(); | 124 TraceConfig* trace_config = new TraceConfig(); |
140 trace_config->AddIncludedCategory("v8-cat"); | 125 trace_config->AddIncludedCategory("v8-cat"); |
141 tracing_controller.StartTracing(trace_config); | 126 tracing_controller.StartTracing(trace_config); |
142 | 127 |
143 TraceObject trace_object; | 128 TraceObject trace_object; |
144 trace_object.InitializeForTesting( | 129 trace_object.InitializeForTesting( |
145 'X', tracing_controller.GetCategoryGroupEnabled("v8-cat"), "Test0", | 130 'X', tracing_controller.GetCategoryGroupEnabled("v8-cat"), "Test0", |
146 v8::internal::tracing::kGlobalScope, 42, 123, 0, nullptr, nullptr, | 131 v8::internal::tracing::kGlobalScope, 42, 123, 0, NULL, NULL, NULL, |
147 nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID, 11, 22, 100, 50, 33, 44); | 132 TRACE_EVENT_FLAG_HAS_ID, 11, 22, 100, 50, 33, 44); |
148 writer->AppendTraceEvent(&trace_object); | 133 writer->AppendTraceEvent(&trace_object); |
149 trace_object.InitializeForTesting( | 134 trace_object.InitializeForTesting( |
150 'Y', tracing_controller.GetCategoryGroupEnabled("v8-cat"), "Test1", | 135 'Y', tracing_controller.GetCategoryGroupEnabled("v8-cat"), "Test1", |
151 v8::internal::tracing::kGlobalScope, 43, 456, 0, nullptr, nullptr, | 136 v8::internal::tracing::kGlobalScope, 43, 456, 0, NULL, NULL, NULL, 0, |
152 nullptr, nullptr, 0, 55, 66, 110, 55, 77, 88); | 137 55, 66, 110, 55, 77, 88); |
153 writer->AppendTraceEvent(&trace_object); | 138 writer->AppendTraceEvent(&trace_object); |
154 tracing_controller.StopTracing(); | 139 tracing_controller.StopTracing(); |
155 } | 140 } |
156 | 141 |
157 std::string trace_str = stream.str(); | 142 std::string trace_str = stream.str(); |
158 std::string expected_trace_str = | 143 std::string expected_trace_str = |
159 "{\"traceEvents\":[{\"pid\":11,\"tid\":22,\"ts\":100,\"tts\":50," | 144 "{\"traceEvents\":[{\"pid\":11,\"tid\":22,\"ts\":100,\"tts\":50," |
160 "\"ph\":\"X\",\"cat\":\"v8-cat\",\"name\":\"Test0\",\"dur\":33," | 145 "\"ph\":\"X\",\"cat\":\"v8-cat\",\"name\":\"Test0\",\"dur\":33," |
161 "\"tdur\":44,\"id\":\"0x2a\",\"args\":{}},{\"pid\":55,\"tid\":66," | 146 "\"tdur\":44,\"id\":\"0x2a\",\"args\":{}},{\"pid\":55,\"tid\":66," |
162 "\"ts\":110,\"tts\":55,\"ph\":\"Y\",\"cat\":\"v8-cat\",\"name\":" | 147 "\"ts\":110,\"tts\":55,\"ph\":\"Y\",\"cat\":\"v8-cat\",\"name\":" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 TRACE_EVENT2("v8", "v8.Test2.2", "mm1", TRACE_STR_COPY(mm.c_str()), "mm2", | 257 TRACE_EVENT2("v8", "v8.Test2.2", "mm1", TRACE_STR_COPY(mm.c_str()), "mm2", |
273 TRACE_STR_COPY(mmm.c_str())); | 258 TRACE_STR_COPY(mmm.c_str())); |
274 | 259 |
275 // Check copies are correct. | 260 // Check copies are correct. |
276 TRACE_EVENT_COPY_INSTANT0("v8", mm.c_str(), TRACE_EVENT_SCOPE_THREAD); | 261 TRACE_EVENT_COPY_INSTANT0("v8", mm.c_str(), TRACE_EVENT_SCOPE_THREAD); |
277 TRACE_EVENT_COPY_INSTANT2("v8", mm.c_str(), TRACE_EVENT_SCOPE_THREAD, "mm1", | 262 TRACE_EVENT_COPY_INSTANT2("v8", mm.c_str(), TRACE_EVENT_SCOPE_THREAD, "mm1", |
278 mm.c_str(), "mm2", mmm.c_str()); | 263 mm.c_str(), "mm2", mmm.c_str()); |
279 mm = "CHANGED"; | 264 mm = "CHANGED"; |
280 mmm = "CHANGED"; | 265 mmm = "CHANGED"; |
281 | 266 |
282 TRACE_EVENT_INSTANT1("v8", "v8.Test", TRACE_EVENT_SCOPE_THREAD, "a1", | |
283 new ConvertableToTraceFormatMock(42)); | |
284 std::unique_ptr<ConvertableToTraceFormatMock> trace_event_arg( | |
285 new ConvertableToTraceFormatMock(42)); | |
286 TRACE_EVENT_INSTANT2("v8", "v8.Test", TRACE_EVENT_SCOPE_THREAD, "a1", | |
287 std::move(trace_event_arg), "a2", | |
288 new ConvertableToTraceFormatMock(123)); | |
289 | |
290 tracing_controller.StopTracing(); | 267 tracing_controller.StopTracing(); |
291 } | 268 } |
292 | 269 |
293 std::string trace_str = stream.str(); | 270 std::string trace_str = stream.str(); |
294 | 271 |
295 std::vector<std::string> all_args, all_names, all_cats; | 272 std::vector<std::string> all_args, all_names, all_cats; |
296 GetJSONStrings(all_args, trace_str, "\"args\"", "{", "}"); | 273 GetJSONStrings(all_args, trace_str, "\"args\"", "{", "}"); |
297 GetJSONStrings(all_names, trace_str, "\"name\"", "\"", "\""); | 274 GetJSONStrings(all_names, trace_str, "\"name\"", "\"", "\""); |
298 GetJSONStrings(all_cats, trace_str, "\"cat\"", "\"", "\""); | 275 GetJSONStrings(all_cats, trace_str, "\"cat\"", "\"", "\""); |
299 | 276 |
300 CHECK_EQ(all_args.size(), 24); | 277 CHECK_EQ(all_args.size(), 22); |
301 CHECK_EQ(all_args[0], "\"aa\":11"); | 278 CHECK_EQ(all_args[0], "\"aa\":11"); |
302 CHECK_EQ(all_args[1], "\"bb\":22"); | 279 CHECK_EQ(all_args[1], "\"bb\":22"); |
303 CHECK_EQ(all_args[2], "\"cc\":33"); | 280 CHECK_EQ(all_args[2], "\"cc\":33"); |
304 CHECK_EQ(all_args[3], "\"dd\":44"); | 281 CHECK_EQ(all_args[3], "\"dd\":44"); |
305 CHECK_EQ(all_args[4], "\"ee\":-55"); | 282 CHECK_EQ(all_args[4], "\"ee\":-55"); |
306 CHECK_EQ(all_args[5], "\"ff\":-66"); | 283 CHECK_EQ(all_args[5], "\"ff\":-66"); |
307 CHECK_EQ(all_args[6], "\"gg\":-77"); | 284 CHECK_EQ(all_args[6], "\"gg\":-77"); |
308 CHECK_EQ(all_args[7], "\"hh\":-88"); | 285 CHECK_EQ(all_args[7], "\"hh\":-88"); |
309 CHECK_EQ(all_args[8], "\"ii1\":true"); | 286 CHECK_EQ(all_args[8], "\"ii1\":true"); |
310 CHECK_EQ(all_args[9], "\"ii2\":false"); | 287 CHECK_EQ(all_args[9], "\"ii2\":false"); |
311 CHECK_EQ(all_args[10], "\"jj1\":99.0"); | 288 CHECK_EQ(all_args[10], "\"jj1\":99.0"); |
312 CHECK_EQ(all_args[11], "\"jj2\":1e+100"); | 289 CHECK_EQ(all_args[11], "\"jj2\":1e+100"); |
313 CHECK_EQ(all_args[12], "\"jj3\":\"NaN\""); | 290 CHECK_EQ(all_args[12], "\"jj3\":\"NaN\""); |
314 CHECK_EQ(all_args[13], "\"jj4\":\"Infinity\""); | 291 CHECK_EQ(all_args[13], "\"jj4\":\"Infinity\""); |
315 CHECK_EQ(all_args[14], "\"jj5\":\"-Infinity\""); | 292 CHECK_EQ(all_args[14], "\"jj5\":\"-Infinity\""); |
316 std::ostringstream pointer_stream; | 293 std::ostringstream pointer_stream; |
317 pointer_stream << "\"kk\":\"" << &aa << "\""; | 294 pointer_stream << "\"kk\":\"" << &aa << "\""; |
318 CHECK_EQ(all_args[15], pointer_stream.str()); | 295 CHECK_EQ(all_args[15], pointer_stream.str()); |
319 CHECK_EQ(all_args[16], "\"ll\":\"100\""); | 296 CHECK_EQ(all_args[16], "\"ll\":\"100\""); |
320 CHECK_EQ(all_args[17], "\"mm\":\"\\\"INIT\\\"\""); | 297 CHECK_EQ(all_args[17], "\"mm\":\"\\\"INIT\\\"\""); |
321 | 298 |
322 CHECK_EQ(all_names[18], "v8.Test2.1"); | 299 CHECK_EQ(all_names[18], "v8.Test2.1"); |
323 CHECK_EQ(all_args[18], "\"aa\":11,\"ll\":\"100\""); | 300 CHECK_EQ(all_args[18], "\"aa\":11,\"ll\":\"100\""); |
324 CHECK_EQ(all_args[19], "\"mm1\":\"INIT\",\"mm2\":\"\\\"INIT\\\"\""); | 301 CHECK_EQ(all_args[19], "\"mm1\":\"INIT\",\"mm2\":\"\\\"INIT\\\"\""); |
325 | 302 |
326 CHECK_EQ(all_names[20], "INIT"); | 303 CHECK_EQ(all_names[20], "INIT"); |
327 CHECK_EQ(all_names[21], "INIT"); | 304 CHECK_EQ(all_names[21], "INIT"); |
328 CHECK_EQ(all_args[21], "\"mm1\":\"INIT\",\"mm2\":\"\\\"INIT\\\"\""); | 305 CHECK_EQ(all_args[21], "\"mm1\":\"INIT\",\"mm2\":\"\\\"INIT\\\"\""); |
329 CHECK_EQ(all_args[22], "\"a1\":[42,42]"); | |
330 CHECK_EQ(all_args[23], "\"a1\":[42,42],\"a2\":[123,123]"); | |
331 | 306 |
332 i::V8::SetPlatformForTesting(old_platform); | 307 i::V8::SetPlatformForTesting(old_platform); |
333 } | 308 } |
334 | 309 |
335 } // namespace tracing | 310 } // namespace tracing |
336 } // namespace platform | 311 } // namespace platform |
337 } // namespace v8 | 312 } // namespace v8 |
OLD | NEW |