OSDN Git Service

Merge "Tests for candidate source address restriction"
[android-x86/system-extras.git] / simpleperf / record_file_test.cpp
1 /*
2  * Copyright (C) 2015 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 #include <gtest/gtest.h>
18
19 #include <string.h>
20 #include "environment.h"
21 #include "event_attr.h"
22 #include "event_fd.h"
23 #include "event_type.h"
24 #include "record.h"
25 #include "record_file.h"
26
27 #include "record_equal_test.h"
28
29 using namespace PerfFileFormat;
30
31 class RecordFileTest : public ::testing::Test {
32  protected:
33   virtual void SetUp() {
34     filename = "temporary.record_file";
35     std::unique_ptr<EventTypeAndModifier> event_type_modifier = ParseEventType("cpu-cycles");
36     ASSERT_TRUE(event_type_modifier != nullptr);
37     event_attr = CreateDefaultPerfEventAttr(event_type_modifier->event_type);
38     event_attr.sample_id_all = 1;
39     event_attr.sample_type |= PERF_SAMPLE_TIME;
40     std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(event_attr, getpid(), -1);
41     ASSERT_TRUE(event_fd != nullptr);
42     event_fds.push_back(std::move(event_fd));
43   }
44
45   std::string filename;
46   perf_event_attr event_attr;
47   std::vector<std::unique_ptr<EventFd>> event_fds;
48 };
49
50 TEST_F(RecordFileTest, smoke) {
51   // Write to a record file.
52   std::unique_ptr<RecordFileWriter> writer =
53       RecordFileWriter::CreateInstance(filename, event_attr, event_fds);
54   ASSERT_TRUE(writer != nullptr);
55
56   // Write data section.
57   MmapRecord mmap_record =
58       CreateMmapRecord(event_attr, true, 1, 1, 0x1000, 0x2000, 0x3000, "mmap_record_example");
59   ASSERT_TRUE(writer->WriteData(mmap_record.BinaryFormat()));
60
61   // Check data section that has been written.
62   std::vector<std::unique_ptr<Record>> records;
63   ASSERT_TRUE(writer->ReadDataSection(&records));
64   ASSERT_EQ(1u, records.size());
65   CheckRecordEqual(mmap_record, *records[0]);
66
67   // Write feature section.
68   ASSERT_TRUE(writer->WriteFeatureHeader(1));
69   char p[BuildId::Size()];
70   for (size_t i = 0; i < BuildId::Size(); ++i) {
71     p[i] = i;
72   }
73   BuildId build_id(p);
74   BuildIdRecord build_id_record = CreateBuildIdRecord(false, getpid(), build_id, "init");
75   ASSERT_TRUE(writer->WriteBuildIdFeature({build_id_record}));
76   ASSERT_TRUE(writer->Close());
77
78   // Read from a record file.
79   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(filename);
80   ASSERT_TRUE(reader != nullptr);
81   const FileHeader* file_header = reader->FileHeader();
82   ASSERT_TRUE(file_header != nullptr);
83   std::vector<const FileAttr*> attrs = reader->AttrSection();
84   ASSERT_EQ(1u, attrs.size());
85   ASSERT_EQ(0, memcmp(&attrs[0]->attr, &event_attr, sizeof(perf_event_attr)));
86   std::vector<uint64_t> ids = reader->IdsForAttr(attrs[0]);
87   ASSERT_EQ(1u, ids.size());
88
89   // Read and check data section.
90   records = reader->DataSection();
91   ASSERT_EQ(1u, records.size());
92   CheckRecordEqual(mmap_record, *records[0]);
93
94   // Read and check feature section.
95   ASSERT_TRUE(file_header->features[FEAT_BUILD_ID / 8] & (1 << (FEAT_BUILD_ID % 8)));
96   std::map<int, SectionDesc> sections = reader->FeatureSectionDescriptors();
97   ASSERT_EQ(1u, sections.size());
98   ASSERT_TRUE(sections.find(FEAT_BUILD_ID) != sections.end());
99   const perf_event_header* header = reinterpret_cast<const perf_event_header*>(
100       reader->DataAtOffset(sections[FEAT_BUILD_ID].offset));
101   ASSERT_TRUE(header != nullptr);
102   ASSERT_EQ(sections[FEAT_BUILD_ID].size, header->size);
103   CheckRecordEqual(build_id_record, BuildIdRecord(header));
104
105   ASSERT_TRUE(reader->Close());
106 }
107
108 TEST_F(RecordFileTest, records_sorted_by_time) {
109   // Write to a record file;
110   std::unique_ptr<RecordFileWriter> writer =
111       RecordFileWriter::CreateInstance(filename, event_attr, event_fds);
112   ASSERT_TRUE(writer != nullptr);
113
114   // Write data section.
115   MmapRecord r1 = CreateMmapRecord(event_attr, true, 1, 1, 0x100, 0x2000, 0x3000, "mmap_record1");
116   MmapRecord r2 = r1;
117   MmapRecord r3 = r1;
118   r1.sample_id.time_data.time = 2;
119   r2.sample_id.time_data.time = 1;
120   r3.sample_id.time_data.time = 3;
121   ASSERT_TRUE(writer->WriteData(r1.BinaryFormat()));
122   ASSERT_TRUE(writer->WriteData(r2.BinaryFormat()));
123   ASSERT_TRUE(writer->WriteData(r3.BinaryFormat()));
124   ASSERT_TRUE(writer->Close());
125
126   // Read from a record file.
127   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(filename);
128   ASSERT_TRUE(reader != nullptr);
129   std::vector<std::unique_ptr<Record>> records = reader->DataSection();
130   ASSERT_EQ(3u, records.size());
131   CheckRecordEqual(r2, *records[0]);
132   CheckRecordEqual(r1, *records[1]);
133   CheckRecordEqual(r3, *records[2]);
134
135   ASSERT_TRUE(reader->Close());
136 }