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 16 #ifndef BUFFER_WRITER_H 17 #define BUFFER_WRITER_H 18 19 #include <atomic> 20 #include <memory> 21 #include "event_notifier.h" 22 #include "plugin_module_api.h" 23 #include "share_memory_allocator.h" 24 #include "writer.h" 25 26 27 class BufferWriter : public Writer { 28 public: 29 BufferWriter(std::string name, 30 uint32_t size, 31 int smbFd, 32 int eventFd, 33 uint32_t pluginId); 34 ~BufferWriter(); 35 long Write(const void* data, size_t size) override; 36 bool Flush() override; 37 38 bool WriteMessage(const google::protobuf::Message& pmsg); 39 private: 40 void DoStats(long bytes); 41 void Report() const; 42 43 private: 44 std::string pluginName_; 45 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock_; 46 EventNotifierPtr eventNotifier_ = nullptr; 47 std::chrono::steady_clock::time_point lastFlushTime_; 48 std::atomic<uint64_t> bytesCount_ = 0; 49 std::atomic<uint32_t> bytesPending_ = 0; 50 std::atomic<uint64_t> writeCount_ = 0; 51 std::atomic<uint32_t> flushCount_ = 0; 52 uint32_t pluginId_ = 0; 53 }; 54 55 using BufferWriterPtr = STD_PTR(shared, BufferWriter); 56 57 #endif // BUFFER_WRITER_H 58