1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. 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 FFRT_PROFILER_MANAGER_H 17 #define FFRT_PROFILER_MANAGER_H 18 19 #include <thread> 20 #include <memory> 21 #include <ctime> 22 23 #include "ffrt_profiler_socket_service.h" 24 #include "manager_interface.h" 25 #include "epoll_event_poller.h" 26 #include "share_memory_allocator.h" 27 #include "event_notifier.h" 28 #include "ffrt_profiler_config.pb.h" 29 #include "buffer_writer.h" 30 #include "writer_adapter.h" 31 #include "ffrt_profiler_handle.h" 32 33 namespace OHOS::Developtools::Profiler { 34 class FfrtProfilerManager : public ManagerInterface, public std::enable_shared_from_this<FfrtProfilerManager> { 35 public: 36 struct FfrtProfilerCtx { FfrtProfilerCtxFfrtProfilerCtx37 FfrtProfilerCtx(int32_t pid) : pid(pid) {} FfrtProfilerCtxFfrtProfilerCtx38 FfrtProfilerCtx(const std::string& name) : processName(name) {} 39 FfrtProfilerCtx(int32_t pid, const std::string& name, bool restart = false) pidFfrtProfilerCtx40 : pid(pid), processName(name), restart(restart) {} ~FfrtProfilerCtxFfrtProfilerCtx41 ~FfrtProfilerCtx() {} 42 int32_t pid = -1; 43 std::string processName; 44 std::string smbName; 45 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock = nullptr; 46 std::shared_ptr<EventNotifier> eventNotifier = nullptr; 47 std::unique_ptr<EpollEventPoller> eventPoller = nullptr; 48 std::shared_ptr<FfrtProfilerHandle> handle = nullptr; 49 bool restart = false; 50 }; 51 52 FfrtProfilerManager(); 53 ~FfrtProfilerManager(); 54 #ifdef UNIT_TEST SetConfig(FfrtProfilerConfig & config)55 void SetConfig(FfrtProfilerConfig& config) 56 { 57 config_ = config; 58 return; 59 } 60 #endif 61 void Init(); 62 bool StartFfrtProfiler(); 63 void StopFfrtProfiler(); 64 void ReadShareMemory(std::shared_ptr<FfrtProfilerCtx> ctx); 65 std::pair<int, int> GetFfrtProfilerCtx(int32_t pid = 0, const std::string& name = ""); 66 67 bool LoadPlugin(const std::string& pluginPath) override; 68 bool UnloadPlugin(const std::string& pluginPath) override; 69 bool UnloadPlugin(const uint32_t pluginId) override; 70 bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config) override; 71 bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds) override; 72 bool StartPluginSession(const std::vector<uint32_t>& pluginIds, const std::vector<ProfilerPluginConfig>& config, 73 PluginResult& result) override; 74 bool StopPluginSession(const std::vector<uint32_t>& pluginIds) override; 75 bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds) override; 76 bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd, 77 bool isProtobufSerialize = true) override; 78 bool ResetWriter(uint32_t pluginId) override; 79 void SetCommandPoller(const std::shared_ptr<CommandPoller>& p) override; 80 bool RegisterAgentPlugin(const std::string& pluginPath); 81 82 private: 83 bool CheckConfig(); 84 bool HandleFfrtProfilerContext(const std::shared_ptr<FfrtProfilerCtx>& ctx); 85 clockid_t GetClockId(FfrtProfilerConfig::ClockId clockType); 86 87 private: 88 std::shared_ptr<FfrtProfilerSocketService> socketService_{nullptr}; 89 std::vector<std::shared_ptr<FfrtProfilerCtx>> ffrtCtx_; 90 FfrtProfilerConfig config_; 91 std::string paramValue_; 92 std::shared_ptr<Writer> writer_{nullptr}; 93 std::shared_ptr<WriterAdapter> writerAdapter_{nullptr}; 94 bool isProtobufSerialize_{true}; 95 std::shared_ptr<CommandPoller> commandPoller_{nullptr}; 96 int agentIndex_ = -1; 97 }; 98 } // namespace OHOS::Developtools::Profiler 99 100 #endif // FFRT_PROFILER_MANAGER_H