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>
20 #include "environment.h"
21 #include "event_attr.h"
23 #include "event_type.h"
25 #include "record_file.h"
27 #include "record_equal_test.h"
29 using namespace PerfFileFormat;
31 class RecordFileTest : public ::testing::Test {
33 virtual void SetUp() {
34 filename = "temporary.record_file";
35 const EventType* event_type = EventTypeFactory::FindEventTypeByName("cpu-cycles");
36 ASSERT_TRUE(event_type != nullptr);
37 event_attr = CreateDefaultPerfEventAttr(*event_type);
38 std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFileForProcess(event_attr, getpid());
39 ASSERT_TRUE(event_fd != nullptr);
40 event_fds.push_back(std::move(event_fd));
44 perf_event_attr event_attr;
45 std::vector<std::unique_ptr<EventFd>> event_fds;
48 TEST_F(RecordFileTest, smoke) {
49 // Write to a record file.
50 std::unique_ptr<RecordFileWriter> writer =
51 RecordFileWriter::CreateInstance(filename, event_attr, event_fds);
52 ASSERT_TRUE(writer != nullptr);
54 // Write data section.
55 MmapRecord mmap_record =
56 CreateMmapRecord(event_attr, true, 1, 1, 0x1000, 0x2000, 0x3000, "mmap_record_example");
57 ASSERT_TRUE(writer->WriteData(mmap_record.BinaryFormat()));
59 // Write feature section.
60 ASSERT_TRUE(writer->WriteFeatureHeader(1));
62 for (size_t i = 0; i < build_id.size(); ++i) {
65 BuildIdRecord build_id_record = CreateBuildIdRecord(false, getpid(), build_id, "init");
66 ASSERT_TRUE(writer->WriteBuildIdFeature({build_id_record}));
67 ASSERT_TRUE(writer->Close());
69 // Read from a record file.
70 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(filename);
71 ASSERT_TRUE(reader != nullptr);
72 const FileHeader* file_header = reader->FileHeader();
73 ASSERT_TRUE(file_header != nullptr);
74 std::vector<const FileAttr*> attrs = reader->AttrSection();
75 ASSERT_EQ(1u, attrs.size());
76 ASSERT_EQ(0, memcmp(&attrs[0]->attr, &event_attr, sizeof(perf_event_attr)));
77 std::vector<uint64_t> ids = reader->IdsForAttr(attrs[0]);
78 ASSERT_EQ(1u, ids.size());
80 // Read and check data section.
81 std::vector<std::unique_ptr<const Record>> records = reader->DataSection();
82 ASSERT_EQ(1u, records.size());
83 ASSERT_EQ(mmap_record.header.type, records[0]->header.type);
84 CheckRecordEqual(mmap_record, *records[0]);
86 // Read and check feature section.
87 ASSERT_TRUE(file_header->features[FEAT_BUILD_ID / 8] & (1 << (FEAT_BUILD_ID % 8)));
88 std::vector<SectionDesc> sections = reader->FeatureSectionDescriptors();
89 ASSERT_EQ(1u, sections.size());
90 const perf_event_header* header =
91 reinterpret_cast<const perf_event_header*>(reader->DataAtOffset(sections[0].offset));
92 ASSERT_TRUE(header != nullptr);
93 ASSERT_EQ(sections[0].size, header->size);
94 CheckRecordEqual(build_id_record, BuildIdRecord(header));
96 ASSERT_TRUE(reader->Close());