blob: 23d87fd6460a2b8eec72a1bf2388c1b26e422334 [file] [log] [blame]
Steven Moreland72530412020-01-13 16:13:58 -08001/*
2 * Copyright (C) 2018 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
Connor O'Brien57337192018-11-20 12:49:16 -080017
Connor O'Briend65f2a02019-08-28 16:15:38 -070018#include <bpf_timeinstate.h>
Connor O'Briena178a732019-06-05 18:27:47 -070019
20#include <sys/sysinfo.h>
21
Connor O'Brien26de80f2019-06-11 13:49:19 -070022#include <numeric>
Connor O'Brien57337192018-11-20 12:49:16 -080023#include <unordered_map>
24#include <vector>
25
26#include <gtest/gtest.h>
27
Connor O'Briena178a732019-06-05 18:27:47 -070028#include <android-base/unique_fd.h>
29#include <bpf/BpfMap.h>
Connor O'Brien57337192018-11-20 12:49:16 -080030#include <cputimeinstate.h>
Connor O'Briena178a732019-06-05 18:27:47 -070031#include <libbpf.h>
Connor O'Brien57337192018-11-20 12:49:16 -080032
33namespace android {
34namespace bpf {
35
Connor O'Briena178a732019-06-05 18:27:47 -070036static constexpr uint64_t NSEC_PER_SEC = 1000000000;
37static constexpr uint64_t NSEC_PER_YEAR = NSEC_PER_SEC * 60 * 60 * 24 * 365;
38
Connor O'Brien57337192018-11-20 12:49:16 -080039using std::vector;
40
Connor O'Brien26de80f2019-06-11 13:49:19 -070041TEST(TimeInStateTest, SingleUidTimeInState) {
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070042 auto times = getUidCpuFreqTimes(0);
43 ASSERT_TRUE(times.has_value());
44 EXPECT_FALSE(times->empty());
Connor O'Brien57337192018-11-20 12:49:16 -080045}
46
Connor O'Brien26de80f2019-06-11 13:49:19 -070047TEST(TimeInStateTest, SingleUidConcurrentTimes) {
48 auto concurrentTimes = getUidConcurrentTimes(0);
49 ASSERT_TRUE(concurrentTimes.has_value());
50 ASSERT_FALSE(concurrentTimes->active.empty());
51 ASSERT_FALSE(concurrentTimes->policy.empty());
52
53 uint64_t policyEntries = 0;
54 for (const auto &policyTimeVec : concurrentTimes->policy) policyEntries += policyTimeVec.size();
55 ASSERT_EQ(concurrentTimes->active.size(), policyEntries);
56}
57
58static void TestConcurrentTimesConsistent(const struct concurrent_time_t &concurrentTime) {
59 size_t maxPolicyCpus = 0;
60 for (const auto &vec : concurrentTime.policy) {
61 maxPolicyCpus = std::max(maxPolicyCpus, vec.size());
62 }
63 uint64_t policySum = 0;
64 for (size_t i = 0; i < maxPolicyCpus; ++i) {
65 for (const auto &vec : concurrentTime.policy) {
66 if (i < vec.size()) policySum += vec[i];
67 }
68 ASSERT_LE(concurrentTime.active[i], policySum);
69 policySum -= concurrentTime.active[i];
70 }
71 policySum = 0;
72 for (size_t i = 0; i < concurrentTime.active.size(); ++i) {
73 for (const auto &vec : concurrentTime.policy) {
74 if (i < vec.size()) policySum += vec[vec.size() - 1 - i];
75 }
76 auto activeSum = concurrentTime.active[concurrentTime.active.size() - 1 - i];
77 // This check is slightly flaky because we may read a map entry in the middle of an update
78 // when active times have been updated but policy times have not. This happens infrequently
79 // and can be distinguished from more serious bugs by re-running the test: if the underlying
80 // data itself is inconsistent, the test will fail every time.
81 ASSERT_LE(activeSum, policySum);
82 policySum -= activeSum;
83 }
84}
85
86static void TestUidTimesConsistent(const std::vector<std::vector<uint64_t>> &timeInState,
87 const struct concurrent_time_t &concurrentTime) {
88 ASSERT_NO_FATAL_FAILURE(TestConcurrentTimesConsistent(concurrentTime));
89 ASSERT_EQ(timeInState.size(), concurrentTime.policy.size());
90 uint64_t policySum = 0;
91 for (uint32_t i = 0; i < timeInState.size(); ++i) {
92 uint64_t tisSum =
93 std::accumulate(timeInState[i].begin(), timeInState[i].end(), (uint64_t)0);
94 uint64_t concurrentSum = std::accumulate(concurrentTime.policy[i].begin(),
95 concurrentTime.policy[i].end(), (uint64_t)0);
96 if (tisSum < concurrentSum)
97 ASSERT_LE(concurrentSum - tisSum, NSEC_PER_SEC);
98 else
99 ASSERT_LE(tisSum - concurrentSum, NSEC_PER_SEC);
100 policySum += concurrentSum;
101 }
102 uint64_t activeSum = std::accumulate(concurrentTime.active.begin(), concurrentTime.active.end(),
103 (uint64_t)0);
104 EXPECT_EQ(activeSum, policySum);
105}
106
107TEST(TimeInStateTest, SingleUidTimesConsistent) {
108 auto times = getUidCpuFreqTimes(0);
109 ASSERT_TRUE(times.has_value());
110
111 auto concurrentTimes = getUidConcurrentTimes(0);
112 ASSERT_TRUE(concurrentTimes.has_value());
113
114 ASSERT_NO_FATAL_FAILURE(TestUidTimesConsistent(*times, *concurrentTimes));
115}
116
117TEST(TimeInStateTest, AllUidTimeInState) {
Connor O'Brien57337192018-11-20 12:49:16 -0800118 vector<size_t> sizes;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700119 auto map = getUidsCpuFreqTimes();
120 ASSERT_TRUE(map.has_value());
Connor O'Brien57337192018-11-20 12:49:16 -0800121
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700122 ASSERT_FALSE(map->empty());
Connor O'Brien57337192018-11-20 12:49:16 -0800123
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700124 auto firstEntry = map->begin()->second;
Connor O'Brien57337192018-11-20 12:49:16 -0800125 for (const auto &subEntry : firstEntry) sizes.emplace_back(subEntry.size());
126
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700127 for (const auto &vec : *map) {
Connor O'Brien57337192018-11-20 12:49:16 -0800128 ASSERT_EQ(vec.second.size(), sizes.size());
129 for (size_t i = 0; i < vec.second.size(); ++i) ASSERT_EQ(vec.second[i].size(), sizes[i]);
130 }
131}
132
Connor O'Brien26de80f2019-06-11 13:49:19 -0700133TEST(TimeInStateTest, SingleAndAllUidTimeInStateConsistent) {
Connor O'Briena178a732019-06-05 18:27:47 -0700134 auto map = getUidsCpuFreqTimes();
135 ASSERT_TRUE(map.has_value());
136 ASSERT_FALSE(map->empty());
137
138 for (const auto &kv : *map) {
139 uint32_t uid = kv.first;
140 auto times1 = kv.second;
141 auto times2 = getUidCpuFreqTimes(uid);
142 ASSERT_TRUE(times2.has_value());
143
144 ASSERT_EQ(times1.size(), times2->size());
145 for (uint32_t i = 0; i < times1.size(); ++i) {
146 ASSERT_EQ(times1[i].size(), (*times2)[i].size());
147 for (uint32_t j = 0; j < times1[i].size(); ++j) {
148 ASSERT_LE((*times2)[i][j] - times1[i][j], NSEC_PER_SEC);
149 }
150 }
151 }
152}
153
Connor O'Brien26de80f2019-06-11 13:49:19 -0700154TEST(TimeInStateTest, AllUidConcurrentTimes) {
155 auto map = getUidsConcurrentTimes();
156 ASSERT_TRUE(map.has_value());
157 ASSERT_FALSE(map->empty());
158
159 auto firstEntry = map->begin()->second;
160 for (const auto &kv : *map) {
161 ASSERT_EQ(kv.second.active.size(), firstEntry.active.size());
162 ASSERT_EQ(kv.second.policy.size(), firstEntry.policy.size());
163 for (size_t i = 0; i < kv.second.policy.size(); ++i) {
164 ASSERT_EQ(kv.second.policy[i].size(), firstEntry.policy[i].size());
165 }
166 }
167}
168
169TEST(TimeInStateTest, SingleAndAllUidConcurrentTimesConsistent) {
170 auto map = getUidsConcurrentTimes();
171 ASSERT_TRUE(map.has_value());
172 for (const auto &kv : *map) {
173 uint32_t uid = kv.first;
174 auto times1 = kv.second;
175 auto times2 = getUidConcurrentTimes(uid);
176 ASSERT_TRUE(times2.has_value());
177 for (uint32_t i = 0; i < times1.active.size(); ++i) {
178 ASSERT_LE(times2->active[i] - times1.active[i], NSEC_PER_SEC);
179 }
180 for (uint32_t i = 0; i < times1.policy.size(); ++i) {
181 for (uint32_t j = 0; j < times1.policy[i].size(); ++j) {
182 ASSERT_LE(times2->policy[i][j] - times1.policy[i][j], NSEC_PER_SEC);
183 }
184 }
185 }
186}
187
Connor O'Briena178a732019-06-05 18:27:47 -0700188void TestCheckDelta(uint64_t before, uint64_t after) {
189 // Times should never decrease
190 ASSERT_LE(before, after);
191 // UID can't have run for more than ~1s on each CPU
192 ASSERT_LE(after - before, NSEC_PER_SEC * 2 * get_nprocs_conf());
193}
194
Connor O'Brien26de80f2019-06-11 13:49:19 -0700195TEST(TimeInStateTest, AllUidTimeInStateMonotonic) {
Connor O'Briena178a732019-06-05 18:27:47 -0700196 auto map1 = getUidsCpuFreqTimes();
197 ASSERT_TRUE(map1.has_value());
198 sleep(1);
199 auto map2 = getUidsCpuFreqTimes();
200 ASSERT_TRUE(map2.has_value());
201
202 for (const auto &kv : *map1) {
203 uint32_t uid = kv.first;
204 auto times = kv.second;
205 ASSERT_NE(map2->find(uid), map2->end());
206 for (uint32_t policy = 0; policy < times.size(); ++policy) {
207 for (uint32_t freqIdx = 0; freqIdx < times[policy].size(); ++freqIdx) {
208 auto before = times[policy][freqIdx];
209 auto after = (*map2)[uid][policy][freqIdx];
210 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
211 }
212 }
213 }
214}
215
Connor O'Brien26de80f2019-06-11 13:49:19 -0700216TEST(TimeInStateTest, AllUidConcurrentTimesMonotonic) {
217 auto map1 = getUidsConcurrentTimes();
218 ASSERT_TRUE(map1.has_value());
219 ASSERT_FALSE(map1->empty());
220 sleep(1);
221 auto map2 = getUidsConcurrentTimes();
222 ASSERT_TRUE(map2.has_value());
223 ASSERT_FALSE(map2->empty());
224
225 for (const auto &kv : *map1) {
226 uint32_t uid = kv.first;
227 auto times = kv.second;
228 ASSERT_NE(map2->find(uid), map2->end());
229 for (uint32_t i = 0; i < times.active.size(); ++i) {
230 auto before = times.active[i];
231 auto after = (*map2)[uid].active[i];
232 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
233 }
234 for (uint32_t policy = 0; policy < times.policy.size(); ++policy) {
235 for (uint32_t idx = 0; idx < times.policy[policy].size(); ++idx) {
236 auto before = times.policy[policy][idx];
237 auto after = (*map2)[uid].policy[policy][idx];
238 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
239 }
240 }
241 }
242}
243
244TEST(TimeInStateTest, AllUidTimeInStateSanityCheck) {
Connor O'Briena178a732019-06-05 18:27:47 -0700245 auto map = getUidsCpuFreqTimes();
246 ASSERT_TRUE(map.has_value());
247
248 bool foundLargeValue = false;
249 for (const auto &kv : *map) {
250 for (const auto &timeVec : kv.second) {
251 for (const auto &time : timeVec) {
252 ASSERT_LE(time, NSEC_PER_YEAR);
253 if (time > UINT32_MAX) foundLargeValue = true;
254 }
255 }
256 }
257 // UINT32_MAX nanoseconds is less than 5 seconds, so if every part of our pipeline is using
258 // uint64_t as expected, we should have some times higher than that.
259 ASSERT_TRUE(foundLargeValue);
260}
261
Connor O'Brien26de80f2019-06-11 13:49:19 -0700262TEST(TimeInStateTest, AllUidConcurrentTimesSanityCheck) {
263 auto concurrentMap = getUidsConcurrentTimes();
264 ASSERT_TRUE(concurrentMap);
265
266 bool activeFoundLargeValue = false;
267 bool policyFoundLargeValue = false;
268 for (const auto &kv : *concurrentMap) {
269 for (const auto &time : kv.second.active) {
270 ASSERT_LE(time, NSEC_PER_YEAR);
271 if (time > UINT32_MAX) activeFoundLargeValue = true;
272 }
273 for (const auto &policyTimeVec : kv.second.policy) {
274 for (const auto &time : policyTimeVec) {
275 ASSERT_LE(time, NSEC_PER_YEAR);
276 if (time > UINT32_MAX) policyFoundLargeValue = true;
277 }
278 }
279 }
280 // UINT32_MAX nanoseconds is less than 5 seconds, so if every part of our pipeline is using
281 // uint64_t as expected, we should have some times higher than that.
282 ASSERT_TRUE(activeFoundLargeValue);
283 ASSERT_TRUE(policyFoundLargeValue);
284}
285
286TEST(TimeInStateTest, AllUidTimesConsistent) {
287 auto tisMap = getUidsCpuFreqTimes();
288 ASSERT_TRUE(tisMap.has_value());
289
290 auto concurrentMap = getUidsConcurrentTimes();
291 ASSERT_TRUE(concurrentMap.has_value());
292
293 ASSERT_EQ(tisMap->size(), concurrentMap->size());
294 for (const auto &kv : *tisMap) {
295 uint32_t uid = kv.first;
296 auto times = kv.second;
297 ASSERT_NE(concurrentMap->find(uid), concurrentMap->end());
298
299 auto concurrentTimes = (*concurrentMap)[uid];
300 ASSERT_NO_FATAL_FAILURE(TestUidTimesConsistent(times, concurrentTimes));
301 }
302}
303
Connor O'Brien57337192018-11-20 12:49:16 -0800304TEST(TimeInStateTest, RemoveUid) {
Connor O'Briena178a732019-06-05 18:27:47 -0700305 uint32_t uid = 0;
306 {
307 // Find an unused UID
308 auto times = getUidsCpuFreqTimes();
309 ASSERT_TRUE(times.has_value());
310 ASSERT_FALSE(times->empty());
311 for (const auto &kv : *times) uid = std::max(uid, kv.first);
312 ++uid;
313 }
314 {
315 // Add a map entry for our fake UID by copying a real map entry
Connor O'Brien26de80f2019-06-11 13:49:19 -0700316 android::base::unique_fd fd{
317 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_time_in_state_map")};
Connor O'Briena178a732019-06-05 18:27:47 -0700318 ASSERT_GE(fd, 0);
319 time_key_t k;
320 ASSERT_FALSE(getFirstMapKey(fd, &k));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700321 std::vector<tis_val_t> vals(get_nprocs_conf());
Connor O'Brien1a180402019-06-07 16:39:49 -0700322 ASSERT_FALSE(findMapEntry(fd, &k, vals.data()));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700323 uint32_t copiedUid = k.uid;
Connor O'Briena178a732019-06-05 18:27:47 -0700324 k.uid = uid;
Connor O'Brien1a180402019-06-07 16:39:49 -0700325 ASSERT_FALSE(writeToMapEntry(fd, &k, vals.data(), BPF_NOEXIST));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700326
327 android::base::unique_fd fd2{
328 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")};
329 k.uid = copiedUid;
330 k.bucket = 0;
331 std::vector<concurrent_val_t> cvals(get_nprocs_conf());
332 ASSERT_FALSE(findMapEntry(fd2, &k, cvals.data()));
333 k.uid = uid;
334 ASSERT_FALSE(writeToMapEntry(fd2, &k, cvals.data(), BPF_NOEXIST));
Connor O'Briena178a732019-06-05 18:27:47 -0700335 }
336 auto times = getUidCpuFreqTimes(uid);
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700337 ASSERT_TRUE(times.has_value());
338 ASSERT_FALSE(times->empty());
Connor O'Brien57337192018-11-20 12:49:16 -0800339
Connor O'Brien26de80f2019-06-11 13:49:19 -0700340 auto concurrentTimes = getUidConcurrentTimes(0);
341 ASSERT_TRUE(concurrentTimes.has_value());
342 ASSERT_FALSE(concurrentTimes->active.empty());
343 ASSERT_FALSE(concurrentTimes->policy.empty());
344
Connor O'Brien57337192018-11-20 12:49:16 -0800345 uint64_t sum = 0;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700346 for (size_t i = 0; i < times->size(); ++i) {
347 for (auto x : (*times)[i]) sum += x;
Connor O'Brien57337192018-11-20 12:49:16 -0800348 }
349 ASSERT_GT(sum, (uint64_t)0);
350
Connor O'Brien26de80f2019-06-11 13:49:19 -0700351 uint64_t activeSum = 0;
352 for (size_t i = 0; i < concurrentTimes->active.size(); ++i) {
353 activeSum += concurrentTimes->active[i];
354 }
355 ASSERT_GT(activeSum, (uint64_t)0);
356
357 ASSERT_TRUE(clearUidTimes(uid));
Connor O'Brien57337192018-11-20 12:49:16 -0800358
Connor O'Briena178a732019-06-05 18:27:47 -0700359 auto allTimes = getUidsCpuFreqTimes();
360 ASSERT_TRUE(allTimes.has_value());
361 ASSERT_FALSE(allTimes->empty());
362 ASSERT_EQ(allTimes->find(uid), allTimes->end());
Connor O'Brien26de80f2019-06-11 13:49:19 -0700363
364 auto allConcurrentTimes = getUidsConcurrentTimes();
365 ASSERT_TRUE(allConcurrentTimes.has_value());
366 ASSERT_FALSE(allConcurrentTimes->empty());
367 ASSERT_EQ(allConcurrentTimes->find(uid), allConcurrentTimes->end());
Connor O'Brien57337192018-11-20 12:49:16 -0800368}
369
Connor O'Brien8f296eb2019-10-01 17:58:38 -0700370TEST(TimeInStateTest, GetCpuFreqs) {
371 auto freqs = getCpuFreqs();
372 ASSERT_TRUE(freqs.has_value());
373
374 auto times = getUidCpuFreqTimes(0);
375 ASSERT_TRUE(times.has_value());
376
377 ASSERT_EQ(freqs->size(), times->size());
378 for (size_t i = 0; i < freqs->size(); ++i) EXPECT_EQ((*freqs)[i].size(), (*times)[i].size());
379}
380
Connor O'Brien57337192018-11-20 12:49:16 -0800381} // namespace bpf
382} // namespace android