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