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