1 /*
2  * Copyright (c) 2021 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 "perf_profile.h"
17 #include <cinttypes>
18 
19 #include "app_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
PerfProfile()23 PerfProfile::PerfProfile()
24 {
25     APP_LOGD("create");
26 }
27 
~PerfProfile()28 PerfProfile::~PerfProfile()
29 {
30     APP_LOGD("destroy");
31 }
32 
GetBmsLoadStartTime() const33 int64_t PerfProfile::GetBmsLoadStartTime() const
34 {
35     return bmsLoadStart_;
36 }
37 
SetBmsLoadStartTime(int64_t time)38 void PerfProfile::SetBmsLoadStartTime(int64_t time)
39 {
40     bmsLoadStart_ = (time > 0) ? time : 0;
41 }
42 
GetBmsLoadEndTime() const43 int64_t PerfProfile::GetBmsLoadEndTime() const
44 {
45     return bmsLoadEnd_;
46 }
47 
SetBmsLoadEndTime(int64_t time)48 void PerfProfile::SetBmsLoadEndTime(int64_t time)
49 {
50     bmsLoadEnd_ = (time > 0 && time > bmsLoadStart_) ? time : bmsLoadStart_;
51 }
52 
GetBundleScanStartTime() const53 int64_t PerfProfile::GetBundleScanStartTime() const
54 {
55     return bundleScanStart_;
56 }
57 
SetBundleScanStartTime(int64_t time)58 void PerfProfile::SetBundleScanStartTime(int64_t time)
59 {
60     bundleScanStart_ = (time > 0) ? time : 0;
61 }
62 
GetBundleScanEndTime() const63 int64_t PerfProfile::GetBundleScanEndTime() const
64 {
65     return bundleScanEnd_;
66 }
67 
SetBundleScanEndTime(int64_t time)68 void PerfProfile::SetBundleScanEndTime(int64_t time)
69 {
70     bundleScanEnd_ = (time > 0 && time > bundleScanStart_) ? time : bundleScanStart_;
71 }
72 
GetBundleDownloadStartTime() const73 int64_t PerfProfile::GetBundleDownloadStartTime() const
74 {
75     return bundleDownloadStart_;
76 }
77 
SetBundleDownloadStartTime(int64_t time)78 void PerfProfile::SetBundleDownloadStartTime(int64_t time)
79 {
80     bundleDownloadStart_ = (time > 0) ? time : 0;
81 }
82 
GetBundleDownloadEndTime() const83 int64_t PerfProfile::GetBundleDownloadEndTime() const
84 {
85     return bundleDownloadEnd_;
86 }
87 
SetBundleDownloadEndTime(int64_t time)88 void PerfProfile::SetBundleDownloadEndTime(int64_t time)
89 {
90     bundleDownloadEnd_ = (time > 0 && time > bundleDownloadStart_) ? time : bundleDownloadStart_;
91 }
92 
GetBundleInstallStartTime() const93 int64_t PerfProfile::GetBundleInstallStartTime() const
94 {
95     return bundleInstallStart_;
96 }
97 
SetBundleInstallStartTime(int64_t time)98 void PerfProfile::SetBundleInstallStartTime(int64_t time)
99 {
100     bundleInstallStart_ = (time > 0) ? time : 0;
101 }
102 
GetBundleInstallEndTime() const103 int64_t PerfProfile::GetBundleInstallEndTime() const
104 {
105     return bundleInstallEnd_;
106 }
107 
SetBundleInstallEndTime(int64_t time)108 void PerfProfile::SetBundleInstallEndTime(int64_t time)
109 {
110     if (time >= 0 && time > bundleInstallStart_) {
111         bundleInstallEnd_ = time;
112         bundleInstallTime_ += (bundleInstallEnd_ - bundleInstallStart_);
113     } else {
114         bundleInstallEnd_ = bundleInstallStart_;
115     }
116 }
117 
GetBundleTotalInstallTime() const118 int64_t PerfProfile::GetBundleTotalInstallTime() const
119 {
120     return bundleInstallTime_;
121 }
122 
GetBundleUninstallStartTime() const123 int64_t PerfProfile::GetBundleUninstallStartTime() const
124 {
125     return bundleUninstallStart_;
126 }
127 
SetBundleUninstallStartTime(int64_t time)128 void PerfProfile::SetBundleUninstallStartTime(int64_t time)
129 {
130     bundleUninstallStart_ = (time > 0) ? time : 0;
131 }
132 
GetBundleUninstallEndTime() const133 int64_t PerfProfile::GetBundleUninstallEndTime() const
134 {
135     return bundleUninstallEnd_;
136 }
137 
SetBundleUninstallEndTime(int64_t time)138 void PerfProfile::SetBundleUninstallEndTime(int64_t time)
139 {
140     bundleUninstallEnd_ = (time > 0 && time > bundleUninstallStart_) ? time : bundleUninstallStart_;
141 }
142 
GetBundleParseStartTime() const143 int64_t PerfProfile::GetBundleParseStartTime() const
144 {
145     return bundleParseStart_;
146 }
147 
SetBundleParseStartTime(int64_t time)148 void PerfProfile::SetBundleParseStartTime(int64_t time)
149 {
150     bundleParseStart_ = (time > 0) ? time : 0;
151 }
152 
GetBundleParseEndTime() const153 int64_t PerfProfile::GetBundleParseEndTime() const
154 {
155     return bundleParseEnd_;
156 }
157 
SetBundleParseEndTime(int64_t time)158 void PerfProfile::SetBundleParseEndTime(int64_t time)
159 {
160     bundleParseEnd_ = (time > 0 && time > bundleParseStart_) ? time : bundleParseStart_;
161 }
162 
GetAmsLoadStartTime() const163 int64_t PerfProfile::GetAmsLoadStartTime() const
164 {
165     return amsLoadStart_;
166 }
167 
SetAmsLoadStartTime(int64_t time)168 void PerfProfile::SetAmsLoadStartTime(int64_t time)
169 {
170     amsLoadStart_ = (time > 0) ? time : 0;
171 }
172 
GetAmsLoadEndTime() const173 int64_t PerfProfile::GetAmsLoadEndTime() const
174 {
175     return amsLoadEnd_;
176 }
177 
SetAmsLoadEndTime(int64_t time)178 void PerfProfile::SetAmsLoadEndTime(int64_t time)
179 {
180     amsLoadEnd_ = (time > 0 && time > amsLoadStart_) ? time : amsLoadStart_;
181 }
182 
GetAbilityLoadStartTime() const183 int64_t PerfProfile::GetAbilityLoadStartTime() const
184 {
185     return abilityLoadStart_;
186 }
187 
SetAbilityLoadStartTime(int64_t time)188 void PerfProfile::SetAbilityLoadStartTime(int64_t time)
189 {
190     abilityLoadStart_ = (time > 0) ? time : 0;
191 }
192 
GetAbilityLoadEndTime() const193 int64_t PerfProfile::GetAbilityLoadEndTime() const
194 {
195     return abilityLoadEnd_;
196 }
197 
SetAbilityLoadEndTime(int64_t time)198 void PerfProfile::SetAbilityLoadEndTime(int64_t time)
199 {
200     abilityLoadEnd_ = (time > 0 && time > abilityLoadStart_) ? time : abilityLoadStart_;
201 }
202 
GetAppForkStartTime() const203 int64_t PerfProfile::GetAppForkStartTime() const
204 {
205     return appForkStart_;
206 }
207 
SetAppForkStartTime(int64_t time)208 void PerfProfile::SetAppForkStartTime(int64_t time)
209 {
210     appForkStart_ = (time > 0) ? time : 0;
211 }
212 
GetAppForkEndTime() const213 int64_t PerfProfile::GetAppForkEndTime() const
214 {
215     return appForkEnd_;
216 }
217 
SetAppForkEndTime(int64_t time)218 void PerfProfile::SetAppForkEndTime(int64_t time)
219 {
220     appForkEnd_ = (time > 0 && time > appForkStart_) ? time : appForkStart_;
221 }
222 
GetPerfProfileEnabled() const223 bool PerfProfile::GetPerfProfileEnabled() const
224 {
225     return enableProfile_;
226 }
227 
SetPerfProfileEnabled(bool enable)228 void PerfProfile::SetPerfProfileEnabled(bool enable)
229 {
230     enableProfile_ = enable;
231 }
232 
Reset()233 void PerfProfile::Reset()
234 {
235     bmsLoadStart_ = 0;
236     bmsLoadEnd_ = 0;
237 
238     bundleScanStart_ = 0;
239     bundleScanEnd_ = 0;
240 
241     bundleDownloadStart_ = 0;
242     bundleDownloadEnd_ = 0;
243 
244     bundleInstallStart_ = 0;
245     bundleInstallEnd_ = 0;
246     bundleInstallTime_ = 0;
247 
248     bundleUninstallStart_ = 0;
249     bundleUninstallEnd_ = 0;
250 
251     bundleParseStart_ = 0;
252     bundleParseEnd_ = 0;
253 
254     amsLoadStart_ = 0;
255     amsLoadEnd_ = 0;
256 
257     abilityLoadStart_ = 0;
258     abilityLoadEnd_ = 0;
259     appForkStart_ = 0;
260     appForkEnd_ = 0;
261 
262     enableProfile_ = true;
263 }
264 
Dump() const265 void PerfProfile::Dump() const
266 {
267     if (!enableProfile_) {
268         APP_LOGI("profile disabled!\n");
269         return;
270     }
271 
272     // only dump the valid perf time
273     if ((bundleScanEnd_ > bundleScanStart_) || (bundleInstallTime_ > 0) ||
274         (bundleUninstallEnd_ > bundleUninstallStart_) || (bundleParseEnd_ > bundleParseStart_) ||
275         (abilityLoadEnd_ > abilityLoadStart_) || (bmsLoadEnd_ > bmsLoadStart_) || (amsLoadEnd_ > amsLoadStart_)) {
276         APP_LOGD("start\n");
277         if (bmsLoadEnd_ > bmsLoadStart_) {
278             APP_LOGD("BmsLoadTime: %{public}" PRId64 "(ms) \n", (bmsLoadEnd_ - bmsLoadStart_));
279         }
280         if (bundleScanEnd_ > bundleScanStart_) {
281             APP_LOGD("BundleScanTime: %{public}" PRId64 "(ms) \n", (bundleScanEnd_ - bundleScanStart_));
282         }
283         if (bundleInstallTime_ > 0) {
284             APP_LOGD("BundleInstallTime: %{public}" PRId64 "(ms) \n", bundleInstallTime_);
285         }
286         if (bundleUninstallEnd_ > bundleUninstallStart_) {
287             APP_LOGD("BundleUninstallTime: %{public}" PRId64 "(ms) \n", (bundleUninstallEnd_ - bundleUninstallStart_));
288         }
289         if (bundleParseEnd_ > bundleParseStart_) {
290             APP_LOGD("BundleParseTime: %{public}" PRId64 "(ms) \n", (bundleParseEnd_ - bundleParseStart_));
291         }
292         if (amsLoadEnd_ > amsLoadStart_) {
293             APP_LOGD("AmsLoadTime: %{public}" PRId64 "(ms) \n", (amsLoadEnd_ - amsLoadStart_));
294         }
295         if (abilityLoadEnd_ > abilityLoadStart_) {
296             APP_LOGD("AbilityLoadTime: %{public}" PRId64 "(ms) \n", (abilityLoadEnd_ - abilityLoadStart_));
297             APP_LOGD("AppForkTime: %{public}" PRId64 "(ms) \n", (appForkEnd_ - appForkStart_));
298         }
299         APP_LOGD("end\n");
300     }
301 }
302 }  // namespace AppExecFwk
303 }  // namespace OHOS
304