1 /*
2 * Copyright (c) 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 "event_period_info_util.h"
16
17 #include "hiview_logger.h"
18 #include "parameter_ex.h"
19 #include "running_status_logger.h"
20 #include "string_util.h"
21 #include "sys_event.h"
22 #include "time_util.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_TAG("EventPeriodInfoUtil");
27 namespace {
28 constexpr char SOURCE_PERIOD_CNT_ITEM_CONCATE[] = " ";
29 constexpr size_t SOURCE_PERIOD_INFO_ITEM_CNT = 3;
30
LogEventPeriodInfo(std::shared_ptr<EventPeriodInfo> info)31 void LogEventPeriodInfo(std::shared_ptr<EventPeriodInfo> info)
32 {
33 if (info == nullptr) {
34 HIVIEW_LOGE("info is null");
35 return;
36 }
37
38 std::string logInfo;
39 // append period
40 logInfo.append("period=[").append(info->timeStamp).append("]; ");
41 // append num of the event which is need to be stored;
42 logInfo.append("need_store_event_num=[").append(std::to_string(info->preserveCnt)).append("]; ");
43 // append num of the event which is need to be exported;
44 logInfo.append("need_export_event_num=[").append(std::to_string(info->exportCnt)).append("]");
45 RunningStatusLogger::GetInstance().LogEventCountStatisticInfo(logInfo);
46 }
47 }
48
Init(HiviewContext * context)49 void EventPeriodInfoUtil::Init(HiviewContext* context)
50 {
51 if (!Parameter::IsBetaVersion() || context == nullptr) {
52 return;
53 }
54
55 periodFileOpt_ = std::make_unique<PeriodInfoFileOperator>(context, "event_source_period_count");
56 periodFileOpt_->ReadPeriodInfoFromFile(SOURCE_PERIOD_INFO_ITEM_CNT,
57 [this] (const std::vector<std::string>& infoDetails) {
58 uint64_t preserveCnt = 0;
59 StringUtil::ConvertStringTo(infoDetails[1], preserveCnt); // 1 is the index of preserve count
60 uint64_t exportCnt = 0;
61 StringUtil::ConvertStringTo(infoDetails[2], exportCnt); // 2 is the index of export count
62 periodInfoList_.emplace_back(
63 std::make_shared<EventPeriodInfo>(infoDetails[0], preserveCnt, exportCnt));
64 });
65 }
66
UpdatePeriodInfo(const std::shared_ptr<SysEvent> event)67 void EventPeriodInfoUtil::UpdatePeriodInfo(const std::shared_ptr<SysEvent> event)
68 {
69 if (!Parameter::IsBetaVersion()) {
70 return;
71 }
72
73 // set current period
74 auto curTimeStamp = TimeUtil::TimestampFormatToDate(TimeUtil::GetSeconds(), "%Y%m%d%H");
75 EventPeriodSeqInfo eventPeriodSeqInfo;
76 eventPeriodSeqInfo.timeStamp = curTimeStamp;
77 std::shared_ptr<EventPeriodInfo> recentPeriodInfo = periodInfoList_.empty() ? nullptr : periodInfoList_.back();
78 if (recentPeriodInfo != nullptr && recentPeriodInfo->timeStamp == curTimeStamp) {
79 eventPeriodSeqInfo.periodSeq = ++periodSeq_;
80 if (event->collect_) {
81 ++(recentPeriodInfo->exportCnt);
82 eventPeriodSeqInfo.isNeedExport = true;
83 }
84 if (event->preserve_) {
85 ++(recentPeriodInfo->preserveCnt);
86 }
87 event->SetEventPeriodSeqInfo(eventPeriodSeqInfo);
88 RecordEventPeriodInfo();
89 return;
90 }
91
92 // clear and log historical info
93 ClearPeriodInfoList();
94
95 // set next period
96 periodSeq_ = DEFAULT_PERIOD_SEQ;
97 eventPeriodSeqInfo.isNeedExport = event->collect_;
98 eventPeriodSeqInfo.periodSeq = periodSeq_;
99 event->SetEventPeriodSeqInfo(eventPeriodSeqInfo);
100 recentPeriodInfo = std::make_shared<EventPeriodInfo>(
101 curTimeStamp, static_cast<uint64_t>(event->preserve_), static_cast<uint64_t>(event->collect_));
102 periodInfoList_.emplace_back(recentPeriodInfo);
103 RecordEventPeriodInfo();
104 }
105
ClearPeriodInfoList()106 void EventPeriodInfoUtil::ClearPeriodInfoList()
107 {
108 while (!periodInfoList_.empty()) {
109 auto infoItem = periodInfoList_.front();
110 LogEventPeriodInfo(infoItem);
111 periodInfoList_.pop_front();
112 }
113 }
114
RecordEventPeriodInfo()115 void EventPeriodInfoUtil::RecordEventPeriodInfo()
116 {
117 if (periodFileOpt_ == nullptr) {
118 return;
119 }
120
121 periodFileOpt_->WritePeriodInfoToFile([this] () {
122 std::string periodInfoContent;
123 for (const auto& periodInfo : periodInfoList_) {
124 if (periodInfo == nullptr) {
125 HIVIEW_LOGE("event source info item is null");
126 continue;
127 }
128 periodInfoContent.append(periodInfo->timeStamp).append(SOURCE_PERIOD_CNT_ITEM_CONCATE);
129 periodInfoContent.append(std::to_string(periodInfo->preserveCnt)).append(SOURCE_PERIOD_CNT_ITEM_CONCATE);
130 periodInfoContent.append(std::to_string(periodInfo->exportCnt)).append("\n");
131 }
132 return periodInfoContent;
133 });
134 }
135 } // namespace HiviewDFX
136 } // namespace OHOS
137