1 /* 2 * Copyright (c) 2024 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 FAULT_INFO_BASE_H 16 #define FAULT_INFO_BASE_H 17 18 #include <string> 19 #include <vector> 20 21 #include "fault_common_base.h" 22 #include "fault_detector_util.h" 23 24 namespace OHOS { 25 namespace HiviewDFX { 26 27 class FaultInfoBase { 28 public: ~FaultInfoBase()29 virtual ~FaultInfoBase() {}; GetState()30 uint32_t GetState() const 31 { 32 return state_; 33 } 34 SetState(FaultStateType state)35 void SetState(FaultStateType state) 36 { 37 state_ = state; 38 } 39 GetProcessName()40 std::string GetProcessName() const 41 { 42 return processName_; 43 } 44 SetProcessName(std::string name)45 void SetProcessName(std::string name) 46 { 47 processName_ = name; 48 } 49 GetPid()50 uint32_t GetPid() const 51 { 52 return pid_; 53 } 54 SetPid(uint32_t pid)55 void SetPid(uint32_t pid) 56 { 57 pid_ = pid; 58 } 59 IsMonitoredStat()60 uint32_t IsMonitoredStat() const 61 { 62 return isMonitored_; 63 } 64 SetIsMonitoredStat(bool isMonitored)65 void SetIsMonitoredStat(bool isMonitored) 66 { 67 isMonitored_ = isMonitored; 68 } 69 GetLeakedTime()70 std::string GetLeakedTime() const 71 { 72 return leakedTime_; 73 } 74 RecordLeakedTime()75 void RecordLeakedTime() 76 { 77 leakedTime_ = FaultDetectorUtil::GetRealTimeStampStr(); 78 } 79 GetHapVersion()80 std::string GetHapVersion() const 81 { 82 return hapVersion_; 83 } 84 SetHapVersion(std::string hapVersion)85 void SetHapVersion(std::string hapVersion) 86 { 87 hapVersion_ = hapVersion; 88 } 89 90 protected: 91 FaultStateType state_; 92 std::string processName_; 93 std::string leakedTime_; 94 std::string hapVersion_; 95 uint32_t pid_; 96 bool isMonitored_ { false }; 97 }; 98 99 class MemoryLeakInfoBase : public FaultInfoBase { 100 public: ~MemoryLeakInfoBase()101 virtual ~MemoryLeakInfoBase() {}; 102 virtual std::string GetSampleFilePath() = 0; 103 virtual std::string GetRsMemPath() = 0; 104 virtual std::string GetLogFilePath() = 0; 105 GetMemoryLimit()106 uint64_t GetMemoryLimit() const 107 { 108 return memoryLimit_; 109 } 110 SetMemoryLimit(uint64_t value)111 void SetMemoryLimit(uint64_t value) 112 { 113 memoryLimit_ = value; 114 } 115 GetCpuTime()116 const std::vector<time_t> &GetCpuTime() const 117 { 118 return cpuTime_; 119 } 120 AddMemory(uint64_t memory)121 void AddMemory(uint64_t memory) 122 { 123 // rename to memorySamples_ 124 sampleMemory_.push_back(memory); 125 } 126 RemoveMemory(uint32_t removeCnt)127 void RemoveMemory(uint32_t removeCnt) 128 { 129 if (removeCnt > sampleMemory_.size()) { 130 removeCnt = sampleMemory_.size(); 131 } 132 sampleMemory_.erase(sampleMemory_.begin(), sampleMemory_.begin() + removeCnt); 133 } 134 GetMemory()135 const std::vector<uint64_t> &GetMemory() const 136 { 137 return sampleMemory_; 138 } 139 AddCpuTime(time_t value)140 void AddCpuTime(time_t value) 141 { 142 cpuTime_.push_back(value); 143 } 144 GetRealTime()145 const std::vector<std::string> &GetRealTime() const 146 { 147 return realTime_; 148 } 149 AddRealTime(const std::string & value)150 void AddRealTime(const std::string &value) 151 { 152 realTime_.push_back(value); 153 } 154 RemoveTime(uint32_t removeCnt)155 void RemoveTime(uint32_t removeCnt) 156 { 157 uint32_t cpuRemoveCnt = removeCnt; 158 if (cpuRemoveCnt > cpuTime_.size()) { 159 cpuRemoveCnt = cpuTime_.size(); 160 } 161 cpuTime_.erase(cpuTime_.begin(), cpuTime_.begin() + cpuRemoveCnt); 162 163 uint32_t realRemoveCnt = removeCnt; 164 if (realRemoveCnt > realTime_.size()) { 165 realRemoveCnt = realTime_.size(); 166 } 167 realTime_.erase(realTime_.begin(), realTime_.begin() + realRemoveCnt); 168 } 169 GetTopMemory()170 uint64_t GetTopMemory() const 171 { 172 return topMemory_; 173 } 174 SetTopMemory(uint64_t value)175 void SetTopMemory(uint64_t value) 176 { 177 topMemory_ = value; 178 } 179 GetEventMsg()180 const std::string &GetEventMsg() const 181 { 182 return eventMsg_; 183 } 184 SetEventMsg(const std::string & msg)185 void SetEventMsg(const std::string &msg) 186 { 187 eventMsg_ = msg; 188 } 189 190 private: 191 uint64_t memoryLimit_ {0}; 192 uint64_t topMemory_ {0}; 193 std::string eventMsg_; 194 std::vector<uint64_t> sampleMemory_; 195 std::vector<time_t> cpuTime_; 196 std::vector<std::string> realTime_; 197 }; 198 } // HiviewDFX 199 } // OHOS 200 #endif // FAULT_INFO_BASE_H 201