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_H
17 #define SOC_PERF_SERVICES_CORE_INCLUDE_SOCPERF_H
18 
19 #include <set>
20 #include "libxml/tree.h"
21 #include "socperf_thread_wrap.h"
22 #include "socperf_config.h"
23 
24 namespace OHOS {
25 namespace SOCPERF {
26 class SocPerf {
27 public:
28     bool Init();
29     void PerfRequest(int32_t cmdId, const std::string& msg);
30     void PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg);
31     void PowerLimitBoost(bool onOffTag, const std::string& msg);
32     void ThermalLimitBoost(bool onOffTag, const std::string& msg);
33     void LimitRequest(int32_t clientId,
34         const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg);
35     void SetRequestStatus(bool status, const std::string& msg);
36     void SetThermalLevel(int32_t level);
37     void RequestDeviceMode(const std::string& mode, bool status);
38     std::string RequestCmdIdCount(const std::string& msg);
39 public:
40     SocPerf();
41     ~SocPerf();
42 
43 private:
44     bool enabled_ = false;
45     std::shared_ptr<SocPerfThreadWrap> socperfThreadWrap_;
46     std::set<std::string> recordDeviceMode_;
47     std::vector<std::unordered_map<int32_t, int32_t>> limitRequest_ =
48         std::vector<std::unordered_map<int32_t, int32_t>>(ACTION_TYPE_MAX);
49     volatile bool perfRequestEnable_ = true;
50     int32_t thermalLvl_ = MIN_THERMAL_LVL;
51     SocPerfConfig &socPerfConfig_ = SocPerfConfig::GetInstance();
52     std::unordered_map<int32_t, uint32_t> boostCmdCount_;
53     std::unordered_map<int32_t, uint64_t> boostTime_;
54 private:
55     std::mutex mutex_;
56     std::mutex mutexDeviceMode_;
57     std::mutex mutexBoostCmdCount_;
58     std::mutex mutexBoostTime_;
59     bool CreateThreadWraps();
60     void InitThreadWraps();
61     void DoFreqActions(std::shared_ptr<Actions> actions, int32_t onOff, int32_t actionType);
62     bool DoPerfRequestThremalLvl(int32_t cmdId, std::shared_ptr<Action> action, int32_t onOff);
63     void SendLimitRequestEvent(int32_t clientId, int32_t resId, int64_t resValue);
64     int32_t MatchDeviceModeCmd(int32_t cmdId, bool isTagOnOff);
65     void SendLimitRequestEventOff(std::shared_ptr<SocPerfThreadWrap> threadWrap,
66         int32_t clientId, int32_t resId, int32_t eventId);
67     void SendLimitRequestEventOn(std::shared_ptr<SocPerfThreadWrap> threadWrap,
68         int32_t clientId, int32_t resId, int64_t resValue, int32_t eventId);
69     void ClearAllAliveRequest();
70     void UpdateCmdIdCount(int32_t cmdId);
71     bool CheckTimeInterval(bool onOff, int32_t cmdId);
72 };
73 } // namespace SOCPERF
74 } // namespace OHOS
75 #endif // SOC_PERF_SERVICES_CORE_INCLUDE_SOCPERF_H
76