1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. 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 FFRT_PROFILER_WRITER_H 18 #define FFRT_PROFILER_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 namespace OHOS::Developtools::Profiler { 29 class FfrtProfilerWriter : public Writer { 30 public: 31 FfrtProfilerWriter(std::string name, 32 uint32_t size, 33 int smbFd, 34 int eventFd, 35 bool block); 36 ~FfrtProfilerWriter(); 37 long Write(const void* data, size_t size) override; 38 long WriteTimeout(const void* data, size_t size, const std::function<bool()>& callback); 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 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock_{nullptr}; 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 bool block_{false}; 57 }; 58 } // namespace OHOS::Developtools::Profiler 59 60 #endif // FFRT_PROFILER_WRITER_H