1 /* 2 * Copyright (c) 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 HIVIEW_CORE_PLATFROM_MONITOR_H 17 #define HIVIEW_CORE_PLATFROM_MONITOR_H 18 #include <atomic> 19 #include <cstdint> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <vector> 25 26 #include "event_loop.h" 27 #include "pipeline.h" 28 #include "sys_event.h" 29 #include "time_util.h" 30 31 namespace OHOS { 32 namespace HiviewDFX { 33 struct PerfMeasure { 34 uint32_t maxTotalCount; 35 uint32_t maxTotalSize; 36 std::vector<std::string> domains; 37 std::vector<uint32_t> domainCounts; 38 uint32_t totalCount; 39 uint32_t totalSize; 40 uint8_t breakCount; 41 uint64_t breakDuration; 42 uint32_t minSpeed; 43 uint32_t maxSpeed; 44 std::vector<uint32_t> realCounts; 45 std::vector<uint32_t> processCounts; 46 std::vector<uint32_t> waitCounts; 47 uint32_t finishedCount; 48 uint32_t overRealTotalCount; 49 uint32_t overProcessTotalCount; 50 uint32_t realPercent; 51 uint32_t processpercent; 52 }; 53 54 class PlatformMonitor { 55 public: PlatformMonitor()56 PlatformMonitor(): maxTotalCount_(0), maxTotalSize_(0), looper_(nullptr) {} ~PlatformMonitor()57 ~PlatformMonitor() {} 58 void Breaking(); 59 void CollectCostTime(PipelineEvent *event); 60 void CollectEvent(std::shared_ptr<PipelineEvent> event); 61 void CollectPerfProfiler(); 62 void ReportBreakProfile(); 63 void ReportCycleProfile(); 64 void ReportRecoverProfile(); 65 void StartMonitor(std::shared_ptr<EventLoop> looper); 66 67 private: 68 void AccumulateTimeInterval(int64_t costTime, std::map<int8_t, uint32_t> &stat); 69 void CalcOverBenckMarkPct(PerfMeasure &perfMeasure); 70 std::shared_ptr<SysEvent> CreateProfileReport(PerfMeasure &perfMeasure); 71 void GetCostTimeInterval(PerfMeasure &perfMeasure); 72 void GetDomainsStat(PerfMeasure &perfMeasure); 73 void GetMaxSpeed(PerfMeasure &perfMeasure) const; 74 void GetMaxTotalMeasure(PerfMeasure &perfMeasure); 75 void GetBreakStat(PerfMeasure &perfMeasure); 76 void GetTopDomains(std::vector<std::string> &domains, std::vector<uint32_t> &counts); 77 void GetTopEvents(std::vector<std::string> &events, std::vector<uint32_t> &counts); 78 void InitData(); 79 80 private: 81 static constexpr uint8_t PCT = 100; 82 uint32_t collectPeriod_ = 5 * TimeUtil::SECONDS_PER_MINUTE; // 5 minute 83 uint32_t reportPeriod_ = TimeUtil::SECONDS_PER_HOUR; // one hour 84 uint32_t totalSizeBenchMark_ = 200 * 1024 * 1024; // 200M 85 uint32_t realTimeBenchMark_ = 100 * 1000; // 100 millisecond 86 uint32_t processTimeBenchMark_ = 200 * 1000; // millisecond 87 // 50, 100, 200, 500 millisecond, 1, 10, 100, 500, 1000 second, 88 static constexpr uint64_t intervals_[] = {50 * 1000, 100 * 1000, 200 * 1000, 500 * 1000, 1000 * 1000, 89 10 * 1000 * 1000, 100 * 1000 * 1000, 500 * 1000 * 1000, 90 1000 * 1000 * 1000}; 91 92 // intervals 93 std::mutex statMutex_; 94 std::map<int8_t, uint32_t> realStat_; 95 std::map<int8_t, uint32_t> processStat_; 96 std::map<int8_t, uint32_t> waitTimeStat_; 97 98 // max 99 std::atomic<uint32_t> maxTotalCount_; 100 std::atomic<uint32_t> maxTotalSize_; 101 102 // break 103 uint32_t totalCount_ = 0; 104 uint32_t totalSize_ = 0; 105 uint64_t breakTimestamp_ = 0; 106 uint64_t recoverTimestamp_ = 0; 107 uint8_t breakCount_ = 0; 108 uint64_t breakDuration_ = 0; 109 110 // over brenchmark 111 uint32_t finishedCount_ = 0; 112 uint32_t overRealTotalCount_ = 0; 113 uint32_t overProcessTotalCount_ = 0; 114 115 // speed 116 uint32_t onceTotalRealTime_ = 0; 117 uint32_t onceTotalProcTime_ = 0; 118 uint32_t onceTotalWaitTime_ = 0; 119 uint32_t onceTotalCnt_ = 0; 120 uint32_t minSpeed_ = 0; 121 uint32_t maxSpeed_ = 0; 122 uint32_t curRealSpeed_ = 0; 123 uint32_t curProcSpeed_ = 0; 124 125 // avg 126 double avgRealTime_ = 0; 127 double avgProcessTime_ = 0; 128 double avgWaitTime_ = 0; 129 130 // topK event 131 std::mutex topMutex_; 132 std::map<std::string, uint32_t> topEvents_; 133 std::map<std::string, uint32_t> topDomains_; 134 135 std::shared_ptr<EventLoop> looper_; 136 }; // PlatformMonitor 137 } // namespace HiviewDFX 138 } // namespace OHOS 139 #endif // HIVIEW_CORE_PLATFROM_MONITOR_H