1 /*
2 * Copyright (c) 2022 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
16 #include "socperf_plugin.h"
17 #include "res_type.h"
18 #include "res_sched_log.h"
19 #include "plugin_mgr.h"
20 #include "config_info.h"
21
22 namespace OHOS {
23 namespace ResourceSchedule {
24 using namespace ResType;
25 namespace {
26 const std::string LIB_NAME = "libsocperf_plugin.z.so";
27 const int PERF_REQUEST_CMD_ID_APP_COLD_START_FIRST = 10000;
28 const int PERF_REQUEST_CMD_ID_APP_COLD_START_SECOND = 10001;
29 const int PERF_REQUEST_CMD_ID_APP_WARM_START_FIRST = 10002;
30 const int PERF_REQUEST_CMD_ID_APP_WARM_START_SECOND = 10003;
31 const int PERF_REQUEST_CMD_ID_WINDOW_SWITCH_FIRST = 10004;
32 const int PERF_REQUEST_CMD_ID_WINDOW_SWITCH_SECOND = 10005;
33 const int PERF_REQUEST_CMD_ID_EVENT_CLICK = 10006;
34 const int PERF_REQUEST_CMD_ID_PUSH_PAGE = 10007;
35 const int PERF_REQUEST_CMD_ID_EVENT_SLIDE = 10008;
36 const int PERF_REQUEST_CMD_ID_EVENT_SLIDE_OVER = 10009;
37 const int EVENT_ON = 1;
38 const int EVENT_OFF = 0;
39 const int WINDOW_FOCUSED = 0;
40 }
IMPLEMENT_SINGLE_INSTANCE(SocPerfPlugin)41 IMPLEMENT_SINGLE_INSTANCE(SocPerfPlugin)
42
43 void SocPerfPlugin::Init()
44 {
45 functionMap = {
46 { RES_TYPE_APP_STATE_CHANGE,
47 [this](const std::shared_ptr<ResData>& data) { HandleAppStateChange(data); } },
48 { RES_TYPE_WINDOW_FOCUS,
49 [this](const std::shared_ptr<ResData>& data) { HandleWindowFocus(data); } },
50 { RES_TYPE_CLICK_RECOGNIZE,
51 [this](const std::shared_ptr<ResData>& data) { HandleEventClick(data); } },
52 { RES_TYPE_PUSH_PAGE,
53 [this](const std::shared_ptr<ResData>& data) { HandlePushPage(data); } },
54 { RTS_TYPE_SLIDE_RECOGNIZE,
55 [this](const std::shared_ptr<ResData>& data) { HandleEventSlide(data); } },
56 };
57 resTypes = {
58 RES_TYPE_APP_STATE_CHANGE,
59 RES_TYPE_WINDOW_FOCUS,
60 RES_TYPE_CLICK_RECOGNIZE,
61 RES_TYPE_PUSH_PAGE,
62 RTS_TYPE_SLIDE_RECOGNIZE,
63 };
64 for (auto resType : resTypes) {
65 PluginMgr::GetInstance().SubscribeResource(LIB_NAME, resType);
66 }
67 RESSCHED_LOGI("SocPerfPlugin::Init success");
68 }
69
Disable()70 void SocPerfPlugin::Disable()
71 {
72 functionMap.clear();
73 for (auto resType : resTypes) {
74 PluginMgr::GetInstance().UnSubscribeResource(LIB_NAME, resType);
75 }
76 resTypes.clear();
77 RESSCHED_LOGI("SocPerfPlugin::Disable success");
78 }
79
DispatchResource(const std::shared_ptr<ResData> & data)80 void SocPerfPlugin::DispatchResource(const std::shared_ptr<ResData>& data)
81 {
82 RESSCHED_LOGI("SocPerfPlugin::DispatchResource resType=%{public}u, value=%{public}lld",
83 data->resType, data->value);
84 auto funcIter = functionMap.find(data->resType);
85 if (funcIter != functionMap.end()) {
86 auto function = funcIter->second;
87 if (function) {
88 function(data);
89 }
90 }
91 }
92
HandleAppStateChange(const std::shared_ptr<ResData> & data)93 void SocPerfPlugin::HandleAppStateChange(const std::shared_ptr<ResData>& data)
94 {
95 if (data->value == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_CREATE)) {
96 RESSCHED_LOGI("SocPerfPlugin: socperf->APP_COLD_START");
97 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(PERF_REQUEST_CMD_ID_APP_COLD_START_FIRST, "");
98 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(PERF_REQUEST_CMD_ID_APP_COLD_START_SECOND, "");
99 } else if (data->value == static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND)) {
100 RESSCHED_LOGI("SocPerfPlugin: socperf->APP_WARM_START");
101 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(PERF_REQUEST_CMD_ID_APP_WARM_START_FIRST, "");
102 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(PERF_REQUEST_CMD_ID_APP_WARM_START_SECOND, "");
103 }
104 }
105
HandleWindowFocus(const std::shared_ptr<ResData> & data)106 void SocPerfPlugin::HandleWindowFocus(const std::shared_ptr<ResData>& data)
107 {
108 if (data->value == WINDOW_FOCUSED) {
109 RESSCHED_LOGI("SocPerfPlugin: socperf->WINDOW_SWITCH");
110 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(PERF_REQUEST_CMD_ID_WINDOW_SWITCH_FIRST, "");
111 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(PERF_REQUEST_CMD_ID_WINDOW_SWITCH_SECOND, "");
112 }
113 }
114
HandleEventClick(const std::shared_ptr<ResData> & data)115 void SocPerfPlugin::HandleEventClick(const std::shared_ptr<ResData>& data)
116 {
117 RESSCHED_LOGI("SocPerfPlugin: socperf->CLICK_NORMAL");
118 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(PERF_REQUEST_CMD_ID_EVENT_CLICK, "");
119 }
120
HandlePushPage(const std::shared_ptr<ResData> & data)121 void SocPerfPlugin::HandlePushPage(const std::shared_ptr<ResData>& data)
122 {
123 RESSCHED_LOGI("SocPerfPlugin: socperf->PUSH_PAGE");
124 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(PERF_REQUEST_CMD_ID_PUSH_PAGE, "");
125 }
126
HandleEventSlide(const std::shared_ptr<ResData> & data)127 void SocPerfPlugin::HandleEventSlide(const std::shared_ptr<ResData>& data)
128 {
129 RESSCHED_LOGI("SocPerfPlugin: socperf->SLIDE_NORMAL: %{public}lld", data->value);
130 if (data->value == EVENT_ON) {
131 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(PERF_REQUEST_CMD_ID_EVENT_SLIDE, true, "");
132 } else if (data->value == EVENT_OFF) {
133 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(PERF_REQUEST_CMD_ID_EVENT_SLIDE, false, "");
134 OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(PERF_REQUEST_CMD_ID_EVENT_SLIDE_OVER, "");
135 }
136 }
137
OnPluginInit(std::string & libName)138 extern "C" bool OnPluginInit(std::string& libName)
139 {
140 if (libName != LIB_NAME) {
141 RESSCHED_LOGE("SocPerfPlugin::OnPluginInit lib name is not match");
142 return false;
143 }
144 SocPerfPlugin::GetInstance().Init();
145 RESSCHED_LOGI("SocPerfPlugin::OnPluginInit success.");
146 return true;
147 }
148
OnPluginDisable()149 extern "C" void OnPluginDisable()
150 {
151 SocPerfPlugin::GetInstance().Disable();
152 RESSCHED_LOGI("SocPerfPlugin::OnPluginDisable success.");
153 }
154
OnDispatchResource(const std::shared_ptr<ResData> & data)155 extern "C" void OnDispatchResource(const std::shared_ptr<ResData>& data)
156 {
157 SocPerfPlugin::GetInstance().DispatchResource(data);
158 RESSCHED_LOGI("SocPerfPlugin::OnDispatchResource success.");
159 }
160 }
161 }
162