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 41 private: 42 void StopHook(uint32_t pid, std::string name = "", bool timeout = false); 43 int32_t StartHook(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, uint32_t fd = 0); 44 struct TaskConfig; 45 bool CheckConfig(std::shared_ptr<NativeMemoryProfilerSaConfig>& config, uint32_t fd = 0); 46 void FillTaskConfigContext(int32_t pid, const std::string& name); 47 bool ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size) override; 48 void DelayedShutdown(bool cancel); 49 50 private: 51 struct TaskConfig { 52 TaskConfig(std::shared_ptr<HookManager> manager, int32_t pid, const std::string& processName, 53 const std::string& filePath, int32_t timerFd, bool startupMode, uint32_t fd = 0) hookMgrTaskConfig54 : hookMgr(manager), pid(pid), processName(processName), filePath(filePath), timerFd(timerFd), 55 isStartupMode(startupMode), fd(fd) {} ~TaskConfigTaskConfig56 ~TaskConfig() {}; 57 std::shared_ptr<HookManager> hookMgr = nullptr; 58 int32_t pid{0}; 59 std::string processName; 60 std::string filePath; 61 int32_t timerFd{-1}; 62 bool isStartupMode{false}; 63 uint32_t fd{0}; 64 }; 65 bool HasProfilingPermission(); 66 std::unordered_map<std::string, std::shared_ptr<TaskConfig>> nameAndFilePathCtx_; 67 std::unordered_map<int32_t, std::shared_ptr<TaskConfig>> pidCtx_; 68 std::mutex mtx_; 69 std::mutex nmdMtx_; 70 ScheduleTaskManager scheduleTaskManager_; 71 bool hasStartupMode_{false}; 72 std::string startupModeProcessName_; 73 int32_t taskNum_{0}; 74 std::shared_ptr<ServiceEntry> serviceEntry_{nullptr}; 75 int32_t delayedShutdownTimerFd_{-1}; 76 std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> nmdPidType_; 77 }; 78 } // namespace OHOS::Developtools::NativeDaemon 79 80 #endif // NATIVE_MEMORY_PROFILER_SA_SERVICE_H