xref: /aosp_15_r20/external/perfetto/src/trace_processor/storage/trace_storage.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2018 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/storage/trace_storage.h"
18 
19 #include <cstdint>
20 #include <cstring>
21 #include <string>
22 #include <vector>
23 
24 #include "perfetto/base/logging.h"
25 #include "perfetto/ext/base/no_destructor.h"
26 #include "perfetto/trace_processor/basic_types.h"
27 #include "src/trace_processor/containers/null_term_string_view.h"
28 #include "src/trace_processor/types/variadic.h"
29 
30 namespace perfetto::trace_processor {
31 
32 namespace {
33 
CreateRefTypeStringMap()34 std::vector<NullTermStringView> CreateRefTypeStringMap() {
35   std::vector<NullTermStringView> map(static_cast<size_t>(RefType::kRefMax));
36   map[static_cast<size_t>(RefType::kRefNoRef)] = NullTermStringView();
37   map[static_cast<size_t>(RefType::kRefUtid)] = "utid";
38   map[static_cast<size_t>(RefType::kRefCpuId)] = "cpu";
39   map[static_cast<size_t>(RefType::kRefGpuId)] = "gpu";
40   map[static_cast<size_t>(RefType::kRefIrq)] = "irq";
41   map[static_cast<size_t>(RefType::kRefSoftIrq)] = "softirq";
42   map[static_cast<size_t>(RefType::kRefUpid)] = "upid";
43   map[static_cast<size_t>(RefType::kRefTrack)] = "track";
44   return map;
45 }
46 
47 }  // namespace
48 
GetRefTypeStringMap()49 const std::vector<NullTermStringView>& GetRefTypeStringMap() {
50   static const base::NoDestructor<std::vector<NullTermStringView>> map(
51       CreateRefTypeStringMap());
52   return map.ref();
53 }
54 
TraceStorage(const Config &)55 TraceStorage::TraceStorage(const Config&) {
56   for (uint32_t i = 0; i < variadic_type_ids_.size(); ++i) {
57     variadic_type_ids_[i] = InternString(Variadic::kTypeNames[i]);
58   }
59 }
60 
~TraceStorage()61 TraceStorage::~TraceStorage() {}
62 
RecordQueryBegin(const std::string & query,int64_t time_started)63 uint32_t TraceStorage::SqlStats::RecordQueryBegin(const std::string& query,
64                                                   int64_t time_started) {
65   if (queries_.size() >= kMaxLogEntries) {
66     queries_.pop_front();
67     times_started_.pop_front();
68     times_first_next_.pop_front();
69     times_ended_.pop_front();
70     popped_queries_++;
71   }
72   queries_.push_back(query);
73   times_started_.push_back(time_started);
74   times_first_next_.push_back(0);
75   times_ended_.push_back(0);
76   return static_cast<uint32_t>(popped_queries_ + queries_.size() - 1);
77 }
78 
RecordQueryFirstNext(uint32_t row,int64_t time_first_next)79 void TraceStorage::SqlStats::RecordQueryFirstNext(uint32_t row,
80                                                   int64_t time_first_next) {
81   // This means we've popped this query off the queue of queries before it had
82   // a chance to finish. Just silently drop this number.
83   if (popped_queries_ > row)
84     return;
85   uint32_t queue_row = row - popped_queries_;
86   PERFETTO_DCHECK(queue_row < queries_.size());
87   times_first_next_[queue_row] = time_first_next;
88 }
89 
RecordQueryEnd(uint32_t row,int64_t time_ended)90 void TraceStorage::SqlStats::RecordQueryEnd(uint32_t row, int64_t time_ended) {
91   // This means we've popped this query off the queue of queries before it had
92   // a chance to finish. Just silently drop this number.
93   if (popped_queries_ > row)
94     return;
95   uint32_t queue_row = row - popped_queries_;
96   PERFETTO_DCHECK(queue_row < queries_.size());
97   times_ended_[queue_row] = time_ended;
98 }
99 
100 }  // namespace perfetto::trace_processor
101