• 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 NETWORK_PROFILER_MANAGER_H
17 #define NETWORK_PROFILER_MANAGER_H
18 
19 #include <thread>
20 #include <memory>
21 #include <ctime>
22 
23 #include "network_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 "network_profiler_config.pb.h"
29 #include "buffer_writer.h"
30 #include "writer_adapter.h"
31 #include "network_profiler_handle.h"
32 
33 namespace OHOS::Developtools::Profiler {
34 class NetworkProfilerManager : public ManagerInterface, public std::enable_shared_from_this<NetworkProfilerManager> {
35 public:
36     struct NetworkProfilerCtx {
NetworkProfilerCtxNetworkProfilerCtx37         NetworkProfilerCtx(int32_t pid) : pid(pid) {}
NetworkProfilerCtxNetworkProfilerCtx38         NetworkProfilerCtx(const std::string& name) : processName(name) {}
39         NetworkProfilerCtx(int32_t pid, const std::string& name, bool restart = false)
pidNetworkProfilerCtx40             : pid(pid), processName(name), restart(restart) {}
~NetworkProfilerCtxNetworkProfilerCtx41         ~NetworkProfilerCtx() {}
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<NetworkProfilerHandle> handle = nullptr;
49         bool restart = false;
50     };
51 
52     NetworkProfilerManager();
53     ~NetworkProfilerManager();
54 #ifdef NETWORK_UNITTEST
SetConfig(NetworkProfilerConfig & config)55     void SetConfig(NetworkProfilerConfig& config)
56     {
57         config_ = config;
58         return;
59     }
60 #endif
61     void Init();
62     bool StartNetworkProfiler();
63     void StopNetworkProfiler();
64     void ReadShareMemory(std::shared_ptr<NetworkProfilerCtx> ctx);
65     std::pair<int, int> GetNetworkProfilerCtx(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     std::string GetCmdArgs(const NetworkProfilerConfig& traceConfig);
82 
83 private:
84     bool CheckConfig();
85     bool CheckConfigPid(std::set<int32_t>& pidCache);
86     bool CheckStartupProcessName();
87     bool CheckRestartProcessName(std::set<int32_t>& pidCache);
88     bool HandleNetworkProfilerContext(const std::shared_ptr<NetworkProfilerCtx>& ctx);
89     clockid_t GetClockId(NetworkProfilerConfig::ClockId clockType);
90 
91 private:
92     std::shared_ptr<NetworkProfilerSocketService> socketService_{nullptr};
93     std::vector<std::shared_ptr<NetworkProfilerCtx>> networkCtx_;
94     NetworkProfilerConfig config_;
95     std::string paramValue_;
96     std::shared_ptr<Writer> writer_{nullptr};
97     std::atomic<bool> firstData_ = true;
98     std::atomic<uint32_t> firstPluginId_ = 0;
99     std::shared_ptr<WriterAdapter> writerAdapter_{nullptr};
100     bool isProtobufSerialize_{true};
101     std::shared_ptr<CommandPoller> commandPoller_{nullptr};
102     int agentIndex_ = -1;
103 };
104 } // namespace OHOS::Developtools::Profiler
105 
106 #endif // NETWORK_PROFILER_MANAGER_H