1 /* 2 * Copyright (c) 2021-2023 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 std::string version, 31 uint32_t size, 32 int smbFd, 33 int eventFd, 34 uint32_t pluginId); 35 ~BufferWriter(); 36 long Write(const void* data, size_t size) override; 37 bool Flush() override; 38 bool Clear() override; 39 40 bool WriteMessage(const google::protobuf::Message& pmsg, const std::string& pluginName); 41 SetClockId(clockid_t clockId)42 void SetClockId(clockid_t clockId) override 43 { 44 clockId_ = clockId; 45 } 46 private: 47 void DoStats(long bytes); 48 void Report() const; 49 50 private: 51 std::string pluginName_; 52 std::string pluginVersion_; 53 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock_; 54 EventNotifierPtr eventNotifier_ = nullptr; 55 std::chrono::steady_clock::time_point lastFlushTime_; 56 std::atomic<uint64_t> bytesCount_ = 0; 57 std::atomic<uint32_t> bytesPending_ = 0; 58 std::atomic<uint64_t> writeCount_ = 0; 59 std::atomic<uint32_t> flushCount_ = 0; 60 uint32_t pluginId_ = 0; 61 clockid_t clockId_ = CLOCK_REALTIME; 62 }; 63 64 using BufferWriterPtr = STD_PTR(shared, BufferWriter); 65 66 #endif // BUFFER_WRITER_H 67