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