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