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 
16 #ifndef CGROUP_SCHED_FRAMEWORK_UTILS_INCLUDE_RESSCHED_UTILS_H_
17 #define CGROUP_SCHED_FRAMEWORK_UTILS_INCLUDE_RESSCHED_UTILS_H_
18 
19 #include <iostream>
20 #include "cgroup_adjuster.h"
21 #include "supervisor.h"
22 #include "sys/types.h"
23 #include "nlohmann/json.hpp"
24 
25 namespace OHOS {
26 namespace ResourceSchedule {
27 using ReportDataFunc = void (*)(uint32_t resType, int64_t value, const nlohmann::json& payload);
28 using ReportArbitrationResultFunc = void (*)(Application &app, ProcessRecord &pr, AdjustSource source);
29 using ReportSysEventFunc = void (*)(Application &app, ProcessRecord &pr, uint32_t resType, int32_t state);
30 using DispatchResourceExtFunc = void (*)(uint32_t resType, int64_t value, const nlohmann::json& payload);
31 using ReportAppStateFunc = void (*)(int32_t state, int32_t pid);
32 class ResSchedUtils {
33 public:
34     static ResSchedUtils& GetInstance();
35     void ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload);
36     void ReportArbitrationResult(Application &app, ProcessRecord &pr, AdjustSource source);
37     void ReportSysEvent(Application &app, ProcessRecord &pr, uint32_t resType, int32_t state);
38     std::string GetProcessFilePath(int32_t uid, std::string bundleName, int32_t pid);
39     bool CheckTidIsInPid(int32_t pid, int32_t tid);
40     void DispatchResourceExt(uint32_t resType, int64_t value, const nlohmann::json& payload);
41     void ReportAppStateInProcess(int32_t state, int32_t pid);
42 
43 private:
ResSchedUtils()44     ResSchedUtils()
45     {
46         LoadUtils();
47         LoadUtilsExtra();
48     }
~ResSchedUtils()49     ~ResSchedUtils()
50     {
51         reportFunc_ = nullptr;
52         reportArbitrationResultFunc_ = nullptr;
53         reportSysEventFunc_ = nullptr;
54     }
55     void LoadUtils();
56     void LoadUtilsExtra();
57 
58     ResSchedUtils(const ResSchedUtils&) = delete;
59     ResSchedUtils& operator=(const ResSchedUtils &) = delete;
60     ResSchedUtils(ResSchedUtils&&) = delete;
61     ResSchedUtils& operator=(ResSchedUtils&&) = delete;
62 
63     ReportDataFunc reportFunc_ = nullptr;
64     ReportArbitrationResultFunc reportArbitrationResultFunc_ = nullptr;
65     ReportSysEventFunc reportSysEventFunc_ = nullptr;
66     DispatchResourceExtFunc dispatchResourceExtFunc_ = nullptr;
67     ReportAppStateFunc reportAppStateFunc_ = nullptr;
68 };
69 } // namespace ResourceSchedule
70 } // namespace OHOS
71 #endif // CGROUP_SCHED_FRAMEWORK_UTILS_INCLUDE_RESSCHED_UTILS_H_
72