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 #ifndef CONFIG_FILTER_H 17 #define CONFIG_FILTER_H 18 #include <iostream> 19 #include <fstream> 20 #include <string> 21 #include <vector> 22 #include <log.h> 23 #include <json.hpp> 24 #include "string_help.h" 25 #include "filter_base.h" 26 #include "trace_data_cache.h" 27 #include "trace_streamer_config.h" 28 #include "trace_streamer_filters.h" 29 #include "animation_filter.h" 30 namespace SysTuning { 31 namespace TraceStreamer { 32 using namespace SysTuning::base; 33 using json = nlohmann::json; 34 bool CheckIfStartWithKeywords(const std::string &eventName, const std::vector<std::string> &keywords); 35 bool CheckIfEndWithKeywords(const std::string &eventName, const std::vector<std::string> &keywords); 36 class AnimationConfig { 37 public: 38 AnimationConfig() = default; AnimationConfig(const json & config)39 AnimationConfig(const json &config) 40 { 41 animationProcEvents_ = config.value("animationProcEvents_", std::vector<std::string>{}); 42 onAnimationStartEvents_ = config.value("onAnimationStartEvents_", std::vector<std::string>{}); 43 frameRateCmd_ = config.value("frameRateCmd_", std::vector<std::string>{}); 44 realFrameRateCmd_ = config.value("realFrameRateCmd_", std::vector<std::string>{}); 45 frameCountCmd_ = config.value("frameCountCmd_", std::vector<std::string>{}); 46 frameBeginCmd_ = config.value("frameBeginCmd_", std::vector<std::string>{}); 47 screenSizeCmd_ = config.value("screenSizeCmd_", std::vector<std::string>{}); 48 frameEndTimeCmd_ = config.value("frameEndTimeCmd_", std::vector<std::string>{}); 49 parallelCmd_ = config.value("parallelCmd_", std::vector<std::string>{}); 50 } 51 ~AnimationConfig() = default; 52 std::vector<std::string> GetOnAnimationStartEvents() const; 53 bool CheckIfAnimationEvents(const std::string &eventName) const; 54 bool CheckIfFrameRateCmd(const std::string &eventName) const; 55 bool CheckIfRealFrameRateCmd(const std::string &eventName) const; 56 bool CheckIfFrameCountCmd(const std::string &eventName) const; 57 bool CheckIfFrameBeginCmd(const std::string &eventName) const; 58 bool CheckIfScreenSizeCmd(const std::string &eventName) const; 59 bool CheckIfFrameEndTimeCmd(const std::string &eventName) const; 60 bool CheckIfParallelCmd(const std::string &eventName) const; 61 62 private: 63 std::vector<std::string> animationProcEvents_; 64 std::vector<std::string> onAnimationStartEvents_; 65 std::vector<std::string> frameRateCmd_; 66 std::vector<std::string> realFrameRateCmd_; 67 std::vector<std::string> frameCountCmd_; 68 std::vector<std::string> frameBeginCmd_; 69 std::vector<std::string> screenSizeCmd_; 70 std::vector<std::string> frameEndTimeCmd_; 71 std::vector<std::string> parallelCmd_; 72 }; 73 struct StartUpPhase { 74 std::string pName; 75 std::vector<std::string> start; 76 std::vector<std::string> end; 77 }; 78 class AppStartupConfig { 79 public: 80 AppStartupConfig() = default; AppStartupConfig(const json & config)81 AppStartupConfig(const json &config) 82 { 83 phase1_ = GetPhaseValue(config, "phase1"); 84 phase2_ = GetPhaseValue(config, "phase2"); 85 phase3_ = GetPhaseValue(config, "phase3"); 86 phase4_ = GetPhaseValue(config, "phase4"); 87 phase5_ = GetPhaseValue(config, "phase5"); 88 phase6_ = GetPhaseValue(config, "phase6"); 89 } 90 ~AppStartupConfig() = default; 91 bool CheckIfPhase1(const std::string &eventName) const; 92 bool CheckIfPhase2(const std::string &eventName) const; 93 bool CheckIfPhase3(const std::string &eventName) const; 94 bool CheckIfPhase4(const std::string &eventName) const; 95 bool CheckIfPhase5(const std::string &eventName) const; 96 bool CheckIfPhase6(const std::string &eventName) const; 97 98 private: GetPhaseValue(const json & phaseConfig,const std::string & phaseName)99 StartUpPhase GetPhaseValue(const json &phaseConfig, const std::string &phaseName) 100 { 101 auto phaseContent = phaseConfig.value(phaseName, json::object()); 102 return {phaseContent.value("pName", ""), phaseContent.value("start", std::vector<std::string>{}), 103 phaseContent.value("end", std::vector<std::string>{})}; 104 } 105 StartUpPhase phase1_; 106 StartUpPhase phase2_; 107 StartUpPhase phase3_; 108 StartUpPhase phase4_; 109 StartUpPhase phase5_; 110 StartUpPhase phase6_; 111 }; 112 113 class SwitchConfig { 114 public: 115 SwitchConfig() = default; 116 SwitchConfig(const json &config); 117 ~SwitchConfig() = default; 118 bool AppConfigEnabled() const; 119 bool AnimationConfigEnabled() const; 120 bool TaskPoolConfigEnabled() const; 121 bool BinderRunnableConfigEnabled() const; 122 bool HMKernelTraceEnabled() const; 123 bool RawTraceCutStartTsEnabled() const; 124 bool FfrtConfigEnabled() const; 125 const std::set<uint32_t> &SyscallsTsSet() const; 126 127 private: 128 void UpdateSyscallsTsSet(const std::string &syscalls); 129 130 private: 131 bool appConfigEnabled_ = false; 132 bool animationConfigEnabled_ = false; 133 bool taskPoolConfigEnabled_ = false; 134 bool binderRunnableConfigEnabled_ = false; 135 bool HMKernelTraceEnabled_ = false; 136 bool rawTraceCutStartTsEnabled_ = false; 137 bool ffrtConvertEnabled_ = false; 138 std::set<uint32_t> syscallNrSet_; 139 }; 140 class ConfigFilter : private FilterBase { 141 public: 142 ConfigFilter(TraceDataCache *dataCache, const TraceStreamerFilters *filter); 143 ConfigFilter(const ConfigFilter &) = delete; 144 ConfigFilter &operator=(const ConfigFilter &) = delete; 145 ~ConfigFilter() override; 146 bool SetConfig(const std::string &configFile); 147 const AnimationConfig &GetAnimationConfig() const; 148 const AppStartupConfig &GetAppStartupConfig() const; 149 const SwitchConfig &GetSwitchConfig() const; 150 151 private: 152 AnimationConfig animationConfig_; 153 AppStartupConfig appStartupConfig_; 154 SwitchConfig switchConfig_; 155 void InitConfig(const json &config); 156 }; 157 } // namespace TraceStreamer 158 } // namespace SysTuning 159 #endif // CONFIG_FILTER_H 160