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 #include <gtest/gtest.h>
23 #include "environment.h"
24 #include "event_attr.h"
25 #include "event_type.h"
27 #include "record_file.h"
29 #include "record_equal_test.h"
31 using namespace PerfFileFormat;
33 class RecordFileTest : public ::testing::Test {
35 virtual void SetUp() {
36 filename_ = "temporary.record_file";
39 void AddEventType(const std::string& event_type_str) {
40 std::unique_ptr<EventTypeAndModifier> event_type_modifier = ParseEventType(event_type_str);
41 ASSERT_TRUE(event_type_modifier != nullptr);
42 perf_event_attr attr = CreateDefaultPerfEventAttr(event_type_modifier->event_type);
43 attrs_.push_back(std::unique_ptr<perf_event_attr>(new perf_event_attr(attr)));
45 attr_id.attr = attrs_.back().get();
46 attr_id.ids.push_back(attrs_.size()); // Fake id.
47 attr_ids_.push_back(attr_id);
50 std::string filename_;
51 std::vector<std::unique_ptr<perf_event_attr>> attrs_;
52 std::vector<AttrWithId> attr_ids_;
55 TEST_F(RecordFileTest, smoke) {
56 // Write to a record file.
57 std::unique_ptr<RecordFileWriter> writer = RecordFileWriter::CreateInstance(filename_);
58 ASSERT_TRUE(writer != nullptr);
60 // Write attr section.
61 AddEventType("cpu-cycles");
62 ASSERT_TRUE(writer->WriteAttrSection(attr_ids_));
64 // Write data section.
65 MmapRecord mmap_record = CreateMmapRecord(*(attr_ids_[0].attr), true, 1, 1, 0x1000, 0x2000,
66 0x3000, "mmap_record_example");
67 ASSERT_TRUE(writer->WriteData(mmap_record.BinaryFormat()));
69 // Check data section that has been written.
70 std::vector<std::unique_ptr<Record>> records;
71 ASSERT_TRUE(writer->ReadDataSection(&records));
72 ASSERT_EQ(1u, records.size());
73 CheckRecordEqual(mmap_record, *records[0]);
75 // Write feature section.
76 ASSERT_TRUE(writer->WriteFeatureHeader(1));
77 char p[BuildId::Size()];
78 for (size_t i = 0; i < BuildId::Size(); ++i) {
82 BuildIdRecord build_id_record = CreateBuildIdRecord(false, getpid(), build_id, "init");
83 ASSERT_TRUE(writer->WriteBuildIdFeature({build_id_record}));
84 ASSERT_TRUE(writer->Close());
86 // Read from a record file.
87 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(filename_);
88 ASSERT_TRUE(reader != nullptr);
89 const FileHeader* file_header = reader->FileHeader();
90 ASSERT_TRUE(file_header != nullptr);
91 std::vector<const FileAttr*> file_attrs = reader->AttrSection();
92 ASSERT_EQ(1u, file_attrs.size());
93 ASSERT_EQ(0, memcmp(&file_attrs[0]->attr, attr_ids_[0].attr, sizeof(perf_event_attr)));
94 std::vector<uint64_t> ids = reader->IdsForAttr(file_attrs[0]);
95 ASSERT_EQ(ids, attr_ids_[0].ids);
97 // Read and check data section.
98 records = reader->DataSection();
99 ASSERT_EQ(1u, records.size());
100 CheckRecordEqual(mmap_record, *records[0]);
102 // Read and check feature section.
103 ASSERT_TRUE(file_header->features[FEAT_BUILD_ID / 8] & (1 << (FEAT_BUILD_ID % 8)));
104 std::map<int, SectionDesc> sections = reader->FeatureSectionDescriptors();
105 ASSERT_EQ(1u, sections.size());
106 ASSERT_TRUE(sections.find(FEAT_BUILD_ID) != sections.end());
107 const perf_event_header* header = reinterpret_cast<const perf_event_header*>(
108 reader->DataAtOffset(sections[FEAT_BUILD_ID].offset));
109 ASSERT_TRUE(header != nullptr);
110 ASSERT_EQ(sections[FEAT_BUILD_ID].size, header->size);
111 CheckRecordEqual(build_id_record, BuildIdRecord(header));
113 ASSERT_TRUE(reader->Close());
116 TEST_F(RecordFileTest, records_sorted_by_time) {
117 // Write to a record file.
118 std::unique_ptr<RecordFileWriter> writer = RecordFileWriter::CreateInstance(filename_);
119 ASSERT_TRUE(writer != nullptr);
121 // Write attr section.
122 AddEventType("cpu-cycles");
123 attrs_[0]->sample_id_all = 1;
124 attrs_[0]->sample_type |= PERF_SAMPLE_TIME;
125 ASSERT_TRUE(writer->WriteAttrSection(attr_ids_));
127 // Write data section.
129 CreateMmapRecord(*(attr_ids_[0].attr), true, 1, 1, 0x100, 0x2000, 0x3000, "mmap_record1");
132 r1.sample_id.time_data.time = 2;
133 r2.sample_id.time_data.time = 1;
134 r3.sample_id.time_data.time = 3;
135 ASSERT_TRUE(writer->WriteData(r1.BinaryFormat()));
136 ASSERT_TRUE(writer->WriteData(r2.BinaryFormat()));
137 ASSERT_TRUE(writer->WriteData(r3.BinaryFormat()));
138 ASSERT_TRUE(writer->Close());
140 // Read from a record file.
141 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(filename_);
142 ASSERT_TRUE(reader != nullptr);
143 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
144 ASSERT_EQ(3u, records.size());
145 CheckRecordEqual(r2, *records[0]);
146 CheckRecordEqual(r1, *records[1]);
147 CheckRecordEqual(r3, *records[2]);
149 ASSERT_TRUE(reader->Close());
152 TEST_F(RecordFileTest, record_more_than_one_attr) {
153 // Write to a record file.
154 std::unique_ptr<RecordFileWriter> writer = RecordFileWriter::CreateInstance(filename_);
155 ASSERT_TRUE(writer != nullptr);
157 // Write attr section.
158 AddEventType("cpu-cycles");
159 AddEventType("cpu-clock");
160 AddEventType("task-clock");
161 ASSERT_TRUE(writer->WriteAttrSection(attr_ids_));
163 ASSERT_TRUE(writer->Close());
165 // Read from a record file.
166 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(filename_);
167 ASSERT_TRUE(reader != nullptr);
168 std::vector<const FileAttr*> file_attrs = reader->AttrSection();
169 ASSERT_EQ(3u, file_attrs.size());
170 for (size_t i = 0; i < file_attrs.size(); ++i) {
171 ASSERT_EQ(0, memcmp(&file_attrs[i]->attr, attr_ids_[i].attr, sizeof(perf_event_attr)));
172 std::vector<uint64_t> ids = reader->IdsForAttr(file_attrs[i]);
173 ASSERT_EQ(ids, attr_ids_[i].ids);