• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <string>
17 #include <dlfcn.h>
18 #include "dfx/log/ffrt_log_api.h"
19 #include "cpu_boost_wrapper.h"
20 
21 namespace ffrt {
22 int cpu_boost_start(int ctx_id);
23 int cpu_boost_end(int ctx_id);
24 int cpu_boost_save(int ctx_id);
25 int cpu_boost_restore(int ctx_id);
26 constexpr const char* CPU_BOOST_LIB_PATH = "lib_cpuboost.so";
27 class CPUBoostAdapter {
28 public:
CPUBoostAdapter()29     CPUBoostAdapter()
30     {
31         Load();
32     }
33 
~CPUBoostAdapter()34     ~CPUBoostAdapter()
35     {
36         UnLoad();
37     }
38 
Instance()39     static CPUBoostAdapter* Instance()
40     {
41         static CPUBoostAdapter instance;
42         return &instance;
43     }
44 
45 #define REG_FUNC(func) using func##Type = decltype(func)*; func##Type func##Temp = nullptr
46     REG_FUNC(cpu_boost_start);
47     REG_FUNC(cpu_boost_end);
48     REG_FUNC(cpu_boost_save);
49     REG_FUNC(cpu_boost_restore);
50 #undef REG_FUNC
51 
52 private:
Load()53     bool Load()
54     {
55         if (handle != nullptr) {
56             FFRT_LOGD("handle exits");
57             return true;
58         }
59 
60         handle = dlopen(CPU_BOOST_LIB_PATH, RTLD_NOW | RTLD_LOCAL);
61         if (handle == nullptr) {
62             FFRT_LOGE("load so[%s] fail", CPU_BOOST_LIB_PATH);
63             return false;
64         }
65 
66         bool loadFlag = true;
67 
68 #define LOAD_FUNC(x) x##Temp = reinterpret_cast<x##Type>(dlsym(handle, #x)); \
69         if (x##Temp == nullptr) { \
70             FFRT_LOGE("load func %s from %s failed", #x, CPU_BOOST_LIB_PATH); \
71             loadFlag = false; \
72         }
73             LOAD_FUNC(cpu_boost_start);
74             LOAD_FUNC(cpu_boost_end);
75             LOAD_FUNC(cpu_boost_save);
76             LOAD_FUNC(cpu_boost_restore);
77 #undef LOAD_FUNC
78         return loadFlag;
79     }
80 
UnLoad()81     bool UnLoad()
82     {
83         if (handle != nullptr) {
84             if (dlclose(handle) != 0) {
85                 return false;
86             }
87             handle = nullptr;
88             return true;
89         }
90         return true;
91     }
92 
93     void* handle = nullptr;
94 };
95 }
96 
97 #define EXECUTE_CPU_BOOST_FUNC(x, ctxId, ret) auto func = ffrt::CPUBoostAdapter::Instance()->x##Temp; \
98         if (func != nullptr) { \
99             ret = (func)(ctxId); \
100         } else { \
101             ret = -1; \
102         }
103 
CpuBoostStart(int ctxId)104 int CpuBoostStart(int ctxId)
105 {
106     int ret = 0;
107     EXECUTE_CPU_BOOST_FUNC(cpu_boost_start, ctxId, ret);
108     return ret;
109 }
110 
CpuBoostEnd(int ctxId)111 int CpuBoostEnd(int ctxId)
112 {
113     int ret = 0;
114     EXECUTE_CPU_BOOST_FUNC(cpu_boost_end, ctxId, ret);
115     return ret;
116 }
117 
CpuBoostSave(int ctxId)118 int CpuBoostSave(int ctxId)
119 {
120     int ret = 0;
121     EXECUTE_CPU_BOOST_FUNC(cpu_boost_save, ctxId, ret);
122     return ret;
123 }
124 
CpuBoostRestore(int ctxId)125 int CpuBoostRestore(int ctxId)
126 {
127     int ret = 0;
128     EXECUTE_CPU_BOOST_FUNC(cpu_boost_restore, ctxId, ret);
129     return ret;
130 }