1 /* 2 * Copyright (C) 2020 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 "src/trace_processor/tp_metatrace.h" 18 19 namespace perfetto { 20 namespace trace_processor { 21 namespace metatrace { 22 23 namespace { 24 25 using ProtoEnum = protos::pbzero::MetatraceCategories; MetatraceCategoriesToProtoEnum(MetatraceCategories categories)26ProtoEnum MetatraceCategoriesToProtoEnum(MetatraceCategories categories) { 27 // Note: these are intentionally chained ifs and not else-ifs as it's possible 28 // for multiple of these if statements to be true. 29 ProtoEnum result = ProtoEnum::NONE; 30 if (categories & MetatraceCategories::QUERY_TIMELINE) 31 result = static_cast<ProtoEnum>(result | ProtoEnum::QUERY_TIMELINE); 32 if (categories & MetatraceCategories::FUNCTION_CALL) 33 result = static_cast<ProtoEnum>(result | ProtoEnum::FUNCTION_CALL); 34 if (categories & MetatraceCategories::QUERY_DETAILED) 35 result = static_cast<ProtoEnum>(result | ProtoEnum::QUERY_DETAILED); 36 if (categories & MetatraceCategories::DB) 37 result = static_cast<ProtoEnum>(result | ProtoEnum::DB); 38 if (categories & MetatraceCategories::API_TIMELINE) 39 result = static_cast<ProtoEnum>(result | ProtoEnum::API_TIMELINE); 40 return result; 41 } 42 43 } // namespace 44 45 Category g_enabled_categories = Category::NONE; 46 Enable(MetatraceConfig config)47void Enable(MetatraceConfig config) { 48 g_enabled_categories = MetatraceCategoriesToProtoEnum(config.categories); 49 if (config.override_buffer_size) { 50 RingBuffer::GetInstance()->Resize(config.override_buffer_size); 51 } 52 } 53 DisableAndReadBuffer(std::function<void (Record *)> fn)54void DisableAndReadBuffer(std::function<void(Record*)> fn) { 55 g_enabled_categories = Category::NONE; 56 if (!fn) 57 return; 58 RingBuffer::GetInstance()->ReadAll(fn); 59 } 60 RingBuffer()61RingBuffer::RingBuffer() : data_(kDefaultCapacity) { 62 static_assert((kDefaultCapacity & (kDefaultCapacity - 1)) == 0, 63 "Capacity should be a power of 2"); 64 } 65 Resize(size_t requested_capacity)66void RingBuffer::Resize(size_t requested_capacity) { 67 size_t actual_capacity = 1; 68 while (actual_capacity < requested_capacity) 69 actual_capacity <<= 1; 70 data_.resize(actual_capacity); 71 start_idx_ = 0; 72 write_idx_ = 0; 73 } 74 ReadAll(std::function<void (Record *)> fn)75void RingBuffer::ReadAll(std::function<void(Record*)> fn) { 76 // Mark as reading so we don't get reentrancy in obtaining new 77 // trace events. 78 is_reading_ = true; 79 80 uint64_t start = (write_idx_ - start_idx_) < data_.size() 81 ? start_idx_ 82 : write_idx_ - data_.size(); 83 uint64_t end = write_idx_; 84 85 // Increment the write index by kCapacity + 1. This ensures that if 86 // ScopedEntry is destoryed in |fn| below, we won't get overwrites 87 // while reading the buffer. 88 // This works because of the logic in ~ScopedEntry and 89 // RingBuffer::HasOverwritten which ensures that we don't overwrite entries 90 // more than kCapcity elements in the past. 91 write_idx_ += data_.size() + 1; 92 93 for (uint64_t i = start; i < end; ++i) { 94 Record* record = At(i); 95 96 // If the slice was unfinished for some reason, don't emit it. 97 if (record->duration_ns != 0) { 98 fn(record); 99 } 100 } 101 102 // Ensure that the start pointer is updated to the write pointer. 103 start_idx_ = write_idx_; 104 105 // Remove the reading marker. 106 is_reading_ = false; 107 } 108 109 } // namespace metatrace 110 } // namespace trace_processor 111 } // namespace perfetto 112