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 SEGMENT_ANALYSIS_H 16 #define SEGMENT_ANALYSIS_H 17 #include <memory> 18 #include <sstream> 19 #include <tuple> 20 #include <utility> 21 #include <vector> 22 23 #include "segment.h" 24 #include "syntax_rules.h" 25 namespace OHOS { 26 namespace HiviewDFX { 27 namespace { 28 static const std::string SEGMENT_CURSOR = "LineCursor"; 29 static const std::string SEGMENT_CMD_LAYER_ONE = "LayerOneCmd"; 30 static const std::string SEGMENT_DELIM = "."; 31 static const std::string RESULT_REASON = "REASON"; 32 static const std::string RESULT_BLOCKED_CHAIN = "BLOCKED_CHAIN"; 33 static const std::string RESULT_ALL_STACKS = "ALL_STACKS"; 34 static const std::string RESULT_END_STACK = "END_STACK"; 35 } 36 37 // base class 38 using ParamFeature = std::vector<std::pair<std::string, LineFeature>>; 39 using ParamFeatureIter = std::vector<std::pair<std::string, LineFeature>>::const_iterator; 40 using UniqSegment = std::unique_ptr<Segment>; 41 class SegmentAnalysis { 42 public: SegmentAnalysis()43 SegmentAnalysis() {} 44 virtual ~SegmentAnalysis(); 45 SegmentAnalysis(const SegmentAnalysis&) = delete; 46 SegmentAnalysis& operator=(const SegmentAnalysis&) = delete; 47 SegmentAnalysis(SegmentAnalysis&&) = delete; 48 SegmentAnalysis& operator=(SegmentAnalysis&&) = delete; 49 50 bool Analyze(const std::string& logFile, const ParamFeature& feature, 51 const std::vector<std::string>& startSeg, const std::vector<std::string>& segStackMatch); 52 void GetResult(std::map<std::string, std::string>& result) const; SetSegStatusCfg(std::map<std::string,std::vector<std::string>> & segStatusCfg)53 void SetSegStatusCfg(std::map<std::string, std::vector<std::string>>& segStatusCfg) 54 { 55 segStatusCfg_ = segStatusCfg; 56 }; 57 58 protected: 59 void AddSegment(int id, UniqSegment& seg); 60 bool GetSegment(int id, UniqSegment& seg); 61 std::string GetNameById(int id) const; 62 void RecordLayerOneSegment(const ParamFeature& feature); 63 virtual bool CheckParam(const ParamFeature& feature, const std::vector<std::string>& startSeg) const = 0; 64 virtual void RecordOtherSegment(const ParamFeature& feature, 65 int layerOneId, ParamFeatureIter begin, ParamFeatureIter end) = 0; 66 virtual bool GetStartSegment(const std::vector<std::string>& startSeg) = 0; 67 virtual bool NeedAnalyzeBlockedChain(void) const = 0; 68 virtual void AnalyzeBlockedChain() = 0; 69 virtual void GetBlockedChain(void) = 0; 70 virtual void GetEndStack(const std::vector<std::string>& segStack) = 0; 71 72 protected: 73 std::map<int, UniqSegment> segMap_; // key: id 74 std::vector<std::tuple<int, std::string>> idNameVec_; // tuple: (id, name) 75 UniqSegment startSeg_; 76 std::string reason_; 77 std::string blockedChain_; 78 std::string allStacks_; 79 std::string endStack_; 80 std::stringstream errLogBuf_; 81 std::map<std::string, std::vector<std::string>> segStatusCfg_; 82 static constexpr int LAYER_ONE_CMD_PARAM_NUM = 2; 83 }; 84 } // namespace HiviewDFX 85 } // namespace OHOS 86 #endif /* SEGMENT_ANALYSIS_H */ 87