• 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_SYSEVENT_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_SYSEVENT_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_SYSEVENT_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                 FFRT_SYSEVENT_LOGE("failed to close the handle");
86                 return false;
87             }
88             handle = nullptr;
89             return true;
90         }
91         return true;
92     }
93 
94     void* handle = nullptr;
95 };
96 }
97 
98 #define EXECUTE_CPU_BOOST_FUNC(x, ctxId, ret) auto func = ffrt::CPUBoostAdapter::Instance()->x##Temp; \
99         if (func != nullptr) { \
100             ret = (func)(ctxId); \
101         } else { \
102             ret = -1; \
103         }
104 
CpuBoostStart(int ctxId)105 int CpuBoostStart(int ctxId)
106 {
107     int ret = 0;
108     EXECUTE_CPU_BOOST_FUNC(cpu_boost_start, ctxId, ret);
109     return ret;
110 }
111 
CpuBoostEnd(int ctxId)112 int CpuBoostEnd(int ctxId)
113 {
114     int ret = 0;
115     EXECUTE_CPU_BOOST_FUNC(cpu_boost_end, ctxId, ret);
116     return ret;
117 }
118 
CpuBoostSave(int ctxId)119 int CpuBoostSave(int ctxId)
120 {
121     int ret = 0;
122     EXECUTE_CPU_BOOST_FUNC(cpu_boost_save, ctxId, ret);
123     return ret;
124 }
125 
CpuBoostRestore(int ctxId)126 int CpuBoostRestore(int ctxId)
127 {
128     int ret = 0;
129     EXECUTE_CPU_BOOST_FUNC(cpu_boost_restore, ctxId, ret);
130     return ret;
131 }