• 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 #ifndef HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_STAT_H
17 #define HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_STAT_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 class SysEventStat {
27 public:
28     SysEventStat();
29     ~SysEventStat();
30 
31 public:
32     void AccumulateEvent(bool isValid = true);
33     void AccumulateEvent(const std::string &domain, const std::string &eventName, bool isValid = true);
34     void StatSummary(int fd);
35     void StatDetail(int fd);
36     void StatInvalidDetail(int fd);
37     void Clear(int fd);
38 
39 private:
40     void AddValidEvent(const std::string &domain, const std::string &eventName);
41     void AddInValidEvent(const std::string &domain, const std::string &eventName);
42 
43 private:
44     std::mutex mutex_;
45     int64_t inValidNum_;
46     int64_t validNum_;
47     std::map<std::string, std::map<std::string, int64_t>> d2es_;
48     std::map<std::string, std::string> d2ies_;
49 }; // SysEventStat
50 
51 class BaseStat {
52 public:
53     BaseStat(int fd, int64_t validNum, int64_t inValidNum);
54     virtual ~BaseStat();
55 
56 public:
57     void Stat() const;
58 
59 protected:
60     virtual void CalcStat() const = 0;
61 
62 protected:
63     int fd_;
64 
65 private:
66     void StatHead() const;
67     void StatDetail() const;
68 
69 private:
70     int64_t validNum_;
71     int64_t inValidNum_;
72 }; // BaseStat
73 
74 class SysEventSummaryStat : public BaseStat {
75 public:
76     SysEventSummaryStat(int fd, int64_t validNum, int64_t inValidNum,
77         std::map<std::string, std::map<std::string, int64_t>> &d2es);
78     ~SysEventSummaryStat();
79 
80     void CalcStat() const override;
81 
82 protected:
83     virtual void StatDomainEvent(int fd, std::map<std::string, int64_t> &events) const;
84 
85 protected:
86     std::map<std::string, std::map<std::string, int64_t>> &d2es_;
87 
88 private:
89     void GetDomains(std::vector<std::string> &domains) const;
90     int64_t GetEventCount(std::map<std::string, int64_t> &events) const;
91 }; // SysEventSummaryStat
92 
93 class SysEventDetailStat : public SysEventSummaryStat {
94 public:
95     SysEventDetailStat(int fd, int64_t validNum, int64_t inValidNum,
96         std::map<std::string, std::map<std::string, int64_t>> &d2es);
97     ~SysEventDetailStat();
98 
99 protected:
100     void StatDomainEvent(int fd, std::map<std::string, int64_t> &events) const override;
101 
102 private:
103     void GetEventNames(std::map<std::string, int64_t> &events, std::vector<std::string> &eventNames) const;
104 }; // SysEventDetailStat
105 
106 class SysEventInvalidStat : public BaseStat {
107 public:
108     SysEventInvalidStat(int fd, int64_t validNum, int64_t inValidNum, std::map<std::string, std::string> &d2ies);
109     ~SysEventInvalidStat();
110 
111 public:
112     void CalcStat() const override;
113 
114 private:
115     std::map<std::string, std::string> &d2ies_;
116 }; // SysEventInvalidStat
117 } // namespace HiviewDFX
118 } // namespace OHOS
119 #endif // HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_SYS_EVENT_STAT_H