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 "profiler_service.grpc.pb.h" 23 24 class PluginService; 25 26 class PluginSession; 27 class ResultDemuxer; 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; 99 std::shared_ptr<TraceFileWriter> traceFileWriter; 100 std::shared_ptr<ResultDemuxer> resultDemuxer; 101 102 SessionContext() = default; 103 ~SessionContext(); 104 105 bool CreatePluginSessions(); 106 bool StartPluginSessions(); 107 bool StopPluginSessions(); 108 109 size_t UpdatePluginConfigs(const std::vector<ProfilerPluginConfig>& newPluginConfigs); 110 111 void SetKeepAliveTime(uint32_t timeout); 112 void StartSessionExpireTask(); 113 void StopSessionExpireTask(); 114 DISALLOW_COPY_AND_MOVE(SessionContext); 115 }; 116 117 using SessionContextPtr = STD_PTR(shared, SessionContext); 118 119 SessionContextPtr GetSessionContext(uint32_t sessionId) const; 120 121 bool AddSessionContext(uint32_t sessionId, const SessionContextPtr& sessionCtx); 122 123 bool RemoveSessionContext(uint32_t sessionId); 124 125 private: 126 mutable std::mutex sessionContextMutex_ = {}; 127 PluginServicePtr pluginService_ = nullptr; 128 PluginSessionManagerPtr pluginSessionManager_ = nullptr; 129 std::atomic<uint32_t> sessionIdCounter_ = 0; 130 std::atomic<uint32_t> responseIdCounter_ = 0; 131 std::map<uint32_t, SessionContextPtr> sessionContext_ = {}; 132 std::unique_ptr<grpc::Server> server_ = nullptr; 133 134 DISALLOW_COPY_AND_MOVE(ProfilerService); 135 }; 136 137 #endif // PROFILER_SERVICE_H 138