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 "hiview_event_report.h"
16 
17 #include "hiview_event_common.h"
18 #include "hiview_event_cacher.h"
19 #include "hiview_logger.h"
20 #include "parameter_ex.h"
21 #include "plugin_fault_event_factory.h"
22 #include "plugin_load_event_factory.h"
23 #include "plugin_unload_event_factory.h"
24 #include "time_util.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 DEFINE_LOG_LABEL(LABEL_DOMAIN, "HiView-HiviewEventReport");
ReportPluginLoad(const std::string & name,uint32_t result,uint32_t duration)29 void HiviewEventReport::ReportPluginLoad(const std::string &name, uint32_t result, uint32_t duration)
30 {
31     auto factory = std::make_unique<PluginLoadEventFactory>();
32     auto event = factory->Create();
33     event->Update(PluginEventSpace::KEY_OF_PLUGIN_NAME, name);
34     event->Update(PluginEventSpace::KEY_OF_RESULT, result);
35     event->Update(PluginEventSpace::KEY_OF_DURATION, duration);
36     event->Report();
37     HIVIEW_LOGI("report plugin load event=%{public}s", event->ToJsonString().c_str());
38 }
39 
ReportPluginUnload(const std::string & name,uint32_t result)40 void HiviewEventReport::ReportPluginUnload(const std::string &name, uint32_t result)
41 {
42     auto factory = std::make_unique<PluginUnloadEventFactory>();
43     auto event = factory->Create();
44     event->Update(PluginEventSpace::KEY_OF_PLUGIN_NAME, name);
45     event->Update(PluginEventSpace::KEY_OF_RESULT, result);
46     event->Report();
47     HIVIEW_LOGI("report plugin unload event=%{public}s", event->ToJsonString().c_str());
48 }
49 
ReportPluginFault(const std::string & name,const std::string & reason)50 void HiviewEventReport::ReportPluginFault(const std::string &name, const std::string &reason)
51 {
52     auto factory = std::make_unique<PluginFaultEventFactory>();
53     auto event = factory->Create();
54     event->Update(PluginFaultEventSpace::KEY_OF_PLUGIN_NAME, name);
55     event->Update(PluginFaultEventSpace::KEY_OF_REASON, reason);
56     event->Report();
57     HIVIEW_LOGI("report plugin fault event=%{public}s", event->ToJsonString().c_str());
58 }
59 
ReportPluginStats()60 void HiviewEventReport::ReportPluginStats()
61 {
62     std::vector<std::shared_ptr<LoggerEvent>> events;
63     HiviewEventCacher::GetInstance().GetPluginStatsEvents(events);
64     for (auto event : events) {
65         event->Report();
66         HIVIEW_LOGI("report plugin stat event=%{public}s", event->ToJsonString().c_str());
67     }
68     HiviewEventCacher::GetInstance().ClearPluginStatsEvents();
69 }
70 
UpdatePluginStats(const std::string & name,const std::string & procName,uint32_t procTime)71 void HiviewEventReport::UpdatePluginStats(const std::string &name, const std::string &procName, uint32_t procTime)
72 {
73     HIVIEW_LOGD("UpdatePluginStats pluginName=%{public}s, procName=%{public}s, time=%{public}d",
74         name.c_str(), procName.c_str(), procTime);
75     HiviewEventCacher::GetInstance().UpdatePluginStatsEvent(name, procName, procTime);
76 }
77 
ReportCpuScene(const std::string & sceneId)78 void HiviewEventReport::ReportCpuScene(const std::string &sceneId)
79 {
80     if (!Parameter::IsBetaVersion()) {
81         HIVIEW_LOGD("no need to report cpu scene event");
82         return;
83     }
84     auto ret = HiSysEventWrite(CpuSceneEvent::DOMAIN, "CPU_SCENE_ENTRY", HiSysEvent::BEHAVIOR,
85         "PACKAGE_NAME", "hiview",
86         "SCENE_ID", sceneId,
87         "HAPPEN_TIME", TimeUtil::GetMilliseconds());
88     if (ret != 0) {
89         HIVIEW_LOGW("failed to report cpu scene event, sceneId=%{public}s, ret=%{public}d", sceneId.c_str(), ret);
90     } else {
91         HIVIEW_LOGI("succ to report cpu scene event, sceneId=%{public}s", sceneId.c_str());
92     }
93 }
94 } // namespace HiviewDFX
95 } // namespace OHOS
96