1 /*
2 * Copyright (C) 2020 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/importers/common/track_compressor.h"
18
19 #include <algorithm>
20 #include <cstdint>
21 #include <iterator>
22 #include <vector>
23
24 #include "perfetto/base/logging.h"
25 #include "src/trace_processor/types/trace_processor_context.h"
26
27 namespace perfetto::trace_processor {
28
TrackCompressor(TraceProcessorContext * context)29 TrackCompressor::TrackCompressor(TraceProcessorContext* context)
30 : context_(context) {}
31
BeginInternal(NestingBehaviour nesting_behaviour,uint64_t hash,int64_t cookie)32 uint32_t TrackCompressor::BeginInternal(NestingBehaviour nesting_behaviour,
33 uint64_t hash,
34 int64_t cookie) {
35 TrackSetNew& set = sets_[hash];
36 TrackState& state = GetOrCreateTrackForCookie(set.tracks, cookie);
37 switch (nesting_behaviour) {
38 case NestingBehaviour::kNestable:
39 state.nest_count++;
40 break;
41 case NestingBehaviour::kLegacySaturatingUnnestable:
42 PERFETTO_DCHECK(state.nest_count <= 1);
43 state.nest_count = 1;
44 break;
45 }
46 return static_cast<uint32_t>(std::distance(&set.tracks.front(), &state));
47 }
48
EndInternal(uint64_t hash,int64_t cookie)49 uint32_t TrackCompressor::EndInternal(uint64_t hash, int64_t cookie) {
50 TrackSetNew& set = sets_[hash];
51 TrackState& state = GetOrCreateTrackForCookie(set.tracks, cookie);
52
53 // It's possible to have a nest count of 0 even when we know about the track.
54 // Suppose the following sequence of events for some |id| and |cookie|:
55 // Begin
56 // (trace starts)
57 // Begin
58 // End
59 // End <- nest count == 0 here even though we have a record of this track.
60 if (state.nest_count > 0)
61 state.nest_count--;
62 return static_cast<uint32_t>(std::distance(&set.tracks.front(), &state));
63 }
64
ScopedInternal(uint64_t hash,int64_t ts,int64_t dur)65 uint32_t TrackCompressor::ScopedInternal(uint64_t hash,
66 int64_t ts,
67 int64_t dur) {
68 TrackSetNew& set = sets_[hash];
69
70 auto it = std::find_if(
71 set.tracks.begin(), set.tracks.end(), [ts](const TrackState& state) {
72 return state.slice_type == TrackState::SliceType::kTimestamp &&
73 state.ts_end <= ts;
74 });
75 if (it != set.tracks.end()) {
76 it->ts_end = ts + dur;
77 return static_cast<uint32_t>(std::distance(set.tracks.begin(), it));
78 }
79
80 TrackState state;
81 state.slice_type = TrackState::SliceType::kTimestamp;
82 state.ts_end = ts + dur;
83 set.tracks.emplace_back(state);
84 return static_cast<uint32_t>(set.tracks.size() - 1);
85 }
86
GetOrCreateTrackForCookie(std::vector<TrackState> & tracks,int64_t cookie)87 TrackCompressor::TrackState& TrackCompressor::GetOrCreateTrackForCookie(
88 std::vector<TrackState>& tracks,
89 int64_t cookie) {
90 auto it = std::find_if(
91 tracks.begin(), tracks.end(), [cookie](const TrackState& state) {
92 return state.slice_type == TrackState::SliceType::kCookie &&
93 state.cookie == cookie;
94 });
95 if (it != tracks.end())
96 return *it;
97
98 it = std::find_if(tracks.begin(), tracks.end(), [](const TrackState& state) {
99 return state.slice_type == TrackState::SliceType::kCookie &&
100 state.nest_count == 0;
101 });
102 if (it != tracks.end()) {
103 // Adopt this track for the cookie to make sure future slices with this
104 // cookie also get associated to this track.
105 it->cookie = cookie;
106 return *it;
107 }
108
109 TrackState state;
110 state.slice_type = TrackState::SliceType::kCookie;
111 state.cookie = cookie;
112 state.nest_count = 0;
113 tracks.emplace_back(state);
114
115 return tracks.back();
116 }
117
118 } // namespace perfetto::trace_processor
119