1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 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 HIPERF_REPORT_PROTOBUF_FILE 17 #define HIPERF_REPORT_PROTOBUF_FILE 18 19 #include <fstream> 20 #include <stdint.h> 21 #include <linux/perf_event.h> 22 23 #include "google/protobuf/io/coded_stream.h" 24 #include "google/protobuf/io/zero_copy_stream_impl_lite.h" 25 26 #include "debug_logger.h" 27 #include "perf_event_record.h" 28 #include "report_sample.pb.h" 29 #include "symbols_file.h" 30 #include "utilities.h" 31 32 namespace Proto = OHOS::Developtools::Hiperf::Proto; 33 namespace OHOS { 34 namespace Developtools { 35 namespace HiPerf { 36 static const char FILE_MAGIC[] = "HIPERF_PB_"; 37 static const uint16_t FILE_VERSION = 1u; 38 using ProtobufReadBack = std::function<void(Proto::HiperfRecord &record)>; 39 40 class ReportProtobufFileWriter : public google::protobuf::io::CopyingOutputStream { 41 public: 42 bool Create(const std::string fileName); 43 44 bool ProcessRecord(const PerfEventRecord &record); 45 bool ProcessSampleRecord(const PerfRecordSample &recordSample, uint32_t configIndex, 46 const std::vector<std::unique_ptr<SymbolsFile>> &symbolsFiles); 47 bool ProcessSymbolsFiles(const std::vector<std::unique_ptr<SymbolsFile>> &symbolsFiles); 48 bool ProcessReportInfo(const std::vector<std::string> &configNames, 49 const std::string &workloadCmd); 50 51 ~ReportProtobufFileWriter(); 52 void Close(); 53 54 private: 55 std::unique_ptr<google::protobuf::io::CopyingOutputStreamAdaptor> protpbufOutputStream_; 56 std::unique_ptr<google::protobuf::io::CodedOutputStream> protpbufCodedOutputStream_; 57 std::string fileName_; 58 std::unique_ptr<std::ofstream> protobufFileStream_ = std::make_unique<std::ofstream>(); 59 60 uint64_t recordCount_ = 0; 61 uint64_t recordLost_ = 0; 62 63 bool IsOpen(); 64 bool Write(const void *buffer, const int size) override; 65 virtual bool ProcessRecord(const PerfRecordComm &recordComm); 66 virtual bool ProcessRecord(const PerfRecordLost &recordLost); 67 void BeforeClose(); 68 }; 69 70 class ReportProtobufFileReader : public google::protobuf::io::CopyingInputStream { 71 public: 72 bool Dump(const std::string fileName, ProtobufReadBack readBack = nullptr); 73 74 private: 75 std::unique_ptr<google::protobuf::io::CopyingInputStreamAdaptor> protpbufInputStream_; 76 std::unique_ptr<google::protobuf::io::CodedInputStream> protpbufCodedInputStream_; 77 std::string fileName_; 78 std::unique_ptr<std::ifstream> protobufFileStream_ = std::make_unique<std::ifstream>(); 79 80 bool IsOpen(); 81 bool CheckFileMagic(); 82 int Read(void *buffer, const int size) override; 83 bool Dump(const Proto::HiperfRecord &record, const int indent = 0); 84 bool Dump(const Proto::CallStackSample &message, const int indent = 0); 85 bool Dump(const Proto::SampleStatistic &message, const int indent = 0); 86 bool Dump(const Proto::SymbolTableFile &message, const int indent = 0); 87 bool Dump(const Proto::VirtualThreadInfo &message, const int indent = 0); 88 bool Dump(const Proto::ReportInfo &message, const int indent = 0); 89 }; 90 } // namespace HiPerf 91 } // namespace Developtools 92 } // namespace OHOS 93 #endif // HIPERF_REPORT_PROTOBUF_FILE 94