1 /** 2 * Copyright (c) 2021-2022 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 #ifndef DPROF_CONVERTER_FEATURES_MANAGER_H 17 #define DPROF_CONVERTER_FEATURES_MANAGER_H 18 19 #include "dprof/storage.h" 20 #include "utils/logger.h" 21 22 #include <vector> 23 #include <unordered_map> 24 #include <functional> 25 26 namespace panda::dprof { 27 class FeaturesManager { 28 public: 29 struct Functor { 30 virtual ~Functor() = default; 31 virtual bool operator()(const AppData &appData, const std::vector<uint8_t> &data) = 0; 32 }; 33 RegisterFeature(const std::string & featureName,Functor & functor)34 bool RegisterFeature(const std::string &featureName, Functor &functor) 35 { 36 auto it = map_.find(featureName); 37 if (it != map_.end()) { 38 LOG(ERROR, DPROF) << "Feature already exists, featureName=" << featureName; 39 return false; 40 } 41 map_.insert({featureName, functor}); 42 return true; 43 } 44 UnregisterFeature(const std::string & featureName)45 bool UnregisterFeature(const std::string &featureName) 46 { 47 if (map_.erase(featureName) != 1) { 48 LOG(ERROR, DPROF) << "Feature does not exist, featureName=" << featureName; 49 return false; 50 } 51 return true; 52 } 53 ProcessingFeature(const AppData & appData,const std::string & featureName,const std::vector<uint8_t> & data)54 bool ProcessingFeature(const AppData &appData, const std::string &featureName, 55 const std::vector<uint8_t> &data) const 56 { 57 auto it = map_.find(featureName); 58 if (it == map_.end()) { 59 LOG(ERROR, DPROF) << "Feature is not supported, featureName=" << featureName; 60 return false; 61 } 62 63 return it->second(appData, data); 64 } 65 ProcessingFeatures(const AppData & appData)66 bool ProcessingFeatures(const AppData &appData) const 67 { 68 for (const auto &it : appData.GetFeaturesMap()) { 69 if (!ProcessingFeature(appData, it.first, it.second)) { 70 LOG(ERROR, DPROF) << "Cannot processing feature: " << it.first << ", app: " << appData.GetName(); 71 return false; 72 } 73 } 74 return true; 75 } 76 77 private: 78 std::unordered_map<std::string, Functor &> map_; 79 }; 80 } // namespace panda::dprof 81 82 #endif // DPROF_CONVERTER_FEATURES_MANAGER_H 83