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 HIVIEW_BASE_APP_EVENT_HANDLER_H
16 #define HIVIEW_BASE_APP_EVENT_HANDLER_H
17 
18 #include <cstdint>
19 #include <ostream>
20 #include <string>
21 #include <vector>
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 class AppEventHandler {
26 public:
27     struct BundleInfo {
28         std::string bundleName;
29         std::string bundleVersion;
30     };
31 
32     struct ProcessInfo {
33         std::string processName;
34     };
35 
36     struct AbilityInfo {
37         std::string abilityName;
38     };
39 
40     struct MemoryInfo {
41         uint64_t pss = 0;
42         uint64_t rss = 0;
43         uint64_t vss = 0;
44         uint64_t avaliableMem = 0;
45         uint64_t freeMem = 0;
46         uint64_t totalMem = 0;
47     };
48 
49     struct AppLaunchInfo : public BundleInfo, public ProcessInfo {
50         int32_t startType = 0;
51         uint64_t iconInputTime = 0;
52         uint64_t animationFinishTime = 0;
53         uint64_t extendTime = 0;
54     };
55 
56     struct ScrollJankInfo : public BundleInfo, public ProcessInfo, public AbilityInfo {
57         uint64_t beginTime = 0;
58         uint64_t duration = 0;
59         int32_t totalAppFrames = 0;
60         int32_t totalAppMissedFrames = 0;
61         uint64_t maxAppFrametime = 0;
62         int32_t maxAppSeqFrames = 0;
63         int32_t totalRenderFrames = 0;
64         int32_t totalRenderMissedFrames = 0;
65         uint64_t maxRenderFrametime = 0;
66         int32_t maxRenderSeqFrames = 0;
67     };
68 
69     struct TimeInfo {
70         uint64_t time = 0;
71         uint64_t beginTime = 0;
72         uint64_t endTime = 0;
73     };
74 
75     struct ThreadInfo {
76         std::string name;
77         int32_t tid = 0;
78         double usage = 0;
79         friend std::ostream &operator<<(std::ostream &os, const ThreadInfo &p)
80         {
81             os << "{\"name\":\"" << p.name << "\",\"tid\":" << p.tid << ",\"usage\":" << p.usage << "}";
82             return os;
83         }
84     };
85 
86     struct LogInfo {
87         std::string file;
LogInfoLogInfo88         LogInfo(std::string file_)
89         {
90             file = file_;
91         }
92         friend std::ostream &operator<<(std::ostream &os, const LogInfo &f)
93         {
94             os << "\"" << f.file << "\"";
95             return os;
96         }
97     };
98 
99     struct CpuUsageHighInfo : public BundleInfo, public TimeInfo {
100         bool isForeground = false;
101         uint64_t usage = 0;
102         std::vector<ThreadInfo> threads;
103         std::vector<LogInfo> externalLog;
104         bool logOverLimit = false;
105     };
106 
107     struct UsageStatInfo {
108         std::vector<uint64_t> fgUsages = std::vector<uint64_t>(24); // 24 : statistics per hour, fg : foreground
109         std::vector<uint64_t> bgUsages = std::vector<uint64_t>(24); // 24 : statistics per hour, bg : background
110     };
111 
112     struct BatteryUsageInfo : public BundleInfo, public TimeInfo {
113         UsageStatInfo usage;
114         UsageStatInfo cpuEnergy;
115         UsageStatInfo gpuEnergy;
116         UsageStatInfo ddrEnergy;
117         UsageStatInfo displayEnergy;
118         UsageStatInfo audioEnergy;
119         UsageStatInfo modemEnergy;
120         UsageStatInfo romEnergy;
121         UsageStatInfo wifiEnergy;
122         UsageStatInfo othersEnergy;
123     };
124 
125     struct ResourceOverLimitInfo : public BundleInfo, public MemoryInfo {
126         int32_t pid = 0;
127         int32_t uid = 0;
128         std::string resourceType;
129         uint64_t limitSize = 0;
130         uint64_t liveobjectSize = 0;
131         uint32_t fdNum = 0;
132         std::string topFdType;
133         uint32_t topFdNum = 0;
134         uint32_t threadNum = 0;
135         std::vector<std::string> logPath;
136     };
137 
138     int PostEvent(const AppLaunchInfo& event);
139     int PostEvent(const ScrollJankInfo& event);
140     int PostEvent(const ResourceOverLimitInfo& event);
141     int PostEvent(const CpuUsageHighInfo& event);
142     int PostEvent(const BatteryUsageInfo& event);
143 };
144 } // namespace HiviewDFX
145 } // namespace OHOS
146 
147 #endif // HIVIEW_BASE_APP_EVENT_HANDLER_H