2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef SIMPLE_PERF_RECORD_H_
18 #define SIMPLE_PERF_RECORD_H_
23 #include "perf_event.h"
30 enum user_record_type {
31 PERF_RECORD_ATTR = 64,
32 PERF_RECORD_EVENT_TYPE,
33 PERF_RECORD_TRACING_DATA,
35 PERF_RECORD_FINISHED_ROUND,
38 struct PerfSampleIpType {
42 struct PerfSampleTidType {
46 struct PerfSampleTimeType {
50 struct PerfSampleAddrType {
54 struct PerfSampleIdType {
58 struct PerfSampleStreamIdType {
62 struct PerfSampleCpuType {
66 struct PerfSamplePeriodType {
70 // SampleId is optional at the end of a record in binary format. Its content is determined by
71 // sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to
72 // perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure.
77 PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID.
78 PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME.
79 PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID.
80 PerfSampleStreamIdType stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID.
81 PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU.
85 // Create the content of sample_id. It depends on the attr we use.
86 size_t CreateContent(const perf_event_attr& attr);
88 // Parse sample_id from binary format in the buffer pointed by p.
89 void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end);
91 // Write the binary format of sample_id to the buffer pointed by p.
92 void WriteToBinaryFormat(char*& p) const;
93 void Dump(size_t indent) const;
96 // Usually one record contains the following three parts in order in binary format:
97 // perf_event_header (at the head of a record, containing type and size information)
98 // data depends on the record type
99 // sample_id (optional part at the end of a record)
100 // We hold the common parts (perf_event_header and sample_id) in the base class Record, and
101 // hold the type specific data part in classes derived from Record.
103 perf_event_header header;
107 Record(const perf_event_header* pheader);
112 void Dump(size_t indent = 0) const;
115 virtual void DumpData(size_t) const {
119 struct MmapRecord : public Record {
120 struct MmapRecordDataType {
126 std::string filename;
128 MmapRecord() { // For storage in std::vector.
131 MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader);
132 void DumpData(size_t indent) const override;
133 std::vector<char> BinaryFormat() const;
136 struct CommRecord : public Record {
137 struct CommRecordDataType {
145 CommRecord(const perf_event_attr& attr, const perf_event_header* pheader);
146 void DumpData(size_t indent) const override;
147 std::vector<char> BinaryFormat() const;
150 struct ExitRecord : public Record {
151 struct ExitRecordDataType {
157 ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader);
158 void DumpData(size_t indent) const override;
161 struct SampleRecord : public Record {
162 uint64_t sample_type; // sample_type is a bit mask determining which fields below are valid.
164 PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP.
165 PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID.
166 PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME.
167 PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR.
168 PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID.
169 PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID.
170 PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU.
171 PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD.
173 SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader);
174 void DumpData(size_t indent) const override;
177 std::unique_ptr<const Record> ReadRecordFromBuffer(const perf_event_attr& attr,
178 const perf_event_header* pheader);
180 #endif // SIMPLE_PERF_RECORD_H_