• 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 "task_pool_filter.h"
17 #include "parting_string.h"
18 #include "string_to_numerical.h"
19 
20 namespace SysTuning {
21 namespace TraceStreamer {
TaskPoolFilter(TraceDataCache * dataCache,const TraceStreamerFilters * filter)22 TaskPoolFilter::TaskPoolFilter(TraceDataCache* dataCache, const TraceStreamerFilters* filter)
23     : FilterBase(dataCache, filter), IpidExecuteMap_(INVALID_INT32)
24 {
25 }
26 TaskPoolFilter::~TaskPoolFilter() = default;
27 
GetIpId(uint32_t index)28 uint32_t TaskPoolFilter::GetIpId(uint32_t index)
29 {
30     if (index >= traceDataCache_->GetConstInternalSlicesData().CallIds().size()) {
31         return INVALID_UINT32;
32     }
33     auto itid = traceDataCache_->GetConstInternalSlicesData().CallIds()[index];
34     auto thread = traceDataCache_->GetThreadData(itid);
35     if (!thread) {
36         return INVALID_UINT32;
37     }
38     return thread->internalPid_;
39 }
40 
CheckTheSameTask(uint32_t executeId,uint32_t index)41 uint32_t TaskPoolFilter::CheckTheSameTask(uint32_t executeId, uint32_t index)
42 {
43     return IpidExecuteMap_.Find(GetIpId(index), executeId);
44 }
45 
TaskPoolFieldSegmentation(const std::string & taskPoolStr,std::unordered_map<std::string,std::string> & args)46 void TaskPoolFilter::TaskPoolFieldSegmentation(const std::string& taskPoolStr,
47                                                std::unordered_map<std::string, std::string>& args)
48 {
49     for (base::PartingString ss(taskPoolStr, ','); ss.Next();) {
50         std::string key;
51         std::string value;
52         for (base::PartingString inner(ss.GetCur(), ':'); inner.Next();) {
53             if (key.empty()) {
54                 key = inner.GetCur();
55             } else {
56                 value = inner.GetCur();
57             }
58         }
59         args.emplace(std::move(key), std::move(value));
60     }
61 }
62 
TaskPoolEvent(const std::string & taskPoolStr,uint32_t index)63 bool TaskPoolFilter::TaskPoolEvent(const std::string& taskPoolStr, uint32_t index)
64 {
65     if (StartWith(taskPoolStr, targetStr_)) {
66         std::unordered_map<std::string, std::string> args;
67         if (StartWith(taskPoolStr, allocationStr_)) {
68             const auto& infoStr = taskPoolStr.substr(allocationStr_.length(), taskPoolStr.length());
69             TaskPoolFieldSegmentation(infoStr, args);
70             return UpdateAssignData(args, index);
71         }
72         if (StartWith(taskPoolStr, executeStr_)) {
73             const auto& infoStr = taskPoolStr.substr(executeStr_.length(), taskPoolStr.length());
74             TaskPoolFieldSegmentation(infoStr, args);
75             return UpdateExecuteData(args, index);
76         }
77         if (StartWith(taskPoolStr, returnStr_)) {
78             const auto& infoStr = taskPoolStr.substr(returnStr_.length(), taskPoolStr.length());
79             TaskPoolFieldSegmentation(infoStr, args);
80             return UpdateReturnData(args, index);
81         }
82     }
83     if (StartWith(taskPoolStr, timeoutStr_)) {
84         return AppendTimeoutRow(index);
85     }
86     return false;
87 }
88 
UpdateAssignData(const std::unordered_map<std::string,std::string> & args,uint32_t index)89 bool TaskPoolFilter::UpdateAssignData(const std::unordered_map<std::string, std::string>& args, uint32_t index)
90 {
91     if (index >= traceDataCache_->GetConstInternalSlicesData().CallIds().size()) {
92         return false;
93     }
94     auto allocItid = traceDataCache_->GetConstInternalSlicesData().CallIds()[index];
95     auto executeId = base::StrToInt<uint32_t>(args.at(" executeId "));
96     auto priority = base::StrToInt<uint32_t>(args.at(" priority "));
97     auto executeState = base::StrToInt<uint32_t>(args.at(" executeState "));
98 
99     uint32_t returnValue = CheckTheSameTask(executeId.value(), index);
100     if (returnValue == INVALID_INT32) {
101         uint32_t taskIndex = traceDataCache_->GetTaskPoolData()->AppendAllocationTaskData(
102             index, allocItid, executeId.value(), priority.value(), executeState.value());
103         IpidExecuteMap_.Insert(GetIpId(index), executeId.value(), taskIndex);
104     } else {
105         traceDataCache_->GetTaskPoolData()->UpdateAllocationTaskData(returnValue, index, allocItid, priority.value(),
106                                                                      executeState.value());
107     }
108     return true;
109 }
110 
UpdateExecuteData(const std::unordered_map<std::string,std::string> & args,uint32_t index)111 bool TaskPoolFilter::UpdateExecuteData(const std::unordered_map<std::string, std::string>& args, uint32_t index)
112 {
113     if (index >= traceDataCache_->GetConstInternalSlicesData().CallIds().size()) {
114         return false;
115     }
116     auto executeItid = traceDataCache_->GetConstInternalSlicesData().CallIds()[index];
117     auto executeId = base::StrToInt<uint32_t>(args.at(" executeId "));
118 
119     uint32_t returnValue = CheckTheSameTask(executeId.value(), index);
120     if (returnValue == INVALID_INT32) {
121         uint32_t taskIndex =
122             traceDataCache_->GetTaskPoolData()->AppendExecuteTaskData(index, executeItid, executeId.value());
123         IpidExecuteMap_.Insert(GetIpId(index), executeId.value(), taskIndex);
124         timeoutMap_.emplace(executeItid, taskIndex);
125         if (timeoutMap_.at(executeItid) < taskIndex) {
126             timeoutMap_.at(executeItid) = taskIndex;
127         }
128     } else {
129         traceDataCache_->GetTaskPoolData()->UpdateExecuteTaskData(returnValue, index, executeItid);
130         timeoutMap_.emplace(executeItid, returnValue);
131         if (timeoutMap_.at(executeItid) < returnValue) {
132             timeoutMap_.at(executeItid) = returnValue;
133         }
134     }
135     return true;
136 }
137 
UpdateReturnData(const std::unordered_map<std::string,std::string> & args,uint32_t index)138 bool TaskPoolFilter::UpdateReturnData(const std::unordered_map<std::string, std::string>& args, uint32_t index)
139 {
140     if (index >= traceDataCache_->GetConstInternalSlicesData().CallIds().size()) {
141         return false;
142     }
143     auto returnItid = traceDataCache_->GetConstInternalSlicesData().CallIds()[index];
144     auto executeId = base::StrToInt<uint32_t>(args.at(" executeId "));
145     auto returnStr_ = std::string_view(args.at(" performResult "));
146     uint32_t returnState = returnStr_.compare(" Successful") ? 0 : 1;
147 
148     uint32_t returnValue = CheckTheSameTask(executeId.value(), index);
149     if (returnValue == INVALID_INT32) {
150         uint32_t taskIndex =
151             traceDataCache_->GetTaskPoolData()->AppendReturnTaskData(index, returnItid, executeId.value(), returnState);
152         IpidExecuteMap_.Insert(GetIpId(index), executeId.value(), taskIndex);
153     } else {
154         traceDataCache_->GetTaskPoolData()->UpdateReturnTaskData(returnValue, index, returnItid, returnState);
155     }
156     return true;
157 }
158 
AppendTimeoutRow(uint32_t index)159 bool TaskPoolFilter::AppendTimeoutRow(uint32_t index)
160 {
161     if (index >= traceDataCache_->GetConstInternalSlicesData().CallIds().size()) {
162         return false;
163     }
164     auto timeoutItid = traceDataCache_->GetConstInternalSlicesData().CallIds()[index];
165     traceDataCache_->GetTaskPoolData()->AppendTimeoutRow(timeoutMap_.at(timeoutItid), index);
166     return true;
167 }
168 } // namespace TraceStreamer
169 } // namespace SysTuning
170