[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium 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 | |
| 5 | #include "base/debug/trace_event.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/command_line.h" |
| 9 | #include "base/json/json_reader.h" |
| 10 | #include "base/json/json_writer.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 12 | #include "base/process_util.h" |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 13 | #include "base/stringprintf.h" |
| 14 | #include "base/synchronization/waitable_event.h" |
| 15 | #include "base/threading/thread.h" |
| 16 | #include "base/values.h" |
| 17 | #include "testing/gmock/include/gmock/gmock.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | namespace base { |
| 21 | namespace debug { |
| 22 | |
| 23 | namespace { |
| 24 | |
[email protected] | 73e5f815 | 2011-05-13 23:30:35 | [diff] [blame] | 25 | struct JsonKeyValue { |
| 26 | const char* key; |
| 27 | const char* value; |
| 28 | }; |
| 29 | |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 30 | class TraceEventTestFixture : public testing::Test { |
| 31 | public: |
[email protected] | d1f0351 | 2011-07-21 12:28:59 | [diff] [blame] | 32 | // This fixture does not use SetUp() because the fixture must be manually set |
| 33 | // up multiple times when testing AtExit. Use ManualTestSetUp for this. |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 34 | void ManualTestSetUp(); |
[email protected] | 6a341fb | 2011-06-26 16:22:50 | [diff] [blame] | 35 | void OnTraceDataCollected( |
| 36 | scoped_refptr<TraceLog::RefCountedString> json_events_str); |
[email protected] | 73e5f815 | 2011-05-13 23:30:35 | [diff] [blame] | 37 | bool FindMatchingTraceEntry(const JsonKeyValue* key_values); |
| 38 | bool FindNamePhase(const char* name, const char* phase); |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 39 | |
| 40 | std::string trace_string_; |
| 41 | ListValue trace_parsed_; |
| 42 | |
| 43 | private: |
| 44 | // We want our singleton torn down after each test. |
| 45 | ShadowingAtExitManager at_exit_manager_; |
| 46 | }; |
| 47 | |
| 48 | void TraceEventTestFixture::ManualTestSetUp() { |
| 49 | TraceLog::Resurrect(); |
| 50 | TraceLog* tracelog = TraceLog::GetInstance(); |
| 51 | ASSERT_TRUE(tracelog); |
| 52 | ASSERT_FALSE(tracelog->IsEnabled()); |
| 53 | tracelog->SetOutputCallback( |
| 54 | base::Bind(&TraceEventTestFixture::OnTraceDataCollected, |
| 55 | base::Unretained(this))); |
| 56 | } |
| 57 | |
| 58 | void TraceEventTestFixture::OnTraceDataCollected( |
[email protected] | 6a341fb | 2011-06-26 16:22:50 | [diff] [blame] | 59 | scoped_refptr<TraceLog::RefCountedString> json_events_str) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 60 | trace_string_ += json_events_str->data; |
| 61 | |
| 62 | scoped_ptr<Value> root; |
| 63 | root.reset(base::JSONReader::Read(json_events_str->data, false)); |
| 64 | |
| 65 | ListValue* root_list = NULL; |
| 66 | ASSERT_TRUE(root.get()); |
| 67 | ASSERT_TRUE(root->GetAsList(&root_list)); |
| 68 | |
| 69 | // Move items into our aggregate collection |
| 70 | while (root_list->GetSize()) { |
| 71 | Value* item = NULL; |
| 72 | root_list->Remove(0, &item); |
| 73 | trace_parsed_.Append(item); |
| 74 | } |
| 75 | } |
| 76 | |
[email protected] | 73e5f815 | 2011-05-13 23:30:35 | [diff] [blame] | 77 | static bool IsKeyValueInDict(const JsonKeyValue* key_value, |
| 78 | DictionaryValue* dict) { |
| 79 | Value* value = NULL; |
| 80 | std::string value_str; |
| 81 | if (dict->Get(key_value->key, &value) && |
| 82 | value->GetAsString(&value_str) && |
| 83 | value_str == key_value->value) |
| 84 | return true; |
| 85 | |
| 86 | // Recurse to test arguments |
| 87 | DictionaryValue* args_dict = NULL; |
| 88 | dict->GetDictionary("args", &args_dict); |
| 89 | if (args_dict) |
| 90 | return IsKeyValueInDict(key_value, args_dict); |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | static bool IsAllKeyValueInDict(const JsonKeyValue* key_values, |
| 96 | DictionaryValue* dict) { |
| 97 | // Scan all key_values, they must all be present and equal. |
| 98 | while (key_values && key_values->key) { |
| 99 | if (!IsKeyValueInDict(key_values, dict)) |
| 100 | return false; |
| 101 | ++key_values; |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool TraceEventTestFixture::FindMatchingTraceEntry( |
| 107 | const JsonKeyValue* key_values) { |
| 108 | // Scan all items |
| 109 | size_t trace_parsed_count = trace_parsed_.GetSize(); |
| 110 | for (size_t i = 0; i < trace_parsed_count; i++) { |
| 111 | Value* value = NULL; |
| 112 | trace_parsed_.Get(i, &value); |
| 113 | if (!value || value->GetType() != Value::TYPE_DICTIONARY) |
| 114 | continue; |
| 115 | DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
| 116 | |
| 117 | if (IsAllKeyValueInDict(key_values, dict)) |
| 118 | return true; |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | bool TraceEventTestFixture::FindNamePhase(const char* name, const char* phase) { |
| 124 | JsonKeyValue key_values[] = { |
| 125 | {"name", name}, |
| 126 | {"ph", phase}, |
| 127 | {0, 0} |
| 128 | }; |
| 129 | return FindMatchingTraceEntry(key_values); |
| 130 | } |
| 131 | |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 132 | bool IsStringInDict(const char* string_to_match, DictionaryValue* dict) { |
| 133 | for (DictionaryValue::key_iterator ikey = dict->begin_keys(); |
| 134 | ikey != dict->end_keys(); ++ikey) { |
| 135 | Value* child = NULL; |
| 136 | if (!dict->GetWithoutPathExpansion(*ikey, &child)) |
| 137 | continue; |
| 138 | |
| 139 | if ((*ikey).find(string_to_match) != std::string::npos) |
| 140 | return true; |
| 141 | |
| 142 | std::string value_str; |
| 143 | child->GetAsString(&value_str); |
| 144 | if (value_str.find(string_to_match) != std::string::npos) |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | // Recurse to test arguments |
| 149 | DictionaryValue* args_dict = NULL; |
| 150 | dict->GetDictionary("args", &args_dict); |
| 151 | if (args_dict) |
| 152 | return IsStringInDict(string_to_match, args_dict); |
| 153 | |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | DictionaryValue* FindTraceEntry(const ListValue& trace_parsed, |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 158 | const char* string_to_match, |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 159 | DictionaryValue* match_after_this_item = NULL) { |
| 160 | // Scan all items |
| 161 | size_t trace_parsed_count = trace_parsed.GetSize(); |
| 162 | for (size_t i = 0; i < trace_parsed_count; i++) { |
| 163 | Value* value = NULL; |
| 164 | trace_parsed.Get(i, &value); |
| 165 | if (match_after_this_item) { |
| 166 | if (value == match_after_this_item) |
| 167 | match_after_this_item = NULL; |
| 168 | continue; |
| 169 | } |
| 170 | if (!value || value->GetType() != Value::TYPE_DICTIONARY) |
| 171 | continue; |
| 172 | DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
| 173 | |
| 174 | if (IsStringInDict(string_to_match, dict)) |
| 175 | return dict; |
| 176 | } |
| 177 | return NULL; |
| 178 | } |
| 179 | |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 180 | std::vector<DictionaryValue*> FindTraceEntries( |
| 181 | const ListValue& trace_parsed, |
| 182 | const char* string_to_match) { |
| 183 | std::vector<DictionaryValue*> hits; |
| 184 | size_t trace_parsed_count = trace_parsed.GetSize(); |
| 185 | for (size_t i = 0; i < trace_parsed_count; i++) { |
| 186 | Value* value = NULL; |
| 187 | trace_parsed.Get(i, &value); |
| 188 | if (!value || value->GetType() != Value::TYPE_DICTIONARY) |
| 189 | continue; |
| 190 | DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
| 191 | |
| 192 | if (IsStringInDict(string_to_match, dict)) |
| 193 | hits.push_back(dict); |
| 194 | } |
| 195 | return hits; |
| 196 | } |
| 197 | |
| 198 | void TraceWithAllMacroVariants(WaitableEvent* task_complete_event) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 199 | { |
| 200 | TRACE_EVENT_BEGIN_ETW("TRACE_EVENT_BEGIN_ETW call", 1122, "extrastring1"); |
| 201 | TRACE_EVENT_END_ETW("TRACE_EVENT_END_ETW call", 3344, "extrastring2"); |
| 202 | TRACE_EVENT_INSTANT_ETW("TRACE_EVENT_INSTANT_ETW call", |
| 203 | 5566, "extrastring3"); |
| 204 | |
| 205 | TRACE_EVENT0("all", "TRACE_EVENT0 call"); |
| 206 | TRACE_EVENT1("all", "TRACE_EVENT1 call", "name1", "value1"); |
| 207 | TRACE_EVENT2("all", "TRACE_EVENT2 call", |
| 208 | "name1", "value1", |
| 209 | "name2", "value2"); |
| 210 | |
| 211 | TRACE_EVENT_INSTANT0("all", "TRACE_EVENT_INSTANT0 call"); |
| 212 | TRACE_EVENT_INSTANT1("all", "TRACE_EVENT_INSTANT1 call", "name1", "value1"); |
| 213 | TRACE_EVENT_INSTANT2("all", "TRACE_EVENT_INSTANT2 call", |
| 214 | "name1", "value1", |
| 215 | "name2", "value2"); |
| 216 | |
| 217 | TRACE_EVENT_BEGIN0("all", "TRACE_EVENT_BEGIN0 call"); |
| 218 | TRACE_EVENT_BEGIN1("all", "TRACE_EVENT_BEGIN1 call", "name1", "value1"); |
| 219 | TRACE_EVENT_BEGIN2("all", "TRACE_EVENT_BEGIN2 call", |
| 220 | "name1", "value1", |
| 221 | "name2", "value2"); |
| 222 | |
| 223 | TRACE_EVENT_END0("all", "TRACE_EVENT_END0 call"); |
| 224 | TRACE_EVENT_END1("all", "TRACE_EVENT_END1 call", "name1", "value1"); |
| 225 | TRACE_EVENT_END2("all", "TRACE_EVENT_END2 call", |
| 226 | "name1", "value1", |
| 227 | "name2", "value2"); |
| 228 | } // Scope close causes TRACE_EVENT0 etc to send their END events. |
| 229 | |
| 230 | if (task_complete_event) |
| 231 | task_complete_event->Signal(); |
| 232 | } |
| 233 | |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 234 | void ValidateAllTraceMacrosCreatedData(const ListValue& trace_parsed, |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 235 | const std::string& trace_string) { |
| 236 | DictionaryValue* item = NULL; |
| 237 | |
| 238 | #define EXPECT_FIND_(string) \ |
| 239 | EXPECT_TRUE((item = FindTraceEntry(trace_parsed, string))); |
| 240 | #define EXPECT_NOT_FIND_(string) \ |
| 241 | EXPECT_FALSE((item = FindTraceEntry(trace_parsed, string))); |
| 242 | #define EXPECT_SUB_FIND_(string) \ |
| 243 | if (item) EXPECT_TRUE((IsStringInDict(string, item))); |
| 244 | |
| 245 | EXPECT_FIND_("ETW Trace Event"); |
| 246 | EXPECT_FIND_("all"); |
| 247 | EXPECT_FIND_("TRACE_EVENT_BEGIN_ETW call"); |
| 248 | { |
| 249 | int int_val = 0; |
| 250 | EXPECT_TRUE(item && item->GetInteger("args.id", &int_val)); |
| 251 | EXPECT_EQ(1122, int_val); |
| 252 | } |
| 253 | EXPECT_SUB_FIND_("extrastring1"); |
| 254 | EXPECT_FIND_("TRACE_EVENT_END_ETW call"); |
| 255 | EXPECT_FIND_("TRACE_EVENT_INSTANT_ETW call"); |
| 256 | EXPECT_FIND_("TRACE_EVENT0 call"); |
| 257 | { |
| 258 | std::string ph_begin; |
| 259 | std::string ph_end; |
| 260 | EXPECT_TRUE((item = FindTraceEntry(trace_parsed, "TRACE_EVENT0 call"))); |
| 261 | EXPECT_TRUE((item && item->GetString("ph", &ph_begin))); |
| 262 | EXPECT_TRUE((item = FindTraceEntry(trace_parsed, "TRACE_EVENT0 call", |
| 263 | item))); |
| 264 | EXPECT_TRUE((item && item->GetString("ph", &ph_end))); |
| 265 | EXPECT_EQ("B", ph_begin); |
| 266 | EXPECT_EQ("E", ph_end); |
| 267 | } |
| 268 | EXPECT_FIND_("TRACE_EVENT1 call"); |
| 269 | EXPECT_FIND_("TRACE_EVENT2 call"); |
| 270 | EXPECT_SUB_FIND_("name1"); |
| 271 | EXPECT_SUB_FIND_("value1"); |
| 272 | EXPECT_SUB_FIND_("name2"); |
| 273 | EXPECT_SUB_FIND_("value2"); |
| 274 | EXPECT_FIND_("TRACE_EVENT_INSTANT0 call"); |
| 275 | EXPECT_FIND_("TRACE_EVENT_INSTANT1 call"); |
| 276 | EXPECT_FIND_("TRACE_EVENT_INSTANT2 call"); |
| 277 | EXPECT_SUB_FIND_("name1"); |
| 278 | EXPECT_SUB_FIND_("value1"); |
| 279 | EXPECT_SUB_FIND_("name2"); |
| 280 | EXPECT_SUB_FIND_("value2"); |
| 281 | EXPECT_FIND_("TRACE_EVENT_BEGIN0 call"); |
| 282 | EXPECT_FIND_("TRACE_EVENT_BEGIN1 call"); |
| 283 | EXPECT_FIND_("TRACE_EVENT_BEGIN2 call"); |
| 284 | EXPECT_SUB_FIND_("name1"); |
| 285 | EXPECT_SUB_FIND_("value1"); |
| 286 | EXPECT_SUB_FIND_("name2"); |
| 287 | EXPECT_SUB_FIND_("value2"); |
| 288 | EXPECT_FIND_("TRACE_EVENT_END0 call"); |
| 289 | EXPECT_FIND_("TRACE_EVENT_END1 call"); |
| 290 | EXPECT_FIND_("TRACE_EVENT_END2 call"); |
| 291 | EXPECT_SUB_FIND_("name1"); |
| 292 | EXPECT_SUB_FIND_("value1"); |
| 293 | EXPECT_SUB_FIND_("name2"); |
| 294 | EXPECT_SUB_FIND_("value2"); |
| 295 | } |
| 296 | |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 297 | void TraceManyInstantEvents(int thread_id, int num_events, |
| 298 | WaitableEvent* task_complete_event) { |
| 299 | for (int i = 0; i < num_events; i++) { |
| 300 | TRACE_EVENT_INSTANT2("all", "multi thread event", |
| 301 | "thread", thread_id, |
| 302 | "event", i); |
| 303 | } |
| 304 | |
| 305 | if (task_complete_event) |
| 306 | task_complete_event->Signal(); |
| 307 | } |
| 308 | |
| 309 | void ValidateInstantEventPresentOnEveryThread(const ListValue& trace_parsed, |
| 310 | const std::string& trace_string, |
| 311 | int num_threads, int num_events) { |
| 312 | std::map<int, std::map<int, bool> > results; |
| 313 | |
| 314 | size_t trace_parsed_count = trace_parsed.GetSize(); |
| 315 | for (size_t i = 0; i < trace_parsed_count; i++) { |
| 316 | Value* value = NULL; |
| 317 | trace_parsed.Get(i, &value); |
| 318 | if (!value || value->GetType() != Value::TYPE_DICTIONARY) |
| 319 | continue; |
| 320 | DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
| 321 | std::string name; |
| 322 | dict->GetString("name", &name); |
| 323 | if (name != "multi thread event") |
| 324 | continue; |
| 325 | |
| 326 | int thread = 0; |
| 327 | int event = 0; |
| 328 | EXPECT_TRUE(dict->GetInteger("args.thread", &thread)); |
| 329 | EXPECT_TRUE(dict->GetInteger("args.event", &event)); |
| 330 | results[thread][event] = true; |
| 331 | } |
| 332 | |
| 333 | EXPECT_FALSE(results[-1][-1]); |
| 334 | for (int thread = 0; thread < num_threads; thread++) { |
| 335 | for (int event = 0; event < num_events; event++) { |
| 336 | EXPECT_TRUE(results[thread][event]); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | void TraceCallsWithCachedCategoryPointersPointers(const char* name_str) { |
| 342 | TRACE_EVENT0("category name1", name_str); |
| 343 | TRACE_EVENT_INSTANT0("category name2", name_str); |
| 344 | TRACE_EVENT_BEGIN0("category name3", name_str); |
| 345 | TRACE_EVENT_END0("category name4", name_str); |
| 346 | } |
| 347 | |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 348 | } // namespace |
| 349 | |
| 350 | // Simple Test for emitting data and validating it was received. |
| 351 | TEST_F(TraceEventTestFixture, DataCaptured) { |
| 352 | ManualTestSetUp(); |
| 353 | TraceLog::GetInstance()->SetEnabled(true); |
| 354 | |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 355 | TraceWithAllMacroVariants(NULL); |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 356 | |
| 357 | TraceLog::GetInstance()->SetEnabled(false); |
| 358 | |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 359 | ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 360 | } |
| 361 | |
[email protected] | 73e5f815 | 2011-05-13 23:30:35 | [diff] [blame] | 362 | // Simple Test for time threshold events. |
| 363 | TEST_F(TraceEventTestFixture, DataCapturedThreshold) { |
| 364 | ManualTestSetUp(); |
| 365 | TraceLog::GetInstance()->SetEnabled(true); |
| 366 | |
| 367 | // Test that events at the same level are properly filtered by threshold. |
| 368 | { |
| 369 | TRACE_EVENT_IF_LONGER_THAN0(100, "time", "threshold 100"); |
| 370 | TRACE_EVENT_IF_LONGER_THAN0(1000, "time", "threshold 1000"); |
| 371 | TRACE_EVENT_IF_LONGER_THAN0(10000, "time", "threshold 10000"); |
| 372 | // 100+ seconds to avoid flakiness. |
| 373 | TRACE_EVENT_IF_LONGER_THAN0(100000000, "time", "threshold long1"); |
| 374 | TRACE_EVENT_IF_LONGER_THAN0(200000000, "time", "threshold long2"); |
| 375 | base::PlatformThread::Sleep(20); // 20000 us |
| 376 | } |
| 377 | |
| 378 | // Test that a normal nested event remains after it's parent event is dropped. |
| 379 | { |
| 380 | TRACE_EVENT_IF_LONGER_THAN0(1000000, "time", "2threshold10000"); |
| 381 | { |
| 382 | TRACE_EVENT0("time", "nonthreshold1"); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // Test that parent thresholded events are dropped while some nested events |
| 387 | // remain. |
| 388 | { |
| 389 | TRACE_EVENT0("time", "nonthreshold3"); |
| 390 | { |
| 391 | TRACE_EVENT_IF_LONGER_THAN0(200000000, "time", "3thresholdlong2"); |
| 392 | { |
| 393 | TRACE_EVENT_IF_LONGER_THAN0(100000000, "time", "3thresholdlong1"); |
| 394 | { |
| 395 | TRACE_EVENT_IF_LONGER_THAN0(10000, "time", "3threshold10000"); |
| 396 | { |
| 397 | TRACE_EVENT_IF_LONGER_THAN0(1000, "time", "3threshold1000"); |
| 398 | { |
| 399 | TRACE_EVENT_IF_LONGER_THAN0(100, "time", "3threshold100"); |
| 400 | base::PlatformThread::Sleep(20); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | // Test that child thresholded events are dropped while some parent events |
| 409 | // remain. |
| 410 | { |
| 411 | TRACE_EVENT0("time", "nonthreshold4"); |
| 412 | { |
| 413 | TRACE_EVENT_IF_LONGER_THAN0(100, "time", "4threshold100"); |
| 414 | { |
| 415 | TRACE_EVENT_IF_LONGER_THAN0(1000, "time", "4threshold1000"); |
| 416 | { |
| 417 | TRACE_EVENT_IF_LONGER_THAN0(10000, "time", "4threshold10000"); |
| 418 | { |
| 419 | TRACE_EVENT_IF_LONGER_THAN0(100000000, "time", |
| 420 | "4thresholdlong1"); |
| 421 | { |
| 422 | TRACE_EVENT_IF_LONGER_THAN0(200000000, "time", |
| 423 | "4thresholdlong2"); |
| 424 | base::PlatformThread::Sleep(20); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | TraceLog::GetInstance()->SetEnabled(false); |
| 433 | |
| 434 | #define EXPECT_FIND_BE_(str) \ |
| 435 | EXPECT_TRUE(FindNamePhase(str, "B")); \ |
| 436 | EXPECT_TRUE(FindNamePhase(str, "E")) |
| 437 | #define EXPECT_NOT_FIND_BE_(str) \ |
| 438 | EXPECT_FALSE(FindNamePhase(str, "B")); \ |
| 439 | EXPECT_FALSE(FindNamePhase(str, "E")) |
| 440 | |
| 441 | EXPECT_FIND_BE_("threshold 100"); |
| 442 | EXPECT_FIND_BE_("threshold 1000"); |
| 443 | EXPECT_FIND_BE_("threshold 10000"); |
| 444 | EXPECT_NOT_FIND_BE_("threshold long1"); |
| 445 | EXPECT_NOT_FIND_BE_("threshold long2"); |
| 446 | |
| 447 | EXPECT_NOT_FIND_BE_("2threshold10000"); |
| 448 | EXPECT_FIND_BE_("nonthreshold1"); |
| 449 | |
| 450 | EXPECT_FIND_BE_("nonthreshold3"); |
| 451 | EXPECT_FIND_BE_("3threshold100"); |
| 452 | EXPECT_FIND_BE_("3threshold1000"); |
| 453 | EXPECT_FIND_BE_("3threshold10000"); |
| 454 | EXPECT_NOT_FIND_BE_("3thresholdlong1"); |
| 455 | EXPECT_NOT_FIND_BE_("3thresholdlong2"); |
| 456 | |
| 457 | EXPECT_FIND_BE_("nonthreshold4"); |
| 458 | EXPECT_FIND_BE_("4threshold100"); |
| 459 | EXPECT_FIND_BE_("4threshold1000"); |
| 460 | EXPECT_FIND_BE_("4threshold10000"); |
| 461 | EXPECT_NOT_FIND_BE_("4thresholdlong1"); |
| 462 | EXPECT_NOT_FIND_BE_("4thresholdlong2"); |
| 463 | } |
| 464 | |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 465 | // Test that data sent from other threads is gathered |
| 466 | TEST_F(TraceEventTestFixture, DataCapturedOnThread) { |
| 467 | ManualTestSetUp(); |
| 468 | TraceLog::GetInstance()->SetEnabled(true); |
| 469 | |
| 470 | Thread thread("1"); |
| 471 | WaitableEvent task_complete_event(false, false); |
| 472 | thread.Start(); |
| 473 | |
| 474 | thread.message_loop()->PostTask( |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 475 | FROM_HERE, NewRunnableFunction(&TraceWithAllMacroVariants, |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 476 | &task_complete_event)); |
| 477 | task_complete_event.Wait(); |
| 478 | |
| 479 | TraceLog::GetInstance()->SetEnabled(false); |
| 480 | thread.Stop(); |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 481 | ValidateAllTraceMacrosCreatedData(trace_parsed_, trace_string_); |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 482 | } |
| 483 | |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 484 | // Test that data sent from multiple threads is gathered |
| 485 | TEST_F(TraceEventTestFixture, DataCapturedManyThreads) { |
| 486 | ManualTestSetUp(); |
| 487 | TraceLog::GetInstance()->SetEnabled(true); |
| 488 | |
| 489 | const int num_threads = 4; |
| 490 | const int num_events = 4000; |
| 491 | Thread* threads[num_threads]; |
| 492 | WaitableEvent* task_complete_events[num_threads]; |
| 493 | for (int i = 0; i < num_threads; i++) { |
| 494 | threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); |
| 495 | task_complete_events[i] = new WaitableEvent(false, false); |
| 496 | threads[i]->Start(); |
| 497 | threads[i]->message_loop()->PostTask( |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 498 | FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 499 | i, num_events, task_complete_events[i])); |
| 500 | } |
| 501 | |
| 502 | for (int i = 0; i < num_threads; i++) { |
| 503 | task_complete_events[i]->Wait(); |
| 504 | } |
| 505 | |
| 506 | TraceLog::GetInstance()->SetEnabled(false); |
| 507 | |
| 508 | for (int i = 0; i < num_threads; i++) { |
| 509 | threads[i]->Stop(); |
| 510 | delete threads[i]; |
| 511 | delete task_complete_events[i]; |
| 512 | } |
| 513 | |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 514 | ValidateInstantEventPresentOnEveryThread(trace_parsed_, trace_string_, |
| 515 | num_threads, num_events); |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 516 | } |
| 517 | |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 518 | // Test that thread and process names show up in the trace |
| 519 | TEST_F(TraceEventTestFixture, ThreadNames) { |
| 520 | ManualTestSetUp(); |
| 521 | |
| 522 | // Create threads before we enable tracing to make sure |
| 523 | // that tracelog still captures them. |
| 524 | const int num_threads = 4; |
| 525 | const int num_events = 10; |
| 526 | Thread* threads[num_threads]; |
| 527 | PlatformThreadId thread_ids[num_threads]; |
| 528 | for (int i = 0; i < num_threads; i++) |
| 529 | threads[i] = new Thread(StringPrintf("Thread %d", i).c_str()); |
| 530 | |
| 531 | // Enable tracing. |
| 532 | TraceLog::GetInstance()->SetEnabled(true); |
| 533 | |
| 534 | // Now run some trace code on these threads. |
| 535 | WaitableEvent* task_complete_events[num_threads]; |
| 536 | for (int i = 0; i < num_threads; i++) { |
| 537 | task_complete_events[i] = new WaitableEvent(false, false); |
| 538 | threads[i]->Start(); |
| 539 | thread_ids[i] = threads[i]->thread_id(); |
| 540 | threads[i]->message_loop()->PostTask( |
| 541 | FROM_HERE, NewRunnableFunction(&TraceManyInstantEvents, |
| 542 | i, num_events, task_complete_events[i])); |
| 543 | } |
| 544 | for (int i = 0; i < num_threads; i++) { |
| 545 | task_complete_events[i]->Wait(); |
| 546 | } |
| 547 | |
| 548 | // Shut things down. |
| 549 | TraceLog::GetInstance()->SetEnabled(false); |
| 550 | for (int i = 0; i < num_threads; i++) { |
| 551 | threads[i]->Stop(); |
| 552 | delete threads[i]; |
| 553 | delete task_complete_events[i]; |
| 554 | } |
| 555 | |
| 556 | std::string tmp; |
| 557 | int tmp_int; |
| 558 | DictionaryValue* item; |
| 559 | |
| 560 | // Make sure we get thread name metadata. |
| 561 | // Note, the test suite may have created a ton of threads. |
| 562 | // So, we'll have thread names for threads we didn't create. |
| 563 | std::vector<DictionaryValue*> items = |
| 564 | FindTraceEntries(trace_parsed_, "thread_name"); |
| 565 | for (int i = 0; i < static_cast<int>(items.size()); i++) { |
| 566 | item = items[i]; |
| 567 | EXPECT_TRUE(item); |
| 568 | EXPECT_TRUE(item->GetInteger("tid", &tmp_int)); |
| 569 | |
| 570 | // See if this thread name is one of the threads we just created |
| 571 | for (int j = 0; j < num_threads; j++) { |
| 572 | if(static_cast<int>(thread_ids[j]) != tmp_int) |
| 573 | continue; |
| 574 | |
| 575 | std::string expected_name = StringPrintf("Thread %d", j).c_str(); |
| 576 | EXPECT_TRUE(item->GetString("ph", &tmp) && tmp == "M"); |
| 577 | EXPECT_TRUE(item->GetInteger("pid", &tmp_int) && |
| 578 | tmp_int == static_cast<int>(base::GetCurrentProcId())); |
| 579 | EXPECT_TRUE(item->GetString("args.name", &tmp) && |
| 580 | tmp == expected_name); |
| 581 | } |
| 582 | } |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 583 | } |
| 584 | |
[email protected] | 5b4926e | 2011-08-09 15:16:25 | [diff] [blame^] | 585 | |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 586 | // Test trace calls made after tracing singleton shut down. |
| 587 | // |
| 588 | // The singleton is destroyed by our base::AtExitManager, but there can be |
| 589 | // code still executing as the C++ static objects are destroyed. This test |
| 590 | // forces the singleton to destroy early, and intentinally makes trace calls |
| 591 | // afterwards. |
| 592 | TEST_F(TraceEventTestFixture, AtExit) { |
| 593 | // Repeat this test a few times. Besides just showing robustness, it also |
| 594 | // allows us to test that events at shutdown do not appear with valid events |
| 595 | // recorded after the system is started again. |
| 596 | for (int i = 0; i < 4; i++) { |
| 597 | // Scope to contain the then destroy the TraceLog singleton. |
| 598 | { |
| 599 | base::ShadowingAtExitManager exit_manager_will_destroy_singletons; |
| 600 | |
| 601 | // Setup TraceLog singleton inside this test's exit manager scope |
| 602 | // so that it will be destroyed when this scope closes. |
| 603 | ManualTestSetUp(); |
| 604 | |
| 605 | TRACE_EVENT_INSTANT0("all", "not recorded; system not enabled"); |
| 606 | |
| 607 | TraceLog::GetInstance()->SetEnabled(true); |
| 608 | |
| 609 | TRACE_EVENT_INSTANT0("all", "is recorded 1; system has been enabled"); |
| 610 | // Trace calls that will cache pointers to categories; they're valid here |
| 611 | TraceCallsWithCachedCategoryPointersPointers( |
| 612 | "is recorded 2; system has been enabled"); |
| 613 | |
| 614 | TraceLog::GetInstance()->SetEnabled(false); |
| 615 | } // scope to destroy singleton |
| 616 | ASSERT_FALSE(TraceLog::GetInstance()); |
| 617 | |
| 618 | // Now that singleton is destroyed, check what trace events were recorded |
| 619 | DictionaryValue* item = NULL; |
| 620 | ListValue& trace_parsed = trace_parsed_; |
| 621 | EXPECT_FIND_("is recorded 1"); |
| 622 | EXPECT_FIND_("is recorded 2"); |
| 623 | EXPECT_NOT_FIND_("not recorded"); |
| 624 | |
| 625 | // Make additional trace event calls on the shutdown system. They should |
| 626 | // all pass cleanly, but the data not be recorded. We'll verify that next |
| 627 | // time around the loop (the only way to flush the trace buffers). |
| 628 | TRACE_EVENT_BEGIN_ETW("not recorded; system shutdown", 0, NULL); |
| 629 | TRACE_EVENT_END_ETW("not recorded; system shutdown", 0, NULL); |
| 630 | TRACE_EVENT_INSTANT_ETW("not recorded; system shutdown", 0, NULL); |
| 631 | TRACE_EVENT0("all", "not recorded; system shutdown"); |
| 632 | TRACE_EVENT_INSTANT0("all", "not recorded; system shutdown"); |
| 633 | TRACE_EVENT_BEGIN0("all", "not recorded; system shutdown"); |
| 634 | TRACE_EVENT_END0("all", "not recorded; system shutdown"); |
| 635 | |
| 636 | TRACE_EVENT0("new category 0!", "not recorded; system shutdown"); |
| 637 | TRACE_EVENT_INSTANT0("new category 1!", "not recorded; system shutdown"); |
| 638 | TRACE_EVENT_BEGIN0("new category 2!", "not recorded; system shutdown"); |
| 639 | TRACE_EVENT_END0("new category 3!", "not recorded; system shutdown"); |
| 640 | |
| 641 | // Cached categories should be safe to check, and still disable traces |
| 642 | TraceCallsWithCachedCategoryPointersPointers( |
| 643 | "not recorded; system shutdown"); |
| 644 | } |
| 645 | } |
| 646 | |
[email protected] | d1f0351 | 2011-07-21 12:28:59 | [diff] [blame] | 647 | TEST_F(TraceEventTestFixture, NormallyNoDeepCopy) { |
| 648 | // Test that the TRACE_EVENT macros do not deep-copy their string. If they |
| 649 | // do so it may indicate a performance regression, but more-over it would |
| 650 | // make the DEEP_COPY overloads redundant. |
| 651 | ManualTestSetUp(); |
| 652 | |
| 653 | std::string name_string("event name"); |
| 654 | |
| 655 | TraceLog::GetInstance()->SetEnabled(true); |
| 656 | TRACE_EVENT_INSTANT0("category", name_string.c_str()); |
| 657 | |
| 658 | // Modify the string in place (a wholesale reassignment may leave the old |
| 659 | // string intact on the heap). |
| 660 | name_string[0] = '@'; |
| 661 | |
| 662 | TraceLog::GetInstance()->SetEnabled(false); |
| 663 | |
| 664 | EXPECT_FALSE(FindTraceEntry(trace_parsed_, "event name")); |
| 665 | EXPECT_TRUE(FindTraceEntry(trace_parsed_, name_string.c_str())); |
| 666 | } |
| 667 | |
| 668 | TEST_F(TraceEventTestFixture, DeepCopy) { |
| 669 | ManualTestSetUp(); |
| 670 | |
| 671 | static const char kOriginalName1[] = "name1"; |
| 672 | static const char kOriginalName2[] = "name2"; |
| 673 | static const char kOriginalName3[] = "name3"; |
| 674 | std::string name1(kOriginalName1); |
| 675 | std::string name2(kOriginalName2); |
| 676 | std::string name3(kOriginalName3); |
| 677 | std::string arg1("arg1"); |
| 678 | std::string arg2("arg2"); |
| 679 | std::string val1("val1"); |
| 680 | std::string val2("val2"); |
| 681 | |
| 682 | TraceLog::GetInstance()->SetEnabled(true); |
| 683 | TRACE_EVENT_COPY_INSTANT0("category", name1.c_str()); |
| 684 | TRACE_EVENT_COPY_BEGIN1("category", name2.c_str(), |
| 685 | arg1.c_str(), 5); |
| 686 | TRACE_EVENT_COPY_END2("category", name3.c_str(), |
| 687 | arg1.c_str(), val1.c_str(), |
| 688 | arg2.c_str(), val2.c_str()); |
| 689 | |
| 690 | // As per NormallyNoDeepCopy, modify the strings in place. |
| 691 | name1[0] = name2[0] = name3[0] = arg1[0] = arg2[0] = val1[0] = val2[0] = '@'; |
| 692 | |
| 693 | TraceLog::GetInstance()->SetEnabled(false); |
| 694 | |
| 695 | EXPECT_FALSE(FindTraceEntry(trace_parsed_, name1.c_str())); |
| 696 | EXPECT_FALSE(FindTraceEntry(trace_parsed_, name2.c_str())); |
| 697 | EXPECT_FALSE(FindTraceEntry(trace_parsed_, name3.c_str())); |
| 698 | |
| 699 | DictionaryValue* entry1 = FindTraceEntry(trace_parsed_, kOriginalName1); |
| 700 | DictionaryValue* entry2 = FindTraceEntry(trace_parsed_, kOriginalName2); |
| 701 | DictionaryValue* entry3 = FindTraceEntry(trace_parsed_, kOriginalName3); |
| 702 | ASSERT_TRUE(entry1); |
| 703 | ASSERT_TRUE(entry2); |
| 704 | ASSERT_TRUE(entry3); |
| 705 | |
| 706 | int i; |
| 707 | EXPECT_FALSE(entry2->GetInteger("args.@rg1", &i)); |
| 708 | EXPECT_TRUE(entry2->GetInteger("args.arg1", &i)); |
| 709 | EXPECT_EQ(5, i); |
| 710 | |
| 711 | std::string s; |
| 712 | EXPECT_TRUE(entry3->GetString("args.arg1", &s)); |
| 713 | EXPECT_EQ("val1", s); |
| 714 | EXPECT_TRUE(entry3->GetString("args.arg2", &s)); |
| 715 | EXPECT_EQ("val2", s); |
| 716 | } |
| 717 | |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 718 | } // namespace debug |
| 719 | } // namespace base |