1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* 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.
*/
#include <gtest/gtest.h>
#include "event_attr.h"
#include "event_type.h"
#include "record.h"
#include "record_equal_test.h"
class RecordTest : public ::testing::Test {
protected:
virtual void SetUp() {
const EventType* type = FindEventTypeByName("cpu-cycles");
ASSERT_TRUE(type != nullptr);
event_attr = CreateDefaultPerfEventAttr(*type);
event_attr.sample_id_all = 1;
}
void CheckRecordMatchBinary(const Record& record) {
const char* p = record.Binary();
std::vector<std::unique_ptr<Record>> records =
ReadRecordsFromBuffer(event_attr, p, record.size());
ASSERT_EQ(1u, records.size());
CheckRecordEqual(record, *records[0]);
}
perf_event_attr event_attr;
};
TEST_F(RecordTest, MmapRecordMatchBinary) {
MmapRecord record(event_attr, true, 1, 2, 0x1000, 0x2000, 0x3000,
"MmapRecord", 0);
CheckRecordMatchBinary(record);
}
TEST_F(RecordTest, CommRecordMatchBinary) {
CommRecord record(event_attr, 1, 2, "CommRecord", 0, 7);
CheckRecordMatchBinary(record);
}
TEST_F(RecordTest, SampleRecordMatchBinary) {
event_attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME
| PERF_SAMPLE_ID | PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD | PERF_SAMPLE_CALLCHAIN;
SampleRecord record(event_attr, 1, 2, 3, 4, 5, 6, 7, {8, 9, 10});
CheckRecordMatchBinary(record);
}
TEST_F(RecordTest, RecordCache_smoke) {
event_attr.sample_id_all = 1;
event_attr.sample_type |= PERF_SAMPLE_TIME;
RecordCache cache(true, 2, 2);
// Push r1.
MmapRecord* r1 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
"mmap_record1", 0, 3);
cache.Push(std::unique_ptr<Record>(r1));
ASSERT_EQ(nullptr, cache.Pop());
// Push r2.
MmapRecord* r2 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
"mmap_record1", 0, 1);
cache.Push(std::unique_ptr<Record>(r2));
// Pop r2.
std::unique_ptr<Record> popped_r = cache.Pop();
ASSERT_TRUE(popped_r != nullptr);
ASSERT_EQ(r2, popped_r.get());
ASSERT_EQ(nullptr, cache.Pop());
// Push r3.
MmapRecord* r3 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
"mmap_record1", 0, 4);
cache.Push(std::unique_ptr<Record>(r3));
ASSERT_EQ(nullptr, cache.Pop());
// Push r4.
MmapRecord* r4 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
"mmap_record1", 0, 6);
cache.Push(std::unique_ptr<Record>(r4));
// Pop r1.
popped_r = cache.Pop();
ASSERT_TRUE(popped_r != nullptr);
ASSERT_EQ(r1, popped_r.get());
// Pop r3.
popped_r = cache.Pop();
ASSERT_TRUE(popped_r != nullptr);
ASSERT_EQ(r3, popped_r.get());
ASSERT_EQ(nullptr, cache.Pop());
// Pop r4.
std::vector<std::unique_ptr<Record>> last_records = cache.PopAll();
ASSERT_EQ(1u, last_records.size());
ASSERT_EQ(r4, last_records[0].get());
}
TEST_F(RecordTest, RecordCache_FIFO) {
event_attr.sample_id_all = 1;
event_attr.sample_type |= PERF_SAMPLE_TIME;
RecordCache cache(true, 2, 2);
std::vector<MmapRecord*> records;
for (size_t i = 0; i < 10; ++i) {
records.push_back(new MmapRecord(event_attr, true, 1, i, 0x100, 0x200,
0x300, "mmap_record1", 0));
cache.Push(std::unique_ptr<Record>(records.back()));
}
std::vector<std::unique_ptr<Record>> out_records = cache.PopAll();
ASSERT_EQ(records.size(), out_records.size());
for (size_t i = 0; i < records.size(); ++i) {
ASSERT_EQ(records[i], out_records[i].get());
}
}
TEST_F(RecordTest, RecordCache_PushRecordVector) {
event_attr.sample_id_all = 1;
event_attr.sample_type |= PERF_SAMPLE_TIME;
RecordCache cache(true, 2, 2);
MmapRecord* r1 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
"mmap_record1", 0, 1);
MmapRecord* r2 = new MmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300,
"mmap_record1", 0, 3);
std::vector<std::unique_ptr<Record>> records;
records.push_back(std::unique_ptr<Record>(r1));
records.push_back(std::unique_ptr<Record>(r2));
cache.Push(std::move(records));
std::unique_ptr<Record> popped_r = cache.Pop();
ASSERT_TRUE(popped_r != nullptr);
ASSERT_EQ(r1, popped_r.get());
std::vector<std::unique_ptr<Record>> last_records = cache.PopAll();
ASSERT_EQ(1u, last_records.size());
ASSERT_EQ(r2, last_records[0].get());
}
|