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 RESULT_DEMUXER_H 16 #define RESULT_DEMUXER_H 17 18 #include <chrono> 19 #include <thread> 20 21 #include "logging.h" 22 #include "nocopyable.h" 23 #include "plugin_session_manager.h" 24 #include "profiler_data_repeater.h" 25 #include "profiler_service.grpc.pb.h" 26 #include "trace_file_writer.h" 27 28 using PluginSessionManagerPtr = STD_PTR(shared, PluginSessionManager); 29 30 class ResultDemuxer { 31 public: 32 explicit ResultDemuxer(const ProfilerDataRepeaterPtr& dataRepeater, PluginSessionManagerPtr pluginSessionManager); 33 34 ~ResultDemuxer(); 35 36 void SetTraceWriter(const TraceFileWriterPtr& traceWriter); 37 38 void SetFlushInterval(std::chrono::milliseconds interval); 39 40 bool StartTakeResults(); 41 42 bool StopTakeResults(); 43 44 private: 45 void TakeResults(); 46 47 private: 48 TraceFileWriterPtr traceWriter_ = nullptr; 49 ProfilerDataRepeaterPtr dataRepeater_ = nullptr; 50 std::chrono::milliseconds flushInterval_ {}; 51 std::chrono::steady_clock::time_point lastFlushTime_ {}; 52 std::thread demuxerThread_ {}; 53 bool isStopTakeData_ = false; 54 PluginSessionManagerPtr pluginSessionManager_ = nullptr; 55 56 DISALLOW_COPY_AND_MOVE(ResultDemuxer); 57 }; 58 59 #endif // RESULT_DEMUXER_H