• 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);
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(std::unique_ptr<PerfEventRecord> record)>;
65     bool ReadDataSection(ProcessRecordCB &callback);
66     bool ReadRecords(ProcessRecordCB &callback);
67     bool Read(void *buf, size_t len);
68 
69 private:
70     std::string fileBuffer_;
71 
72     bool GetFilePos(uint64_t &pos) const;
73     bool Write(const void *buf, size_t len);
74     bool WriteHeader();
75     bool WriteFeatureData();
76 
77     std::string fileName_;
78     FILE *fp_ = nullptr;
79 
80     perf_file_header header_;
81     perf_file_section attrSection_;
82     perf_file_section dataSection_;
83     std::vector<std::unique_ptr<PerfFileSection>> featureSections_;
84 
85     perf_event_attr defaultEventAttr_;
86 
87     uint recordCount_ = 0;
88     bool compressData_ = false;
89     bool isWritingRecord = false;
90 };
91 } // namespace HiPerf
92 } // namespace Developtools
93 } // namespace OHOS
94 #endif // HIPERF_PERF_FILE_WRITER_H
95