• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
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 PLUGIN_SERVICE_H
17 #define PLUGIN_SERVICE_H
18 
19 #include <atomic>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <thread>
24 #include <vector>
25 
26 #include "common_types.pb.h"
27 #include "epoll_event_poller.h"
28 #include "event_notifier.h"
29 #include "i_semaphore.h"
30 #include "logging.h"
31 #include "plugin_service_types.pb.h"
32 #include "profiler_service_types.pb.h"
33 #include "service_entry.h"
34 
35 class PluginServiceImpl;
36 class ProfilerDataRepeater;
37 class SocketContext;
38 class ShareMemoryBlock;
39 class PluginCommandBuilder;
40 class PluginSessionManager;
41 
42 using ProfilerDataRepeaterPtr = STD_PTR(shared, ProfilerDataRepeater);
43 using ProfilerPluginStatePtr = STD_PTR(shared, ProfilerPluginState);
44 
45 struct PluginInfo {
46     uint32_t id = 0;
47     std::string name;
48     std::string path;
49     std::string sha256;
50     uint32_t bufferSizeHint;
51     SocketContext* context = nullptr;
52 };
53 
54 struct PluginContext {
55     std::string name;
56     std::string path;
57     std::string sha256;
58     uint32_t bufferSizeHint;
59     SocketContext* context = nullptr;
60     ProfilerPluginConfig config;
61     ProfilerDataRepeaterPtr profilerDataRepeater;
62     std::shared_ptr<ShareMemoryBlock> shareMemoryBlock;
63     EventNotifierPtr eventNotifier;
64     ProfilerPluginState profilerPluginState;
65 };
66 
67 using PluginContextPtr = std::shared_ptr<PluginContext>;
68 
69 using PluginSessionManagerPtr = std::shared_ptr<PluginSessionManager>;
70 
71 class PluginService {
72 public:
73     PluginService();
74     ~PluginService();
75 
76     bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
77                              const ProfilerSessionConfig::BufferConfig& bufferConfig,
78                              const ProfilerDataRepeaterPtr& dataRepeater);
79     bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig, const ProfilerDataRepeaterPtr& dataRepeater);
80     bool StartPluginSession(const ProfilerPluginConfig& config);
81     bool StopPluginSession(const std::string& pluginName);
82     bool DestroyPluginSession(const std::string& pluginName);
83 
84     bool AddPluginInfo(const PluginInfo& pluginInfo);
85     bool GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo);
86     bool RemovePluginInfo(const PluginInfo& pluginInfo);
87 
88     bool AppendResult(NotifyResultRequest& request);
89 
90     std::vector<ProfilerPluginState> GetPluginStatus();
91     uint32_t GetPluginIdByName(std::string name);
92 
93     void SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager);
94 
95 private:
96     bool StartService(const std::string& unixSocketName);
97 
98     SemaphorePtr GetSemaphore(uint32_t) const;
99     void ReadShareMemory(PluginContext&);
100 
101     std::pair<uint32_t, PluginContextPtr> GetPluginContext(const std::string& pluginName);
102     PluginContextPtr GetPluginContextById(uint32_t id);
103 
104     bool RemovePluginSessionCtx(const std::string& pluginName);
105 
106     mutable std::mutex mutex_;
107     std::map<uint32_t, PluginContextPtr> pluginContext_;
108     std::map<uint32_t, SemaphorePtr> waitSemphores_;
109     std::map<std::string, uint32_t> nameIndex_;
110 
111     std::atomic<uint32_t> pluginIdCounter_;
112     std::shared_ptr<ServiceEntry> serviceEntry_;
113     std::shared_ptr<PluginServiceImpl> pluginServiceImpl_;
114     std::shared_ptr<PluginCommandBuilder> pluginCommandBuilder_;
115     std::unique_ptr<EpollEventPoller> eventPoller_;
116     PluginSessionManagerPtr pluginSessionManager_;
117 };
118 
119 #endif // PLUGIN_SERVICE_H