1 /*
2 * Copyright (c) 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
16 #include "concurrent_task_controller_interface.h"
17
18 #include <cinttypes>
19 #include <fcntl.h>
20 #include <hitrace_meter.h>
21 #include <mutex>
22 #include <sched.h>
23 #include <securec.h>
24 #include <unistd.h>
25
26 #include <linux/sched.h>
27 #include <sys/ioctl.h>
28 #include <sys/resource.h>
29
30 #include "accesstoken_kit.h"
31 #include "concurrent_task_log.h"
32 #include "concurrent_task_type.h"
33 #include "ipc_skeleton.h"
34 #include "parameter.h"
35 #include "rtg_interface.h"
36
37 namespace OHOS {
38 namespace ConcurrentTask {
TaskControllerInterface()39 TaskControllerInterface::TaskControllerInterface() : funcLoader_("libtask_controller.z.so") {}
40
GetInstance()41 TaskControllerInterface& TaskControllerInterface::GetInstance()
42 {
43 static TaskControllerInterface instance;
44 return instance;
45 }
46
RequestAuth(const Json::Value & payload)47 void TaskControllerInterface::RequestAuth(const Json::Value& payload)
48 {
49 if (!inited_) {
50 CONCUR_LOGE("[TaskControllerInterface] RequestAuth failed, funcLoader_ load func failed");
51 return;
52 }
53 requestAuthFunc_(payload);
54 }
55
ReportData(uint32_t resType,int64_t value,const Json::Value & payload)56 void TaskControllerInterface::ReportData(uint32_t resType, int64_t value, const Json::Value& payload)
57 {
58 if (!inited_) {
59 CONCUR_LOGE("[TaskControllerInterface] ReportData failed, funcLoader_ load func failed");
60 return;
61 }
62 reportDataFunc_(resType, value, payload);
63 }
64
ReportSceneInfo(uint32_t type,const Json::Value & payload)65 void TaskControllerInterface::ReportSceneInfo(uint32_t type, const Json::Value& payload)
66 {
67 if (!inited_) {
68 CONCUR_LOGE("[TaskControllerInterface] ReportSceneInfo failed, funcLoader_ load func failed");
69 return;
70 }
71 reportSceneInfoFunc_(type, payload);
72 }
73
QueryInterval(int queryItem,IntervalReply & queryRs)74 void TaskControllerInterface::QueryInterval(int queryItem, IntervalReply& queryRs)
75 {
76 if (!inited_) {
77 CONCUR_LOGE("[TaskControllerInterface] QueryInterval failed, funcLoader_ load func failed");
78 return;
79 }
80 queryIntervalFunc_(queryItem, queryRs);
81 }
82
QueryDeadline(int queryItem,DeadlineReply & ddlReply,const Json::Value & payload)83 void TaskControllerInterface::QueryDeadline(int queryItem, DeadlineReply& ddlReply, const Json::Value& payload)
84 {
85 if (!inited_) {
86 CONCUR_LOGE("[TaskControllerInterface] QueryDeadline failed, funcLoader_ load func failed");
87 return;
88 }
89 queryDeadlineFunc_(queryItem, ddlReply, payload);
90 }
91
Init()92 void TaskControllerInterface::Init()
93 {
94 std::lock_guard<std::mutex> autoLock(funcLoaderLock_);
95 if (inited_) {
96 return;
97 }
98 if (!LoadFunc()) {
99 qosPolicy_.Init();
100 CONCUR_LOGE("TaskControllerInterface load function failed.");
101 return;
102 }
103 CONCUR_LOGI("TaskControllerInterface load function success.");
104 inited_ = true;
105 initFunc_();
106 qosPolicy_.Init();
107 }
108
Release()109 void TaskControllerInterface::Release()
110 {
111 if (!inited_) {
112 CONCUR_LOGE("[TaskControllerInterface] Release failed, funcLoader_ load func failed");
113 return;
114 }
115 releaseFunc_();
116 }
117
LoadFunc()118 bool TaskControllerInterface::LoadFunc()
119 {
120 reportDataFunc_ = ReportDataFunc(funcLoader_.LoadSymbol("ReportData"));
121 reportSceneInfoFunc_ = ReportSceneInfoFunc(funcLoader_.LoadSymbol("ReportSceneInfo"));
122 queryIntervalFunc_ = QueryIntervalFunc(funcLoader_.LoadSymbol("QueryInterval"));
123 queryDeadlineFunc_ = QueryDeadlineFunc(funcLoader_.LoadSymbol("QueryDeadline"));
124 requestAuthFunc_ = RequestAuthFunc(funcLoader_.LoadSymbol("RequestAuth"));
125 initFunc_ = InitFunc(funcLoader_.LoadSymbol("Init"));
126 releaseFunc_ = ReleaseFunc(funcLoader_.LoadSymbol("Release"));
127
128 return funcLoader_.GetLoadSuccess();
129 }
130 } // namespace ConcurrentTask
131 } // namespace OHOS