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 "command.h"
20 #include "environment.h"
21 #include "record.h"
22 #include "record_file.h"
23 
24 using namespace PerfFileFormat;
25 
26 class RecordCommandTest : public ::testing::Test {
27  protected:
SetUp()28   virtual void SetUp() {
29     record_cmd = Command::FindCommandByName("record");
30     ASSERT_TRUE(record_cmd != nullptr);
31   }
32 
33   Command* record_cmd;
34 };
35 
TEST_F(RecordCommandTest,no_options)36 TEST_F(RecordCommandTest, no_options) {
37   ASSERT_TRUE(record_cmd->Run({"record", "sleep", "1"}));
38 }
39 
TEST_F(RecordCommandTest,system_wide_option)40 TEST_F(RecordCommandTest, system_wide_option) {
41   ASSERT_TRUE(record_cmd->Run({"record", "-a", "sleep", "1"}));
42 }
43 
TEST_F(RecordCommandTest,sample_period_option)44 TEST_F(RecordCommandTest, sample_period_option) {
45   ASSERT_TRUE(record_cmd->Run({"record", "-c", "100000", "sleep", "1"}));
46 }
47 
TEST_F(RecordCommandTest,event_option)48 TEST_F(RecordCommandTest, event_option) {
49   ASSERT_TRUE(record_cmd->Run({"record", "-e", "cpu-clock", "sleep", "1"}));
50 }
51 
TEST_F(RecordCommandTest,freq_option)52 TEST_F(RecordCommandTest, freq_option) {
53   ASSERT_TRUE(record_cmd->Run({"record", "-f", "99", "sleep", "1"}));
54   ASSERT_TRUE(record_cmd->Run({"record", "-F", "99", "sleep", "1"}));
55 }
56 
TEST_F(RecordCommandTest,output_file_option)57 TEST_F(RecordCommandTest, output_file_option) {
58   ASSERT_TRUE(record_cmd->Run({"record", "-o", "perf2.data", "sleep", "1"}));
59 }
60 
TEST_F(RecordCommandTest,dump_kernel_mmap)61 TEST_F(RecordCommandTest, dump_kernel_mmap) {
62   ASSERT_TRUE(record_cmd->Run({"record", "sleep", "1"}));
63   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance("perf.data");
64   ASSERT_TRUE(reader != nullptr);
65   std::vector<std::unique_ptr<const Record>> records = reader->DataSection();
66   ASSERT_GT(records.size(), 0U);
67   bool have_kernel_mmap = false;
68   for (auto& record : records) {
69     if (record->header.type == PERF_RECORD_MMAP) {
70       const MmapRecord* mmap_record = static_cast<const MmapRecord*>(record.get());
71       if (mmap_record->filename == DEFAULT_KERNEL_MMAP_NAME) {
72         have_kernel_mmap = true;
73         break;
74       }
75     }
76   }
77   ASSERT_TRUE(have_kernel_mmap);
78 }
79 
TEST_F(RecordCommandTest,dump_build_id_feature)80 TEST_F(RecordCommandTest, dump_build_id_feature) {
81   ASSERT_TRUE(record_cmd->Run({"record", "sleep", "1"}));
82   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance("perf.data");
83   ASSERT_TRUE(reader != nullptr);
84   const FileHeader* file_header = reader->FileHeader();
85   ASSERT_TRUE(file_header != nullptr);
86   ASSERT_TRUE(file_header->features[FEAT_BUILD_ID / 8] & (1 << (FEAT_BUILD_ID % 8)));
87   ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
88 }
89