• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NATIVE_MEMORY_PROFILER_SA_SERVICE_H
17 #define NATIVE_MEMORY_PROFILER_SA_SERVICE_H
18 
19 #include <unordered_map>
20 #include <mutex>
21 
22 #include "iremote_object.h"
23 #include "system_ability.h"
24 #include "hook_manager.h"
25 #include "native_memory_profiler_sa_stub.h"
26 #include "schedule_task_manager.h"
27 #include "service_entry.h"
28 
29 namespace OHOS::Developtools::NativeDaemon {
30 
31 struct TaskConfig {
32     TaskConfig(std::shared_ptr<HookManager> manager, int32_t pid, const std::string& processName,
33         const std::string& filePath, int32_t timerFd, bool startupMode, uint32_t fd = 0)
hookMgrTaskConfig34         : hookMgr(manager), pid(pid), processName(processName), filePath(filePath), timerFd(timerFd),
35           isStartupMode(startupMode), fd(fd) {}
~TaskConfigTaskConfig36     ~TaskConfig() {};
37     std::shared_ptr<HookManager> hookMgr = nullptr;
38     int32_t pid{0};
39     std::string processName;
40     std::string filePath;
41     int32_t timerFd{-1};
42     bool isStartupMode{false};
43     uint32_t fd{0};
44 };
45 
46 class NativeMemoryProfilerSaService : public SystemAbility, public NativeMemoryProfilerSaStub, public ServiceBase {
47     DECLARE_SYSTEM_ABILITY(NativeMemoryProfilerSaService);
48 public:
49     NativeMemoryProfilerSaService();
50     ~NativeMemoryProfilerSaService();
51     static bool StartServiceAbility();
52     int32_t Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config) override;
53     int32_t Stop(uint32_t pid) override;
54     int32_t Stop(const std::string& name) override;
55     int32_t DumpData(uint32_t fd, std::shared_ptr<NativeMemoryProfilerSaConfig>& config) override;
56     int32_t Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, MessageParcel &reply) override;
Start(std::shared_ptr<NativeMemoryProfilerSaConfig> & config,std::string & replyStats)57     int32_t Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config,
58                   std::string& replyStats) override { return 0; }
59     static void StopHook(uint32_t pid, std::string name = "");
60     static void ClientDisconnectCallback(int socketFd);
61     static bool HasProfilingPermission();
62     static void CloseSocketFd(int pid);
63     static void DelayedShutdown(bool cancel);
64 
65 private:
66     int32_t StartHook(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, uint32_t fd = 0);
67     int32_t StartHookLock(std::shared_ptr<NativeMemoryProfilerSaConfig>& config,
68                           uint32_t fd, std::shared_ptr<HookManager>& hook, std::string& args);
69     int32_t StartHook(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, uint32_t fd, MessageParcel &reply);
70     bool CheckConfig(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, uint32_t fd = 0);
71     void FillTaskConfigContext(int32_t pid, const std::string& name);
72     bool ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size) override;
73     std::string GetCmdArgs(std::shared_ptr<NativeMemoryProfilerSaConfig>& config);
74 
75 private:
76     std::mutex nmdMtx_;
77     std::string startupModeProcessName_;
78     int32_t taskMaxNum_ = 4;
79 };
80 } // namespace OHOS::Developtools::NativeDaemon
81 
82 #endif // NATIVE_MEMORY_PROFILER_SA_SERVICE_H