1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. 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 #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 #include "common_types.pbencoder.h" 27 28 class BufferWriter : public Writer { 29 public: 30 BufferWriter(std::string name, 31 std::string version, 32 uint32_t size, 33 int smbFd, 34 int eventFd, 35 uint32_t pluginId); 36 ~BufferWriter(); 37 long Write(const void* data, size_t size) override; 38 bool Flush() override; 39 void UseMemory(int32_t size) override; 40 RandomWriteCtx* StartReport() override; 41 void FinishReport(int32_t size) override; 42 void ResetPos() override; GetCtx()43 RandomWriteCtx* GetCtx() override 44 { 45 return writeCtx_; 46 } 47 48 bool WriteMessage(const google::protobuf::Message& pmsg, const std::string& pluginName); 49 SetClockId(clockid_t clockId)50 void SetClockId(clockid_t clockId) override 51 { 52 clockId_ = clockId; 53 } 54 private: 55 void DoStats(long bytes); 56 void Report() const; 57 58 private: 59 std::string pluginName_; 60 std::string pluginVersion_; 61 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock_; 62 EventNotifierPtr eventNotifier_ = nullptr; 63 std::chrono::steady_clock::time_point lastFlushTime_; 64 std::atomic<uint64_t> bytesCount_ = 0; 65 std::atomic<uint32_t> bytesPending_ = 0; 66 std::atomic<uint64_t> writeCount_ = 0; 67 std::atomic<uint32_t> flushCount_ = 0; 68 uint32_t pluginId_ = 0; 69 clockid_t clockId_ = CLOCK_REALTIME; 70 RandomWriteCtx* writeCtx_{nullptr}; 71 OHOS::Developtools::Profiler::ProtoEncoder::ProfilerPluginData profilerPluginData_; 72 }; 73 74 using BufferWriterPtr = STD_PTR(shared, BufferWriter); 75 76 #endif // BUFFER_WRITER_H 77