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