• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef HIPERF_PERF_FILE_WRITER_H
16 #define HIPERF_PERF_FILE_WRITER_H
17 
18 #include <functional>
19 #include <unordered_map>
20 #include <vector>
21 
22 #include "perf_event_record.h"
23 #include "perf_file_format.h"
24 #include "symbols_file.h"
25 #include "virtual_runtime.h"
26 
27 namespace OHOS {
28 namespace Developtools {
29 namespace HiPerf {
30 constexpr const int WRITER_BUFFER_SIZE = 4 * 1024 * 1024;
31 
32 // write record to data file, like perf.data.
33 // format of file follow
34 // tools/perf/Documentation/perf.data-file-format.txt
35 class PerfFileWriter {
36 public:
PerfFileWriter()37     PerfFileWriter()
38     {
39         attrSection_.offset = 0;
40         attrSection_.size = 0;
41         defaultEventAttr_.branch_sample_type = 0;
42         defaultEventAttr_.namespaces = 0;
43     }
44     ~PerfFileWriter();
45 
46     bool Open(const std::string &fileName, bool compressData = false);
47     // WriteAttrAndId() must be called before WriteRecord()
48     bool WriteAttrAndId(const std::vector<AttrWithId> &attrIds, bool isSpe = false);
49     bool WriteRecord(const PerfEventRecord &record);
50     bool AddNrCpusFeature(FEATURE feature, uint32_t nrCpusAvailable, uint32_t nrCpusOnline);
51     bool AddEventDescFeature(FEATURE feature, const std::vector<AttrWithId> &eventDesces);
52     bool AddStringFeature(FEATURE feature, std::string string);
53     bool AddU64Feature(FEATURE feature, uint64_t v);
54     bool AddBoolFeature(FEATURE feature);
55     bool AddSymbolsFeature(const std::vector<std::unique_ptr<SymbolsFile>> &);
56     bool AddUniStackTableFeature(const ProcessStackMap *table);
57     // close file
58     bool Close();
59 
60     uint64_t GetDataSize() const;
61     uint GetRecordCount() const;
62     std::chrono::microseconds writeTimes_ = std::chrono::microseconds::zero();
63 
64     using ProcessRecordCB = const std::function<bool(PerfEventRecord& record)>;
65     bool ReadDataSection(ProcessRecordCB &callback);
66     bool ReadRecords(ProcessRecordCB &callback);
67     bool Read(void *buf, size_t len);
68     void SetWriteRecordStat(bool isWrite);
69 
70 private:
71     std::string fileBuffer_;
72 
73     bool GetFilePos(uint64_t &pos) const;
74     bool Write(const void *buf, size_t len);
75     bool WriteHeader();
76     bool WriteFeatureData();
77     bool WriteTimeConvEvent();
78     bool WriteAuxTraceInfoEvent();
79     bool WriteCpuMapEvent();
80     bool WriteAuxTraceEvent(bool isSpe);
81 
82     std::string fileName_;
83     FILE *fp_ = nullptr;
84 
85     perf_file_header header_;
86     perf_file_section attrSection_;
87     perf_file_section dataSection_;
88     std::vector<std::unique_ptr<PerfFileSection>> featureSections_;
89 
90     perf_event_attr defaultEventAttr_;
91 
92     uint recordCount_ = 0;
93     bool compressData_ = false;
94     bool isWritingRecord = false;
95 };
96 } // namespace HiPerf
97 } // namespace Developtools
98 } // namespace OHOS
99 #endif // HIPERF_PERF_FILE_WRITER_H
100