• 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 class NativeMemoryProfilerSaService : public SystemAbility, public NativeMemoryProfilerSaStub, public ServiceBase {
31     DECLARE_SYSTEM_ABILITY(NativeMemoryProfilerSaService);
32 public:
33     NativeMemoryProfilerSaService();
34     ~NativeMemoryProfilerSaService();
35     static bool StartServiceAbility();
36     int32_t Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config) override;
37     int32_t Stop(uint32_t pid) override;
38     int32_t Stop(const std::string& name) override;
39     int32_t DumpData(uint32_t fd, std::shared_ptr<NativeMemoryProfilerSaConfig>& config) override;
40     int32_t Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, MessageParcel &reply) override;
Start(std::shared_ptr<NativeMemoryProfilerSaConfig> & config,std::string & replyStats)41     int32_t Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config,
42                   std::string& replyStats) override { return 0; }
43 
44 private:
45     void StopHook(uint32_t pid, std::string name = "");
46     int32_t StartHook(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, uint32_t fd = 0);
47     int32_t StartHookLock(std::shared_ptr<NativeMemoryProfilerSaConfig>& config,
48                           uint32_t fd, std::shared_ptr<HookManager>& hook, std::string& args);
49     int32_t StartHook(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, uint32_t fd, MessageParcel &reply);
50     struct TaskConfig;
51     bool CheckConfig(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, uint32_t fd = 0);
52     void FillTaskConfigContext(int32_t pid, const std::string& name);
53     bool ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size) override;
54     void DelayedShutdown(bool cancel);
55     std::string GetCmdArgs(std::shared_ptr<NativeMemoryProfilerSaConfig>& config);
56 
57 private:
58     struct TaskConfig {
59         TaskConfig(std::shared_ptr<HookManager> manager, int32_t pid, const std::string& processName,
60             const std::string& filePath, int32_t timerFd, bool startupMode, uint32_t fd = 0)
hookMgrTaskConfig61             : hookMgr(manager), pid(pid), processName(processName), filePath(filePath), timerFd(timerFd),
62               isStartupMode(startupMode), fd(fd) {}
~TaskConfigTaskConfig63         ~TaskConfig() {};
64         std::shared_ptr<HookManager> hookMgr = nullptr;
65         int32_t pid{0};
66         std::string processName;
67         std::string filePath;
68         int32_t timerFd{-1};
69         bool isStartupMode{false};
70         uint32_t fd{0};
71     };
72     bool HasProfilingPermission();
73     std::unordered_map<std::string, std::shared_ptr<TaskConfig>> nameAndFilePathCtx_;
74     std::unordered_map<int32_t, std::shared_ptr<TaskConfig>> pidCtx_;
75     std::mutex mtx_;
76     std::mutex nmdMtx_;
77     ScheduleTaskManager scheduleTaskManager_;
78     bool hasStartupMode_{false};
79     std::string startupModeProcessName_;
80     int32_t taskNum_{0};
81     std::shared_ptr<ServiceEntry> serviceEntry_{nullptr};
82     int32_t delayedShutdownTimerFd_{-1};
83     std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> nmdPidType_;
84 };
85 } // namespace OHOS::Developtools::NativeDaemon
86 
87 #endif // NATIVE_MEMORY_PROFILER_SA_SERVICE_H