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 ResultDemuxer; 29 class TraceFileWriter; 30 class ProfilerDataRepeater; 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 // stop tracing session, deactive server side tracing triggers. 64 ::grpc::Status StopSession(::grpc::ServerContext* context, 65 const ::StopSessionRequest* request, 66 ::StopSessionResponse* response) override; 67 68 // destroy tracing session. 69 ::grpc::Status DestroySession(::grpc::ServerContext* context, 70 const ::DestroySessionRequest* request, 71 ::DestroySessionResponse* response) override; 72 73 // keep tracing session alive. 74 ::grpc::Status KeepSession(::grpc::ServerContext* context, 75 const ::KeepSessionRequest* request, 76 ::KeepSessionResponse* response) override; 77 78 // only used in main, for service control. 79 bool StartService(const std::string& listenUri); 80 void WaitServiceDone(); 81 void StopService(); 82 83 private: 84 static constexpr size_t DEFAULT_REPEATER_BUFFER_SIZE = 2000; 85 86 using BufferConfig = ProfilerSessionConfig::BufferConfig; 87 88 struct SessionContext : public std::enable_shared_from_this<SessionContext> { 89 uint32_t id = 0; 90 std::string name; 91 std::string offlineTask; // offline sample task name 92 std::string timeoutTask; // session timeout task name 93 ProfilerService* service = nullptr; 94 std::mutex sessionMutex; 95 ProfilerSessionConfig sessionConfig; 96 std::vector<std::string> pluginNames; 97 std::vector<BufferConfig> bufferConfigs; 98 std::vector<ProfilerPluginConfig> pluginConfigs; 99 std::shared_ptr<ProfilerDataRepeater> dataRepeater; 100 std::shared_ptr<TraceFileWriter> traceFileWriter; 101 std::shared_ptr<ResultDemuxer> resultDemuxer; 102 103 SessionContext() = default; 104 ~SessionContext(); 105 106 bool CreatePluginSessions(); 107 bool StartPluginSessions(); 108 bool StopPluginSessions(); 109 110 size_t UpdatePluginConfigs(const std::vector<ProfilerPluginConfig>& newPluginConfigs); 111 112 void SetKeepAliveTime(uint32_t timeout); 113 void StartSessionExpireTask(); 114 void StopSessionExpireTask(); 115 DISALLOW_COPY_AND_MOVE(SessionContext); 116 }; 117 118 using SessionContextPtr = STD_PTR(shared, SessionContext); 119 120 SessionContextPtr GetSessionContext(uint32_t sessionId) const; 121 122 bool AddSessionContext(uint32_t sessionId, const SessionContextPtr& sessionCtx); 123 124 bool RemoveSessionContext(uint32_t sessionId); 125 126 void MergeStandaloneFile(const std::string& resultFile, const std::string& pluginName, 127 const std::string& outputFile, const std::string& pluginVersion); 128 129 private: 130 mutable std::mutex sessionContextMutex_ = {}; 131 PluginServicePtr pluginService_ = nullptr; 132 PluginSessionManagerPtr pluginSessionManager_ = nullptr; 133 std::atomic<uint32_t> sessionIdCounter_ = 0; 134 std::atomic<uint32_t> responseIdCounter_ = 0; 135 std::map<uint32_t, SessionContextPtr> sessionContext_ = {}; 136 std::unique_ptr<grpc::Server> server_ = nullptr; 137 138 DISALLOW_COPY_AND_MOVE(ProfilerService); 139 }; 140 141 #endif // PROFILER_SERVICE_H 142