1 /*
2 * Copyright (c) 2022-2025 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_util.h"
22 #include "usage_event_common.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_TAG("SysUsageEventFactory");
27 namespace {
28 constexpr uint64_t TIME_ERROR = 10;
29 }
30 using namespace SysUsageEventSpace;
31
Create()32 std::unique_ptr<LoggerEvent> SysUsageEventFactory::Create()
33 {
34 std::unique_ptr<LoggerEvent> event =
35 std::make_unique<SysUsageEvent>(EVENT_NAME, HiSysEvent::STATISTIC);
36 event->Update(KEY_OF_START, DEFAULT_UINT64);
37 event->Update(KEY_OF_END, TimeUtil::GetMilliseconds());
38
39 // correct the time error caused by interface call
40 auto monotonicTime = TimeUtil::GetMonotonicTimeMs();
41 auto bootTime = TimeUtil::GetBootTimeMs();
42 if (bootTime < (monotonicTime + TIME_ERROR)) {
43 bootTime = monotonicTime;
44 }
45 HIVIEW_LOGI("get monotonicTime=%{public}" PRIu64 ", bootTime=%{public}" PRIu64, monotonicTime, bootTime);
46 event->Update(KEY_OF_POWER, bootTime);
47 event->Update(KEY_OF_RUNNING, monotonicTime);
48 return event;
49 }
50 } // namespace HiviewDFX
51 } // namespace OHOS
52