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 SOC_PERF_SERVICES_CORE_INCLUDE_SOCPERF_THREAD_WRAP_H
17 #define SOC_PERF_SERVICES_CORE_INCLUDE_SOCPERF_THREAD_WRAP_H
18 
19 #ifdef SOCPERF_ADAPTOR_FFRT
20 #include "ffrt.h"
21 #include "ffrt_inner.h"
22 #else
23 #include "event_handler.h"
24 #endif
25 #include "socperf_common.h"
26 #include "socperf_config.h"
27 namespace OHOS { namespace SOCPERF { class GovResNode; } }
28 namespace OHOS { namespace SOCPERF { class ResAction; } }
29 namespace OHOS { namespace SOCPERF { class ResNode; } }
30 namespace OHOS { namespace SOCPERF { class ResStatus; } }
31 
32 namespace OHOS {
33 namespace SOCPERF {
34 enum SocPerfInnerEvent : uint32_t {
35     INNER_EVENT_ID_INIT_RESOURCE_NODE_INFO = 0,
36     INNER_EVENT_ID_DO_FREQ_ACTION,
37     INNER_EVENT_ID_DO_FREQ_ACTION_PACK,
38     INNER_EVENT_ID_DO_FREQ_ACTION_DELAYED,
39     INNER_EVENT_ID_POWER_LIMIT_BOOST_FREQ,
40     INNER_EVENT_ID_THERMAL_LIMIT_BOOST_FREQ,
41     INNER_EVENT_ID_DO_FREQ_ACTION_LEVEL,
42     INNER_EVENT_ID_CLEAR_ALL_ALIVE_REQUEST
43 };
44 
45 enum RssExeEventType {
46     SOCPERF_EVENT_IVALID = 0,
47     SOCPERF_EVENT_WIRTE_NODE = 1,
48 };
49 
50 namespace {
51     const std::string QOSID_STRING = "qosId";
52     const std::string VALUE_STRING = "value";
53 }
54 
55 #ifdef SOCPERF_ADAPTOR_FFRT
56 class SocPerfThreadWrap {
57 public:
58     explicit SocPerfThreadWrap();
59     ~SocPerfThreadWrap();
60 #else
61 class SocPerfThreadWrap : public AppExecFwk::EventHandler {
62 public:
63     explicit SocPerfThreadWrap(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
64     ~SocPerfThreadWrap() override;
65     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override;
66 #endif
67     void InitResourceNodeInfo(std::shared_ptr<ResourceNode> resourceNode);
68     void DoFreqActionPack(std::shared_ptr<ResActionItem> head);
69     void UpdatePowerLimitBoostFreq(bool powerLimitBoost);
70     void UpdateThermalLimitBoostFreq(bool thermalLimitBoost);
71     void UpdateLimitStatus(int32_t eventId, std::shared_ptr<ResAction> resAction, int32_t resId);
72 #ifdef SOCPERF_ADAPTOR_FFRT
73     void PostDelayTask(std::shared_ptr<ResActionItem> queueHead);
74 #else
75     void PostDelayTask(int32_t resId, std::shared_ptr<ResAction> resAction);
76 #endif
77     void ClearAllAliveRequest();
78 
79 private:
80     static const int32_t SCALES_OF_MILLISECONDS_TO_MICROSECONDS = 1000;
81     std::unordered_map<int32_t, std::shared_ptr<ResStatus>> resStatusInfo_;
82     SocPerfConfig &socPerfConfig_ = SocPerfConfig::GetInstance();
83 #ifdef SOCPERF_ADAPTOR_FFRT
84     ffrt::queue socperfQueue_;
85 #endif
86     bool powerLimitBoost_ = false;
87     bool thermalLimitBoost_ = false;
88 
89 private:
90     void SendResStatusToPerfSo();
91     void ReportToPerfSo(std::vector<int32_t>& qosId, std::vector<int64_t>& value, std::vector<int64_t>& endTime);
92     void ReportToRssExe(std::vector<int32_t>& qosId, std::vector<int64_t>& value, std::vector<int64_t>& endTime);
93     bool GetResValueByLevel(int32_t resId, int32_t level, int64_t& resValue);
94     void UpdateResActionList(int32_t resId, std::shared_ptr<ResAction> resAction, bool delayed);
95     void UpdateResActionListByDelayedMsg(int32_t resId, int32_t type,
96         std::shared_ptr<ResAction> resAction, std::shared_ptr<ResStatus> resStatus);
97     void UpdateResActionListByInstantMsg(int32_t resId, int32_t type,
98         std::shared_ptr<ResAction> resAction, std::shared_ptr<ResStatus> resStatus);
99     void UpdateCandidatesValue(int32_t resId, int32_t type);
100     void InnerArbitrateCandidatesValue(int32_t type, std::shared_ptr<ResStatus> resStatus);
101     void ArbitrateCandidate(int32_t resId);
102     void ArbitratePairRes(int32_t resId, bool perfRequestLimit);
103     void ProcessLimitCase(int32_t resId);
104     bool ArbitratePairResInPerfLvl(int32_t resId);
105     void UpdatePairResValue(int32_t minResId, int64_t minResValue, int32_t maxResId, int64_t maxResValue);
106     void UpdateCurrentValue(int32_t resId, int64_t value);
107     bool ExistNoCandidate(int32_t resId, std::shared_ptr<ResStatus> resStatus);
108     void DoFreqAction(int32_t resId, std::shared_ptr<ResAction> resAction);
109     void DoFreqActionLevel(int32_t resId, std::shared_ptr<ResAction> resAction);
110     void HandleResAction(int32_t resId, int32_t type,
111         std::shared_ptr<ResAction> resAction, std::shared_ptr<ResStatus> resStatus);
112 };
113 } // namespace SOCPERF
114 } // namespace OHOS
115 
116 #endif // SOC_PERF_SERVICES_CORE_INCLUDE_SOCPERF_THREAD_WRAP_H
117