• 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 License at
6 *
7 *     http://www.apache.org/license/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 
17 #ifndef __FFRT_TASKCLIENT_ADAPTER_H__
18 #define __FFRT_TASKCLIENT_ADAPTER_H__
19 
20 #include <atomic>
21 #include <chrono>
22 #include <dlfcn.h>
23 #include "internal_inc/osal.h"
24 #include "dfx/log/ffrt_log_api.h"
25 
26 #if (defined(QOS_WORKER_FRAME_RTG) || defined(QOS_FRAME_RTG))
27 
28 struct IntervalReply {
29     int rtgId;
30     int tid;
31     int paramA;
32     int paramB;
33     std::string bundleName;
34 };
35 
36 enum QueryIntervalItem {
37     QUERY_UI = 0,
38     QUERY_RENDER = 1,
39     QUERY_RENDER_SERVICE = 2,
40     QUERY_COMPOSER = 3,
41     QUERY_HARDWARE = 4,
42     QUERY_EXECUTOR_START = 5,
43     QUERY_TYPE_MAX
44 };
45 
46 extern "C" {
47     int AddThreadToRtg(int tid, int grpId, int prioType = 0);
48     int DestroyRtgGrp(int grpId);
49     int BeginFrameFreq(int stateParam);
50     int EndFrameFreq(int stateParam);
51     void CTC_QueryInterval(int queryItem, IntervalReply& queryRs);
52 }
53 
54 constexpr const char* TRACE_LIB_PATH_1 = "/system/lib64/libconcurrentsvc.z.so";
55 constexpr const char* TRACE_LIB_PATH_2 = "/system/lib64/libconcurrent_task_client.z.so";
56 
57 class TaskClientAdapter {
58 public:
TaskClientAdapter()59     TaskClientAdapter()
60     {
61         Load();
62     }
63 
~TaskClientAdapter()64     ~TaskClientAdapter()
65     {
66         UnLoad();
67     }
68 
Instance()69     static TaskClientAdapter* Instance()
70     {
71         static TaskClientAdapter instance;
72         return &instance;
73     }
74 
75 #define REG_FUNC(func) using func##Type = decltype(func)*; func##Type func##Func = nullptr
76     REG_FUNC(AddThreadToRtg);
77     REG_FUNC(BeginFrameFreq);
78     REG_FUNC(EndFrameFreq);
79     REG_FUNC(DestroyRtgGrp);
80     REG_FUNC(CTC_QueryInterval);
81 #undef REG_FUNC
82 
83 private:
Load()84     void Load()
85     {
86         if (handle_1 != nullptr) {
87             FFRT_LOGD("handle_1 exits");
88             return;
89         }
90         if (handle_2 != nullptr) {
91             FFRT_LOGD("handle_2 exits");
92             return;
93         }
94 
95         handle_1 = dlopen(TRACE_LIB_PATH_1, RTLD_NOW | RTLD_LOCAL);
96         if (handle_1 == nullptr) {
97             FFRT_LOGE("load so[%s] fail", TRACE_LIB_PATH_1);
98             return;
99         }
100 
101         handle_2 = dlopen(TRACE_LIB_PATH_2, RTLD_NOW | RTLD_LOCAL);
102         if (handle_2 == nullptr) {
103             FFRT_LOGE("load so[%s] fail", TRACE_LIB_PATH_2);
104             return;
105         }
106 
107 #define LOAD_FUNC(func) func##Func = reinterpret_cast<func##Type>(dlsym(handle_1, #func));      \
108         if (func##Func != nullptr) {                                                            \
109             FFRT_LOGI("load func %s from %s success", #func, TRACE_LIB_PATH_1);         \
110         } else {                                                                                \
111             func##Func = reinterpret_cast<func##Type>(dlsym(handle_2, #func));                  \
112             if (func##Func == nullptr) {                                                        \
113                 FFRT_LOGE("load func %s from %s failed", #func, TRACE_LIB_PATH_2);      \
114             }                                                                                   \
115         }
116 
117             LOAD_FUNC(AddThreadToRtg);
118             LOAD_FUNC(BeginFrameFreq);
119             LOAD_FUNC(EndFrameFreq);
120             LOAD_FUNC(DestroyRtgGrp);
121             LOAD_FUNC(CTC_QueryInterval);
122 #undef LOAD_FUNC
123     }
124 
UnLoad()125     void UnLoad()
126     {
127         if (handle_1 != nullptr) {
128             if (dlclose(handle_1) != 0) {
129                 FFRT_LOGE("unLoad handle_1 fail");
130             }
131             handle_1 = nullptr;
132         }
133         if (handle_2 != nullptr) {
134             if (dlclose(handle_2) != 0) {
135                 FFRT_LOGE("unLoad handle_2 fail");
136             }
137             handle_2 = nullptr;
138         }
139     }
140 
141     void* handle_1 = nullptr;
142     void* handle_2 = nullptr;
143 };
144 
EndFrameFreqAdapter(int stateParam)145 static int EndFrameFreqAdapter(int stateParam)
146 {
147     auto func = TaskClientAdapter::Instance()->EndFrameFreqFunc;
148     if (func != nullptr) {
149         return func(stateParam);
150     }
151     return -1;
152 }
153 
BeginFrameFreqAdapter(int stateParam)154 static int BeginFrameFreqAdapter(int stateParam)
155 {
156     auto func = TaskClientAdapter::Instance()->BeginFrameFreqFunc;
157     if (func != nullptr) {
158         return func(stateParam);
159     }
160     return -1;
161 }
162 
DestroyRtgGrpAdapter(int grpId)163 static int DestroyRtgGrpAdapter(int grpId)
164 {
165     auto func = TaskClientAdapter::Instance()->DestroyRtgGrpFunc;
166     if (func != nullptr) {
167         return func(grpId);
168     }
169     return -1;
170 }
171 
172 static int AddThreadToRtgAdapter(int tid, int grpId, int prioType = 0)
173 {
174     auto func = TaskClientAdapter::Instance()->AddThreadToRtgFunc;
175     if (func != nullptr) {
176         return func(tid, grpId, prioType);
177     }
178     return -1;
179 }
180 
181 #define CTC_QUERY_INTERVAL(queryItem, queryRs)                             \
182     do {                                                                   \
183         auto func = TaskClientAdapter::Instance()->CTC_QueryIntervalFunc;   \
184         if (func != nullptr) {                                             \
185             func(queryItem, queryRs);                                      \
186         }                                                                  \
187     } while (0)
188 
189 #endif /* __FFRT_TASKCLIENT_ADAPTER_H__ */
190 #endif /* QOS_FRAME_RTG */
191