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 16 17 #ifndef STACK_WRITER_H 18 #define STACK_WRITER_H 19 20 #include <atomic> 21 #include <chrono> 22 #include <memory> 23 #include <string> 24 #include "event_notifier.h" 25 #include "share_memory_block.h" 26 #include "writer.h" 27 28 class StackWriter : public Writer { 29 public: 30 StackWriter(std::string name, 31 uint32_t size, 32 int smbFd, 33 int eventFd, 34 bool blocked = false, 35 bool isSaMode = false); 36 ~StackWriter(); 37 long Write(const void* data, size_t size) override; 38 long WriteTimeout(const void* data, size_t size); 39 long WriteWithPayloadTimeout(const void* data, size_t size, const void* payload, size_t payloadSize, 40 const std::function<bool()>& callback); 41 bool Flush() override; 42 43 private: 44 void DoStats(long bytes); 45 void Report() const; 46 47 private: 48 std::string pluginName_; 49 bool blocked_ = false; 50 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock_; 51 EventNotifierPtr eventNotifier_ = nullptr; 52 std::chrono::steady_clock::time_point lastFlushTime_; 53 std::atomic<uint64_t> bytesCount_ = 0; 54 std::atomic<uint32_t> bytesPending_ = 0; 55 std::atomic<uint64_t> writeCount_ = 0; 56 std::atomic<uint32_t> flushCount_ = 0; 57 }; 58 59 60 #endif // STACK_WRITER_H