• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void Init();
55     bool StartFfrtProfiler();
56     void StopFfrtProfiler();
57     void ReadShareMemory(std::shared_ptr<FfrtProfilerCtx> ctx);
58     std::pair<int, int> GetFfrtProfilerCtx(int32_t pid = 0, const std::string& name = "");
59 
60     bool LoadPlugin(const std::string& pluginPath) override;
61     bool UnloadPlugin(const std::string& pluginPath) override;
62     bool UnloadPlugin(const uint32_t pluginId) override;
63     bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config) override;
64     bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds) override;
65     bool StartPluginSession(const std::vector<uint32_t>& pluginIds, const std::vector<ProfilerPluginConfig>& config,
66         PluginResult& result) override;
67     bool StopPluginSession(const std::vector<uint32_t>& pluginIds) override;
68     bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds) override;
69     bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd,
70         bool isProtobufSerialize = true) override;
71     bool ResetWriter(uint32_t pluginId) override;
72     void SetCommandPoller(const std::shared_ptr<CommandPoller>& p) override;
73     bool RegisterAgentPlugin(const std::string& pluginPath);
74 
75 private:
76     bool CheckConfig();
77     bool HandleFfrtProfilerContext(const std::shared_ptr<FfrtProfilerCtx>& ctx);
78     clockid_t GetClockId(FfrtProfilerConfig::ClockId clockType);
79 
80 private:
81     std::shared_ptr<FfrtProfilerSocketService> socketService_{nullptr};
82     std::vector<std::shared_ptr<FfrtProfilerCtx>> ffrtCtx_;
83     FfrtProfilerConfig config_;
84     std::string paramValue_;
85     std::shared_ptr<Writer> writer_{nullptr};
86     std::shared_ptr<WriterAdapter> writerAdapter_{nullptr};
87     bool isProtobufSerialize_{true};
88     std::shared_ptr<CommandPoller> commandPoller_{nullptr};
89     int agentIndex_ = -1;
90 };
91 } // namespace OHOS::Developtools::Profiler
92 
93 #endif // FFRT_PROFILER_MANAGER_H