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
16#ifndef HIPERF_FILE_READER
17#define HIPERF_FILE_READER
18
19#include <functional>
20#include <string>
21#include <unordered_map>
22
23#include "perf_event_record.h"
24#include "perf_file_format.h"
25
26namespace OHOS {
27namespace Developtools {
28namespace HiPerf {
29using ProcessRecordCB = const std::function<bool(std::unique_ptr<PerfEventRecord> record)>;
30// read record from data file, like perf.data.
31// format of file follow
32// tools/perf/Documentation/perf.data-file-format.txt
33class PerfFileReader {
34public:
35    virtual ~PerfFileReader();
36
37    static std::unique_ptr<PerfFileReader> Instance(const std::string &fileName);
38
39    const perf_file_header &GetHeader() const;
40
41    std::vector<AttrWithId> GetAttrSection() const;
42
43    // read data section, construct record, call callback for each record
44    bool ReadDataSection(ProcessRecordCB &callback);
45
46    bool ReadFeatureSection();
47    const std::vector<FEATURE> &GetFeatures() const;
48    const std::vector<std::unique_ptr<PerfFileSection>> &GetFeatureSections() const;
49    const PerfFileSection *GetFeatureSection(FEATURE feature) const;
50    explicit PerfFileReader(const std::string &fileName, FILE *fp);
51
52    const std::string GetFeatureString(const FEATURE feature) const;
53
54    bool IsFeatrureStringSection(const FEATURE featureId) const
55    {
56        return find(FeatureStrings.begin(), FeatureStrings.end(), featureId) !=
57               FeatureStrings.end();
58    }
59
60    // fuzz user this
61protected:
62    virtual bool Read(void *buf, size_t len);
63    virtual bool Read(char *buf, uint64_t offset, size_t len);
64    FILE *fp_ = nullptr;
65    bool ReadFileHeader();
66    bool ReadAttrSection();
67
68private:
69    bool ReadRecord(ProcessRecordCB &callback);
70    bool IsValidDataFile();
71    bool IsGzipFile();
72
73    // file header must be read first
74
75    bool ReadIdsForAttr(const perf_file_attr &attr, std::vector<uint64_t> *ids);
76
77    const perf_event_attr *GetDefaultAttr();
78
79    const std::string fileName_;
80    uint64_t dataSectionSize_;
81    bool compressData_ = false;
82
83    perf_file_header header_;
84    std::vector<perf_file_attr> vecAttr_;
85    std::vector<std::vector<uint64_t>> vecAttrIds_;
86
87    std::unordered_map<uint64_t, size_t> mapId2Attr_;
88    uint64_t featureSectionOffset_;
89    std::vector<FEATURE> features_;
90    std::vector<std::unique_ptr<PerfFileSection>> perfFileSections_;
91
92    size_t fileSize_ = 0;
93#ifdef HIPERF_DEBUG_TIME
94    std::chrono::microseconds readRecordTime_ = std::chrono::microseconds::zero();
95    std::chrono::microseconds readCallbackTime_ = std::chrono::microseconds::zero();
96#endif
97};
98} // namespace HiPerf
99} // namespace Developtools
100} // namespace OHOS
101#endif // HIPERF_FILE_READER
102