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 #ifndef SUB_EVENT_PARSER_H 16 #define SUB_EVENT_PARSER_H 17 #include <cstdint> 18 #include <functional> 19 #include <map> 20 #include "ftrace_field_parser.h" 21 #include "trace_plugin_result.pb.h" 22 23 FTRACE_NS_BEGIN 24 class SubEventParserRegisterar; 25 class SubEventParser { 26 public: GetInstance()27 static inline SubEventParser& GetInstance() 28 { 29 static SubEventParser instance; 30 return instance; 31 } 32 33 bool IsSupport(const std::string& eventName) const; 34 bool SetupEvent(const EventFormat& format); 35 36 using ParseFunction = std::function<void(FtraceEvent&, uint8_t[], size_t, const EventFormat&)>; 37 struct ParseEventCtx { 38 EventFormat format; 39 ParseFunction func; 40 }; 41 GetParseEventCtx(uint32_t eventId)42 inline ParseEventCtx* GetParseEventCtx(uint32_t eventId) 43 { 44 if (eventId == schedSwitchEventID) { 45 return schedSwitchCtx; 46 } else if (eventId == schedWakingEventID) { 47 return schedWakingCtx; 48 } else if (eventId == schedWakupEventID) { 49 return schedWakingCtx; 50 } 51 52 auto it = idToFunctions_.find(eventId); 53 if (it != idToFunctions_.end()) { 54 return it->second; 55 } 56 return nullptr; 57 } 58 ParseEvent(FtraceEvent & event,uint8_t data[],size_t size,const ParseEventCtx * parseEventCtx)59 inline void ParseEvent(FtraceEvent& event, 60 uint8_t data[], 61 size_t size, 62 const ParseEventCtx* parseEventCtx) const 63 { 64 parseEventCtx->func(event, data, size, parseEventCtx->format); 65 } 66 67 protected: 68 friend class SubEventParserRegisterar; 69 void RegisterParseFunction(const std::string& name, ParseFunction&& func); 70 void UnregisterParseFunction(const std::string& name); 71 72 private: 73 SubEventParser(); 74 ~SubEventParser(); 75 DISALLOW_COPY_AND_MOVE(SubEventParser); 76 std::unordered_map<std::string, ParseEventCtx> nameToFunctions_; 77 std::unordered_map<uint32_t, ParseEventCtx*> idToFunctions_; 78 79 uint32_t schedSwitchEventID = (uint32_t)-1; 80 uint32_t schedWakingEventID = (uint32_t)-1; 81 uint32_t schedWakupEventID = (uint32_t)-1; 82 ParseEventCtx* schedSwitchCtx = nullptr; 83 ParseEventCtx* schedWakingCtx = nullptr; 84 ParseEventCtx* schedWakupCtx = nullptr; 85 }; 86 87 class SubEventParserRegisterar { 88 public: 89 SubEventParserRegisterar(const std::string& name, SubEventParser::ParseFunction&& func); 90 ~SubEventParserRegisterar(); 91 92 private: 93 DISALLOW_COPY_AND_MOVE(SubEventParserRegisterar); 94 std::string name_; 95 }; 96 FTRACE_NS_END 97 98 #define REGISTER_FTRACE_EVENT_PARSE_FUNCTION(name, func) \ 99 static FTRACE_NS::SubEventParserRegisterar g_eventRegisterar##name(#name, func) 100 101 #endif // SUB_EVENT_PARSER_H 102