1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string.h>
#include <memory>
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "base/trace_event/category_registry.h"
#include "base/trace_event/trace_category.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace trace_event {
// Static initializers are generally forbidden. However, in the past we ran in
// the case of some test using tracing in a static initializer. This test checks
// That the category registry doesn't rely on static initializers itself and is
// functional even if called from another static initializer.
bool Initializer() {
return CategoryRegistry::kCategoryMetadata &&
CategoryRegistry::kCategoryMetadata->is_valid();
}
bool g_initializer_check = Initializer();
class TraceCategoryTest : public testing::Test {
public:
void SetUp() override { CategoryRegistry::Initialize(); }
void TearDown() override { CategoryRegistry::ResetForTesting(); }
static bool GetOrCreateCategoryByName(const char* name, TraceCategory** cat) {
static LazyInstance<Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER;
bool is_new_cat = false;
*cat = CategoryRegistry::GetCategoryByName(name);
if (!*cat) {
AutoLock lock(g_lock.Get());
is_new_cat = CategoryRegistry::GetOrCreateCategoryLocked(
name, [](TraceCategory*) {}, cat);
}
return is_new_cat;
};
static CategoryRegistry::Range GetAllCategories() {
return CategoryRegistry::GetAllCategories();
}
static void TestRaceThreadMain(WaitableEvent* event) {
TraceCategory* cat = nullptr;
event->Wait();
GetOrCreateCategoryByName("__test_race", &cat);
EXPECT_NE(nullptr, cat);
}
};
TEST_F(TraceCategoryTest, Basic) {
ASSERT_NE(nullptr, CategoryRegistry::kCategoryMetadata);
ASSERT_TRUE(CategoryRegistry::kCategoryMetadata->is_valid());
ASSERT_FALSE(CategoryRegistry::kCategoryMetadata->is_enabled());
// Metadata category is built-in and should create a new category.
TraceCategory* cat_meta = nullptr;
const char* kMetadataName = CategoryRegistry::kCategoryMetadata->name();
ASSERT_FALSE(GetOrCreateCategoryByName(kMetadataName, &cat_meta));
ASSERT_EQ(CategoryRegistry::kCategoryMetadata, cat_meta);
TraceCategory* cat_1 = nullptr;
ASSERT_TRUE(GetOrCreateCategoryByName("__test_basic_ab", &cat_1));
ASSERT_FALSE(cat_1->is_enabled());
ASSERT_EQ(0u, cat_1->enabled_filters());
cat_1->set_state_flag(TraceCategory::ENABLED_FOR_RECORDING);
cat_1->set_state_flag(TraceCategory::ENABLED_FOR_FILTERING);
ASSERT_EQ(TraceCategory::ENABLED_FOR_RECORDING |
TraceCategory::ENABLED_FOR_FILTERING,
cat_1->state());
cat_1->set_enabled_filters(129);
ASSERT_EQ(129u, cat_1->enabled_filters());
ASSERT_EQ(cat_1, CategoryRegistry::GetCategoryByStatePtr(cat_1->state_ptr()));
cat_1->clear_state_flag(TraceCategory::ENABLED_FOR_FILTERING);
ASSERT_EQ(TraceCategory::ENABLED_FOR_RECORDING, cat_1->state());
ASSERT_EQ(TraceCategory::ENABLED_FOR_RECORDING, *cat_1->state_ptr());
ASSERT_TRUE(cat_1->is_enabled());
TraceCategory* cat_2 = nullptr;
ASSERT_TRUE(GetOrCreateCategoryByName("__test_basic_a", &cat_2));
ASSERT_FALSE(cat_2->is_enabled());
cat_2->set_state_flag(TraceCategory::ENABLED_FOR_RECORDING);
TraceCategory* cat_2_copy = nullptr;
ASSERT_FALSE(GetOrCreateCategoryByName("__test_basic_a", &cat_2_copy));
ASSERT_EQ(cat_2, cat_2_copy);
TraceCategory* cat_3 = nullptr;
ASSERT_TRUE(
GetOrCreateCategoryByName("__test_basic_ab,__test_basic_a", &cat_3));
ASSERT_FALSE(cat_3->is_enabled());
ASSERT_EQ(0u, cat_3->enabled_filters());
int num_test_categories_seen = 0;
for (const TraceCategory& cat : GetAllCategories()) {
if (strcmp(cat.name(), kMetadataName) == 0)
ASSERT_TRUE(CategoryRegistry::IsBuiltinCategory(&cat));
if (strncmp(cat.name(), "__test_basic_", 13) == 0) {
ASSERT_FALSE(CategoryRegistry::IsBuiltinCategory(&cat));
num_test_categories_seen++;
}
}
ASSERT_EQ(3, num_test_categories_seen);
ASSERT_TRUE(g_initializer_check);
}
// Tries to cover the case of multiple threads creating the same category
// simultaeously. Should never end up with distinct entries with the same name.
TEST_F(TraceCategoryTest, ThreadRaces) {
const int kNumThreads = 32;
std::unique_ptr<Thread> threads[kNumThreads];
for (int i = 0; i < kNumThreads; i++) {
threads[i].reset(new Thread("test thread"));
threads[i]->Start();
}
WaitableEvent sync_event(WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
for (int i = 0; i < kNumThreads; i++) {
threads[i]->task_runner()->PostTask(
FROM_HERE, Bind(&TestRaceThreadMain, Unretained(&sync_event)));
}
sync_event.Signal();
for (int i = 0; i < kNumThreads; i++)
threads[i]->Stop();
int num_times_seen = 0;
for (const TraceCategory& cat : GetAllCategories()) {
if (strcmp(cat.name(), "__test_race") == 0)
num_times_seen++;
}
ASSERT_EQ(1, num_times_seen);
}
} // namespace trace_event
} // namespace base
|