1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/tracing/track_event_category_registry.h"
18
19 namespace perfetto {
20
21 // static
FromDynamicCategory(const char * name)22 Category Category::FromDynamicCategory(const char* name) {
23 if (GetNthNameSize(1, name, name)) {
24 Category group(Group(name));
25 PERFETTO_DCHECK(group.name);
26 return group;
27 }
28 Category category(name);
29 PERFETTO_DCHECK(category.name);
30 return category;
31 }
32
FromDynamicCategory(const DynamicCategory & dynamic_category)33 Category Category::FromDynamicCategory(
34 const DynamicCategory& dynamic_category) {
35 return FromDynamicCategory(dynamic_category.name.c_str());
36 }
37
38 namespace internal {
39
NullCategory(const perfetto::DynamicCategory &)40 perfetto::DynamicCategory NullCategory(const perfetto::DynamicCategory&) {
41 return perfetto::DynamicCategory{};
42 }
43
GetCategory(size_t index) const44 const Category* TrackEventCategoryRegistry::GetCategory(size_t index) const {
45 PERFETTO_DCHECK(index < category_count_);
46 return &categories_[index];
47 }
48
EnableCategoryForInstance(size_t category_index,uint32_t instance_index) const49 void TrackEventCategoryRegistry::EnableCategoryForInstance(
50 size_t category_index,
51 uint32_t instance_index) const {
52 PERFETTO_DCHECK(instance_index < kMaxDataSourceInstances);
53 PERFETTO_DCHECK(category_index < category_count_);
54 // Matches the acquire_load in DataSource::Trace().
55 state_storage_[category_index].fetch_or(
56 static_cast<uint8_t>(1u << instance_index), std::memory_order_release);
57 }
58
DisableCategoryForInstance(size_t category_index,uint32_t instance_index) const59 void TrackEventCategoryRegistry::DisableCategoryForInstance(
60 size_t category_index,
61 uint32_t instance_index) const {
62 PERFETTO_DCHECK(instance_index < kMaxDataSourceInstances);
63 PERFETTO_DCHECK(category_index < category_count_);
64 // Matches the acquire_load in DataSource::Trace().
65 state_storage_[category_index].fetch_and(
66 static_cast<uint8_t>(~(1u << instance_index)), std::memory_order_release);
67 }
68
69 } // namespace internal
70 } // namespace perfetto
71