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 #ifndef NETWORK_PROFILER_HANDLE_H 17 #define NETWORK_PROFILER_HANDLE_H 18 19 #include <chrono> 20 #include <variant> 21 22 #include "logging.h" 23 24 #include "buffer_writer.h" 25 #include "network_profiler_result.pb.h" 26 #include "network_profiler_result.pbencoder.h" 27 28 using WriterStructPtr = std::unique_ptr<WriterStruct>::pointer; 29 namespace OHOS::Developtools::Profiler { 30 class NetworkProfilerManager; 31 32 class NetworkProfilerHandle { 33 public: 34 explicit NetworkProfilerHandle(clockid_t pluginDataClockId, uint32_t bufferSize, bool isProtobufSerialize = true); 35 ~NetworkProfilerHandle(); 36 37 void SetWriter(const std::shared_ptr<Writer>& writer); 38 void SetWriter(const WriterStructPtr& writer); 39 void SerializeData(const int8_t data[], uint32_t size); SetTargetProcessInfo(uint32_t pid,const std::string & name)40 void SetTargetProcessInfo(uint32_t pid, const std::string& name) 41 { 42 pid_ = pid; 43 processName_ = name; 44 } 45 46 private: 47 template <typename T> 48 void FlushCheck(T& protoData); 49 void FlushData(::NetworkProfilerResult& data); 50 void FlushData(ProtoEncoder::NetworkProfilerResult& data); 51 template <typename T> 52 void SerializeDataImpl(T& protoData, const int8_t data[], uint32_t size); 53 template <typename T> 54 void SerializeBaseData(T& baseDataProto, const int8_t* data); 55 template <typename T> 56 void SerializeTraceData(T& protoData, const int8_t data[], uint32_t size); 57 template <typename T> 58 void SerializeThreadData(T& protoData, const int8_t data[], uint32_t size); 59 template <typename T> 60 void SerializeRawData(T& protoData, const int8_t data[], uint32_t size); 61 62 private: 63 std::unique_ptr<uint8_t[]> buffer_{nullptr}; 64 std::shared_ptr<Writer> writer_{nullptr}; 65 WriterStructPtr writerStruct_{nullptr}; 66 bool isProtobufSerialize_{true}; 67 clockid_t pluginDataClockId_ = CLOCK_REALTIME; 68 std::variant<::NetworkProfilerResult, ProtoEncoder::NetworkProfilerResult> protoData_; 69 uint64_t flushSize_{0}; 70 uint32_t flushCount_{0}; 71 uint32_t bufferSize_{0}; 72 uint32_t pid_{0}; 73 std::string processName_; 74 }; 75 } // OHOS::Developtools::Profiler 76 77 #endif // NETWORK_PROFILER_HANDLE_H