Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 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/trace_storage.h" |
| 18 | |
| 19 | #include <string.h> |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | #include <limits> |
| 22 | |
| 23 | namespace { |
| 24 | template <typename T> |
| 25 | void 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 Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 37 | |
| 38 | namespace perfetto { |
| 39 | namespace trace_processor { |
| 40 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 41 | TraceStorage::TraceStorage() { |
Lalit Maganti | 1f64cfa | 2018-09-18 13:20:02 +0100 | [diff] [blame] | 42 | // Upid/utid 0 is reserved for idle processes/threads. |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 43 | unique_processes_.emplace_back(0); |
| 44 | unique_threads_.emplace_back(0); |
| 45 | |
| 46 | // Reserve string ID 0 for the empty string. |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame] | 47 | InternString(""); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 50 | TraceStorage::~TraceStorage() {} |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 51 | |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame] | 52 | StringId TraceStorage::InternString(base::StringView str) { |
| 53 | auto hash = str.Hash(); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 54 | auto id_it = string_index_.find(hash); |
| 55 | if (id_it != string_index_.end()) { |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame] | 56 | PERFETTO_DCHECK(base::StringView(string_pool_[id_it->second]) == str); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 57 | return id_it->second; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 58 | } |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame] | 59 | string_pool_.emplace_back(str.ToStdString()); |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 60 | StringId string_id = static_cast<uint32_t>(string_pool_.size() - 1); |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 61 | string_index_.emplace(hash, string_id); |
| 62 | return string_id; |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 65 | void TraceStorage::ResetStorage() { |
| 66 | *this = TraceStorage(); |
| 67 | } |
| 68 | |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 69 | void TraceStorage::SqlStats::RecordQueryBegin(const std::string& query, |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 70 | int64_t time_queued, |
| 71 | int64_t time_started) { |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 72 | 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 Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 84 | void TraceStorage::SqlStats::RecordQueryEnd(int64_t time_ended) { |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 85 | PERFETTO_DCHECK(!times_ended_.empty()); |
| 86 | PERFETTO_DCHECK(times_ended_.back() == 0); |
| 87 | times_ended_.back() = time_ended; |
| 88 | } |
| 89 | |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 90 | std::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); |
Lalit Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame^] | 95 | MaybeUpdateMinMax(counter_values_.timestamps().begin(), |
| 96 | counter_values_.timestamps().end(), &start_ns, &end_ns); |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 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 Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 110 | } // namespace trace_processor |
| 111 | } // namespace perfetto |