blob: 3a9a890b82a42ac183b57b64cc10c8f14df74e60 [file] [log] [blame]
Lalit Maganticaed37e2018-06-01 03:03:08 +01001/*
2 * Copyright (C) 2017 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#ifndef SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_
18#define SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_
19
Lalit Maganti35622b72018-06-06 12:03:11 +010020#include <array>
Lalit Maganticaed37e2018-06-01 03:03:08 +010021#include <deque>
Isabelle Taylor47328cf2018-06-12 14:33:59 +010022#include <map>
Lalit Maganticaed37e2018-06-01 03:03:08 +010023#include <string>
24#include <unordered_map>
25#include <vector>
26
Lalit Maganti35622b72018-06-06 12:03:11 +010027#include "perfetto/base/logging.h"
28
Lalit Maganticaed37e2018-06-01 03:03:08 +010029namespace perfetto {
30namespace trace_processor {
31
32// Stores a data inside a trace file in a columnar form. This makes it efficient
33// to read or search across a single field of the trace (e.g. all the thread
34// names for a given CPU).
35class TraceStorage {
36 public:
Isabelle Taylor47328cf2018-06-12 14:33:59 +010037 TraceStorage();
38
Lalit Maganti35622b72018-06-06 12:03:11 +010039 constexpr static size_t kMaxCpus = 128;
Isabelle Taylor68e42192018-06-19 16:19:31 +010040
Isabelle Taylor47328cf2018-06-12 14:33:59 +010041 // StringId is an offset into |string_pool_|.
Lalit Maganti35622b72018-06-06 12:03:11 +010042 using StringId = size_t;
Isabelle Taylor68e42192018-06-19 16:19:31 +010043
Isabelle Taylor47328cf2018-06-12 14:33:59 +010044 // UniquePid is an offset into |unique_processes_|. This is necessary because
45 // Unix pids are reused and thus not guaranteed to be unique over a long
46 // period of time.
47 using UniquePid = uint32_t;
48 using UniqueProcessIterator =
49 std::multimap<uint32_t, UniquePid>::const_iterator;
50 using UniqueProcessRange =
51 std::pair<UniqueProcessIterator, UniqueProcessIterator>;
Lalit Maganti35622b72018-06-06 12:03:11 +010052
Isabelle Taylor3dd366c2018-06-22 16:21:41 +010053 // UniqueTid is an offset into |unique_threads_|. Necessary because tids can
54 // be reused.
55 using UniqueTid = uint32_t;
56 using UniqueThreadIterator =
57 std::multimap<uint32_t, UniqueTid>::const_iterator;
58 using UniqueThreadRange =
59 std::pair<UniqueThreadIterator, UniqueThreadIterator>;
60
Lalit Maganti35622b72018-06-06 12:03:11 +010061 class SlicesPerCpu {
62 public:
63 inline void AddSlice(uint64_t start_ns,
64 uint64_t duration_ns,
Isabelle Taylor68e42192018-06-19 16:19:31 +010065 uint32_t tid,
Lalit Maganti35622b72018-06-06 12:03:11 +010066 StringId thread_name_id) {
67 start_ns_.emplace_back(start_ns);
68 durations_.emplace_back(duration_ns);
Isabelle Taylor68e42192018-06-19 16:19:31 +010069
70 auto pair_it = storage_->tids_.equal_range(tid);
Isabelle Taylor68e42192018-06-19 16:19:31 +010071 // If there is a previous utid for that tid, use that.
72 if (pair_it.first != pair_it.second) {
73 UniqueTid prev_utid = std::prev(pair_it.second)->second;
74 utids_.emplace_back(prev_utid);
75 } else {
76 // If none exist, assign a new utid and store it.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +010077 Thread new_thread;
Isabelle Taylor68e42192018-06-19 16:19:31 +010078 new_thread.name_id = thread_name_id;
79 new_thread.start_ns = start_ns;
Isabelle Taylor3dd366c2018-06-22 16:21:41 +010080 new_thread.upid = 0;
Isabelle Taylor68e42192018-06-19 16:19:31 +010081 storage_->tids_.emplace(tid, storage_->unique_threads_.size());
82 utids_.emplace_back(storage_->unique_threads_.size());
83 storage_->unique_threads_.emplace_back(std::move(new_thread));
84 }
Lalit Maganti35622b72018-06-06 12:03:11 +010085 }
86
Isabelle Taylor47328cf2018-06-12 14:33:59 +010087 size_t slice_count() const { return start_ns_.size(); }
Lalit Maganti35622b72018-06-06 12:03:11 +010088
Isabelle Taylor47328cf2018-06-12 14:33:59 +010089 const std::deque<uint64_t>& start_ns() const { return start_ns_; }
Lalit Maganti35622b72018-06-06 12:03:11 +010090
Isabelle Taylor47328cf2018-06-12 14:33:59 +010091 const std::deque<uint64_t>& durations() const { return durations_; }
Lalit Maganti35622b72018-06-06 12:03:11 +010092
Isabelle Taylor68e42192018-06-19 16:19:31 +010093 const std::deque<UniqueTid>& utids() const { return utids_; }
94
95 void InitalizeSlices(TraceStorage* storage) { storage_ = storage; }
96
Lalit Maganti35622b72018-06-06 12:03:11 +010097 private:
98 // Each vector below has the same number of entries (the number of slices
99 // in the trace for the CPU).
100 std::deque<uint64_t> start_ns_;
101 std::deque<uint64_t> durations_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100102 std::deque<UniqueTid> utids_;
103
104 TraceStorage* storage_;
Lalit Maganti35622b72018-06-06 12:03:11 +0100105 };
106
107 struct Stats {
108 uint64_t mismatched_sched_switch_tids_ = 0;
109 };
110
111 virtual ~TraceStorage();
112
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100113 // Information about a unique process seen in a trace.
114 struct Process {
Isabelle Taylor68e42192018-06-19 16:19:31 +0100115 uint64_t start_ns = 0;
116 uint64_t end_ns = 0;
117 StringId name_id;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100118 };
119
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100120 // Information about a unique thread seen in a trace.
121 struct Thread {
122 uint64_t start_ns = 0;
123 uint64_t end_ns = 0;
124 StringId name_id;
125 UniquePid upid;
126 };
127
Lalit Maganticaed37e2018-06-01 03:03:08 +0100128 // Adds a sched slice for a given cpu.
Lalit Maganti35622b72018-06-06 12:03:11 +0100129 // Virtual for testing.
130 virtual void PushSchedSwitch(uint32_t cpu,
131 uint64_t timestamp,
132 uint32_t prev_pid,
133 uint32_t prev_state,
134 const char* prev_comm,
135 size_t prev_comm_len,
136 uint32_t next_pid);
Lalit Maganticaed37e2018-06-01 03:03:08 +0100137
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100138 // Adds a process entry for a given pid.
139 virtual void PushProcess(uint32_t pid,
140 const char* process_name,
141 size_t process_name_len);
142
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100143 // Adds a thread entry for the tid.
144 virtual void MatchThreadToProcess(uint32_t tid, uint32_t tgid);
145
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100146 // Returns the bounds of a range that includes all UniquePids that have the
147 // requested pid.
148 UniqueProcessRange UpidsForPid(uint32_t pid);
149
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100150 // Returns the bounds of a range that includes all UniqueTids that have the
151 // requested tid.
152 UniqueThreadRange UtidsForTid(uint32_t tid);
153
Lalit Maganticaed37e2018-06-01 03:03:08 +0100154 // Reading methods.
Lalit Maganti35622b72018-06-06 12:03:11 +0100155 const SlicesPerCpu& SlicesForCpu(uint32_t cpu) const {
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100156 PERFETTO_CHECK(cpu < cpu_events_.size());
Lalit Maganti35622b72018-06-06 12:03:11 +0100157 return cpu_events_[cpu];
Lalit Maganticaed37e2018-06-01 03:03:08 +0100158 }
159
Lalit Magantie9d40532018-06-29 13:15:06 +0100160 const Process& GetProcess(UniquePid upid) const {
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100161 PERFETTO_CHECK(upid < unique_processes_.size());
162 return unique_processes_[upid];
163 }
164
Lalit Magantie9d40532018-06-29 13:15:06 +0100165 const Thread& GetThread(UniqueTid utid) const {
Isabelle Taylor68e42192018-06-19 16:19:31 +0100166 PERFETTO_CHECK(utid < unique_threads_.size());
167 return unique_threads_[utid];
168 }
169
Lalit Magantie9d40532018-06-29 13:15:06 +0100170 const std::string& GetString(StringId id) const {
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100171 PERFETTO_CHECK(id < string_pool_.size());
172 return string_pool_[id];
173 }
174
Isabelle Taylore7003fb2018-07-17 11:39:01 +0100175 // |unique_processes_| always contains at least 1 element becuase the 0th ID
176 // is reserved to indicate an invalid process.
177 size_t process_count() const { return unique_processes_.size() - 1; }
178 // |unique_threads_| always contains at least 1 element becuase the 0th ID
179 // is reserved to indicate an invalid thread.
180 size_t thread_count() const { return unique_threads_.size() - 1; }
181
Lalit Maganticaed37e2018-06-01 03:03:08 +0100182 private:
Lalit Maganti35622b72018-06-06 12:03:11 +0100183 using StringHash = uint32_t;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100184
Lalit Maganti35622b72018-06-06 12:03:11 +0100185 struct SchedSwitchEvent {
186 uint64_t cpu = 0;
187 uint64_t timestamp = 0;
188 uint32_t prev_pid = 0;
189 uint32_t prev_state = 0;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100190 StringId prev_thread_name_id = 0;
Lalit Maganti35622b72018-06-06 12:03:11 +0100191 uint32_t next_pid = 0;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100192
Lalit Maganti35622b72018-06-06 12:03:11 +0100193 bool valid() const { return timestamp != 0; }
Lalit Maganticaed37e2018-06-01 03:03:08 +0100194 };
195
Lalit Maganti35622b72018-06-06 12:03:11 +0100196 // Return an unqiue identifier for the contents of each string.
197 // The string is copied internally and can be destroyed after this called.
198 StringId InternString(const char* data, size_t length);
199
200 // Metadata counters for events being added.
201 Stats stats_;
202
Lalit Maganticaed37e2018-06-01 03:03:08 +0100203 // One entry for each CPU in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100204 std::array<SchedSwitchEvent, kMaxCpus> last_sched_per_cpu_;
205
206 // One entry for each CPU in the trace.
207 std::array<SlicesPerCpu, kMaxCpus> cpu_events_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100208
209 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100210 std::deque<std::string> string_pool_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100211
212 // One entry for each unique string in the trace.
Lalit Maganti35622b72018-06-06 12:03:11 +0100213 std::unordered_map<StringHash, StringId> string_index_;
Isabelle Taylor47328cf2018-06-12 14:33:59 +0100214
215 // Each pid can have multiple UniquePid entries, a new UniquePid is assigned
216 // each time a process is seen in the trace.
217 std::multimap<uint32_t, UniquePid> pids_;
218
219 // One entry for each UniquePid, with UniquePid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100220 std::deque<Process> unique_processes_;
Isabelle Taylor68e42192018-06-19 16:19:31 +0100221
222 // Each tid can have multiple UniqueTid entries, a new UniqueTid is assigned
223 // each time a thread is seen in the trace.
224 std::multimap<uint32_t, UniqueTid> tids_;
225
226 // One entry for each UniqueTid, with UniqueTid as the index.
Isabelle Taylor3dd366c2018-06-22 16:21:41 +0100227 std::deque<Thread> unique_threads_;
Lalit Maganticaed37e2018-06-01 03:03:08 +0100228};
229
230} // namespace trace_processor
231} // namespace perfetto
232
233#endif // SRC_TRACE_PROCESSOR_TRACE_STORAGE_H_