• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "memorytracker_interface_impl.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include <dlfcn.h>
20 
21 namespace OHOS {
22 namespace HDI {
23 namespace Memorytracker {
24 namespace V1_0 {
25 #define HDF_LOG_TAG           hdf_memorytracker_impl
26 using GetDevMemFunc = int32_t (*)(int32_t, MemoryTrackerType, std::vector<MemoryRecord>&);
27 
MemoryTrackerInterfaceImplGetInstance(void)28 extern "C" IMemoryTrackerInterface *MemoryTrackerInterfaceImplGetInstance(void)
29 {
30     return new (std::nothrow) MemoryTrackerInterfaceImpl();
31 }
32 
GetDevMem(int32_t pid,MemoryTrackerType type,std::vector<MemoryRecord> & records)33 int32_t MemoryTrackerInterfaceImpl::GetDevMem(int32_t pid, MemoryTrackerType type, std::vector<MemoryRecord>& records)
34 {
35     HDF_LOGI("%{public}s called!", __func__);
36 
37     auto libMemTrackHandle = dlopen("libmemorytracker.default.so", RTLD_NOW);
38     if (!libMemTrackHandle) {
39         HDF_LOGE("%{public}s, dlopen libmemorytracker failed!", __func__);
40         return HDF_FAILURE;
41     }
42 
43     auto getDevMemFunc = reinterpret_cast<GetDevMemFunc>(dlsym(libMemTrackHandle, "GetDevMem"));
44     if (!getDevMemFunc) {
45         HDF_LOGE("%{public}s, dlsym getDevMemFunc failed!", __func__);
46         dlclose(libMemTrackHandle);
47         return HDF_FAILURE;
48     }
49 
50     if (getDevMemFunc(pid, type, records) != 0) {
51         HDF_LOGE("%{public}s, get device memory failed!", __func__);
52         dlclose(libMemTrackHandle);
53         return HDF_FAILURE;
54     }
55 
56     dlclose(libMemTrackHandle);
57     HDF_LOGI("%{public}s, get device memory success!", __func__);
58     return HDF_SUCCESS;
59 }
60 } // V1_0
61 } // Memorytracker
62 } // HDI
63 } // OHOS
64