• 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     bool isStandaloneFileData = false;
52     std::string outFileName = "";
53     std::string pluginVersion = "";
54     SocketContext* context = nullptr;
55 };
56 
57 struct PluginContext {
58     std::string name;
59     std::string path;
60     std::string sha256;
61     uint32_t bufferSizeHint;
62     bool isStandaloneFileData = false;
63     std::string outFileName = "";
64     std::string pluginVersion = "";
65     SocketContext* context = nullptr;
66     ProfilerPluginConfig config;
67     ProfilerDataRepeaterPtr profilerDataRepeater;
68     std::shared_ptr<ShareMemoryBlock> shareMemoryBlock;
69     EventNotifierPtr eventNotifier;
70     ProfilerPluginState profilerPluginState;
71 };
72 
73 using PluginContextPtr = std::shared_ptr<PluginContext>;
74 
75 using PluginSessionManagerPtr = std::shared_ptr<PluginSessionManager>;
76 
77 class PluginService {
78 public:
79     PluginService();
80     ~PluginService();
81 
82     bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
83                              const ProfilerSessionConfig::BufferConfig& bufferConfig,
84                              const ProfilerDataRepeaterPtr& dataRepeater);
85     bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig, const ProfilerDataRepeaterPtr& dataRepeater);
86     bool StartPluginSession(const ProfilerPluginConfig& config);
87     bool StopPluginSession(const std::string& pluginName);
88     bool DestroyPluginSession(const std::string& pluginName);
89     bool RefreshPluginSession(const std::string& pluginName);
90 
91     bool AddPluginInfo(const PluginInfo& pluginInfo);
92     bool GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo);
93     bool RemovePluginInfo(const PluginInfo& pluginInfo);
94 
95     bool AppendResult(NotifyResultRequest& request);
96 
97     std::vector<ProfilerPluginState> GetPluginStatus();
98     uint32_t GetPluginIdByName(std::string name);
99 
100     void SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager);
101     void SetProfilerSessionConfig(const ProfilerSessionConfig& profilerSessionConfig);
102 
103     std::pair<uint32_t, PluginContextPtr> GetPluginContext(const std::string& pluginName);
104 
105 private:
106     bool StartService(const std::string& unixSocketName);
107 
108     SemaphorePtr GetSemaphore(uint32_t) const;
109     void ReadShareMemory(PluginContext&);
110     PluginContextPtr GetPluginContextById(uint32_t id);
111 
112     bool RemovePluginSessionCtx(const std::string& pluginName);
113 
114     mutable std::mutex mutex_;
115     std::map<uint32_t, PluginContextPtr> pluginContext_;
116     std::map<uint32_t, SemaphorePtr> waitSemphores_;
117     std::map<std::string, uint32_t> nameIndex_;
118 
119     std::atomic<uint32_t> pluginIdCounter_;
120     std::shared_ptr<ServiceEntry> serviceEntry_;
121     std::shared_ptr<PluginServiceImpl> pluginServiceImpl_;
122     std::shared_ptr<PluginCommandBuilder> pluginCommandBuilder_;
123     std::unique_ptr<EpollEventPoller> eventPoller_;
124     PluginSessionManagerPtr pluginSessionManager_;
125     ProfilerSessionConfig profilerSessionConfig_;
126 };
127 
128 #endif // PLUGIN_SERVICE_H