1 /*
2 * Copyright (c) 2022-2023 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 "res_sched_mgr.h"
17 #include <cinttypes>
18 #include "cgroup_sched.h"
19 #include "res_sched_log.h"
20 #include "plugin_mgr.h"
21 #include "hitrace_meter.h"
22
23 namespace OHOS {
24 namespace ResourceSchedule {
25 using namespace AppExecFwk;
26
27 IMPLEMENT_SINGLE_INSTANCE(ResSchedMgr);
28
Init()29 void ResSchedMgr::Init()
30 {
31 PluginMgr::GetInstance().Init();
32
33 if (!killProcess_) {
34 killProcess_ = std::make_shared<KillProcess>();
35 }
36 }
37
Stop()38 void ResSchedMgr::Stop()
39 {
40 PluginMgr::GetInstance().Stop();
41 }
42
ReportData(uint32_t resType,int64_t value,const nlohmann::json & payload)43 void ResSchedMgr::ReportData(uint32_t resType, int64_t value, const nlohmann::json& payload)
44 {
45 ReportDataInner(resType, value, payload);
46 DispatchResourceInner(resType, value, payload);
47 }
48
ReportDataInner(uint32_t resType,int64_t value,const nlohmann::json & payload)49 void ResSchedMgr::ReportDataInner(uint32_t resType, int64_t value, const nlohmann::json& payload)
50 {
51 RESSCHED_LOGD("%{public}s, receive resType = %{public}u, value = %{public}lld.", __func__,
52 resType, (long long)value);
53 std::string trace_str(__func__);
54 trace_str.append(",resType[").append(std::to_string(resType)).append("]");
55 trace_str.append(",value[").append(std::to_string(value)).append("]");
56 StartTrace(HITRACE_TAG_OHOS, trace_str, -1);
57 PluginMgr::GetInstance().DispatchResource(std::make_shared<ResData>(resType, value, payload));
58 FinishTrace(HITRACE_TAG_OHOS);
59 }
60
KillProcessByClient(const nlohmann::json & payload,std::string killClientInitiator)61 int32_t ResSchedMgr::KillProcessByClient(const nlohmann::json& payload, std::string killClientInitiator)
62 {
63 return killProcess_->KillProcessByPidWithClient(payload, killClientInitiator);
64 }
65
DispatchResourceInner(uint32_t resType,int64_t value,const nlohmann::json & payload)66 void ResSchedMgr::DispatchResourceInner(uint32_t resType, int64_t value, const nlohmann::json& payload)
67 {
68 CgroupSchedDispatch(resType, value, payload);
69 }
70
ReportDataInProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)71 extern "C" void ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
72 {
73 ResSchedMgr::GetInstance().ReportDataInner(resType, value, payload);
74 }
75 } // namespace ResourceSchedule
76 } // namespace OHOS
77