1020a203aSopenharmony_ci/*
2020a203aSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3020a203aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4020a203aSopenharmony_ci * you may not use this file except in compliance with the License.
5020a203aSopenharmony_ci * You may obtain a copy of the License at
6020a203aSopenharmony_ci *
7020a203aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8020a203aSopenharmony_ci *
9020a203aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10020a203aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11020a203aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12020a203aSopenharmony_ci * See the License for the specific language governing permissions and
13020a203aSopenharmony_ci * limitations under the License.
14020a203aSopenharmony_ci */
15020a203aSopenharmony_ci
16020a203aSopenharmony_ci#include "sysevent_source.h"
17020a203aSopenharmony_ci
18020a203aSopenharmony_ci#include <functional>
19020a203aSopenharmony_ci#include <memory>
20020a203aSopenharmony_ci
21020a203aSopenharmony_ci#include "daily_controller.h"
22020a203aSopenharmony_ci#include "decoded/decoded_event.h"
23020a203aSopenharmony_ci#include "defines.h"
24020a203aSopenharmony_ci#include "raw_data_base_def.h"
25020a203aSopenharmony_ci#include "file_util.h"
26020a203aSopenharmony_ci#include "focused_event_util.h"
27020a203aSopenharmony_ci#include "hiview_config_util.h"
28020a203aSopenharmony_ci#include "hiview_logger.h"
29020a203aSopenharmony_ci#include "plugin_factory.h"
30020a203aSopenharmony_ci#include "time_util.h"
31020a203aSopenharmony_ci#include "sys_event.h"
32020a203aSopenharmony_ci#include "hiview_platform.h"
33020a203aSopenharmony_ci#include "param_const_common.h"
34020a203aSopenharmony_ci#include "parameter.h"
35020a203aSopenharmony_ci#include "sys_event_dao.h"
36020a203aSopenharmony_ci#include "sys_event_service_adapter.h"
37020a203aSopenharmony_ci
38020a203aSopenharmony_cinamespace OHOS {
39020a203aSopenharmony_cinamespace HiviewDFX {
40020a203aSopenharmony_ciREGISTER(SysEventSource);
41020a203aSopenharmony_cinamespace {
42020a203aSopenharmony_ciDEFINE_LOG_TAG("HiView-SysEventSource");
43020a203aSopenharmony_ciconstexpr char DEF_FILE_NAME[] = "hisysevent.def";
44020a203aSopenharmony_ciconstexpr char DEF_ZIP_NAME[] = "hisysevent.zip";
45020a203aSopenharmony_ciconstexpr char DEF_CFG_DIR[] = "sys_event_def";
46020a203aSopenharmony_ciconstexpr char TEST_TYPE_PARAM_KEY[] = "hiviewdfx.hiview.testtype";
47020a203aSopenharmony_ciconstexpr char TEST_TYPE_KEY[] = "test_type_";
48020a203aSopenharmony_ci
49020a203aSopenharmony_ciuint64_t GenerateHash(std::shared_ptr<SysEvent> event)
50020a203aSopenharmony_ci{
51020a203aSopenharmony_ci    if (event == nullptr) {
52020a203aSopenharmony_ci        return 0;
53020a203aSopenharmony_ci    }
54020a203aSopenharmony_ci    constexpr size_t infoLenLimit = 256;
55020a203aSopenharmony_ci    size_t infoLen = event->rawData_->GetDataLength();
56020a203aSopenharmony_ci    size_t hashLen = (infoLen < infoLenLimit) ? infoLen : infoLenLimit;
57020a203aSopenharmony_ci    const uint8_t* p = event->rawData_->GetData();
58020a203aSopenharmony_ci    uint64_t ret { 0xCBF29CE484222325ULL }; // basis value
59020a203aSopenharmony_ci    size_t i = 0;
60020a203aSopenharmony_ci    while (i < hashLen) {
61020a203aSopenharmony_ci        ret ^= *(p + i);
62020a203aSopenharmony_ci        ret *= 0x100000001B3ULL; // prime value
63020a203aSopenharmony_ci        i++;
64020a203aSopenharmony_ci    }
65020a203aSopenharmony_ci    return ret;
66020a203aSopenharmony_ci}
67020a203aSopenharmony_ci
68020a203aSopenharmony_civoid ParameterWatchCallback(const char* key, const char* value, void* context)
69020a203aSopenharmony_ci{
70020a203aSopenharmony_ci    if (context == nullptr) {
71020a203aSopenharmony_ci        HIVIEW_LOGE("context is null");
72020a203aSopenharmony_ci        return;
73020a203aSopenharmony_ci    }
74020a203aSopenharmony_ci    auto eventSourcePlugin = reinterpret_cast<SysEventSource*>(context);
75020a203aSopenharmony_ci    if (eventSourcePlugin == nullptr) {
76020a203aSopenharmony_ci        HIVIEW_LOGE("eventsource plugin is null");
77020a203aSopenharmony_ci        return;
78020a203aSopenharmony_ci    }
79020a203aSopenharmony_ci    size_t testTypeStrMaxLen = 256;
80020a203aSopenharmony_ci    std::string testTypeStr(value);
81020a203aSopenharmony_ci    if (testTypeStr.size() > testTypeStrMaxLen) {
82020a203aSopenharmony_ci        HIVIEW_LOGE("length of the test type string set exceeds the limit");
83020a203aSopenharmony_ci        return;
84020a203aSopenharmony_ci    }
85020a203aSopenharmony_ci    HIVIEW_LOGI("test_type is set to be \"%{public}s\"", testTypeStr.c_str());
86020a203aSopenharmony_ci    eventSourcePlugin->UpdateTestType(testTypeStr);
87020a203aSopenharmony_ci}
88020a203aSopenharmony_ci
89020a203aSopenharmony_civoid CheckIfEventDelayed(std::shared_ptr<SysEvent> event)
90020a203aSopenharmony_ci{
91020a203aSopenharmony_ci    // only delay detection is performed on fault events
92020a203aSopenharmony_ci    if (event->GetEventType() != SysEventCreator::EventType::FAULT) {
93020a203aSopenharmony_ci        return;
94020a203aSopenharmony_ci    }
95020a203aSopenharmony_ci
96020a203aSopenharmony_ci    uint64_t happenTime = event->happenTime_;
97020a203aSopenharmony_ci    uint64_t createTime = event->createTime_ / 1000; // 1000: us->ms
98020a203aSopenharmony_ci    if (createTime < happenTime) { // for the time jump scene
99020a203aSopenharmony_ci        return;
100020a203aSopenharmony_ci    }
101020a203aSopenharmony_ci    constexpr uint64_t delayDetectLimit = 5 * 1000; // 5s
102020a203aSopenharmony_ci    if (uint64_t delayTime = createTime - happenTime; delayTime > delayDetectLimit) {
103020a203aSopenharmony_ci        HIVIEW_LOGI("event[%{public}s|%{public}s|%{public}" PRIu64 "] delayed by %{public}" PRIu64 "ms",
104020a203aSopenharmony_ci            event->domain_.c_str(), event->eventName_.c_str(), happenTime, delayTime);
105020a203aSopenharmony_ci    }
106020a203aSopenharmony_ci}
107020a203aSopenharmony_ci}
108020a203aSopenharmony_ci
109020a203aSopenharmony_civoid SysEventReceiver::HandlerEvent(std::shared_ptr<EventRaw::RawData> rawData)
110020a203aSopenharmony_ci{
111020a203aSopenharmony_ci    if (rawData == nullptr || rawData->GetData() == nullptr) {
112020a203aSopenharmony_ci        HIVIEW_LOGW("raw data of sys event is null");
113020a203aSopenharmony_ci        return;
114020a203aSopenharmony_ci    }
115020a203aSopenharmony_ci    std::shared_ptr<PipelineEvent> event = std::make_shared<SysEvent>("SysEventSource",
116020a203aSopenharmony_ci        static_cast<PipelineEventProducer*>(&eventSource), rawData);
117020a203aSopenharmony_ci    if (eventSource.CheckEvent(event)) {
118020a203aSopenharmony_ci        eventSource.PublishPipelineEvent(event);
119020a203aSopenharmony_ci    }
120020a203aSopenharmony_ci}
121020a203aSopenharmony_ci
122020a203aSopenharmony_civoid SysEventSource::OnLoad()
123020a203aSopenharmony_ci{
124020a203aSopenharmony_ci    HIVIEW_LOGI("SysEventSource load ");
125020a203aSopenharmony_ci    std::shared_ptr<EventLoop> looper = GetHiviewContext()->GetSharedWorkLoop();
126020a203aSopenharmony_ci    platformMonitor_.StartMonitor(looper);
127020a203aSopenharmony_ci
128020a203aSopenharmony_ci    sysEventStat_ = std::make_unique<SysEventStat>();
129020a203aSopenharmony_ci    InitController();
130020a203aSopenharmony_ci
131020a203aSopenharmony_ci    // start sys event service
132020a203aSopenharmony_ci    auto notifyFunc = [&] (std::shared_ptr<Event> event) -> void {
133020a203aSopenharmony_ci        this->GetHiviewContext()->PostUnorderedEvent(shared_from_this(), event);
134020a203aSopenharmony_ci    };
135020a203aSopenharmony_ci    SysEventServiceAdapter::StartService(this, notifyFunc);
136020a203aSopenharmony_ci    SysEventServiceAdapter::SetWorkLoop(looper);
137020a203aSopenharmony_ci
138020a203aSopenharmony_ci    auto defFilePath = HiViewConfigUtil::GetConfigFilePath(DEF_ZIP_NAME, DEF_CFG_DIR, DEF_FILE_NAME);
139020a203aSopenharmony_ci    HIVIEW_LOGI("init json parser with %{public}s", defFilePath.c_str());
140020a203aSopenharmony_ci    sysEventParser_ = std::make_shared<EventJsonParser>(defFilePath);
141020a203aSopenharmony_ci
142020a203aSopenharmony_ci    SysEventServiceAdapter::BindGetTagFunc(
143020a203aSopenharmony_ci        [this] (const std::string& domain, const std::string& name) {
144020a203aSopenharmony_ci            return this->sysEventParser_->GetTagByDomainAndName(domain, name);
145020a203aSopenharmony_ci        });
146020a203aSopenharmony_ci    SysEventServiceAdapter::BindGetTypeFunc(
147020a203aSopenharmony_ci        [this] (const std::string& domain, const std::string& name) {
148020a203aSopenharmony_ci            return this->sysEventParser_->GetTypeByDomainAndName(domain, name);
149020a203aSopenharmony_ci        });
150020a203aSopenharmony_ci
151020a203aSopenharmony_ci    // watch parameter
152020a203aSopenharmony_ci    if (WatchParameter(TEST_TYPE_PARAM_KEY, ParameterWatchCallback, this) != 0) {
153020a203aSopenharmony_ci        HIVIEW_LOGW("failed to watch the change of parameter %{public}s", TEST_TYPE_PARAM_KEY);
154020a203aSopenharmony_ci    }
155020a203aSopenharmony_ci}
156020a203aSopenharmony_ci
157020a203aSopenharmony_civoid SysEventSource::InitController()
158020a203aSopenharmony_ci{
159020a203aSopenharmony_ci    auto context = GetHiviewContext();
160020a203aSopenharmony_ci    if (context == nullptr) {
161020a203aSopenharmony_ci        HIVIEW_LOGW("context is null");
162020a203aSopenharmony_ci        return;
163020a203aSopenharmony_ci    }
164020a203aSopenharmony_ci
165020a203aSopenharmony_ci    std::string workPath = context->GetHiViewDirectory(HiviewContext::DirectoryType::WORK_DIRECTORY);
166020a203aSopenharmony_ci    std::string configPath = context->GetHiViewDirectory(HiviewContext::DirectoryType::CONFIG_DIRECTORY);
167020a203aSopenharmony_ci    const std::string configFileName = "event_threshold.json";
168020a203aSopenharmony_ci    controller_ = std::make_unique<DailyController>(workPath, configPath.append(configFileName));
169020a203aSopenharmony_ci}
170020a203aSopenharmony_ci
171020a203aSopenharmony_civoid SysEventSource::OnUnload()
172020a203aSopenharmony_ci{
173020a203aSopenharmony_ci    eventServer_.Stop();
174020a203aSopenharmony_ci    HIVIEW_LOGI("SysEventSource unload");
175020a203aSopenharmony_ci}
176020a203aSopenharmony_ci
177020a203aSopenharmony_civoid SysEventSource::StartEventSource()
178020a203aSopenharmony_ci{
179020a203aSopenharmony_ci    HIVIEW_LOGI("SysEventSource start");
180020a203aSopenharmony_ci    std::shared_ptr<EventReceiver> sysEventReceiver = std::make_shared<SysEventReceiver>(*this);
181020a203aSopenharmony_ci    eventServer_.AddReceiver(sysEventReceiver);
182020a203aSopenharmony_ci    eventServer_.Start();
183020a203aSopenharmony_ci}
184020a203aSopenharmony_ci
185020a203aSopenharmony_civoid SysEventSource::Recycle(PipelineEvent *event)
186020a203aSopenharmony_ci{
187020a203aSopenharmony_ci    platformMonitor_.CollectCostTime(event);
188020a203aSopenharmony_ci}
189020a203aSopenharmony_ci
190020a203aSopenharmony_civoid SysEventSource::PauseDispatch(std::weak_ptr<Plugin> plugin)
191020a203aSopenharmony_ci{
192020a203aSopenharmony_ci    auto requester = plugin.lock();
193020a203aSopenharmony_ci    if (requester != nullptr) {
194020a203aSopenharmony_ci        HIVIEW_LOGI("process pause dispatch event from plugin:%s.\n", requester->GetName().c_str());
195020a203aSopenharmony_ci    }
196020a203aSopenharmony_ci}
197020a203aSopenharmony_ci
198020a203aSopenharmony_cibool SysEventSource::PublishPipelineEvent(std::shared_ptr<PipelineEvent> event)
199020a203aSopenharmony_ci{
200020a203aSopenharmony_ci    platformMonitor_.CollectEvent(event);
201020a203aSopenharmony_ci    platformMonitor_.Breaking();
202020a203aSopenharmony_ci    auto context = GetHiviewContext();
203020a203aSopenharmony_ci    HiviewPlatform* hiviewPlatform = static_cast<HiviewPlatform*>(context);
204020a203aSopenharmony_ci    if (hiviewPlatform == nullptr) {
205020a203aSopenharmony_ci        HIVIEW_LOGW("hiviewPlatform is null");
206020a203aSopenharmony_ci        return false;
207020a203aSopenharmony_ci    }
208020a203aSopenharmony_ci    auto const &pipelineRules = hiviewPlatform->GetPipelineConfigMap();
209020a203aSopenharmony_ci    auto const &pipelineMap = hiviewPlatform->GetPipelineMap();
210020a203aSopenharmony_ci    for (auto it = pipelineRules.begin(); it != pipelineRules.end(); it++) {
211020a203aSopenharmony_ci        std::string pipelineName = it->first;
212020a203aSopenharmony_ci        auto dispathRule = it->second;
213020a203aSopenharmony_ci        if (dispathRule->FindEvent(event->domain_, event->eventName_)) {
214020a203aSopenharmony_ci            pipelineMap.at(pipelineName)->ProcessEvent(event);
215020a203aSopenharmony_ci            return true;
216020a203aSopenharmony_ci        }
217020a203aSopenharmony_ci    }
218020a203aSopenharmony_ci    pipelineMap.at("SysEventPipeline")->ProcessEvent(event);
219020a203aSopenharmony_ci    return true;
220020a203aSopenharmony_ci}
221020a203aSopenharmony_ci
222020a203aSopenharmony_cibool SysEventSource::CheckEvent(std::shared_ptr<Event> event)
223020a203aSopenharmony_ci{
224020a203aSopenharmony_ci    if (isConfigUpdated_) {
225020a203aSopenharmony_ci        auto defFilePath = HiViewConfigUtil::GetConfigFilePath(DEF_ZIP_NAME, DEF_CFG_DIR, DEF_FILE_NAME);
226020a203aSopenharmony_ci        HIVIEW_LOGI("update json parser with %{public}s", defFilePath.c_str());
227020a203aSopenharmony_ci        sysEventParser_->ReadDefFile(defFilePath);
228020a203aSopenharmony_ci        isConfigUpdated_.store(false);
229020a203aSopenharmony_ci    }
230020a203aSopenharmony_ci    std::shared_ptr<SysEvent> sysEvent = Convert2SysEvent(event);
231020a203aSopenharmony_ci    if (sysEvent == nullptr) {
232020a203aSopenharmony_ci        HIVIEW_LOGE("event or event parser is null.");
233020a203aSopenharmony_ci        sysEventStat_->AccumulateEvent(false);
234020a203aSopenharmony_ci        return false;
235020a203aSopenharmony_ci    }
236020a203aSopenharmony_ci    CheckIfEventDelayed(sysEvent);
237020a203aSopenharmony_ci    if (controller_ != nullptr && !controller_->CheckThreshold(sysEvent)) {
238020a203aSopenharmony_ci        sysEventStat_->AccumulateEvent(false);
239020a203aSopenharmony_ci        return false;
240020a203aSopenharmony_ci    }
241020a203aSopenharmony_ci    EventStore::SysEventDao::CheckRepeat(sysEvent);
242020a203aSopenharmony_ci    if (!IsValidSysEvent(sysEvent)) {
243020a203aSopenharmony_ci        sysEventStat_->AccumulateEvent(sysEvent->domain_, sysEvent->eventName_, false);
244020a203aSopenharmony_ci        return false;
245020a203aSopenharmony_ci    }
246020a203aSopenharmony_ci    if (FocusedEventUtil::IsFocusedEvent(sysEvent->domain_, sysEvent->eventName_)) {
247020a203aSopenharmony_ci        HIVIEW_LOGI("event[%{public}s|%{public}s|%{public}" PRIu64 "] is valid.",
248020a203aSopenharmony_ci            sysEvent->domain_.c_str(), sysEvent->eventName_.c_str(), event->happenTime_);
249020a203aSopenharmony_ci    } else {
250020a203aSopenharmony_ci        HIVIEW_LOGD("event[%{public}s|%{public}s|%{public}" PRIu64 "] is valid.",
251020a203aSopenharmony_ci            sysEvent->domain_.c_str(), sysEvent->eventName_.c_str(), event->happenTime_);
252020a203aSopenharmony_ci    }
253020a203aSopenharmony_ci    sysEventStat_->AccumulateEvent();
254020a203aSopenharmony_ci    return true;
255020a203aSopenharmony_ci}
256020a203aSopenharmony_ci
257020a203aSopenharmony_cistd::shared_ptr<SysEvent> SysEventSource::Convert2SysEvent(std::shared_ptr<Event>& event)
258020a203aSopenharmony_ci{
259020a203aSopenharmony_ci    if (event == nullptr) {
260020a203aSopenharmony_ci        HIVIEW_LOGE("event is null");
261020a203aSopenharmony_ci        return nullptr;
262020a203aSopenharmony_ci    }
263020a203aSopenharmony_ci    if (event->messageType_ != Event::MessageType::SYS_EVENT) {
264020a203aSopenharmony_ci        HIVIEW_LOGE("receive out of sys event type");
265020a203aSopenharmony_ci        return nullptr;
266020a203aSopenharmony_ci    }
267020a203aSopenharmony_ci    return Event::DownCastTo<SysEvent>(event);
268020a203aSopenharmony_ci}
269020a203aSopenharmony_ci
270020a203aSopenharmony_cistatic void ShowUsage(int fd, const std::vector<std::string>& cmds)
271020a203aSopenharmony_ci{
272020a203aSopenharmony_ci    dprintf(fd, "invalid cmd:");
273020a203aSopenharmony_ci    for (auto it = cmds.begin(); it != cmds.end(); it++) {
274020a203aSopenharmony_ci        dprintf(fd, "%s ", it->c_str());
275020a203aSopenharmony_ci    }
276020a203aSopenharmony_ci    dprintf(fd, "\n");
277020a203aSopenharmony_ci    dprintf(fd, "usage: SysEventService [sum|detail|invalid|clear]\n");
278020a203aSopenharmony_ci}
279020a203aSopenharmony_ci
280020a203aSopenharmony_civoid SysEventSource::Dump(int fd, const std::vector<std::string>& cmds)
281020a203aSopenharmony_ci{
282020a203aSopenharmony_ci    if (cmds.size() >= 2) { // 2: args from the second item
283020a203aSopenharmony_ci        std::string arg1 = cmds[1];
284020a203aSopenharmony_ci        if (arg1 == "sum") {
285020a203aSopenharmony_ci            sysEventStat_->StatSummary(fd);
286020a203aSopenharmony_ci        } else if (arg1 == "detail") {
287020a203aSopenharmony_ci            sysEventStat_->StatDetail(fd);
288020a203aSopenharmony_ci        } else if (arg1 == "invalid") {
289020a203aSopenharmony_ci            sysEventStat_->StatInvalidDetail(fd);
290020a203aSopenharmony_ci        } else if (arg1 == "clear") {
291020a203aSopenharmony_ci            sysEventStat_->Clear(fd);
292020a203aSopenharmony_ci        } else {
293020a203aSopenharmony_ci            ShowUsage(fd, cmds);
294020a203aSopenharmony_ci        }
295020a203aSopenharmony_ci    } else {
296020a203aSopenharmony_ci        sysEventStat_->StatSummary(fd);
297020a203aSopenharmony_ci    }
298020a203aSopenharmony_ci}
299020a203aSopenharmony_ci
300020a203aSopenharmony_civoid SysEventSource::OnConfigUpdate(const std::string& localCfgPath, const std::string& cloudCfgPath)
301020a203aSopenharmony_ci{
302020a203aSopenharmony_ci    this->isConfigUpdated_.store(true);
303020a203aSopenharmony_ci}
304020a203aSopenharmony_ci
305020a203aSopenharmony_cibool SysEventSource::IsValidSysEvent(const std::shared_ptr<SysEvent> event)
306020a203aSopenharmony_ci{
307020a203aSopenharmony_ci    if (event->domain_.empty() || event->eventName_.empty()) {
308020a203aSopenharmony_ci        HIVIEW_LOGW("domain=%{public}s or name=%{public}s is empty.",
309020a203aSopenharmony_ci            event->domain_.c_str(), event->eventName_.c_str());
310020a203aSopenharmony_ci        return false;
311020a203aSopenharmony_ci    }
312020a203aSopenharmony_ci    auto baseInfo = sysEventParser_->GetDefinedBaseInfoByDomainName(event->domain_, event->eventName_);
313020a203aSopenharmony_ci    if (baseInfo.type == INVALID_EVENT_TYPE) {
314020a203aSopenharmony_ci        HIVIEW_LOGW("type defined for event[%{public}s|%{public}s|%{public}" PRIu64 "] is invalid.",
315020a203aSopenharmony_ci            event->domain_.c_str(), event->eventName_.c_str(), event->happenTime_);
316020a203aSopenharmony_ci        return false;
317020a203aSopenharmony_ci    }
318020a203aSopenharmony_ci    if (event->GetEventType() != baseInfo.type) {
319020a203aSopenharmony_ci        HIVIEW_LOGW("type=%{public}d of event[%{public}s|%{public}s|%{public}" PRIu64 "] is invalid.",
320020a203aSopenharmony_ci            event->GetEventType(), event->domain_.c_str(), event->eventName_.c_str(), event->happenTime_);
321020a203aSopenharmony_ci        return false;
322020a203aSopenharmony_ci    }
323020a203aSopenharmony_ci    // append id
324020a203aSopenharmony_ci    auto eventId = GenerateHash(event);
325020a203aSopenharmony_ci    if (IsDuplicateEvent(eventId)) {
326020a203aSopenharmony_ci        HIVIEW_LOGW("ignore duplicate event[%{public}s|%{public}s|%{public}" PRIu64 "].",
327020a203aSopenharmony_ci            event->domain_.c_str(), event->eventName_.c_str(), eventId);
328020a203aSopenharmony_ci        return false;
329020a203aSopenharmony_ci    }
330020a203aSopenharmony_ci    DecorateSysEvent(event, baseInfo, eventId);
331020a203aSopenharmony_ci    return true;
332020a203aSopenharmony_ci}
333020a203aSopenharmony_ci
334020a203aSopenharmony_civoid SysEventSource::UpdateTestType(const std::string& testType)
335020a203aSopenharmony_ci{
336020a203aSopenharmony_ci    testType_ = testType;
337020a203aSopenharmony_ci}
338020a203aSopenharmony_ci
339020a203aSopenharmony_civoid SysEventSource::DecorateSysEvent(const std::shared_ptr<SysEvent> event, const BaseInfo& baseInfo, uint64_t id)
340020a203aSopenharmony_ci{
341020a203aSopenharmony_ci    if (!baseInfo.level.empty()) {
342020a203aSopenharmony_ci        event->SetLevel(baseInfo.level);
343020a203aSopenharmony_ci    }
344020a203aSopenharmony_ci    if (!baseInfo.tag.empty()) {
345020a203aSopenharmony_ci        event->SetTag(baseInfo.tag);
346020a203aSopenharmony_ci    }
347020a203aSopenharmony_ci    event->SetPrivacy(baseInfo.privacy);
348020a203aSopenharmony_ci    if (!testType_.empty()) {
349020a203aSopenharmony_ci        event->SetEventValue(TEST_TYPE_KEY, testType_);
350020a203aSopenharmony_ci    }
351020a203aSopenharmony_ci    event->preserve_ = baseInfo.preserve;
352020a203aSopenharmony_ci    // add hashcode id
353020a203aSopenharmony_ci    event->SetId(id);
354020a203aSopenharmony_ci}
355020a203aSopenharmony_ci
356020a203aSopenharmony_cibool SysEventSource::IsDuplicateEvent(const uint64_t eventId)
357020a203aSopenharmony_ci{
358020a203aSopenharmony_ci    for (auto iter = eventIdList_.begin(); iter != eventIdList_.end(); iter++) {
359020a203aSopenharmony_ci        if (*iter == eventId) {
360020a203aSopenharmony_ci            return true;
361020a203aSopenharmony_ci        }
362020a203aSopenharmony_ci    }
363020a203aSopenharmony_ci    std::list<std::string>::size_type maxSize { 5 }; // size of queue limit to 5
364020a203aSopenharmony_ci    if (eventIdList_.size() >= maxSize) {
365020a203aSopenharmony_ci        eventIdList_.pop_front();
366020a203aSopenharmony_ci    }
367020a203aSopenharmony_ci    eventIdList_.emplace_back(eventId);
368020a203aSopenharmony_ci    return false;
369020a203aSopenharmony_ci}
370020a203aSopenharmony_ci} // namespace HiviewDFX
371020a203aSopenharmony_ci} // namespace OHOS
372