• 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  * 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 
33 FTRACE_NS_BEGIN
34 using ResultPtr = std::unique_ptr<TracePluginResult>;
35 
36 class ResultTransporter {
37 public:
38     using WriterStructPtr = std::unique_ptr<WriterStruct>::pointer;
39     ResultTransporter(const std::string& name, WriterStructPtr writer);
40     ~ResultTransporter();
41 
42     bool Submit(ResultPtr&& result);
43     void SetFlushInterval(int ms);
44     void SetFlushThreshold(uint32_t nbytes);
45     void Flush();
46 
47 protected:
48     long Write(ResultPtr&& packet);
49 
50 private:
51     DISALLOW_COPY_AND_MOVE(ResultTransporter);
52     bool IsFlushTime() const;
53 
54     std::string name_;
55     std::atomic<uint64_t> bytesCount_ = 0;
56     std::atomic<uint32_t> bytesPending_ = 0;
57     std::atomic<uint64_t> timeCostUs_ = 0;
58     std::atomic<uint32_t> writeCount_ = 0;
59     std::atomic<uint32_t> flushThreshold_ = 0;
60     std::chrono::milliseconds flushInterval_;
61 
62     WriterStructPtr writer_ = nullptr;
63     std::vector<char> buffer_;
64     std::mutex mutex_;
65 };
66 FTRACE_NS_END
67 #endif // RESULT_TRANSPORTER_H
68