Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 #include <stdlib.h> | |
| 5 | |
| 6 #include "src/v8.h" | |
| 7 | |
| 8 #include "src/list.h" | |
| 9 #include "src/list-inl.h" | |
| 10 #include "test/cctest/cctest.h" | |
| 11 | |
| 12 using v8::IdleTask; | |
| 13 using v8::Task; | |
| 14 using v8::Isolate; | |
| 15 | |
| 16 #include "src/tracing/trace-event.h" | |
| 17 | |
| 18 #define GET_TRACE_OBJECTS_LIST platform.GetMockTraceObjects() | |
| 19 | |
| 20 #define GET_TRACE_OBJECT(Index) GET_TRACE_OBJECTS_LIST->at(Index) | |
| 21 | |
| 22 | |
| 23 struct MockTraceObject { | |
| 24 char phase; | |
| 25 const char* name; | |
| 26 uint64_t id; | |
| 27 uint64_t context_id; | |
| 28 uint64_t bind_id; | |
| 29 int numArgs; | |
|
noordhuis
2015/10/07 18:31:54
Style nit: mildly inconsistent mix of camelCase an
fmeawad
2015/11/19 02:00:47
Done.
| |
| 30 unsigned int flags; | |
| 31 MockTraceObject(char phase, const char* name, uint64_t id, | |
| 32 uint64_t context_id, uint64_t bind_id, int numArgs, int flags) | |
| 33 : phase(phase), | |
| 34 name(name), | |
| 35 id(id), | |
| 36 context_id(context_id), | |
| 37 bind_id(bind_id), | |
| 38 numArgs(numArgs), | |
| 39 flags(flags) {} | |
| 40 }; | |
| 41 | |
| 42 typedef v8::internal::List<MockTraceObject*> MockTraceObjectList; | |
| 43 | |
| 44 class MockTracingPlatform : public v8::Platform { | |
| 45 public: | |
| 46 explicit MockTracingPlatform(v8::Platform* platform) {} | |
| 47 virtual ~MockTracingPlatform() { | |
| 48 for (int i = 0; i < trace_object_list_.length(); ++i) { | |
| 49 delete trace_object_list_[i]; | |
| 50 } | |
| 51 trace_object_list_.Clear(); | |
| 52 } | |
| 53 void CallOnBackgroundThread(Task* task, | |
| 54 ExpectedRuntime expected_runtime) override {} | |
| 55 | |
| 56 void CallOnForegroundThread(Isolate* isolate, Task* task) override {} | |
| 57 | |
| 58 void CallDelayedOnForegroundThread(Isolate* isolate, Task* task, | |
| 59 double delay_in_seconds) override {} | |
| 60 | |
| 61 double MonotonicallyIncreasingTime() override { return 0.0; } | |
| 62 | |
| 63 void CallIdleOnForegroundThread(Isolate* isolate, IdleTask* task) override {} | |
| 64 | |
| 65 bool IdleTasksEnabled(Isolate* isolate) override { return false; } | |
| 66 | |
| 67 bool PendingIdleTask() { return false; } | |
| 68 | |
| 69 void PerformIdleTask(double idle_time_in_seconds) {} | |
| 70 | |
| 71 bool PendingDelayedTask() { return false; } | |
| 72 | |
| 73 void PerformDelayedTask() {} | |
| 74 | |
| 75 virtual uint64_t AddTraceEvent(char phase, const uint8_t* categoryEnabledFlag, | |
| 76 const char* name, uint64_t id, | |
| 77 uint64_t context_id, uint64_t bind_id, | |
| 78 int numArgs, const char** argNames, | |
| 79 const uint8_t* argTypes, | |
| 80 const uint64_t* argValues, | |
| 81 unsigned int flags) override { | |
| 82 MockTraceObject* to = new MockTraceObject(phase, name, id, context_id, | |
| 83 bind_id, numArgs, flags); | |
| 84 trace_object_list_.Add(to); | |
| 85 return 0; | |
| 86 } | |
| 87 | |
| 88 virtual void UpdateTraceEventDuration(const uint8_t* categoryEnabledFlag, | |
| 89 const char* name, | |
| 90 uint64_t handle) override {} | |
| 91 | |
| 92 virtual const uint8_t* GetCategoryGroupEnabled(const char* name) override { | |
| 93 if (strcmp(name, "v8-cat")) { | |
| 94 static uint8_t no = 0; | |
| 95 return &no; | |
| 96 } else { | |
| 97 static uint8_t yes = 0x7; | |
| 98 return &yes; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 virtual const char* GetCategoryGroupName( | |
| 103 const uint8_t* categoryEnabledFlag) override { | |
| 104 static const char* dummy = "dummy"; | |
| 105 return dummy; | |
| 106 } | |
| 107 | |
| 108 MockTraceObjectList* GetMockTraceObjects() { return &trace_object_list_; } | |
| 109 | |
| 110 private: | |
| 111 MockTraceObjectList trace_object_list_; | |
| 112 }; | |
| 113 | |
| 114 | |
| 115 TEST(TraceEventDisabledCategory) { | |
| 116 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); | |
| 117 MockTracingPlatform platform(old_platform); | |
| 118 i::V8::SetPlatformForTesting(&platform); | |
| 119 | |
| 120 // Disabled category, will not add events. | |
| 121 TRACE_EVENT_BEGIN0("cat", "e1"); | |
| 122 TRACE_EVENT_END0("cat", "e1"); | |
| 123 CHECK_EQ(0, GET_TRACE_OBJECTS_LIST->length()); | |
| 124 | |
| 125 i::V8::SetPlatformForTesting(old_platform); | |
| 126 } | |
| 127 | |
| 128 | |
| 129 TEST(TraceEventNoArgs) { | |
| 130 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); | |
| 131 MockTracingPlatform platform(old_platform); | |
| 132 i::V8::SetPlatformForTesting(&platform); | |
| 133 | |
| 134 // Enabled category will add 2 events. | |
| 135 TRACE_EVENT_BEGIN0("v8-cat", "e1"); | |
| 136 TRACE_EVENT_END0("v8-cat", "e1"); | |
| 137 | |
| 138 CHECK_EQ(2, GET_TRACE_OBJECTS_LIST->length()); | |
| 139 CHECK_EQ('B', GET_TRACE_OBJECT(0)->phase); | |
| 140 CHECK_EQ("e1", GET_TRACE_OBJECT(0)->name); | |
| 141 CHECK_EQ(0, GET_TRACE_OBJECT(0)->numArgs); | |
| 142 | |
| 143 CHECK_EQ('E', GET_TRACE_OBJECT(1)->phase); | |
| 144 CHECK_EQ("e1", GET_TRACE_OBJECT(1)->name); | |
| 145 CHECK_EQ(0, GET_TRACE_OBJECT(1)->numArgs); | |
| 146 | |
| 147 i::V8::SetPlatformForTesting(old_platform); | |
| 148 } | |
| 149 | |
| 150 | |
| 151 TEST(TraceEventWithOneArg) { | |
| 152 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); | |
| 153 MockTracingPlatform platform(old_platform); | |
| 154 i::V8::SetPlatformForTesting(&platform); | |
| 155 | |
| 156 TRACE_EVENT_BEGIN1("v8-cat", "e1", "arg1", 42); | |
| 157 TRACE_EVENT_END1("v8-cat", "e1", "arg1", 42); | |
| 158 TRACE_EVENT_BEGIN1("v8-cat", "e2", "arg1", "abc"); | |
| 159 TRACE_EVENT_END1("v8-cat", "e2", "arg1", "abc"); | |
| 160 | |
| 161 CHECK_EQ(4, GET_TRACE_OBJECTS_LIST->length()); | |
| 162 | |
| 163 CHECK_EQ(1, GET_TRACE_OBJECT(0)->numArgs); | |
| 164 CHECK_EQ(1, GET_TRACE_OBJECT(1)->numArgs); | |
| 165 CHECK_EQ(1, GET_TRACE_OBJECT(2)->numArgs); | |
| 166 CHECK_EQ(1, GET_TRACE_OBJECT(3)->numArgs); | |
| 167 | |
| 168 i::V8::SetPlatformForTesting(old_platform); | |
| 169 } | |
| 170 | |
| 171 | |
| 172 TEST(TraceEventWithTwoArgs) { | |
| 173 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); | |
| 174 MockTracingPlatform platform(old_platform); | |
| 175 i::V8::SetPlatformForTesting(&platform); | |
| 176 | |
| 177 TRACE_EVENT_BEGIN2("v8-cat", "e1", "arg1", 42, "arg2", "abc"); | |
| 178 TRACE_EVENT_END2("v8-cat", "e1", "arg1", 42, "arg2", "abc"); | |
| 179 TRACE_EVENT_BEGIN2("v8-cat", "e2", "arg1", "abc", "arg2", 43); | |
| 180 TRACE_EVENT_END2("v8-cat", "e2", "arg1", "abc", "arg2", 43); | |
| 181 | |
| 182 CHECK_EQ(4, GET_TRACE_OBJECTS_LIST->length()); | |
| 183 | |
| 184 CHECK_EQ(2, GET_TRACE_OBJECT(0)->numArgs); | |
| 185 CHECK_EQ(2, GET_TRACE_OBJECT(1)->numArgs); | |
| 186 CHECK_EQ(2, GET_TRACE_OBJECT(2)->numArgs); | |
| 187 CHECK_EQ(2, GET_TRACE_OBJECT(3)->numArgs); | |
| 188 | |
| 189 i::V8::SetPlatformForTesting(old_platform); | |
| 190 } | |
| 191 | |
| 192 | |
| 193 TEST(ScopedTraceEvent) { | |
| 194 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); | |
| 195 MockTracingPlatform platform(old_platform); | |
| 196 i::V8::SetPlatformForTesting(&platform); | |
| 197 | |
| 198 { TRACE_EVENT0("v8-cat", "e"); } | |
| 199 | |
| 200 CHECK_EQ(1, GET_TRACE_OBJECTS_LIST->length()); | |
| 201 CHECK_EQ(0, GET_TRACE_OBJECT(0)->numArgs); | |
| 202 | |
| 203 { TRACE_EVENT1("v8-cat", "e1", "arg1", "abc"); } | |
| 204 | |
| 205 CHECK_EQ(2, GET_TRACE_OBJECTS_LIST->length()); | |
| 206 CHECK_EQ(1, GET_TRACE_OBJECT(1)->numArgs); | |
| 207 | |
| 208 { TRACE_EVENT2("v8-cat", "e1", "arg1", "abc", "arg2", 42); } | |
| 209 | |
| 210 CHECK_EQ(3, GET_TRACE_OBJECTS_LIST->length()); | |
| 211 CHECK_EQ(2, GET_TRACE_OBJECT(2)->numArgs); | |
| 212 | |
| 213 i::V8::SetPlatformForTesting(old_platform); | |
| 214 } | |
| 215 | |
| 216 | |
| 217 TEST(TestWithContextId) { | |
| 218 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); | |
| 219 MockTracingPlatform platform(old_platform); | |
| 220 i::V8::SetPlatformForTesting(&platform); | |
| 221 | |
| 222 TRACE_EVENT_BEGIN_WITH_CONTEXT_ID0("v8-cat", "i1", 42); | |
| 223 TRACE_EVENT_BEGIN_WITH_CONTEXT_ID0("v8-cat", "i2", 43); | |
| 224 | |
| 225 CHECK_EQ(2, GET_TRACE_OBJECTS_LIST->length()); | |
| 226 CHECK_EQ(42, GET_TRACE_OBJECT(0)->context_id); | |
| 227 CHECK_EQ(TRACE_EVENT_FLAG_HAS_CONTEXT_ID, GET_TRACE_OBJECT(0)->flags); | |
| 228 CHECK_EQ(43, GET_TRACE_OBJECT(1)->context_id); | |
| 229 CHECK_EQ(TRACE_EVENT_FLAG_HAS_CONTEXT_ID, GET_TRACE_OBJECT(1)->flags); | |
| 230 | |
| 231 i::V8::SetPlatformForTesting(old_platform); | |
| 232 } | |
| 233 | |
| 234 | |
| 235 TEST(TestEventWithFlow) { | |
| 236 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); | |
| 237 MockTracingPlatform platform(old_platform); | |
| 238 i::V8::SetPlatformForTesting(&platform); | |
| 239 | |
| 240 static uint64_t bind_id = 21; | |
| 241 { | |
| 242 TRACE_EVENT_WITH_FLOW0("v8-cat", "f1", bind_id, TRACE_EVENT_FLAG_FLOW_OUT); | |
| 243 } | |
| 244 { | |
| 245 TRACE_EVENT_WITH_FLOW0( | |
| 246 "v8-cat", "f2", bind_id, | |
| 247 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT); | |
| 248 } | |
| 249 { TRACE_EVENT_WITH_FLOW0("v8-cat", "f3", bind_id, TRACE_EVENT_FLAG_FLOW_IN); } | |
| 250 | |
| 251 CHECK_EQ(3, GET_TRACE_OBJECTS_LIST->length()); | |
| 252 CHECK_EQ(bind_id, GET_TRACE_OBJECT(0)->bind_id); | |
| 253 CHECK_EQ(TRACE_EVENT_FLAG_FLOW_OUT, GET_TRACE_OBJECT(0)->flags); | |
| 254 CHECK_EQ(bind_id, GET_TRACE_OBJECT(1)->bind_id); | |
| 255 CHECK_EQ(TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, | |
| 256 GET_TRACE_OBJECT(1)->flags); | |
| 257 CHECK_EQ(bind_id, GET_TRACE_OBJECT(2)->bind_id); | |
| 258 CHECK_EQ(TRACE_EVENT_FLAG_FLOW_IN, GET_TRACE_OBJECT(2)->flags); | |
| 259 | |
| 260 i::V8::SetPlatformForTesting(old_platform); | |
| 261 } | |
| 262 | |
| 263 | |
| 264 TEST(TestEventWithId) { | |
| 265 v8::Platform* old_platform = i::V8::GetCurrentPlatform(); | |
| 266 MockTracingPlatform platform(old_platform); | |
| 267 i::V8::SetPlatformForTesting(&platform); | |
| 268 | |
| 269 static uint64_t event_id = 21; | |
| 270 TRACE_EVENT_ASYNC_BEGIN0("v8-cat", "a1", event_id); | |
| 271 TRACE_EVENT_ASYNC_END0("v8-cat", "a1", event_id); | |
| 272 | |
| 273 CHECK_EQ(2, GET_TRACE_OBJECTS_LIST->length()); | |
| 274 CHECK_EQ(TRACE_EVENT_PHASE_ASYNC_BEGIN, GET_TRACE_OBJECT(0)->phase); | |
| 275 CHECK_EQ(event_id, GET_TRACE_OBJECT(0)->id); | |
| 276 CHECK_EQ(TRACE_EVENT_PHASE_ASYNC_END, GET_TRACE_OBJECT(1)->phase); | |
| 277 CHECK_EQ(event_id, GET_TRACE_OBJECT(1)->id); | |
| 278 | |
| 279 i::V8::SetPlatformForTesting(old_platform); | |
| 280 } | |
| 281 #undef INTERNAL_GET_TRACE_CONTEXT_ID | |
| OLD | NEW |