xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/perf/record_parser.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PERF_RECORD_PARSER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_RECORD_PARSER_H_
19 
20 #include <stdint.h>
21 #include <cstdint>
22 #include <optional>
23 #include <string>
24 
25 #include "perfetto/base/status.h"
26 #include "perfetto/ext/base/flat_hash_map.h"
27 #include "src/trace_processor/importers/common/trace_parser.h"
28 #include "src/trace_processor/importers/perf/mmap_record.h"
29 #include "src/trace_processor/importers/perf/record.h"
30 #include "src/trace_processor/importers/perf/sample.h"
31 #include "src/trace_processor/storage/trace_storage.h"
32 #include "src/trace_processor/util/build_id.h"
33 
34 namespace perfetto {
35 namespace trace_processor {
36 
37 class DummyMemoryMapping;
38 class MappingTracker;
39 class TraceProcessorContext;
40 
41 namespace perf_importer {
42 
43 class PerfDataTracker;
44 class Reader;
45 
46 // Parses samples from perf.data files.
47 class RecordParser : public PerfRecordParser {
48  public:
49   explicit RecordParser(TraceProcessorContext*);
50   ~RecordParser() override;
51 
52   void ParsePerfRecord(int64_t timestamp, Record record) override;
53 
54  private:
55   base::Status ParseRecord(int64_t timestamp, Record record);
56   base::Status ParseSample(int64_t ts, Record record);
57   base::Status ParseComm(Record record);
58   base::Status ParseMmap(int64_t trace_ts, Record record);
59   base::Status ParseMmap2(int64_t trace_ts, Record record);
60   base::Status ParseItraceStart(Record record);
61 
62   base::Status InternSample(Sample sample);
63 
64   base::Status UpdateCounters(const Sample& sample);
65   base::Status UpdateCountersInReadGroups(const Sample& sample);
66 
67   std::optional<CallsiteId> InternCallchain(
68       UniquePid upid,
69       const std::vector<Sample::Frame>& callchain,
70       bool adjust_pc);
71 
72   UniquePid GetUpid(const CommonMmapRecordFields& fields) const;
73 
74   DummyMemoryMapping* GetDummyMapping(UniquePid upid);
75 
76   TraceProcessorContext* const context_;
77   MappingTracker* const mapping_tracker_;
78   base::FlatHashMap<UniquePid, DummyMemoryMapping*> dummy_mappings_;
79 };
80 
81 }  // namespace perf_importer
82 }  // namespace trace_processor
83 }  // namespace perfetto
84 
85 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_RECORD_PARSER_H_
86