• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "sys_event_stat.h"
17 
18 #include <algorithm>
19 #include <cinttypes>
20 #include <cstdio>
21 #include <vector>
22 
23 #include "time_util.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 constexpr int INVALID_SIZE_LIMIT = 32;
28 constexpr int INVALID_EVENT_SIZE_LIMIT = 512;
29 
SysEventStat()30 SysEventStat::SysEventStat(): inValidNum_(0), validNum_(0)
31 {}
32 
~SysEventStat()33 SysEventStat::~SysEventStat()
34 {
35     inValidNum_ = 0;
36     validNum_ = 0;
37 }
38 
AccumulateEvent(bool isValid)39 void SysEventStat::AccumulateEvent(bool isValid)
40 {
41     if (isValid) {
42         validNum_++;
43     } else {
44         inValidNum_++;
45     }
46 }
47 
AddValidEvent(const std::string & domain,const std::string & eventName)48 void SysEventStat::AddValidEvent(const std::string &domain, const std::string &eventName)
49 {
50     auto it = d2es_.find(domain);
51     if (it == d2es_.end()) {
52         std::map<std::string, int64_t> events;
53         events[eventName] = 1;
54         d2es_[domain] = events;
55         return;
56     }
57 
58     std::map<std::string, int64_t> &events = it->second;
59     auto eIt = events.find(eventName);
60     if (eIt == events.end()) {
61         events[eventName] = 1;
62         return;
63     }
64 
65     events[eventName] = events[eventName] + 1;
66 }
67 
AddInValidEvent(const std::string & domain,const std::string & eventName)68 void SysEventStat::AddInValidEvent(const std::string &domain, const std::string &eventName)
69 {
70     auto it = d2ies_.find(domain);
71     if (it == d2ies_.end()) {
72         if (d2ies_.size() >= INVALID_SIZE_LIMIT) {
73             return;
74         }
75         d2ies_[domain].append(eventName + ";");
76         return;
77     }
78     if (it->second.size() >= INVALID_EVENT_SIZE_LIMIT) {
79         return;
80     }
81     std::size_t pos = it->second.find(eventName + ";");
82     if (pos == std::string::npos) {
83         d2ies_[domain].append(eventName + ";");
84     }
85 }
86 
AccumulateEvent(const std::string & domain,const std::string & eventName,bool isValid)87 void SysEventStat::AccumulateEvent(const std::string &domain, const std::string &eventName, bool isValid)
88 {
89     std::lock_guard<std::mutex> lock(mutex_);
90     AccumulateEvent(isValid);
91     if (isValid) {
92         AddValidEvent(domain, eventName);
93     } else {
94         AddInValidEvent(domain, eventName);
95     }
96 }
97 
StatSummary(int fd)98 void SysEventStat::StatSummary(int fd)
99 {
100     std::lock_guard<std::mutex> lock(mutex_);
101     const BaseStat &stat = SysEventSummaryStat(fd, validNum_, inValidNum_, d2es_);
102     stat.Stat();
103 }
104 
StatDetail(int fd)105 void SysEventStat::StatDetail(int fd)
106 {
107     std::lock_guard<std::mutex> lock(mutex_);
108     const BaseStat &stat = SysEventDetailStat(fd, validNum_, inValidNum_, d2es_);
109     stat.Stat();
110 }
111 
StatInvalidDetail(int fd)112 void SysEventStat::StatInvalidDetail(int fd)
113 {
114     std::lock_guard<std::mutex> lock(mutex_);
115     const BaseStat &stat = SysEventInvalidStat(fd, validNum_, inValidNum_, d2ies_);
116     stat.Stat();
117 }
118 
Clear(int fd)119 void SysEventStat::Clear(int fd)
120 {
121     std::lock_guard<std::mutex> lock(mutex_);
122     inValidNum_ = 0;
123     validNum_ = 0;
124     d2es_.clear();
125     d2ies_.clear();
126     dprintf(fd, "clean all stat info\n");
127 }
128 
BaseStat(int fd,int64_t validNum,int64_t inValidNum)129 BaseStat::BaseStat(int fd, int64_t validNum, int64_t inValidNum): fd_(fd), validNum_(validNum), inValidNum_(inValidNum)
130 {}
131 
~BaseStat()132 BaseStat::~BaseStat()
133 {}
134 
StatHead() const135 void BaseStat::StatHead() const
136 {
137     dprintf(fd_, "total count=%" PRId64 ", valid count=%" PRId64 ", invalid count=%" PRId64 "\n",
138         (validNum_ + inValidNum_), validNum_, inValidNum_);
139 }
140 
StatDetail() const141 void BaseStat::StatDetail() const
142 {
143     dprintf(fd_, "stat from %" PRId64  " to now\n", TimeUtil::GetMilliseconds());
144 }
145 
Stat() const146 void BaseStat::Stat() const
147 {
148     StatHead();
149     CalcStat();
150     StatDetail();
151 }
152 
SysEventSummaryStat(int fd,int64_t validNum,int64_t inValidNum,std::map<std::string,std::map<std::string,int64_t>> & d2es)153 SysEventSummaryStat::SysEventSummaryStat(int fd, int64_t validNum, int64_t inValidNum,
154     std::map<std::string, std::map<std::string, int64_t>> &d2es): BaseStat(fd, validNum, inValidNum), d2es_(d2es)
155 {}
156 
~SysEventSummaryStat()157 SysEventSummaryStat::~SysEventSummaryStat()
158 {}
159 
GetDomains(std::vector<std::string> & domains) const160 void SysEventSummaryStat::GetDomains(std::vector<std::string> &domains) const
161 {
162     for (auto it = d2es_.begin(); it != d2es_.end(); it++) {
163         domains.emplace_back(it->first);
164     }
165     std::sort(domains.begin(), domains.end());
166 }
167 
GetEventCount(std::map<std::string,int64_t> & events) const168 int64_t SysEventSummaryStat::GetEventCount(std::map<std::string, int64_t> &events) const
169 {
170     int64_t total = 0;
171     for (auto it = events.begin(); it != events.end(); it++) {
172         total = total + it->second;
173     }
174     return total;
175 }
176 
StatDomainEvent(int fd,std::map<std::string,int64_t> & events) const177 void SysEventSummaryStat::StatDomainEvent(int fd, std::map<std::string, int64_t> &events) const
178 {}
179 
CalcStat() const180 void SysEventSummaryStat::CalcStat() const
181 {
182     std::vector<std::string> domains;
183     GetDomains(domains);
184     for (auto it = domains.begin(); it != domains.end(); it++) {
185         auto events = d2es_.at(*it);
186         dprintf(fd_, "*domain=%s total count=%" PRId64 "\n", it->c_str(), GetEventCount(events));
187         StatDomainEvent(fd_, events);
188     }
189 }
190 
SysEventDetailStat(int fd,int64_t validNum,int64_t inValidNum,std::map<std::string,std::map<std::string,int64_t>> & d2es)191 SysEventDetailStat::SysEventDetailStat(int fd, int64_t validNum, int64_t inValidNum,
192     std::map<std::string, std::map<std::string, int64_t>> &d2es): SysEventSummaryStat(fd, validNum, inValidNum, d2es)
193 {}
194 
~SysEventDetailStat()195 SysEventDetailStat::~SysEventDetailStat()
196 {}
197 
GetEventNames(std::map<std::string,int64_t> & events,std::vector<std::string> & eventNames) const198 void SysEventDetailStat::GetEventNames(std::map<std::string, int64_t> &events,
199     std::vector<std::string> &eventNames) const
200 {
201     for (auto it = events.begin(); it != events.end(); it++) {
202         eventNames.emplace_back(it->first);
203     }
204     std::sort(eventNames.begin(), eventNames.end());
205 }
206 
StatDomainEvent(int fd,std::map<std::string,int64_t> & events) const207 void SysEventDetailStat::StatDomainEvent(int fd, std::map<std::string, int64_t> &events) const
208 {
209     std::vector<std::string> eventNames;
210     GetEventNames(events, eventNames);
211     for (auto it = eventNames.begin(); it != eventNames.end(); it++) {
212         auto count = events.at(*it);
213         dprintf(fd, "    event=%s, count=%" PRId64 "\n", it->c_str(), count);
214     }
215 }
216 
SysEventInvalidStat(int fd,int64_t validNum,int64_t inValidNum,std::map<std::string,std::string> & d2ies)217 SysEventInvalidStat::SysEventInvalidStat(int fd, int64_t validNum, int64_t inValidNum,
218     std::map<std::string, std::string> &d2ies): BaseStat(fd, validNum, inValidNum), d2ies_(d2ies)
219 {}
220 
~SysEventInvalidStat()221 SysEventInvalidStat::~SysEventInvalidStat()
222 {}
223 
CalcStat() const224 void SysEventInvalidStat::CalcStat() const
225 {
226     std::vector<std::string> domains;
227     for (auto it = d2ies_.begin(); it != d2ies_.end(); it++) {
228         domains.emplace_back(it->first);
229     }
230     std::sort(domains.begin(), domains.end());
231     for (auto it = domains.begin(); it != domains.end(); it++) {
232         std::string invalidEvent = d2ies_.at(*it);
233         dprintf(fd_, "*invalid event name of domain=%s\n", it->c_str());
234         dprintf(fd_, "%s\n", invalidEvent.c_str());
235     }
236 }
237 } // namespace HiviewDFX
238 } // namespace OHOS