• 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 #include "sys_usage_event_factory.h"
16 
17 #include <cinttypes>
18 
19 #include "battery_stats_client.h"
20 #include "logger.h"
21 #include "sys_usage_event.h"
22 #include "time_service_client.h"
23 #include "time_util.h"
24 #include "usage_event_common.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_TAG("HiView-SysUsageEventFactory");
29 namespace {
30 constexpr uint64_t TIME_ERROR = 10;
31 constexpr uint64_t SEC_TO_MILLISEC = 1000;
32 }
33 using namespace SysUsageEventSpace;
34 
Create()35 std::unique_ptr<LoggerEvent> SysUsageEventFactory::Create()
36 {
37     std::unique_ptr<LoggerEvent> event =
38         std::make_unique<SysUsageEvent>(EVENT_DOMAIN, EVENT_NAME, HiSysEvent::STATISTIC);
39     event->Update(KEY_OF_START, DEFAULT_UINT64);
40     event->Update(KEY_OF_END, TimeUtil::GetMilliseconds());
41 
42     // correct the time error caused by interface call
43     auto monotonicTime = GetMonotonicTime();
44     auto bootTime = GetBootTime();
45     if (bootTime < (monotonicTime + TIME_ERROR)) {
46         bootTime = monotonicTime;
47     }
48 
49     event->Update(KEY_OF_POWER, bootTime);
50     event->Update(KEY_OF_RUNNING, monotonicTime);
51     event->Update(KEY_OF_SCREEN, GetScreenTime());
52     return event;
53 }
54 
GetBootTime()55 uint64_t SysUsageEventFactory::GetBootTime()
56 {
57     auto powerTime = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs();
58     HIVIEW_LOGI("get boot time=%{public}" PRId64, powerTime);
59     if (powerTime < 0) {
60         HIVIEW_LOGE("failed to get boot time");
61         return DEFAULT_UINT64;
62     }
63     return static_cast<uint64_t>(powerTime);
64 }
65 
GetMonotonicTime()66 uint64_t SysUsageEventFactory::GetMonotonicTime()
67 {
68     auto runningTime = MiscServices::TimeServiceClient::GetInstance()->GetMonotonicTimeMs();
69     HIVIEW_LOGI("get runningTime=%{public}" PRId64, runningTime);
70     if (runningTime < 0) {
71         HIVIEW_LOGE("failed to get Monotonic time");
72         return DEFAULT_UINT64;
73     }
74     return static_cast<uint64_t>(runningTime);
75 }
76 
GetScreenTime()77 uint64_t SysUsageEventFactory::GetScreenTime()
78 {
79     auto screenTime = PowerMgr::BatteryStatsClient::GetInstance().GetTotalTimeSecond(
80         PowerMgr::StatsUtils::STATS_TYPE_SCREEN_ON);
81     HIVIEW_LOGI("get screenTime=%{public}" PRId64, screenTime);
82     return screenTime * SEC_TO_MILLISEC;
83 }
84 } // namespace HiviewDFX
85 } // namespace OHOS
86