• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. 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 #ifndef PROFILER_SERVICE_H
16 #define PROFILER_SERVICE_H
17 
18 #include <grpcpp/grpcpp.h>
19 #include <memory>
20 #include "logging.h"
21 #include "nocopyable.h"
22 #include "hiperf_plugin_config.pb.h"
23 #include "profiler_service.grpc.pb.h"
24 #include "schedule_task_manager.h"
25 #include "profiler_data_repeater.h"
26 
27 class PluginService;
28 
29 class PluginSession;
30 class TraceFileWriter;
31 class PluginSessionManager;
32 
33 using PluginSessionPtr = STD_PTR(shared, PluginSession);
34 using PluginServicePtr = STD_PTR(shared, PluginService);
35 using PluginSessionManagerPtr = STD_PTR(shared, PluginSessionManager);
36 
37 class ProfilerService : public IProfilerService::Service {
38 public:
39     ProfilerService(const PluginServicePtr& pluginService = nullptr);
40 
41     ~ProfilerService();
42 
43     // get all plugin infos and capabilities.
44     ::grpc::Status GetCapabilities(::grpc::ServerContext* context,
45                                    const ::GetCapabilitiesRequest* request,
46                                    ::GetCapabilitiesResponse* response) override;
47 
48     // create tracing sesion and pass tracing config to plugins.
49     ::grpc::Status CreateSession(::grpc::ServerContext* context,
50                                  const ::CreateSessionRequest* request,
51                                  ::CreateSessionResponse* response) override;
52 
53     // start tracing session, active server side tracing triggers.
54     ::grpc::Status StartSession(::grpc::ServerContext* context,
55                                 const ::StartSessionRequest* request,
56                                 ::StartSessionResponse* response) override;
57 
58     // get server-side cached tracing data since current session started.
59     ::grpc::Status FetchData(::grpc::ServerContext* context,
60                              const ::FetchDataRequest* request,
61                              ::grpc::ServerWriter<::FetchDataResponse>* writer) override;
62 
63     // get server-side cached subscribe data since current session started.
64     ::grpc::Status SubscribeProfilerEvt(::grpc::ServerContext* context,
65                              const ::SubscribeProfilerEvtRequest* request,
66                              ::grpc::ServerWriter<::SubscribeProfilerEvtResponse>* writer) override;
67 
68     // stop tracing session, deactive server side tracing triggers.
69     ::grpc::Status StopSession(::grpc::ServerContext* context,
70                                const ::StopSessionRequest* request,
71                                ::StopSessionResponse* response) override;
72 
73     // destroy tracing session.
74     ::grpc::Status DestroySession(::grpc::ServerContext* context,
75                                   const ::DestroySessionRequest* request,
76                                   ::DestroySessionResponse* response) override;
77 
78     // keep tracing session alive.
79     ::grpc::Status KeepSession(::grpc::ServerContext* context,
80                                const ::KeepSessionRequest* request,
81                                ::KeepSessionResponse* response) override;
82 
83     // only used in main, for service control.
84     bool StartService(const std::string& listenUri);
85     void WaitServiceDone();
86     void StopService();
87 
88 private:
89     static constexpr size_t DEFAULT_REPEATER_BUFFER_SIZE = 2000;
90 
91     using BufferConfig = ProfilerSessionConfig::BufferConfig;
92 
93     struct SessionContext : public std::enable_shared_from_this<SessionContext> {
94         uint32_t id = 0;
95         std::string name;
96         int32_t offlineScheduleTaskFd {-1};
97         int32_t timeoutScheduleTaskFd {-1};
98         ProfilerService* service = nullptr;
99         std::mutex sessionMutex;
100         ProfilerSessionConfig sessionConfig;
101         std::vector<std::string> pluginNames;
102         std::vector<BufferConfig> bufferConfigs;
103         std::vector<ProfilerPluginConfig> pluginConfigs;
104         std::shared_ptr<ProfilerDataRepeater<ProfilerPluginData>> dataRepeater {nullptr};
105         std::shared_ptr<ProfilerDataRepeater<ProfilerPluginState>> stateRepeater {nullptr};
106         std::shared_ptr<TraceFileWriter> traceFileWriter {nullptr};
107         ScheduleTaskManager stopExpireTask;
108 
109         SessionContext() = default;
110         ~SessionContext();
111 
112         bool CreatePluginSessions();
113         bool StartPluginSessions();
114         bool StopPluginSessions();
115 
116         size_t UpdatePluginConfigs(const std::vector<ProfilerPluginConfig>& newPluginConfigs);
117 
118         void SetKeepAliveTime(uint32_t timeout);
119         void StartSessionExpireTask(ScheduleTaskManager& task);
120         void StopSessionExpireTask(ScheduleTaskManager& task);
121         DISALLOW_COPY_AND_MOVE(SessionContext);
122     };
123 
124     using SessionContextPtr = STD_PTR(shared, SessionContext);
125 
126     SessionContextPtr GetSessionContext(uint32_t sessionId) const;
127 
128     bool AddSessionContext(uint32_t sessionId, const SessionContextPtr& sessionCtx);
129 
130     bool RemoveSessionContext(uint32_t sessionId);
131 
132     void CheckClientStatus();
133 
134     void MergeStandaloneFile(const std::string& resultFile, const std::string& pluginName,
135         const std::string& outputFile, const std::string& pluginVersion);
136 
137 private:
138     mutable std::mutex sessionContextMutex_ = {};
139     PluginServicePtr pluginService_ = nullptr;
140     PluginSessionManagerPtr pluginSessionManager_ = nullptr;
141     std::atomic<uint32_t> sessionIdCounter_ = 0;
142     std::atomic<uint32_t> responseIdCounter_ = 0;
143     std::map<uint32_t, SessionContextPtr> sessionContext_ = {};
144     std::unique_ptr<grpc::Server> server_ = nullptr;
145     ScheduleTaskManager removeTask_;
146     ScheduleTaskManager checkStatusManager_;
147     std::atomic<int32_t> heartbeatFd_ = -1;
148     std::shared_ptr<ProfilerDataRepeater<ProfilerPluginState>> stateRepeater_ = nullptr;
149 
150     DISALLOW_COPY_AND_MOVE(ProfilerService);
151 };
152 
153 #endif // PROFILER_SERVICE_H
154