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