blob: 3259cb0cef861e56291f4832123b1ff539a60a4a [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
23namespace {
24template <typename T>
25void MaybeUpdateMinMax(T begin_it,
26 T end_it,
27 int64_t* min_value,
28 int64_t* max_value) {
29 if (begin_it == end_it) {
30 return;
31 }
32 std::pair<T, T> minmax = std::minmax_element(begin_it, end_it);
33 *min_value = std::min(*min_value, *minmax.first);
34 *max_value = std::max(*max_value, *minmax.second);
35}
36} // namespace
Lalit Maganticaed37e2018-06-01 03:03:08 +010037
38namespace perfetto {
39namespace trace_processor {
40
Isabelle Taylor47328cf2018-06-12 14:33:59 +010041TraceStorage::TraceStorage() {
Lalit Maganti1f64cfa2018-09-18 13:20:02 +010042 // Upid/utid 0 is reserved for idle processes/threads.
Primiano Tuccib75dcee2018-08-08 12:21:36 +010043 unique_processes_.emplace_back(0);
44 unique_threads_.emplace_back(0);
45
46 // Reserve string ID 0 for the empty string.
Primiano Tucci2da5d2e2018-08-10 14:23:31 +010047 InternString("");
Isabelle Taylor47328cf2018-06-12 14:33:59 +010048}
49
Lalit Maganti35622b72018-06-06 12:03:11 +010050TraceStorage::~TraceStorage() {}
Lalit Maganticaed37e2018-06-01 03:03:08 +010051
Primiano Tucci2da5d2e2018-08-10 14:23:31 +010052StringId TraceStorage::InternString(base::StringView str) {
53 auto hash = str.Hash();
Lalit Maganti35622b72018-06-06 12:03:11 +010054 auto id_it = string_index_.find(hash);
55 if (id_it != string_index_.end()) {
Primiano Tucci2da5d2e2018-08-10 14:23:31 +010056 PERFETTO_DCHECK(base::StringView(string_pool_[id_it->second]) == str);
Lalit Maganti35622b72018-06-06 12:03:11 +010057 return id_it->second;
Lalit Maganticaed37e2018-06-01 03:03:08 +010058 }
Primiano Tucci2da5d2e2018-08-10 14:23:31 +010059 string_pool_.emplace_back(str.ToStdString());
Lalit Maganti85ca4a82018-12-07 17:28:02 +000060 StringId string_id = static_cast<uint32_t>(string_pool_.size() - 1);
Lalit Maganti35622b72018-06-06 12:03:11 +010061 string_index_.emplace(hash, string_id);
62 return string_id;
Lalit Maganticaed37e2018-06-01 03:03:08 +010063}
64
Isabelle Taylora0a22972018-08-03 12:06:12 +010065void TraceStorage::ResetStorage() {
66 *this = TraceStorage();
67}
68
Primiano Tucci5cb84f82018-10-31 21:46:36 -070069void TraceStorage::SqlStats::RecordQueryBegin(const std::string& query,
Lalit Maganti85ca4a82018-12-07 17:28:02 +000070 int64_t time_queued,
71 int64_t time_started) {
Primiano Tucci5cb84f82018-10-31 21:46:36 -070072 if (queries_.size() >= kMaxLogEntries) {
73 queries_.pop_front();
74 times_queued_.pop_front();
75 times_started_.pop_front();
76 times_ended_.pop_front();
77 }
78 queries_.push_back(query);
79 times_queued_.push_back(time_queued);
80 times_started_.push_back(time_started);
81 times_ended_.push_back(0);
82}
83
Lalit Maganti85ca4a82018-12-07 17:28:02 +000084void TraceStorage::SqlStats::RecordQueryEnd(int64_t time_ended) {
Primiano Tucci5cb84f82018-10-31 21:46:36 -070085 PERFETTO_DCHECK(!times_ended_.empty());
86 PERFETTO_DCHECK(times_ended_.back() == 0);
87 times_ended_.back() = time_ended;
88}
89
Ioannis Ilkosb8b11102019-01-29 17:56:55 +000090std::pair<int64_t, int64_t> TraceStorage::GetTraceTimestampBoundsNs() const {
91 int64_t start_ns = std::numeric_limits<int64_t>::max();
92 int64_t end_ns = std::numeric_limits<int64_t>::min();
93 MaybeUpdateMinMax(slices_.start_ns().begin(), slices_.start_ns().end(),
94 &start_ns, &end_ns);
95 MaybeUpdateMinMax(counters_.timestamps().begin(),
96 counters_.timestamps().end(), &start_ns, &end_ns);
97 MaybeUpdateMinMax(instants_.timestamps().begin(),
98 instants_.timestamps().end(), &start_ns, &end_ns);
99 MaybeUpdateMinMax(nestable_slices_.start_ns().begin(),
100 nestable_slices_.start_ns().end(), &start_ns, &end_ns);
101 MaybeUpdateMinMax(android_log_.timestamps().begin(),
102 android_log_.timestamps().end(), &start_ns, &end_ns);
103
104 if (start_ns == std::numeric_limits<int64_t>::max()) {
105 return std::make_pair(0, 0);
106 }
107 return std::make_pair(start_ns, end_ns);
108}
109
Lalit Maganticaed37e2018-06-01 03:03:08 +0100110} // namespace trace_processor
111} // namespace perfetto