• 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  * Description: StreamTransporter class define
16  */
17 #ifndef RESULT_TRANSPORTER_H
18 #define RESULT_TRANSPORTER_H
19 #include <atomic>
20 #include <chrono>
21 #include <condition_variable>
22 #include <fcntl.h>
23 #include <iostream>
24 #include <memory>
25 #include <mutex>
26 #include <thread>
27 #include <vector>
28 
29 #include "ftrace_namespace.h"
30 #include "plugin_module_api.h"
31 #include "trace_plugin_result.pb.h"
32 #include "trace_plugin_result.pbencoder.h"
33 
34 FTRACE_NS_BEGIN
35 using ResultPtr = std::unique_ptr<TracePluginResult>;
36 
37 class ResultTransporter {
38 public:
39     using WriterStructPtr = std::unique_ptr<WriterStruct>::pointer;
40     ResultTransporter(const std::string& name, WriterStructPtr writer);
41     ~ResultTransporter();
42 
43     bool Submit(ResultPtr&& result);
44     void Report(size_t msgSize);
45     void SetFlushInterval(int ms);
46     void SetFlushThreshold(uint32_t nbytes);
47     void Flush();
48 
49 protected:
50     long Write(ResultPtr&& packet);
51 
52 private:
53     DISALLOW_COPY_AND_MOVE(ResultTransporter);
54     bool IsFlushTime() const;
55 
56     std::string name_;
57     std::atomic<uint64_t> bytesCount_ = 0;
58     std::atomic<uint32_t> bytesPending_ = 0;
59     std::atomic<uint64_t> timeCostUs_ = 0;
60     std::atomic<uint32_t> writeCount_ = 0;
61     std::atomic<uint32_t> flushThreshold_ = 0;
62     std::chrono::milliseconds flushInterval_;
63 
64     WriterStructPtr writer_ = nullptr;
65     std::vector<char> buffer_;
66     std::mutex mutex_;
67 };
68 FTRACE_NS_END
69 #endif // RESULT_TRANSPORTER_H
70