• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "config_filter.h"
17 namespace SysTuning {
18 namespace TraceStreamer {
CheckIfStartWithKeywords(const std::string & eventName,const std::vector<std::string> & keywords)19 bool CheckIfStartWithKeywords(const std::string &eventName, const std::vector<std::string> &keywords)
20 {
21     for (auto &keyword : keywords) {
22         if (StartWith(eventName, keyword)) {
23             return true;
24         }
25     }
26     return false;
27 }
CheckIfEndWithKeywords(const std::string & eventName,const std::vector<std::string> & keywords)28 bool CheckIfEndWithKeywords(const std::string &eventName, const std::vector<std::string> &keywords)
29 {
30     for (auto &keyword : keywords) {
31         if (EndWith(eventName, keyword)) {
32             return true;
33         }
34     }
35     return false;
36 }
ConfigFilter(TraceDataCache * dataCache,const TraceStreamerFilters * filter)37 ConfigFilter::ConfigFilter(TraceDataCache *dataCache, const TraceStreamerFilters *filter)
38     : FilterBase(dataCache, filter)
39 {
40 }
41 
~ConfigFilter()42 ConfigFilter::~ConfigFilter() {}
SetConfig(const std::string & configFile)43 bool ConfigFilter::SetConfig(const std::string &configFile)
44 {
45     json configResult = json::parse(configFile, nullptr, false);
46     if (configResult.is_discarded()) {
47         TS_LOGE("Failed to parse config file.");
48         return false;
49     }
50     InitConfig(configResult);
51     return true;
52 }
InitConfig(const json & config)53 void ConfigFilter::InitConfig(const json &config)
54 {
55     if (config.contains("Animation") && !config["Animation"].empty()) {
56         animationConfig_ = AnimationConfig(config["Animation"]);
57     } else {
58         animationConfig_ = AnimationConfig();
59     }
60     if (config.contains("AppStartup") && !config["AppStartup"].empty()) {
61         appStartupConfig_ = AppStartupConfig(config["AppStartup"]);
62     } else {
63         appStartupConfig_ = AppStartupConfig();
64     }
65     if (config.contains("config") && !config["config"].empty()) {
66         switchConfig_ = SwitchConfig(config["config"]);
67     } else {
68         switchConfig_ = SwitchConfig();
69     }
70     streamFilters_->animationFilter_->InitAnimationStartEvents();
71 }
GetAnimationConfig() const72 const AnimationConfig &ConfigFilter::GetAnimationConfig() const
73 {
74     return animationConfig_;
75 }
76 
GetAppStartupConfig() const77 const AppStartupConfig &ConfigFilter::GetAppStartupConfig() const
78 {
79     return appStartupConfig_;
80 }
GetSwitchConfig() const81 const SwitchConfig &ConfigFilter::GetSwitchConfig() const
82 {
83     return switchConfig_;
84 }
85 
CheckIfAnimationEvents(const std::string & eventName) const86 bool AnimationConfig::CheckIfAnimationEvents(const std::string &eventName) const
87 {
88     return CheckIfEndWithKeywords(eventName, animationProcEvents_);
89 }
90 
CheckIfFrameRateCmd(const std::string & eventName) const91 bool AnimationConfig::CheckIfFrameRateCmd(const std::string &eventName) const
92 {
93     return CheckIfStartWithKeywords(eventName, frameRateCmd_);
94 }
CheckIfRealFrameRateCmd(const std::string & eventName) const95 bool AnimationConfig::CheckIfRealFrameRateCmd(const std::string &eventName) const
96 {
97     return CheckIfStartWithKeywords(eventName, realFrameRateCmd_);
98 }
CheckIfFrameCountCmd(const std::string & eventName) const99 bool AnimationConfig::CheckIfFrameCountCmd(const std::string &eventName) const
100 {
101     return CheckIfStartWithKeywords(eventName, frameCountCmd_);
102 }
CheckIfFrameBeginCmd(const std::string & eventName) const103 bool AnimationConfig::CheckIfFrameBeginCmd(const std::string &eventName) const
104 {
105     return CheckIfStartWithKeywords(eventName, frameBeginCmd_);
106 }
CheckIfScreenSizeCmd(const std::string & eventName) const107 bool AnimationConfig::CheckIfScreenSizeCmd(const std::string &eventName) const
108 {
109     return CheckIfStartWithKeywords(eventName, screenSizeCmd_);
110 }
CheckIfFrameEndTimeCmd(const std::string & eventName) const111 bool AnimationConfig::CheckIfFrameEndTimeCmd(const std::string &eventName) const
112 {
113     return CheckIfStartWithKeywords(eventName, frameEndTimeCmd_);
114 }
CheckIfParallelCmd(const std::string & eventName) const115 bool AnimationConfig::CheckIfParallelCmd(const std::string &eventName) const
116 {
117     return CheckIfStartWithKeywords(eventName, parallelCmd_);
118 }
CheckIfPhase1(const std::string & eventName) const119 bool AppStartupConfig::CheckIfPhase1(const std::string &eventName) const
120 {
121     return CheckIfStartWithKeywords(eventName, phase1_.start);
122 }
CheckIfPhase2(const std::string & eventName) const123 bool AppStartupConfig::CheckIfPhase2(const std::string &eventName) const
124 {
125     return CheckIfStartWithKeywords(eventName, phase2_.start);
126 }
CheckIfPhase3(const std::string & eventName) const127 bool AppStartupConfig::CheckIfPhase3(const std::string &eventName) const
128 {
129     return CheckIfStartWithKeywords(eventName, phase3_.start);
130 }
CheckIfPhase4(const std::string & eventName) const131 bool AppStartupConfig::CheckIfPhase4(const std::string &eventName) const
132 {
133     return CheckIfStartWithKeywords(eventName, phase4_.start);
134 }
CheckIfPhase5(const std::string & eventName) const135 bool AppStartupConfig::CheckIfPhase5(const std::string &eventName) const
136 {
137     return CheckIfStartWithKeywords(eventName, phase5_.start);
138 }
CheckIfPhase6(const std::string & eventName) const139 bool AppStartupConfig::CheckIfPhase6(const std::string &eventName) const
140 {
141     return CheckIfStartWithKeywords(eventName, phase6_.start);
142 }
143 
GetOnAnimationStartEvents() const144 std::vector<std::string> AnimationConfig::GetOnAnimationStartEvents() const
145 {
146     return onAnimationStartEvents_;
147 }
SwitchConfig(const json & config)148 SwitchConfig::SwitchConfig(const json &config)
149 {
150     appConfigEnabled_ = config.value("AppStartup", 0) == 1;
151     animationConfigEnabled_ = config.value("AnimationAnalysis", 0) == 1;
152     taskPoolConfigEnabled_ = config.value("TaskPool", 0) == 1;
153     binderRunnableConfigEnabled_ = config.value("BinderRunnable", 0) == 1;
154     HMKernelTraceEnabled_ = config.value("HMKernel", 0) == 1;
155     rawTraceCutStartTsEnabled_ = config.value("RawTraceCutStartTs", 0) == 1;
156     ffrtConvertEnabled_ = config.value("FfrtConvert", 0) == 1;
157     std::string syscalls = config.value("System Calls", "");
158     UpdateSyscallsTsSet(syscalls);
159     TS_LOGI(
160         "appConfigEnabled_=%d, animationConfigEnabled_=%d, taskPoolConfigEnabled_=%d, binderRunnableConfigEnabled_=%d, "
161         "HMKernelTraceEnabled_=%d, rawTraceCutStartTsEnabled_=%d, ffrtConvertEnabled_=%d, syscalls=%s",
162         appConfigEnabled_, animationConfigEnabled_, taskPoolConfigEnabled_, binderRunnableConfigEnabled_,
163         HMKernelTraceEnabled_, rawTraceCutStartTsEnabled_, ffrtConvertEnabled_, syscalls.c_str());
164 }
AppConfigEnabled() const165 bool SwitchConfig::AppConfigEnabled() const
166 {
167     return appConfigEnabled_;
168 }
AnimationConfigEnabled() const169 bool SwitchConfig::AnimationConfigEnabled() const
170 {
171     return animationConfigEnabled_;
172 }
TaskPoolConfigEnabled() const173 bool SwitchConfig::TaskPoolConfigEnabled() const
174 {
175     return taskPoolConfigEnabled_;
176 }
BinderRunnableConfigEnabled() const177 bool SwitchConfig::BinderRunnableConfigEnabled() const
178 {
179     return binderRunnableConfigEnabled_;
180 }
HMKernelTraceEnabled() const181 bool SwitchConfig::HMKernelTraceEnabled() const
182 {
183     return HMKernelTraceEnabled_;
184 }
RawTraceCutStartTsEnabled() const185 bool SwitchConfig::RawTraceCutStartTsEnabled() const
186 {
187     return rawTraceCutStartTsEnabled_;
188 }
FfrtConfigEnabled() const189 bool SwitchConfig::FfrtConfigEnabled() const
190 {
191     return ffrtConvertEnabled_;
192 }
SyscallsTsSet() const193 const std::set<uint32_t> &SwitchConfig::SyscallsTsSet() const
194 {
195     return syscallNrSet_;
196 }
UpdateSyscallsTsSet(const std::string & syscalls)197 void SwitchConfig::UpdateSyscallsTsSet(const std::string &syscalls)
198 {
199     std::stringstream ss(syscalls);
200     std::string token;
201 
202     syscallNrSet_.clear();
203     while (std::getline(ss, token, ';')) {
204         syscallNrSet_.insert(std::stoi(token));
205     }
206 }
207 } // namespace TraceStreamer
208 } // namespace SysTuning
209