summaryrefslogtreecommitdiff
path: root/simpleperf/record.h
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2015-04-28 15:54:13 -0700
committerYabin Cui <yabinc@google.com>2015-05-04 14:32:32 -0700
commit9759e1b1ce76185aa539aeea2fb1cbd8382156e7 (patch)
tree69196161e28ebe74fb27093e026983fd98b004da /simpleperf/record.h
parent249518de7cb7ddb1c066b3bb8b10bc0f66222f7d (diff)
downloadextras-9759e1b1ce76185aa539aeea2fb1cbd8382156e7.tar.gz
Implement simpleperf record/dumprecord subcommands.
Bug: 19483574 Change-Id: Id879713a75c2d3a6289d8847b95ee0bb4a2cc8a0
Diffstat (limited to 'simpleperf/record.h')
-rw-r--r--simpleperf/record.h180
1 files changed, 180 insertions, 0 deletions
diff --git a/simpleperf/record.h b/simpleperf/record.h
new file mode 100644
index 00000000..fbd523d1
--- /dev/null
+++ b/simpleperf/record.h
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SIMPLE_PERF_RECORD_H_
+#define SIMPLE_PERF_RECORD_H_
+
+#include <string>
+#include <vector>
+
+#include "perf_event.h"
+
+struct KernelMmap;
+struct ModuleMmap;
+struct ThreadComm;
+struct ThreadMmap;
+
+enum user_record_type {
+ PERF_RECORD_ATTR = 64,
+ PERF_RECORD_EVENT_TYPE,
+ PERF_RECORD_TRACING_DATA,
+ PERF_RECORD_BUILD_ID,
+ PERF_RECORD_FINISHED_ROUND,
+};
+
+struct PerfSampleIpType {
+ uint64_t ip;
+};
+
+struct PerfSampleTidType {
+ uint32_t pid, tid;
+};
+
+struct PerfSampleTimeType {
+ uint64_t time;
+};
+
+struct PerfSampleAddrType {
+ uint64_t addr;
+};
+
+struct PerfSampleIdType {
+ uint64_t id;
+};
+
+struct PerfSampleStreamIdType {
+ uint64_t stream_id;
+};
+
+struct PerfSampleCpuType {
+ uint32_t cpu, res;
+};
+
+struct PerfSamplePeriodType {
+ uint64_t period;
+};
+
+// SampleId is optional at the end of a record in binary format. Its content is determined by
+// sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to
+// perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure.
+struct SampleId {
+ bool sample_id_all;
+ uint64_t sample_type;
+
+ PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID.
+ PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME.
+ PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID.
+ PerfSampleStreamIdType stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID.
+ PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU.
+
+ SampleId();
+
+ // Create the content of sample_id. It depends on the attr we use.
+ size_t CreateContent(const perf_event_attr& attr);
+
+ // Parse sample_id from binary format in the buffer pointed by p.
+ void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end);
+
+ // Write the binary format of sample_id to the buffer pointed by p.
+ void WriteToBinaryFormat(char*& p) const;
+ void Dump(size_t indent) const;
+};
+
+// Usually one record contains the following three parts in order in binary format:
+// perf_event_header (at the head of a record, containing type and size information)
+// data depends on the record type
+// sample_id (optional part at the end of a record)
+// We hold the common parts (perf_event_header and sample_id) in the base class Record, and
+// hold the type specific data part in classes derived from Record.
+struct Record {
+ perf_event_header header;
+ SampleId sample_id;
+
+ Record();
+ Record(const perf_event_header* pheader);
+
+ virtual ~Record() {
+ }
+
+ void Dump(size_t indent = 0) const;
+
+ protected:
+ virtual void DumpData(size_t) const {
+ }
+};
+
+struct MmapRecord : public Record {
+ struct MmapRecordDataType {
+ uint32_t pid, tid;
+ uint64_t addr;
+ uint64_t len;
+ uint64_t pgoff;
+ } data;
+ std::string filename;
+
+ MmapRecord() { // For storage in std::vector.
+ }
+
+ MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader);
+ void DumpData(size_t indent) const override;
+ std::vector<char> BinaryFormat() const;
+};
+
+struct CommRecord : public Record {
+ struct CommRecordDataType {
+ uint32_t pid, tid;
+ } data;
+ std::string comm;
+
+ CommRecord() {
+ }
+
+ CommRecord(const perf_event_attr& attr, const perf_event_header* pheader);
+ void DumpData(size_t indent) const override;
+ std::vector<char> BinaryFormat() const;
+};
+
+struct ExitRecord : public Record {
+ struct ExitRecordDataType {
+ uint32_t pid, ppid;
+ uint32_t tid, ptid;
+ uint64_t time;
+ } data;
+
+ ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader);
+ void DumpData(size_t indent) const override;
+};
+
+struct SampleRecord : public Record {
+ uint64_t sample_type; // sample_type is a bit mask determining which fields below are valid.
+
+ PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP.
+ PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID.
+ PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME.
+ PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR.
+ PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID.
+ PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID.
+ PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU.
+ PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD.
+
+ SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader);
+ void DumpData(size_t indent) const override;
+};
+
+std::unique_ptr<const Record> ReadRecordFromBuffer(const perf_event_attr& attr,
+ const perf_event_header* pheader);
+
+#endif // SIMPLE_PERF_RECORD_H_