• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
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 
16 #include "sched_stdtype.h"
17 namespace SysTuning {
18 namespace TraceStdtype {
AppendThreadState(InternalTime ts,InternalTime dur,InternalCpu cpu,InternalTid itid,TableRowId idState)19 TableRowId ThreadStateData::AppendThreadState(InternalTime ts,
20                                               InternalTime dur,
21                                               InternalCpu cpu,
22                                               InternalTid itid,
23                                               TableRowId idState)
24 {
25     timeStamps_.emplace_back(ts);
26     durations_.emplace_back(dur);
27     itids_.emplace_back(itid);
28     tids_.emplace_back(INVALID_UINT32);
29     pids_.emplace_back(INVALID_UINT32);
30     states_.emplace_back(idState);
31     cpus_.emplace_back(cpu);
32     argSetIds_.emplace_back(INVALID_UINT32);
33     return itids_.size() - 1;
34 }
35 
SetDuration(TableRowId index,InternalTime dur)36 void ThreadStateData::SetDuration(TableRowId index, InternalTime dur)
37 {
38     durations_[index] = dur;
39 }
40 
SortAllRowByTs()41 void ThreadStateData::SortAllRowByTs()
42 {
43     std::deque<InternalTime> timeStampsTemp;
44     timeStampsTemp = std::move(timeStamps_);
45     std::multimap<uint64_t, uint32_t> timeStampsToIdMap = {};
46     for (auto id = 0; id < timeStampsTemp.size(); ++id) {
47         timeStampsToIdMap.insert({timeStampsTemp[id], id});
48     }
49     std::deque<InternalTime> durationsTemp;
50     std::deque<InternalTid> itidsTemp;
51     std::deque<InternalTid> tidsTemp;
52     std::deque<InternalPid> pidsTemp;
53     std::deque<DataIndex> statesTemp;
54     std::deque<InternalCpu> cpusTemp;
55     std::deque<uint32_t> argSetIdsTemp;
56     durationsTemp = std::move(durations_);
57     itidsTemp = std::move(itids_);
58     tidsTemp = std::move(tids_);
59     pidsTemp = std::move(pids_);
60     statesTemp = std::move(states_);
61     cpusTemp = std::move(cpus_);
62     argSetIdsTemp = std::move(argSetIds_);
63     for (auto itor = timeStampsToIdMap.begin(); itor != timeStampsToIdMap.end(); itor++) {
64         timeStamps_.emplace_back(timeStampsTemp[itor->second]);
65         durations_.emplace_back(durationsTemp[itor->second]);
66         itids_.emplace_back(itidsTemp[itor->second]);
67         tids_.emplace_back(tidsTemp[itor->second]);
68         pids_.emplace_back(pidsTemp[itor->second]);
69         states_.emplace_back(statesTemp[itor->second]);
70         cpus_.emplace_back(cpusTemp[itor->second]);
71         argSetIds_.emplace_back(argSetIdsTemp[itor->second]);
72     }
73 }
74 
UpdateDuration(TableRowId index,InternalTime ts)75 TableRowId ThreadStateData::UpdateDuration(TableRowId index, InternalTime ts)
76 {
77     if (durations_[index] == INVALID_TIME) {
78         durations_[index] = ts - timeStamps_[index];
79     }
80     return itids_[index];
81 }
82 
End(TableRowId index,InternalTime ts)83 bool ThreadStateData::End(TableRowId index, InternalTime ts)
84 {
85     if (durations_[index] == INVALID_TIME) {
86         durations_[index] = -1;
87         return false;
88     }
89     return true;
90 }
UpdateState(TableRowId index,TableRowId idState)91 void ThreadStateData::UpdateState(TableRowId index, TableRowId idState)
92 {
93     states_[index] = idState;
94 }
SetArgSetId(TableRowId index,uint32_t setId)95 void ThreadStateData::SetArgSetId(TableRowId index, uint32_t setId)
96 {
97     argSetIds_[index] = setId;
98 }
99 
UpdateDuration(TableRowId index,InternalTime ts,TableRowId idState)100 void ThreadStateData::UpdateDuration(TableRowId index, InternalTime ts, TableRowId idState)
101 {
102     durations_[index] = ts - timeStamps_[index];
103     states_[index] = idState;
104 }
105 
UpdateTidAndPid(TableRowId index,InternalTid tid,InternalTid pid)106 void ThreadStateData::UpdateTidAndPid(TableRowId index, InternalTid tid, InternalTid pid)
107 {
108     tids_[index] = tid;
109     pids_[index] = pid;
110 }
111 
UpdateDuration(TableRowId index,InternalTime ts,InternalCpu cpu,TableRowId idState)112 TableRowId ThreadStateData::UpdateDuration(TableRowId index, InternalTime ts, InternalCpu cpu, TableRowId idState)
113 {
114     cpus_[index] = cpu;
115     durations_[index] = ts - timeStamps_[index];
116     states_[index] = idState;
117     return itids_[index];
118 }
119 
AppendSchedSlice(uint64_t ts,uint64_t dur,uint64_t cpu,uint32_t internalTid,uint64_t endState,uint64_t priority)120 size_t SchedSlice::AppendSchedSlice(uint64_t ts,
121                                     uint64_t dur,
122                                     uint64_t cpu,
123                                     uint32_t internalTid,
124                                     uint64_t endState,
125                                     uint64_t priority)
126 {
127     timeStamps_.emplace_back(ts);
128     durs_.emplace_back(dur);
129     cpus_.emplace_back(cpu);
130     tsEnds_.emplace_back(0);
131     internalTids_.emplace_back(internalTid);
132     endStates_.emplace_back(endState);
133     priority_.emplace_back(priority);
134     argSets_.emplace_back(INVALID_UINT32);
135     internalPids_.emplace_back(INVALID_UINT32);
136     return Size() - 1;
137 }
138 
SetDuration(size_t index,uint64_t duration)139 void SchedSlice::SetDuration(size_t index, uint64_t duration)
140 {
141     durs_[index] = duration;
142     tsEnds_[index] = timeStamps_[index] + duration;
143 }
144 
Update(uint64_t index,uint64_t ts,uint64_t state)145 void SchedSlice::Update(uint64_t index, uint64_t ts, uint64_t state)
146 {
147     durs_[index] = ts - timeStamps_[index];
148     endStates_[index] = state;
149 }
150 
UpdateEndState(uint64_t index,uint64_t state)151 void SchedSlice::UpdateEndState(uint64_t index, uint64_t state)
152 {
153     endStates_[index] = state;
154 }
155 
UpdateArg(uint64_t index,uint32_t argsetId)156 void SchedSlice::UpdateArg(uint64_t index, uint32_t argsetId)
157 {
158     argSets_[index] = argsetId;
159 }
160 
AppendRawData(uint64_t timeStamp,uint32_t name,uint32_t cpu,uint32_t internalTid)161 size_t Raw::AppendRawData(uint64_t timeStamp, uint32_t name, uint32_t cpu, uint32_t internalTid)
162 {
163     ids_.emplace_back(id_++);
164     timeStamps_.emplace_back(timeStamp);
165     nameDeque_.emplace_back(name);
166     cpuDeque_.emplace_back(cpu);
167     internalTids_.emplace_back(internalTid);
168     return Size() - 1;
169 }
170 
AppendInstantEventData(uint64_t timeStamp,DataIndex nameIndex,int64_t internalTid,int64_t wakeupFromInternalPid)171 size_t Instants::AppendInstantEventData(uint64_t timeStamp,
172                                         DataIndex nameIndex,
173                                         int64_t internalTid,
174                                         int64_t wakeupFromInternalPid)
175 {
176     internalTids_.emplace_back(internalTid);
177     timeStamps_.emplace_back(timeStamp);
178     NameIndexs_.emplace_back(nameIndex);
179     wakeupFromInternalPids_.emplace_back(wakeupFromInternalPid);
180     return Size() - 1;
181 }
182 } // namespace TraceStdtype
183 } // namespace SysTuning
184