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