blob: 38661a42ac38c76bd64fe29f7f03524c58066dc8 [file] [log] [blame]
Lalit Maganticaed37e2018-06-01 03:03:08 +01001/*
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/trace_storage.h"
18
19#include <string.h>
Ioannis Ilkosb8b11102019-01-29 17:56:55 +000020#include <algorithm>
21#include <limits>
22
Eric Seckler972225e2019-04-18 11:07:12 +010023#include "perfetto/base/no_destructor.h"
24
25namespace perfetto {
26namespace trace_processor {
27
Ioannis Ilkosb8b11102019-01-29 17:56:55 +000028namespace {
Eric Seckler972225e2019-04-18 11:07:12 +010029
Ioannis Ilkosb8b11102019-01-29 17:56:55 +000030template <typename T>
31void MaybeUpdateMinMax(T begin_it,
32 T end_it,
33 int64_t* min_value,
34 int64_t* max_value) {
35 if (begin_it == end_it) {
36 return;
37 }
38 std::pair<T, T> minmax = std::minmax_element(begin_it, end_it);
39 *min_value = std::min(*min_value, *minmax.first);
40 *max_value = std::max(*max_value, *minmax.second);
41}
Eric Seckler972225e2019-04-18 11:07:12 +010042
43std::vector<const char*> CreateRefTypeStringMap() {
44 std::vector<const char*> map(RefType::kRefMax);
45 map[RefType::kRefNoRef] = nullptr;
46 map[RefType::kRefUtid] = "utid";
47 map[RefType::kRefCpuId] = "cpu";
48 map[RefType::kRefIrq] = "irq";
49 map[RefType::kRefSoftIrq] = "softirq";
50 map[RefType::kRefUpid] = "upid";
51 map[RefType::kRefUtidLookupUpid] = "upid";
52 return map;
53}
54
Ioannis Ilkosb8b11102019-01-29 17:56:55 +000055} // namespace
Lalit Maganticaed37e2018-06-01 03:03:08 +010056
Eric Seckler972225e2019-04-18 11:07:12 +010057const std::vector<const char*>& GetRefTypeStringMap() {
58 static const base::NoDestructor<std::vector<const char*>> map(
59 CreateRefTypeStringMap());
60 return map.ref();
61}
Lalit Maganticaed37e2018-06-01 03:03:08 +010062
Isabelle Taylor47328cf2018-06-12 14:33:59 +010063TraceStorage::TraceStorage() {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +010064 // Upid/utid 0 is reserved for idle processes/threads.
Primiano Tuccib75dcee2018-08-08 12:21:36 +010065 unique_processes_.emplace_back(0);
66 unique_threads_.emplace_back(0);
Isabelle Taylor47328cf2018-06-12 14:33:59 +010067}
68
Lalit Maganti35622b72018-06-06 12:03:11 +010069TraceStorage::~TraceStorage() {}
Lalit Maganticaed37e2018-06-01 03:03:08 +010070
Isabelle Taylora0a22972018-08-03 12:06:12 +010071void TraceStorage::ResetStorage() {
72 *this = TraceStorage();
73}
74
Primiano Tucci5cb84f82018-10-31 21:46:36 -070075void TraceStorage::SqlStats::RecordQueryBegin(const std::string& query,
Lalit Maganti85ca4a82018-12-07 17:28:02 +000076 int64_t time_queued,
77 int64_t time_started) {
Primiano Tucci5cb84f82018-10-31 21:46:36 -070078 if (queries_.size() >= kMaxLogEntries) {
79 queries_.pop_front();
80 times_queued_.pop_front();
81 times_started_.pop_front();
82 times_ended_.pop_front();
83 }
84 queries_.push_back(query);
85 times_queued_.push_back(time_queued);
86 times_started_.push_back(time_started);
87 times_ended_.push_back(0);
88}
89
Lalit Maganti85ca4a82018-12-07 17:28:02 +000090void TraceStorage::SqlStats::RecordQueryEnd(int64_t time_ended) {
Primiano Tucci5cb84f82018-10-31 21:46:36 -070091 PERFETTO_DCHECK(!times_ended_.empty());
92 PERFETTO_DCHECK(times_ended_.back() == 0);
93 times_ended_.back() = time_ended;
94}
95
Ioannis Ilkosb8b11102019-01-29 17:56:55 +000096std::pair<int64_t, int64_t> TraceStorage::GetTraceTimestampBoundsNs() const {
97 int64_t start_ns = std::numeric_limits<int64_t>::max();
98 int64_t end_ns = std::numeric_limits<int64_t>::min();
99 MaybeUpdateMinMax(slices_.start_ns().begin(), slices_.start_ns().end(),
100 &start_ns, &end_ns);
Lalit Maganti8320e6d2019-03-14 18:49:33 +0000101 MaybeUpdateMinMax(counter_values_.timestamps().begin(),
102 counter_values_.timestamps().end(), &start_ns, &end_ns);
Ioannis Ilkosb8b11102019-01-29 17:56:55 +0000103 MaybeUpdateMinMax(instants_.timestamps().begin(),
104 instants_.timestamps().end(), &start_ns, &end_ns);
105 MaybeUpdateMinMax(nestable_slices_.start_ns().begin(),
106 nestable_slices_.start_ns().end(), &start_ns, &end_ns);
107 MaybeUpdateMinMax(android_log_.timestamps().begin(),
108 android_log_.timestamps().end(), &start_ns, &end_ns);
109
110 if (start_ns == std::numeric_limits<int64_t>::max()) {
111 return std::make_pair(0, 0);
112 }
113 return std::make_pair(start_ns, end_ns);
114}
115
Lalit Maganticaed37e2018-06-01 03:03:08 +0100116} // namespace trace_processor
117} // namespace perfetto