• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LOG_STATS_H
16 #define LOG_STATS_H
17 #include <unordered_map>
18 #include <optional>
19 #include <mutex>
20 
21 #include <log_timestamp.h>
22 #include <hilog_cmd.h>
23 #include <hilog_common.h>
24 #include <hilog/log.h>
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 enum class StatsType {
29     STATS_MASTER,
30     STATS_DOMAIN,
31     STATS_PID,
32     STATS_TAG,
33 };
34 
35 struct StatsInfo {
36     uint16_t level;
37     uint16_t type;
38     uint16_t len;
39     uint16_t dropped;
40     uint32_t domain;
41     uint32_t pid;
42     uint32_t tv_sec;
43     uint32_t tv_nsec;
44     uint32_t mono_sec;
45     std::string tag;
46 };
47 
48 struct StatsEntry {
49     uint32_t lines[LevelNum];
50     uint64_t len[LevelNum];
51     uint32_t dropped;
52     uint32_t tmpLines;
53     float freqMax; // lines per second, average value
54     LogTimeStamp realTimeFreqMax;
55     uint32_t tmpLen;
56     float throughputMax; // length per second, average value
57     LogTimeStamp realTimeThroughputMax;
58     LogTimeStamp realTimeLast;
59     LogTimeStamp monoTimeLast;
60     LogTimeStamp monoTimeAuditStart;
61 
GetFreqMaxStatsEntry62     float GetFreqMax() const
63     {
64         // this 0 is a initialized 0, so no need comapre to epsilon(0.000001)
65         if (freqMax == 0) {
66             LogTimeStamp tmp = monoTimeLast;
67             tmp -= monoTimeAuditStart;
68             float secs = tmp.FloatSecs();
69             if (secs > 1) {
70                 return tmpLines / secs;
71             } else {
72                 return tmpLines;
73             }
74         }
75         return freqMax;
76     }
77 
GetThroughputMaxStatsEntry78     float GetThroughputMax() const
79     {
80         // this 0 is a initialized 0, so no need comapre to epsilon(0.000001)
81         if (throughputMax == 0) {
82             LogTimeStamp tmp = monoTimeLast;
83             tmp -= monoTimeAuditStart;
84             float secs = tmp.FloatSecs();
85             if (secs > 1) {
86                 return tmpLen / secs;
87             } else {
88                 return tmpLen;
89             }
90         }
91         return throughputMax;
92     }
93 
GetTotalLinesStatsEntry94     uint32_t GetTotalLines() const
95     {
96         uint32_t totalLines = 0;
97         for (const uint32_t &i : lines) {
98             totalLines += i;
99         }
100         return totalLines;
101     }
102 
GetTotalLenStatsEntry103     uint64_t GetTotalLen() const
104     {
105         uint64_t totalLen = 0;
106         for (const uint64_t &i : len) {
107             totalLen += i;
108         }
109         return totalLen;
110     }
111 
PrintStatsEntry112     void Print() const
113     {
114         const std::string sep = " | ";
115         std::cout << "Lines: ";
116         std::cout << GetTotalLines() << sep;
117         std::cout << "Length: ";
118         std::cout << GetTotalLen() << sep;
119         std::cout << "Dropped: " << dropped << sep;
120         std::cout << "Max freq: " << GetFreqMax() << sep;
121         std::cout << "Max freq ts: " << realTimeFreqMax.tv_sec << "." << realTimeFreqMax.tv_nsec << sep;
122         std::cout << "Max throughput: " << GetThroughputMax() << sep;
123         std::cout << "Max throughput ts: " << realTimeThroughputMax.tv_sec << ".";
124         std::cout << realTimeThroughputMax.tv_nsec << sep;
125         std::cout << std::endl;
126     }
127 };
128 using TagStatsEntry = struct StatsEntry;
129 using TagTable = std::unordered_map<std::string, TagStatsEntry>;
130 
131 struct DomainStatsEntry {
132     StatsEntry stats;
133     TagTable tagStats;
134 };
135 using DomainTable = std::unordered_map<uint32_t, DomainStatsEntry>;
136 
137 using LogTypeDomainTable = DomainTable[TypeNum];
138 
139 struct PidStatsEntry {
140     StatsEntry statsAll;
141     StatsEntry stats[TypeNum];
142     std::string name;
143     TagTable tagStats;
144 };
145 using PidTable = std::unordered_map<uint32_t, PidStatsEntry>;
146 
147 class LogStats {
148 public:
149     explicit LogStats();
150     ~LogStats();
151 
152     void Count(const StatsInfo &info);
153     void Reset();
154     void Print();
155 
156     const LogTypeDomainTable& GetDomainTable() const;
157     const PidTable& GetPidTable() const;
158     const LogTimeStamp& GetBeginTs() const;
159     const LogTimeStamp& GetBeginMono() const;
160     void GetTotalLines(uint32_t (&in_lines)[LevelNum]) const;
161     void GetTotalLens(uint64_t (&in_lens)[LevelNum]) const;
162     bool IsEnable() const;
163     bool IsTagEnable() const;
164 
165     std::unique_lock<std::mutex> GetLock();
166 
167 private:
168     LogTypeDomainTable domainStats;
169     PidTable pidStats;
170     LogTimeStamp tsBegin;
171     LogTimeStamp monoBegin;
172     uint32_t totalLines[LevelNum];
173     uint64_t totalLens[LevelNum];
174     bool enable;
175     bool tagEnable;
176     std::mutex lock;
177 
178     void UpdateTagTable(TagTable& tt, const StatsInfo &info);
179     void UpdateDomainTable(const StatsInfo &info);
180     void UpdatePidTable(const StatsInfo &info);
181 };
182 } // namespace HiviewDFX
183 } // namespace OHOS
184 #endif // LOG_STATS_H