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