1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 "native_memory_profiler_sa_client_manager.h"
17
18 #include "native_memory_profiler_sa_proxy.h"
19
20 #include "logging.h"
21
22 namespace OHOS::Developtools::NativeDaemon {
23 namespace {
24 constexpr uint32_t LIB_SMS = 4096;
25 constexpr uint32_t CALL_STACK_SMS = 16384;
26 }
27
Start(std::shared_ptr<NativeMemoryProfilerSaConfig> & config)28 int32_t NativeMemoryProfilerSaClientManager::Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config)
29 {
30 CHECK_NOTNULL(config, RET_ERR, "NativeMemoryProfilerSaClientManager: config is nullptr");
31 CHECK_TRUE(CheckConfig(config), RET_ERR, "CheckConfig failed");
32 auto service = GetRemoteService();
33 if (service == nullptr) {
34 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaClientManager: start GetRemoteService failed");
35 return RET_ERR;
36 }
37 NativeMemoryProfilerSaProxy proxy(service);
38 return proxy.Start(config);
39 }
40
Start(NativeMemProfilerType type,uint32_t pid,uint32_t duration,uint32_t sampleIntervel)41 int32_t NativeMemoryProfilerSaClientManager::Start(NativeMemProfilerType type, uint32_t pid, uint32_t duration,
42 uint32_t sampleIntervel)
43 {
44 if (pid == 0) {
45 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaClientManager: pid cannot be 0");
46 return RET_ERR;
47 }
48 auto config = std::make_shared<NativeMemoryProfilerSaConfig>();
49 CHECK_NOTNULL(config, RET_ERR, "NativeMemoryProfilerSaClientManager: config is nullptr");
50 config->pid_ = static_cast<int32_t>(pid);
51 config->duration_ = duration;
52 config->sampleInterval_ = sampleIntervel;
53 if (type == NativeMemProfilerType::MEM_PROFILER_LIBRARY) {
54 config->responseLibraryMode_ = true;
55 } else if (type == NativeMemProfilerType::MEM_PROFILER_CALL_STACK) {
56 config->responseLibraryMode_ = false;
57 }
58 return Start(config);
59 }
60
Stop(uint32_t pid)61 int32_t NativeMemoryProfilerSaClientManager::Stop(uint32_t pid)
62 {
63 CHECK_TRUE(pid != 0, RET_ERR, "NativeMemoryProfilerSaClientManager: pid is 0");
64 auto service = GetRemoteService();
65 if (service == nullptr) {
66 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaClientManager: stop GetRemoteService failed");
67 return RET_ERR;
68 }
69 NativeMemoryProfilerSaProxy proxy(service);
70 return proxy.Stop(pid);
71 }
72
Stop(const std::string & name)73 int32_t NativeMemoryProfilerSaClientManager::Stop(const std::string& name)
74 {
75 CHECK_TRUE(!name.empty(), RET_ERR, "NativeMemoryProfilerSaClientManager: name is empty");
76 auto service = GetRemoteService();
77 if (service == nullptr) {
78 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaClientManager: stop GetRemoteService failed");
79 return RET_ERR;
80 }
81 NativeMemoryProfilerSaProxy proxy(service);
82 return proxy.Stop(name);
83 }
84
DumpData(uint32_t fd,std::shared_ptr<NativeMemoryProfilerSaConfig> & config)85 int32_t NativeMemoryProfilerSaClientManager::DumpData(uint32_t fd,
86 std::shared_ptr<NativeMemoryProfilerSaConfig>& config)
87 {
88 CHECK_TRUE(fd != 0, RET_ERR, "NativeMemoryProfilerSaClientManager: fd is 0");
89 CHECK_NOTNULL(config, RET_ERR, "NativeMemoryProfilerSaClientManager: config is nullptr");
90 CHECK_TRUE(CheckConfig(config), RET_ERR, "CheckConfig failed");
91 auto service = GetRemoteService();
92 if (service == nullptr) {
93 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaClientManager: stop GetRemoteService failed");
94 return RET_ERR;
95 }
96 NativeMemoryProfilerSaProxy proxy(service);
97 return proxy.DumpData(fd, config);
98 }
99
GetRemoteService()100 sptr<IRemoteObject> NativeMemoryProfilerSaClientManager::GetRemoteService()
101 {
102 auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
103 if (abilityManager == nullptr) {
104 return nullptr;
105 }
106 return abilityManager->CheckSystemAbility(INativeMemoryProfilerSa::NATIVE_DAEMON_SYSTEM_ABILITY_ID);
107 }
108
CheckConfig(const std::shared_ptr<NativeMemoryProfilerSaConfig> & config)109 bool NativeMemoryProfilerSaClientManager::CheckConfig(const std::shared_ptr<NativeMemoryProfilerSaConfig>& config)
110 {
111 CHECK_NOTNULL(config, false, "NativeMemoryProfilerSaClientManager: config is nullptr");
112 if (config->duration_ == 0) {
113 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaClientManager: duration cannot be 0");
114 return false;
115 }
116 if (config->shareMemorySize_ == 0) {
117 config->shareMemorySize_ = config->responseLibraryMode_ ? LIB_SMS : CALL_STACK_SMS;
118 }
119 return true;
120 }
121
GetMallocStats(int fd,int pid,int type)122 int32_t NativeMemoryProfilerSaClientManager::GetMallocStats(int fd, int pid, int type)
123 {
124 CHECK_TRUE(fd != 0, RET_ERR, "NativeMemoryProfilerSaClientManager: GetMallocStats fd is 0");
125 CHECK_TRUE(pid > 0, RET_ERR, "NativeMemoryProfilerSaClientManager: GetMallocStats invalid pid");
126 CHECK_TRUE(type == 0 || type == 1, RET_ERR, "NativeMemoryProfilerSaClientManager: type is invalid");
127 std::shared_ptr<NativeMemoryProfilerSaConfig> config = std::make_shared<NativeMemoryProfilerSaConfig>();
128 config->printNmd_ = true;
129 config->nmdPid_ = static_cast<uint32_t>(pid);
130 config->nmdType_ = static_cast<uint32_t>(type);
131 auto service = GetRemoteService();
132 if (service == nullptr) {
133 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaClientManager: stop GetRemoteService failed");
134 return RET_ERR;
135 }
136 NativeMemoryProfilerSaProxy proxy(service);
137 return proxy.DumpData(fd, config);
138 }
139 } // namespace OHOS::Developtools::NativeDaemon