1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "render_service_stdtype.h"
16
17 namespace SysTuning {
18 namespace TraceStdtype {
AppendFrame(uint64_t ts,uint32_t ipid,uint32_t itid,uint32_t vsyncId,uint64_t callStackSliceId)19 size_t FrameSlice::AppendFrame(uint64_t ts, uint32_t ipid, uint32_t itid, uint32_t vsyncId, uint64_t callStackSliceId)
20 {
21 timeStamps_.emplace_back(ts);
22 ipids_.emplace_back(ipid);
23 internalTids_.emplace_back(itid);
24 vsyncIds_.emplace_back(vsyncId);
25 callStackIds_.emplace_back(callStackSliceId);
26 endTss_.emplace_back(INVALID_UINT64);
27 dsts_.emplace_back(INVALID_UINT64);
28 ids_.emplace_back(id_++);
29 durs_.emplace_back(INVALID_UINT64);
30 types_.emplace_back(0);
31 flags_.emplace_back(INVALID_UINT8);
32 srcs_.emplace_back("");
33 depths_.emplace_back(0);
34 frameNos_.emplace_back(0);
35 return Size() - 1;
36 }
AppendFrame(const FrameSliceRow & frameSliceRow)37 size_t FrameSlice::AppendFrame(const FrameSliceRow &frameSliceRow)
38 {
39 auto row = AppendFrame(frameSliceRow.ts, frameSliceRow.ipid, frameSliceRow.itid, frameSliceRow.vsyncId,
40 frameSliceRow.callStackSliceId);
41 SetEndTime(row, frameSliceRow.end);
42 SetType(row, frameSliceRow.type);
43 depths_.emplace_back(0);
44 frameNos_.emplace_back(0);
45 durs_[row] = frameSliceRow.end - frameSliceRow.ts;
46 return row;
47 }
48
UpdateDepth()49 void FrameSlice::UpdateDepth()
50 {
51 DoubleMap<uint32_t, uint8_t, std::shared_ptr<std::vector<uint64_t>>> ipidAndTypesToVEndTime(nullptr);
52 for (auto row = 0; row < Size(); row++) {
53 if (flags_[row] == flagValue_) {
54 continue;
55 }
56 auto endTime = timeStamps_[row] + durs_[row];
57 auto vEndTimes = ipidAndTypesToVEndTime.Find(ipids_[row], types_[row]);
58 auto depth = 0;
59 if (!vEndTimes) {
60 vEndTimes = std::make_shared<std::vector<uint64_t>>();
61 vEndTimes->push_back(endTime);
62 ipidAndTypesToVEndTime.Insert(ipids_[row], types_[row], vEndTimes);
63 depths_[row] = depth;
64 continue;
65 }
66 for (; depth < vEndTimes->size(); depth++) {
67 if (timeStamps_[row] > vEndTimes->at(depth)) {
68 depths_[row] = depth;
69 vEndTimes->at(depth) = endTime;
70 break;
71 }
72 }
73 if (depth == vEndTimes->size()) {
74 depths_[row] = depth;
75 vEndTimes->push_back(endTime);
76 }
77 }
78 }
79
SetEndTime(uint64_t row,uint64_t end)80 void FrameSlice::SetEndTime(uint64_t row, uint64_t end)
81 {
82 endTss_[row] = end;
83 }
SetType(uint64_t row,uint8_t type)84 void FrameSlice::SetType(uint64_t row, uint8_t type)
85 {
86 types_[row] = type;
87 }
SetDst(uint64_t row,uint64_t dst)88 void FrameSlice::SetDst(uint64_t row, uint64_t dst)
89 {
90 dsts_[row] = diskTableSize_ + dst;
91 }
92
SetSrcs(uint64_t row,const std::vector<uint64_t> & fromSlices)93 void FrameSlice::SetSrcs(uint64_t row, const std::vector<uint64_t> &fromSlices)
94 {
95 std::string s = "";
96 for (auto &&i : fromSlices) {
97 s += std::to_string(diskTableSize_ + i) + ",";
98 }
99 s.pop_back();
100 srcs_[row] = s;
101 }
SetFlags(uint64_t row,const uint32_t flags)102 void FrameSlice::SetFlags(uint64_t row, const uint32_t flags)
103 {
104 flags_[row] = flags;
105 }
Ipids() const106 const std::deque<uint32_t> FrameSlice::Ipids() const
107 {
108 return ipids_;
109 }
VsyncIds() const110 const std::deque<uint32_t> FrameSlice::VsyncIds() const
111 {
112 return vsyncIds_;
113 }
CallStackIds() const114 const std::deque<uint64_t> FrameSlice::CallStackIds() const
115 {
116 return callStackIds_;
117 }
EndTss() const118 const std::deque<uint64_t> FrameSlice::EndTss() const
119 {
120 return endTss_;
121 }
Dsts() const122 const std::deque<uint64_t> FrameSlice::Dsts() const
123 {
124 return dsts_;
125 }
Durs() const126 const std::deque<uint64_t> FrameSlice::Durs() const
127 {
128 return durs_;
129 }
Types() const130 const std::deque<uint8_t> FrameSlice::Types() const
131 {
132 return types_;
133 }
Flags() const134 const std::deque<uint8_t> FrameSlice::Flags() const
135 {
136 return flags_;
137 }
138
Depths() const139 const std::deque<uint8_t> FrameSlice::Depths() const
140 {
141 return depths_;
142 }
FrameNos() const143 const std::deque<uint32_t> FrameSlice::FrameNos() const
144 {
145 return frameNos_;
146 }
Srcs() const147 const std::deque<std::string> &FrameSlice::Srcs() const
148 {
149 return srcs_;
150 }
UpdateCallStackSliceId(uint64_t row,uint64_t callStackSliceId)151 void FrameSlice::UpdateCallStackSliceId(uint64_t row, uint64_t callStackSliceId)
152 {
153 callStackIds_[row] = callStackSliceId;
154 }
SetEndTimeAndFlag(uint64_t row,uint64_t ts,uint64_t expectDur,uint64_t expectEnd)155 void FrameSlice::SetEndTimeAndFlag(uint64_t row, uint64_t ts, uint64_t expectDur, uint64_t expectEnd)
156 {
157 Unused(expectDur);
158 durs_[row] = ts - timeStamps_[row];
159 if (flags_[row] != abnormalStartEndTimeState_) {
160 flags_[row] = expectEnd >= ts ? 0 : 1;
161 }
162 }
Erase(uint64_t row)163 void FrameSlice::Erase(uint64_t row)
164 {
165 flags_[row] = invalidRow_;
166 }
167
AppendNew(uint32_t frameRow,uint64_t dur)168 size_t GPUSlice::AppendNew(uint32_t frameRow, uint64_t dur)
169 {
170 ids_.emplace_back(id_++);
171 frameRows_.emplace_back(frameRow);
172 durs_.emplace_back(dur);
173 return Size() - 1;
174 }
FrameRows() const175 const std::deque<uint32_t> &GPUSlice::FrameRows() const
176 {
177 return frameRows_;
178 }
Durs() const179 const std::deque<uint64_t> &GPUSlice::Durs() const
180 {
181 return durs_;
182 }
AppendNew(const DmaFenceRow & dmaFenceRow)183 size_t DmaFence::AppendNew(const DmaFenceRow &dmaFenceRow)
184 {
185 timeStamps_.emplace_back(dmaFenceRow.timeStamp);
186 durs_.emplace_back(dmaFenceRow.duration);
187 cats_.emplace_back(dmaFenceRow.eventName);
188 drivers_.emplace_back(dmaFenceRow.driver);
189 timelines_.emplace_back(dmaFenceRow.timeline);
190 contexts_.emplace_back(dmaFenceRow.context);
191 seqnos_.emplace_back(dmaFenceRow.seqno);
192 return Size() - 1;
193 }
194
DursData() const195 const std::deque<uint64_t> &DmaFence::DursData() const
196 {
197 return durs_;
198 }
199
CatsData() const200 const std::deque<DataIndex> &DmaFence::CatsData() const
201 {
202 return cats_;
203 }
204
DriversData() const205 const std::deque<DataIndex> &DmaFence::DriversData() const
206 {
207 return drivers_;
208 }
209
TimelinesData() const210 const std::deque<DataIndex> &DmaFence::TimelinesData() const
211 {
212 return timelines_;
213 }
214
ContextsData() const215 const std::deque<uint32_t> &DmaFence::ContextsData() const
216 {
217 return contexts_;
218 }
219
SeqnosData() const220 const std::deque<uint32_t> &DmaFence::SeqnosData() const
221 {
222 return seqnos_;
223 }
224
AppendNew(FrameSlice * frameSlice,uint64_t src,uint64_t dst)225 size_t FrameMaps::AppendNew(FrameSlice *frameSlice, uint64_t src, uint64_t dst)
226 {
227 timeStamps_.emplace_back(0);
228 ids_.emplace_back(id_++);
229 srcs_.emplace_back(frameSlice->diskTableSize_ + src);
230 dsts_.emplace_back(frameSlice->diskTableSize_ + dst);
231 if (frameSlice->Types().at(dst) == FrameSlice::EXPECT_SLICE) {
232 uint64_t expRsStartTime = frameSlice->TimeStampData().at(dst);
233 uint64_t expUiEndTime = frameSlice->TimeStampData().at(src) + frameSlice->Durs().at(src);
234 if (std::abs(static_cast<long long>(expRsStartTime - expUiEndTime)) >= ONE_MILLION_NANOSECONDS) {
235 auto acturalRow = dst - 1;
236 frameSlice->SetFlags(acturalRow, FrameSlice::GetAbnormalStartEndTimeState());
237 }
238 }
239
240 return Size() - 1;
241 }
SrcIndexs() const242 const std::deque<uint64_t> &FrameMaps::SrcIndexs() const
243 {
244 return srcs_;
245 }
DstIndexs() const246 const std::deque<uint64_t> &FrameMaps::DstIndexs() const
247 {
248 return dsts_;
249 }
250
251 } // namespace TraceStdtype
252 } // namespace SysTuning
253