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 
CheckMmapRecordDataEqual(const MmapRecord & r1,const MmapRecord & r2)17 static void CheckMmapRecordDataEqual(const MmapRecord& r1, const MmapRecord& r2) {
18   ASSERT_EQ(0, memcmp(r1.data, r2.data, sizeof(*r1.data)));
19   ASSERT_STREQ(r1.filename, r2.filename);
20 }
21 
CheckCommRecordDataEqual(const CommRecord & r1,const CommRecord & r2)22 static void CheckCommRecordDataEqual(const CommRecord& r1, const CommRecord& r2) {
23   ASSERT_EQ(0, memcmp(r1.data, r2.data, sizeof(*r1.data)));
24   ASSERT_STREQ(r1.comm, r2.comm);
25 }
26 
CheckBuildIdRecordDataEqual(const BuildIdRecord & r1,const BuildIdRecord & r2)27 static void CheckBuildIdRecordDataEqual(const BuildIdRecord& r1, const BuildIdRecord& r2) {
28   ASSERT_EQ(r1.pid, r2.pid);
29   ASSERT_EQ(r1.build_id, r2.build_id);
30   ASSERT_STREQ(r1.filename, r2.filename);
31 }
32 
CheckSampleRecordDataEqual(const SampleRecord & r1,const SampleRecord & r2)33 static void CheckSampleRecordDataEqual(const SampleRecord& r1, const SampleRecord& r2) {
34   ASSERT_EQ(r1.sample_type, r2.sample_type);
35   if (r1.sample_type & PERF_SAMPLE_IP) {
36     EXPECT_EQ(r1.ip_data.ip, r2.ip_data.ip);
37   }
38   if (r1.sample_type & PERF_SAMPLE_TID) {
39     EXPECT_EQ(r1.tid_data.pid, r2.tid_data.pid);
40     EXPECT_EQ(r1.tid_data.tid, r2.tid_data.tid);
41   }
42   if (r1.sample_type & PERF_SAMPLE_TIME) {
43     EXPECT_EQ(r1.time_data.time, r2.time_data.time);
44   }
45   if (r1.sample_type & PERF_SAMPLE_ID) {
46     EXPECT_EQ(r1.id_data.id, r2.id_data.id);
47   }
48   if (r1.sample_type & PERF_SAMPLE_CPU) {
49     EXPECT_EQ(r1.cpu_data.cpu, r2.cpu_data.cpu);
50   }
51   if (r1.sample_type & PERF_SAMPLE_PERIOD) {
52     EXPECT_EQ(r1.period_data.period, r2.period_data.period);
53   }
54   if (r1.sample_type & PERF_SAMPLE_CALLCHAIN) {
55     ASSERT_EQ(r1.callchain_data.ip_nr, r2.callchain_data.ip_nr);
56     for (size_t i = 0; i < r1.callchain_data.ip_nr; ++i) {
57       EXPECT_EQ(r1.callchain_data.ips[i], r2.callchain_data.ips[i]);
58     }
59   }
60   if (r1.sample_type & PERF_SAMPLE_REGS_USER) {
61     ASSERT_EQ(r1.regs_user_data.abi, r2.regs_user_data.abi);
62     if (r1.regs_user_data.abi != 0) {
63       ASSERT_EQ(r1.regs_user_data.reg_mask, r2.regs_user_data.reg_mask);
64       ASSERT_EQ(0, memcmp(r1.regs_user_data.regs, r2.regs_user_data.regs,
65                           r1.regs_user_data.reg_nr * sizeof(uint64_t)));
66     }
67   }
68   if (r1.sample_type & PERF_SAMPLE_STACK_USER) {
69     ASSERT_EQ(r1.stack_user_data.size, r2.stack_user_data.size);
70     if (r1.stack_user_data.size > 0) {
71       ASSERT_EQ(r1.stack_user_data.dyn_size, r2.stack_user_data.dyn_size);
72       ASSERT_EQ(0, memcmp(r1.stack_user_data.data, r2.stack_user_data.data,
73                           r1.stack_user_data.size));
74     }
75   }
76 }
77 
CheckRecordEqual(const Record & r1,const Record & r2)78 static void CheckRecordEqual(const Record& r1, const Record& r2) {
79   ASSERT_EQ(r1.type(), r2.type());
80   ASSERT_EQ(r1.misc(), r2.misc());
81   ASSERT_EQ(r1.size(), r2.size());
82   if (r1.type() == PERF_RECORD_SAMPLE) {
83     CheckSampleRecordDataEqual(static_cast<const SampleRecord&>(r1),
84                                static_cast<const SampleRecord&>(r2));
85     return;
86   }
87   ASSERT_EQ(0, memcmp(&r1.sample_id, &r2.sample_id, sizeof(r1.sample_id)));
88   if (r1.type() == PERF_RECORD_MMAP) {
89     CheckMmapRecordDataEqual(static_cast<const MmapRecord&>(r1), static_cast<const MmapRecord&>(r2));
90   } else if (r1.type() == PERF_RECORD_COMM) {
91     CheckCommRecordDataEqual(static_cast<const CommRecord&>(r1), static_cast<const CommRecord&>(r2));
92   } else if (r1.type() == PERF_RECORD_BUILD_ID) {
93     CheckBuildIdRecordDataEqual(static_cast<const BuildIdRecord&>(r1),
94                                 static_cast<const BuildIdRecord&>(r2));
95   }
96 }
97