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 | |
Eric Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame^] | 23 | #include "perfetto/base/no_destructor.h" |
| 24 | |
| 25 | namespace perfetto { |
| 26 | namespace trace_processor { |
| 27 | |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 28 | namespace { |
Eric Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame^] | 29 | |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 30 | template <typename T> |
| 31 | void 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 Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame^] | 42 | |
| 43 | std::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 Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 55 | } // namespace |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 56 | |
Eric Seckler | 972225e | 2019-04-18 11:07:12 +0100 | [diff] [blame^] | 57 | const std::vector<const char*>& GetRefTypeStringMap() { |
| 58 | static const base::NoDestructor<std::vector<const char*>> map( |
| 59 | CreateRefTypeStringMap()); |
| 60 | return map.ref(); |
| 61 | } |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 62 | |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 63 | TraceStorage::TraceStorage() { |
Lalit Maganti | 1f64cfa | 2018-09-18 13:20:02 +0100 | [diff] [blame] | 64 | // Upid/utid 0 is reserved for idle processes/threads. |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 65 | unique_processes_.emplace_back(0); |
| 66 | unique_threads_.emplace_back(0); |
Isabelle Taylor | 47328cf | 2018-06-12 14:33:59 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Lalit Maganti | 35622b7 | 2018-06-06 12:03:11 +0100 | [diff] [blame] | 69 | TraceStorage::~TraceStorage() {} |
Lalit Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 70 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 71 | void TraceStorage::ResetStorage() { |
| 72 | *this = TraceStorage(); |
| 73 | } |
| 74 | |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 75 | void TraceStorage::SqlStats::RecordQueryBegin(const std::string& query, |
Lalit Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 76 | int64_t time_queued, |
| 77 | int64_t time_started) { |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 78 | 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 Maganti | 85ca4a8 | 2018-12-07 17:28:02 +0000 | [diff] [blame] | 90 | void TraceStorage::SqlStats::RecordQueryEnd(int64_t time_ended) { |
Primiano Tucci | 5cb84f8 | 2018-10-31 21:46:36 -0700 | [diff] [blame] | 91 | PERFETTO_DCHECK(!times_ended_.empty()); |
| 92 | PERFETTO_DCHECK(times_ended_.back() == 0); |
| 93 | times_ended_.back() = time_ended; |
| 94 | } |
| 95 | |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 96 | std::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 Maganti | 8320e6d | 2019-03-14 18:49:33 +0000 | [diff] [blame] | 101 | MaybeUpdateMinMax(counter_values_.timestamps().begin(), |
| 102 | counter_values_.timestamps().end(), &start_ns, &end_ns); |
Ioannis Ilkos | b8b1110 | 2019-01-29 17:56:55 +0000 | [diff] [blame] | 103 | 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 Maganti | caed37e | 2018-06-01 03:03:08 +0100 | [diff] [blame] | 116 | } // namespace trace_processor |
| 117 | } // namespace perfetto |