1 /*
2 * Copyright (C) 2017 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 "CallChainJoiner.h"
18
19 #include <gtest/gtest.h>
20
21 #include <environment.h>
22
23 using namespace simpleperf;
24 using namespace simpleperf::call_chain_joiner_impl;
25
JoinCallChain(LRUCache & cache,uint32_t tid,const std::vector<uint64_t> & input_ip,const std::vector<uint64_t> & input_sp,const std::vector<uint64_t> & expected_output_ip,const std::vector<uint64_t> & expected_output_sp)26 static bool JoinCallChain(LRUCache& cache, uint32_t tid, const std::vector<uint64_t>& input_ip,
27 const std::vector<uint64_t>& input_sp,
28 const std::vector<uint64_t>& expected_output_ip,
29 const std::vector<uint64_t>& expected_output_sp) {
30 std::vector<uint64_t> tmp_ip = input_ip;
31 std::vector<uint64_t> tmp_sp = input_sp;
32 cache.AddCallChain(tid, tmp_ip, tmp_sp);
33 return tmp_ip == expected_output_ip && tmp_sp == expected_output_sp;
34 }
35
TEST(LRUCache,different_nodes)36 TEST(LRUCache, different_nodes) {
37 LRUCache cache(sizeof(CacheNode) * 2, 1);
38 ASSERT_EQ(cache.Stat().max_node_count, 2u);
39 // different tids
40 std::vector<uint64_t> ip = {0x1};
41 std::vector<uint64_t> sp = {0x1};
42 ASSERT_TRUE(JoinCallChain(cache, 0, ip, sp, ip, sp));
43 ASSERT_TRUE(JoinCallChain(cache, 1, ip, sp, ip, sp));
44 ASSERT_EQ(cache.Stat().used_node_count, 2u);
45 ASSERT_EQ(cache.Stat().recycled_node_count, 0u);
46 ASSERT_NE(cache.FindNode(0, ip[0], sp[0]), nullptr);
47 ASSERT_NE(cache.FindNode(1, ip[0], sp[0]), nullptr);
48
49 // different ips
50 std::vector<uint64_t> ip2 = {0x2};
51 ASSERT_TRUE(JoinCallChain(cache, 0, ip2, sp, ip2, sp));
52 ASSERT_EQ(cache.Stat().used_node_count, 2u);
53 ASSERT_EQ(cache.Stat().recycled_node_count, 1u);
54 ASSERT_EQ(cache.FindNode(0, ip[0], sp[0]), nullptr);
55 ASSERT_NE(cache.FindNode(0, ip2[0], sp[0]), nullptr);
56 ASSERT_NE(cache.FindNode(1, ip[0], sp[0]), nullptr);
57
58 // different sps
59 std::vector<uint64_t> sp2 = {0x2};
60 ASSERT_TRUE(JoinCallChain(cache, 1, ip, sp2, ip, sp2));
61 ASSERT_EQ(cache.Stat().used_node_count, 2u);
62 ASSERT_EQ(cache.Stat().recycled_node_count, 2u);
63 ASSERT_EQ(cache.FindNode(1, ip[0], sp[0]), nullptr);
64 ASSERT_NE(cache.FindNode(0, ip2[0], sp[0]), nullptr);
65 ASSERT_NE(cache.FindNode(1, ip[0], sp2[0]), nullptr);
66 }
67
TEST(LRUCache,extend_chains)68 TEST(LRUCache, extend_chains) {
69 // matched_node_count_to_extend_callchain = 1
70 // c -> b
71 // b -> a => c -> b -> a
72 LRUCache cache1(sizeof(CacheNode) * 4, 1);
73 ASSERT_TRUE(JoinCallChain(cache1, 0, {0xb, 0xc}, {0xb, 0xc}, {0xb, 0xc}, {0xb, 0xc}));
74 ASSERT_TRUE(JoinCallChain(cache1, 0, {0xa, 0xb}, {0xa, 0xb}, {0xa, 0xb, 0xc}, {0xa, 0xb, 0xc}));
75 ASSERT_EQ(cache1.Stat().used_node_count, 3u);
76
77 // matched_node_count_to_extend_callchain = 2
78 // c -> b
79 // b -> a
80 LRUCache cache2(sizeof(CacheNode) * 4, 2);
81 ASSERT_TRUE(JoinCallChain(cache2, 0, {0xb, 0xc}, {0xb, 0xc}, {0xb, 0xc}, {0xb, 0xc}));
82 ASSERT_TRUE(JoinCallChain(cache2, 0, {0xa, 0xb}, {0xa, 0xb}, {0xa, 0xb}, {0xa, 0xb}));
83 ASSERT_EQ(cache2.Stat().used_node_count, 3u);
84
85 // matched_node_count_to_extend_callchain = 2
86 // d -> c -> b
87 // c -> b -> a => d -> c -> b -> a
88 LRUCache cache3(sizeof(CacheNode) * 4, 2);
89 ASSERT_TRUE(
90 JoinCallChain(cache3, 0, {0xb, 0xc, 0xd}, {0xb, 0xc, 0xd}, {0xb, 0xc, 0xd}, {0xb, 0xc, 0xd}));
91 ASSERT_TRUE(JoinCallChain(cache3, 0, {0xa, 0xb, 0xc}, {0xa, 0xb, 0xc}, {0xa, 0xb, 0xc, 0xd},
92 {0xa, 0xb, 0xc, 0xd}));
93 ASSERT_EQ(cache3.Stat().used_node_count, 4u);
94 }
95
TEST(LRUCache,avoid_ip_sp_loop)96 TEST(LRUCache, avoid_ip_sp_loop) {
97 LRUCache cache(sizeof(CacheNode) * 2, 1);
98 std::vector<uint64_t> ip = {0xa, 0xb};
99 std::vector<uint64_t> sp = {1, 1};
100 ASSERT_TRUE(JoinCallChain(cache, 0, ip, sp, ip, sp));
101 ip = {0xb, 0xa};
102 ASSERT_TRUE(JoinCallChain(cache, 0, ip, sp, ip, sp));
103 ASSERT_EQ(cache.Stat().used_node_count, 2u);
104 ASSERT_EQ(cache.Stat().recycled_node_count, 0u);
105 }
106
TEST(LRUCache,one_chain)107 TEST(LRUCache, one_chain) {
108 LRUCache cache(sizeof(CacheNode) * 4, 1);
109 ASSERT_EQ(cache.Stat().max_node_count, 4u);
110 std::vector<uint64_t> ip;
111 std::vector<uint64_t> sp;
112 for (size_t i = 1u; i <= 4u; ++i) {
113 ip.push_back(i);
114 sp.push_back(i);
115 ASSERT_TRUE(JoinCallChain(cache, 0, ip, sp, ip, sp));
116 }
117 std::vector<uint64_t> origin_ip = ip;
118 std::vector<uint64_t> origin_sp = sp;
119 for (size_t i = ip.size(); i > 1; --i) {
120 ip.pop_back();
121 sp.pop_back();
122 ASSERT_TRUE(JoinCallChain(cache, 0, ip, sp, origin_ip, origin_sp));
123 }
124 ASSERT_EQ(cache.Stat().used_node_count, 4u);
125 ASSERT_EQ(cache.Stat().recycled_node_count, 0u);
126 }
127
TEST(LRUCache,many_chains)128 TEST(LRUCache, many_chains) {
129 LRUCache cache(sizeof(CacheNode) * 12, 1);
130 // 4 -> 3 -> 2 -> 1
131 // 8 -> 7 -> 6 -> 5
132 // d -> c -> b -> a
133 std::vector<uint64_t> ip = {1, 2, 3, 4};
134 std::vector<uint64_t> sp = {1, 2, 3, 4};
135 ASSERT_TRUE(JoinCallChain(cache, 0, ip, sp, ip, sp));
136 ip = {5, 6, 7, 8};
137 sp = {5, 6, 7, 8};
138 ASSERT_TRUE(JoinCallChain(cache, 0, ip, sp, ip, sp));
139 ip = {0xa, 0xb, 0xc, 0xd};
140 sp = {0xa, 0xb, 0xc, 0xd};
141 ASSERT_TRUE(JoinCallChain(cache, 0, ip, sp, ip, sp));
142 ASSERT_EQ(cache.Stat().used_node_count, 12u);
143 ASSERT_EQ(cache.Stat().recycled_node_count, 0u);
144 ASSERT_TRUE(JoinCallChain(cache, 0, {1}, {1}, {1, 2, 3, 4}, {1, 2, 3, 4}));
145 ASSERT_TRUE(JoinCallChain(cache, 0, {5, 6}, {5, 6}, {5, 6, 7, 8}, {5, 6, 7, 8}));
146 ASSERT_TRUE(JoinCallChain(cache, 0, {0xa}, {0xb}, {0xa}, {0xb}));
147 ASSERT_EQ(cache.Stat().used_node_count, 12u);
148 ASSERT_EQ(cache.Stat().recycled_node_count, 1u);
149 ASSERT_EQ(cache.FindNode(0, 0xa, 0xa), nullptr);
150 }
151
152 class CallChainJoinerTest : public ::testing::Test {
153 protected:
SetUp()154 void SetUp() override {
155 #if defined(__ANDROID__)
156 std::string tmpdir = "/data/local/tmp";
157 #else
158 std::string tmpdir = "/tmp";
159 #endif
160 scoped_temp_files_ = ScopedTempFiles::Create(tmpdir);
161 }
162
163 private:
164 std::unique_ptr<ScopedTempFiles> scoped_temp_files_;
165 };
166
TEST_F(CallChainJoinerTest,smoke)167 TEST_F(CallChainJoinerTest, smoke) {
168 CallChainJoiner joiner(sizeof(CacheNode) * 1024, 1, true);
169 for (pid_t pid = 0; pid < 10; ++pid) {
170 ASSERT_TRUE(
171 joiner.AddCallChain(pid, pid, CallChainJoiner::ORIGINAL_OFFLINE, {1, 2, 3}, {1, 2, 3}));
172 ASSERT_TRUE(
173 joiner.AddCallChain(pid, pid, CallChainJoiner::ORIGINAL_REMOTE, {3, 4, 5}, {3, 4, 5}));
174 ASSERT_TRUE(joiner.AddCallChain(pid, pid, CallChainJoiner::ORIGINAL_OFFLINE, {1, 4}, {1, 4}));
175 }
176 ASSERT_TRUE(joiner.JoinCallChains());
177 pid_t pid;
178 pid_t tid;
179 CallChainJoiner::ChainType type;
180 std::vector<uint64_t> ips;
181 std::vector<uint64_t> sps;
182 for (pid_t expected_pid = 0; expected_pid < 10; ++expected_pid) {
183 for (size_t i = 0; i < 2u; ++i) {
184 ASSERT_TRUE(joiner.GetNextCallChain(pid, tid, type, ips, sps));
185 ASSERT_EQ(pid, expected_pid);
186 ASSERT_EQ(tid, expected_pid);
187 if (i == 0u) {
188 ASSERT_EQ(type, CallChainJoiner::ORIGINAL_OFFLINE);
189 ASSERT_EQ(ips, std::vector<uint64_t>({1, 2, 3}));
190 ASSERT_EQ(sps, std::vector<uint64_t>({1, 2, 3}));
191 } else {
192 ASSERT_EQ(type, CallChainJoiner::JOINED_OFFLINE);
193 ASSERT_EQ(ips, std::vector<uint64_t>({1, 2, 3, 4, 5}));
194 ASSERT_EQ(sps, std::vector<uint64_t>({1, 2, 3, 4, 5}));
195 }
196 }
197 for (size_t i = 0; i < 2u; ++i) {
198 ASSERT_TRUE(joiner.GetNextCallChain(pid, tid, type, ips, sps));
199 ASSERT_EQ(pid, expected_pid);
200 ASSERT_EQ(tid, expected_pid);
201 ASSERT_EQ(type, i == 0u ? CallChainJoiner::ORIGINAL_REMOTE : CallChainJoiner::JOINED_REMOTE);
202 ASSERT_EQ(ips, std::vector<uint64_t>({3, 4, 5}));
203 ASSERT_EQ(sps, std::vector<uint64_t>({3, 4, 5}));
204 }
205 for (size_t i = 0; i < 2u; ++i) {
206 ASSERT_TRUE(joiner.GetNextCallChain(pid, tid, type, ips, sps));
207 ASSERT_EQ(pid, expected_pid);
208 ASSERT_EQ(tid, expected_pid);
209 if (i == 0u) {
210 ASSERT_EQ(type, CallChainJoiner::ORIGINAL_OFFLINE);
211 ASSERT_EQ(ips, std::vector<uint64_t>({1, 4}));
212 ASSERT_EQ(sps, std::vector<uint64_t>({1, 4}));
213 } else {
214 ASSERT_EQ(type, CallChainJoiner::JOINED_OFFLINE);
215 ASSERT_EQ(ips, std::vector<uint64_t>({1, 4, 5}));
216 ASSERT_EQ(sps, std::vector<uint64_t>({1, 4, 5}));
217 }
218 }
219 }
220 ASSERT_FALSE(joiner.GetNextCallChain(pid, tid, type, ips, sps));
221 joiner.DumpStat();
222 ASSERT_EQ(joiner.GetCacheStat().cache_size, sizeof(CacheNode) * 1024);
223 ASSERT_EQ(joiner.GetCacheStat().matched_node_count_to_extend_callchain, 1u);
224 ASSERT_EQ(joiner.GetCacheStat().max_node_count, 1024u);
225 ASSERT_EQ(joiner.GetCacheStat().used_node_count, 50u);
226 ASSERT_EQ(joiner.GetCacheStat().recycled_node_count, 0u);
227 ASSERT_EQ(joiner.GetStat().chain_count, 30u);
228 ASSERT_EQ(joiner.GetStat().before_join_node_count, 80u);
229 ASSERT_EQ(joiner.GetStat().after_join_node_count, 110u);
230 ASSERT_EQ(joiner.GetStat().after_join_max_chain_length, 5u);
231 }
232
TEST_F(CallChainJoinerTest,no_original_chains)233 TEST_F(CallChainJoinerTest, no_original_chains) {
234 CallChainJoiner joiner(sizeof(CacheNode) * 1024, 1, false);
235 ASSERT_TRUE(joiner.AddCallChain(0, 0, CallChainJoiner::ORIGINAL_OFFLINE, {1}, {1}));
236 ASSERT_TRUE(joiner.JoinCallChains());
237 pid_t pid;
238 pid_t tid;
239 CallChainJoiner::ChainType type;
240 std::vector<uint64_t> ips;
241 std::vector<uint64_t> sps;
242 ASSERT_TRUE(joiner.GetNextCallChain(pid, tid, type, ips, sps));
243 ASSERT_EQ(pid, 0);
244 ASSERT_EQ(tid, 0);
245 ASSERT_EQ(type, CallChainJoiner::JOINED_OFFLINE);
246 ASSERT_EQ(ips, std::vector<uint64_t>({1}));
247 ASSERT_EQ(sps, std::vector<uint64_t>({1}));
248 ASSERT_FALSE(joiner.GetNextCallChain(pid, tid, type, ips, sps));
249 joiner.DumpStat();
250 }
251
TEST_F(CallChainJoinerTest,no_chains)252 TEST_F(CallChainJoinerTest, no_chains) {
253 CallChainJoiner joiner(sizeof(CacheNode) * 1024, 1, false);
254 ASSERT_TRUE(joiner.JoinCallChains());
255 pid_t pid;
256 pid_t tid;
257 CallChainJoiner::ChainType type;
258 std::vector<uint64_t> ips;
259 std::vector<uint64_t> sps;
260 ASSERT_FALSE(joiner.GetNextCallChain(pid, tid, type, ips, sps));
261 joiner.DumpStat();
262 }
263