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 StopHandle(); 40 void SerializeData(const int8_t data[], uint32_t size); SetTargetProcessInfo(uint32_t pid,const std::string & name)41 void SetTargetProcessInfo(uint32_t pid, const std::string& name) 42 { 43 pid_ = pid; 44 processName_ = name; 45 } 46 47 private: 48 template <typename T> 49 void FlushCheck(T& protoData); 50 void FlushData(::NetworkProfilerResult& data); 51 void FlushData(ProtoEncoder::NetworkProfilerResult& data); 52 template <typename T> 53 void SerializeDataImpl(T& protoData, const int8_t data[], uint32_t size); 54 template <typename T> 55 void SerializeBaseData(T& baseDataProto, const int8_t* data); 56 template <typename T> 57 void SerializeTraceData(T& protoData, const int8_t data[], uint32_t size); 58 template <typename T> 59 void SerializeThreadData(T& protoData, const int8_t data[], uint32_t size); 60 template <typename T> 61 void SerializeRawData(T& protoData, const int8_t data[], uint32_t size); 62 63 private: 64 std::unique_ptr<uint8_t[]> buffer_{nullptr}; 65 std::shared_ptr<Writer> writer_{nullptr}; 66 WriterStructPtr writerStruct_{nullptr}; 67 bool isProtobufSerialize_{true}; 68 clockid_t pluginDataClockId_ = CLOCK_REALTIME; 69 std::variant<::NetworkProfilerResult, ProtoEncoder::NetworkProfilerResult> protoData_; 70 uint64_t flushSize_{0}; 71 uint32_t flushCount_{0}; 72 uint32_t bufferSize_{0}; 73 uint32_t pid_{0}; 74 std::string processName_; 75 }; 76 } // OHOS::Developtools::Profiler 77 78 #endif // NETWORK_PROFILER_HANDLE_H