2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
20 #include "environment.h"
22 #include "record_file.h"
24 using namespace PerfFileFormat;
26 class RecordCommandTest : public ::testing::Test {
28 virtual void SetUp() {
29 record_cmd = Command::FindCommandByName("record");
30 ASSERT_TRUE(record_cmd != nullptr);
36 TEST_F(RecordCommandTest, no_options) {
37 ASSERT_TRUE(record_cmd->Run({"record", "sleep", "1"}));
40 TEST_F(RecordCommandTest, system_wide_option) {
41 ASSERT_TRUE(record_cmd->Run({"record", "-a", "sleep", "1"}));
44 TEST_F(RecordCommandTest, sample_period_option) {
45 ASSERT_TRUE(record_cmd->Run({"record", "-c", "100000", "sleep", "1"}));
48 TEST_F(RecordCommandTest, event_option) {
49 ASSERT_TRUE(record_cmd->Run({"record", "-e", "cpu-clock", "sleep", "1"}));
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"}));
57 TEST_F(RecordCommandTest, output_file_option) {
58 ASSERT_TRUE(record_cmd->Run({"record", "-o", "perf2.data", "sleep", "1"}));
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;
77 ASSERT_TRUE(have_kernel_mmap);
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);