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   }
     31 
     32   template <class RecordType>
     33   void CheckRecordMatchBinary(const RecordType& record);
     34 
     35   perf_event_attr event_attr;
     36 };
     37 
     38 template <class RecordType>
     39 void RecordTest::CheckRecordMatchBinary(const RecordType& record) {
     40   std::vector<char> binary = record.BinaryFormat();
     41   std::vector<std::unique_ptr<Record>> records =
     42       ReadRecordsFromBuffer(event_attr, binary.data(), binary.size());
     43   ASSERT_EQ(1u, records.size());
     44   CheckRecordEqual(record, *records[0]);
     45 }
     46 
     47 TEST_F(RecordTest, MmapRecordMatchBinary) {
     48   MmapRecord record =
     49       CreateMmapRecord(event_attr, true, 1, 2, 0x1000, 0x2000, 0x3000, "MmapRecord");
     50   CheckRecordMatchBinary(record);
     51 }
     52 
     53 TEST_F(RecordTest, CommRecordMatchBinary) {
     54   CommRecord record = CreateCommRecord(event_attr, 1, 2, "CommRecord");
     55   CheckRecordMatchBinary(record);
     56 }
     57 
     58 TEST_F(RecordTest, RecordCache_smoke) {
     59   event_attr.sample_id_all = 1;
     60   event_attr.sample_type |= PERF_SAMPLE_TIME;
     61   RecordCache cache(event_attr, 2, 2);
     62   MmapRecord r1 = CreateMmapRecord(event_attr, true, 1, 1, 0x100, 0x200, 0x300, "mmap_record1");
     63   MmapRecord r2 = r1;
     64   MmapRecord r3 = r1;
     65   MmapRecord r4 = r1;
     66   r1.sample_id.time_data.time = 3;
     67   r2.sample_id.time_data.time = 1;
     68   r3.sample_id.time_data.time = 4;
     69   r4.sample_id.time_data.time = 6;
     70   std::vector<char> buf1 = r1.BinaryFormat();
     71   std::vector<char> buf2 = r2.BinaryFormat();
     72   std::vector<char> buf3 = r3.BinaryFormat();
     73   std::vector<char> buf4 = r4.BinaryFormat();
     74   // Push r1.
     75   cache.Push(buf1.data(), buf1.size());
     76   ASSERT_EQ(nullptr, cache.Pop());
     77   // Push r2.
     78   cache.Push(buf2.data(), buf2.size());
     79   // Pop r2.
     80   std::unique_ptr<Record> popped_r = cache.Pop();
     81   ASSERT_TRUE(popped_r != nullptr);
     82   CheckRecordEqual(r2, *popped_r);
     83   ASSERT_EQ(nullptr, cache.Pop());
     84   // Push r3.
     85   cache.Push(buf3.data(), buf3.size());
     86   ASSERT_EQ(nullptr, cache.Pop());
     87   // Push r4.
     88   cache.Push(buf4.data(), buf4.size());
     89   // Pop r1.
     90   popped_r = cache.Pop();
     91   ASSERT_TRUE(popped_r != nullptr);
     92   CheckRecordEqual(r1, *popped_r);
     93   // Pop r3.
     94   popped_r = cache.Pop();
     95   ASSERT_TRUE(popped_r != nullptr);
     96   CheckRecordEqual(r3, *popped_r);
     97   ASSERT_EQ(nullptr, cache.Pop());
     98   // Pop r4.
     99   std::vector<std::unique_ptr<Record>> last_records = cache.PopAll();
    100   ASSERT_EQ(1u, last_records.size());
    101   CheckRecordEqual(r4, *last_records[0]);
    102 }
    103 
    104 TEST_F(RecordTest, RecordCache_FIFO) {
    105   event_attr.sample_id_all = 1;
    106   event_attr.sample_type |= PERF_SAMPLE_TIME;
    107   RecordCache cache(event_attr, 2, 2);
    108   std::vector<MmapRecord> records;
    109   for (size_t i = 0; i < 10; ++i) {
    110     MmapRecord r = CreateMmapRecord(event_attr, true, 1, i, 0x100, 0x200, 0x300, "mmap_record1");
    111     records.push_back(r);
    112     std::vector<char> buf = r.BinaryFormat();
    113     cache.Push(buf.data(), buf.size());
    114   }
    115   std::vector<std::unique_ptr<Record>> out_records = cache.PopAll();
    116   ASSERT_EQ(records.size(), out_records.size());
    117   for (size_t i = 0; i < records.size(); ++i) {
    118     CheckRecordEqual(records[i], *out_records[i]);
    119   }
    120 }
    121