• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "graphic_memory_collector_impl.h"
17 
18 #include <dlfcn.h>
19 
20 #include "graphic_memory_decorator.h"
21 #include "hiview_logger.h"
22 #include "memory.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace UCollectUtil {
27 namespace {
28 DEFINE_LOG_TAG("GraphicCollector");
29 constexpr const char *LIB_NAME = "libucollection_graphic.z.so";
30 constexpr const char *GET_INSTANCE = "GetInstance";
31 }
Create()32 std::shared_ptr<GraphicMemoryCollector> GraphicMemoryCollector::Create()
33 {
34     return std::make_shared<GraphicMemoryDecorator>(std::make_shared<GraphicMemoryCollectorImpl>());
35 }
36 
GetGraphicUsage(int32_t pid,GraphicType type,bool isLowLatencyMode)37 CollectResult<int32_t> GraphicMemoryCollectorImpl::GetGraphicUsage(int32_t pid, GraphicType type,
38     bool isLowLatencyMode)
39 {
40     std::lock_guard<std::mutex> lock(mutexLock_);
41     static GraphicMemoryCollector *graphCollectorInstance = nullptr;
42     if (graphCollectorInstance != nullptr) {
43         return graphCollectorInstance->GetGraphicUsage(pid, type, false);
44     }
45 
46     CollectResult<int32_t> result;
47     void *handler = dlopen(LIB_NAME, RTLD_LAZY);
48     if (handler == nullptr) {
49         HIVIEW_LOGW("dlopen failed, error: %{public}s", dlerror());
50         return result;
51     }
52 
53     auto getInterface = reinterpret_cast<GraphicMemoryCollector *(*)()>(dlsym(handler, GET_INSTANCE));
54     if (getInterface == nullptr) {
55         HIVIEW_LOGW("dlsym failed, error: %{public}s", dlerror());
56         dlclose(handler);
57         return result;
58     }
59     graphCollectorInstance = getInterface();
60     result = graphCollectorInstance->GetGraphicUsage(pid, type, false);
61     if (!isLowLatencyMode) {
62         dlclose(handler);
63         handler = nullptr;
64         graphCollectorInstance = nullptr;
65     }
66     return result;
67 }
68 } // UCollectUtil
69 } // HiViewDFX
70 } // OHOS
71