blob: ea2a2008b7e68d918609b845bc9b56b6e14e649e [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'Brien2a716a42020-01-31 18:51:56 -0800118 uint64_t zero = 0;
119 auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)};
120 for (const auto &map : maps) {
121 ASSERT_TRUE(map.has_value());
Connor O'Brien57337192018-11-20 12:49:16 -0800122
Connor O'Brien2a716a42020-01-31 18:51:56 -0800123 ASSERT_FALSE(map->empty());
Connor O'Brien57337192018-11-20 12:49:16 -0800124
Connor O'Brien2a716a42020-01-31 18:51:56 -0800125 vector<size_t> sizes;
126 auto firstEntry = map->begin()->second;
127 for (const auto &subEntry : firstEntry) sizes.emplace_back(subEntry.size());
Connor O'Brien57337192018-11-20 12:49:16 -0800128
Connor O'Brien2a716a42020-01-31 18:51:56 -0800129 for (const auto &vec : *map) {
130 ASSERT_EQ(vec.second.size(), sizes.size());
131 for (size_t i = 0; i < vec.second.size(); ++i) ASSERT_EQ(vec.second[i].size(), sizes[i]);
132 }
133 }
134}
135
136void TestCheckUpdate(const std::vector<std::vector<uint64_t>> &before,
137 const std::vector<std::vector<uint64_t>> &after) {
138 ASSERT_EQ(before.size(), after.size());
139 uint64_t sumBefore = 0, sumAfter = 0;
140 for (size_t i = 0; i < before.size(); ++i) {
141 ASSERT_EQ(before[i].size(), after[i].size());
142 for (size_t j = 0; j < before[i].size(); ++j) {
143 // Times should never decrease
144 ASSERT_LE(before[i][j], after[i][j]);
145 }
146 sumBefore += std::accumulate(before[i].begin(), before[i].end(), (uint64_t)0);
147 sumAfter += std::accumulate(after[i].begin(), after[i].end(), (uint64_t)0);
148 }
149 ASSERT_LE(sumBefore, sumAfter);
150 ASSERT_LE(sumAfter - sumBefore, NSEC_PER_SEC);
151}
152
153TEST(TimeInStateTest, AllUidUpdatedTimeInState) {
154 uint64_t lastUpdate = 0;
155 auto map1 = getUidsUpdatedCpuFreqTimes(&lastUpdate);
156 ASSERT_TRUE(map1.has_value());
157 ASSERT_FALSE(map1->empty());
158 ASSERT_NE(lastUpdate, (uint64_t)0);
159 uint64_t oldLastUpdate = lastUpdate;
160
161 // Sleep briefly to trigger a context switch, ensuring we see at least one update.
162 struct timespec ts;
163 ts.tv_sec = 0;
164 ts.tv_nsec = 1000000;
165 nanosleep (&ts, NULL);
166
167 auto map2 = getUidsUpdatedCpuFreqTimes(&lastUpdate);
168 ASSERT_TRUE(map2.has_value());
169 ASSERT_FALSE(map2->empty());
170 ASSERT_NE(lastUpdate, oldLastUpdate);
171
172 bool someUidsExcluded = false;
173 for (const auto &[uid, v] : *map1) {
174 if (map2->find(uid) == map2->end()) {
175 someUidsExcluded = true;
176 break;
177 }
178 }
179 ASSERT_TRUE(someUidsExcluded);
180
181 for (const auto &[uid, newTimes] : *map2) {
182 ASSERT_NE(map1->find(uid), map1->end());
183 ASSERT_NO_FATAL_FAILURE(TestCheckUpdate((*map1)[uid], newTimes));
Connor O'Brien57337192018-11-20 12:49:16 -0800184 }
185}
186
Connor O'Brien26de80f2019-06-11 13:49:19 -0700187TEST(TimeInStateTest, SingleAndAllUidTimeInStateConsistent) {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800188 uint64_t zero = 0;
189 auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)};
190 for (const auto &map : maps) {
191 ASSERT_TRUE(map.has_value());
192 ASSERT_FALSE(map->empty());
Connor O'Briena178a732019-06-05 18:27:47 -0700193
Connor O'Brien2a716a42020-01-31 18:51:56 -0800194 for (const auto &kv : *map) {
195 uint32_t uid = kv.first;
196 auto times1 = kv.second;
197 auto times2 = getUidCpuFreqTimes(uid);
198 ASSERT_TRUE(times2.has_value());
Connor O'Briena178a732019-06-05 18:27:47 -0700199
Connor O'Brien2a716a42020-01-31 18:51:56 -0800200 ASSERT_EQ(times1.size(), times2->size());
201 for (uint32_t i = 0; i < times1.size(); ++i) {
202 ASSERT_EQ(times1[i].size(), (*times2)[i].size());
203 for (uint32_t j = 0; j < times1[i].size(); ++j) {
204 ASSERT_LE((*times2)[i][j] - times1[i][j], NSEC_PER_SEC);
205 }
Connor O'Briena178a732019-06-05 18:27:47 -0700206 }
207 }
208 }
209}
210
Connor O'Brien26de80f2019-06-11 13:49:19 -0700211TEST(TimeInStateTest, AllUidConcurrentTimes) {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800212 uint64_t zero = 0;
213 auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)};
214 for (const auto &map : maps) {
215 ASSERT_TRUE(map.has_value());
216 ASSERT_FALSE(map->empty());
Connor O'Brien26de80f2019-06-11 13:49:19 -0700217
Connor O'Brien2a716a42020-01-31 18:51:56 -0800218 auto firstEntry = map->begin()->second;
219 for (const auto &kv : *map) {
220 ASSERT_EQ(kv.second.active.size(), firstEntry.active.size());
221 ASSERT_EQ(kv.second.policy.size(), firstEntry.policy.size());
222 for (size_t i = 0; i < kv.second.policy.size(); ++i) {
223 ASSERT_EQ(kv.second.policy[i].size(), firstEntry.policy[i].size());
224 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700225 }
226 }
227}
228
Connor O'Brien2a716a42020-01-31 18:51:56 -0800229TEST(TimeInStateTest, AllUidUpdatedConcurrentTimes) {
230 uint64_t lastUpdate = 0;
231 auto map1 = getUidsUpdatedConcurrentTimes(&lastUpdate);
232 ASSERT_TRUE(map1.has_value());
233 ASSERT_FALSE(map1->empty());
234 ASSERT_NE(lastUpdate, (uint64_t)0);
235
236 // Sleep briefly to trigger a context switch, ensuring we see at least one update.
237 struct timespec ts;
238 ts.tv_sec = 0;
239 ts.tv_nsec = 1000000;
240 nanosleep (&ts, NULL);
241
242 uint64_t oldLastUpdate = lastUpdate;
243 auto map2 = getUidsUpdatedConcurrentTimes(&lastUpdate);
244 ASSERT_TRUE(map2.has_value());
245 ASSERT_FALSE(map2->empty());
246 ASSERT_NE(lastUpdate, oldLastUpdate);
247
248 bool someUidsExcluded = false;
249 for (const auto &[uid, v] : *map1) {
250 if (map2->find(uid) == map2->end()) {
251 someUidsExcluded = true;
252 break;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700253 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800254 }
255 ASSERT_TRUE(someUidsExcluded);
256
257 for (const auto &[uid, newTimes] : *map2) {
258 ASSERT_NE(map1->find(uid), map1->end());
259 ASSERT_NO_FATAL_FAILURE(TestCheckUpdate({(*map1)[uid].active},{newTimes.active}));
260 ASSERT_NO_FATAL_FAILURE(TestCheckUpdate((*map1)[uid].policy, newTimes.policy));
261 }
262}
263
264TEST(TimeInStateTest, SingleAndAllUidConcurrentTimesConsistent) {
265 uint64_t zero = 0;
266 auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)};
267 for (const auto &map : maps) {
268 ASSERT_TRUE(map.has_value());
269 for (const auto &kv : *map) {
270 uint32_t uid = kv.first;
271 auto times1 = kv.second;
272 auto times2 = getUidConcurrentTimes(uid);
273 ASSERT_TRUE(times2.has_value());
274 for (uint32_t i = 0; i < times1.active.size(); ++i) {
275 ASSERT_LE(times2->active[i] - times1.active[i], NSEC_PER_SEC);
276 }
277 for (uint32_t i = 0; i < times1.policy.size(); ++i) {
278 for (uint32_t j = 0; j < times1.policy[i].size(); ++j) {
279 ASSERT_LE(times2->policy[i][j] - times1.policy[i][j], NSEC_PER_SEC);
280 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700281 }
282 }
283 }
284}
285
Connor O'Briena178a732019-06-05 18:27:47 -0700286void TestCheckDelta(uint64_t before, uint64_t after) {
287 // Times should never decrease
288 ASSERT_LE(before, after);
289 // UID can't have run for more than ~1s on each CPU
290 ASSERT_LE(after - before, NSEC_PER_SEC * 2 * get_nprocs_conf());
291}
292
Connor O'Brien26de80f2019-06-11 13:49:19 -0700293TEST(TimeInStateTest, AllUidTimeInStateMonotonic) {
Connor O'Briena178a732019-06-05 18:27:47 -0700294 auto map1 = getUidsCpuFreqTimes();
295 ASSERT_TRUE(map1.has_value());
296 sleep(1);
297 auto map2 = getUidsCpuFreqTimes();
298 ASSERT_TRUE(map2.has_value());
299
300 for (const auto &kv : *map1) {
301 uint32_t uid = kv.first;
302 auto times = kv.second;
303 ASSERT_NE(map2->find(uid), map2->end());
304 for (uint32_t policy = 0; policy < times.size(); ++policy) {
305 for (uint32_t freqIdx = 0; freqIdx < times[policy].size(); ++freqIdx) {
306 auto before = times[policy][freqIdx];
307 auto after = (*map2)[uid][policy][freqIdx];
308 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
309 }
310 }
311 }
312}
313
Connor O'Brien26de80f2019-06-11 13:49:19 -0700314TEST(TimeInStateTest, AllUidConcurrentTimesMonotonic) {
315 auto map1 = getUidsConcurrentTimes();
316 ASSERT_TRUE(map1.has_value());
317 ASSERT_FALSE(map1->empty());
318 sleep(1);
319 auto map2 = getUidsConcurrentTimes();
320 ASSERT_TRUE(map2.has_value());
321 ASSERT_FALSE(map2->empty());
322
323 for (const auto &kv : *map1) {
324 uint32_t uid = kv.first;
325 auto times = kv.second;
326 ASSERT_NE(map2->find(uid), map2->end());
327 for (uint32_t i = 0; i < times.active.size(); ++i) {
328 auto before = times.active[i];
329 auto after = (*map2)[uid].active[i];
330 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
331 }
332 for (uint32_t policy = 0; policy < times.policy.size(); ++policy) {
333 for (uint32_t idx = 0; idx < times.policy[policy].size(); ++idx) {
334 auto before = times.policy[policy][idx];
335 auto after = (*map2)[uid].policy[policy][idx];
336 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
337 }
338 }
339 }
340}
341
342TEST(TimeInStateTest, AllUidTimeInStateSanityCheck) {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800343 uint64_t zero = 0;
344 auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)};
345 for (const auto &map : maps) {
346 ASSERT_TRUE(map.has_value());
Connor O'Briena178a732019-06-05 18:27:47 -0700347
Connor O'Brien2a716a42020-01-31 18:51:56 -0800348 bool foundLargeValue = false;
349 for (const auto &kv : *map) {
350 for (const auto &timeVec : kv.second) {
351 for (const auto &time : timeVec) {
352 ASSERT_LE(time, NSEC_PER_YEAR);
353 if (time > UINT32_MAX) foundLargeValue = true;
354 }
Connor O'Briena178a732019-06-05 18:27:47 -0700355 }
356 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800357 // UINT32_MAX nanoseconds is less than 5 seconds, so if every part of our pipeline is using
358 // uint64_t as expected, we should have some times higher than that.
359 ASSERT_TRUE(foundLargeValue);
Connor O'Briena178a732019-06-05 18:27:47 -0700360 }
Connor O'Briena178a732019-06-05 18:27:47 -0700361}
362
Connor O'Brien26de80f2019-06-11 13:49:19 -0700363TEST(TimeInStateTest, AllUidConcurrentTimesSanityCheck) {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800364 uint64_t zero = 0;
365 auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)};
366 for (const auto &concurrentMap : maps) {
367 ASSERT_TRUE(concurrentMap);
Connor O'Brien26de80f2019-06-11 13:49:19 -0700368
Connor O'Brien2a716a42020-01-31 18:51:56 -0800369 bool activeFoundLargeValue = false;
370 bool policyFoundLargeValue = false;
371 for (const auto &kv : *concurrentMap) {
372 for (const auto &time : kv.second.active) {
Connor O'Brien26de80f2019-06-11 13:49:19 -0700373 ASSERT_LE(time, NSEC_PER_YEAR);
Connor O'Brien2a716a42020-01-31 18:51:56 -0800374 if (time > UINT32_MAX) activeFoundLargeValue = true;
375 }
376 for (const auto &policyTimeVec : kv.second.policy) {
377 for (const auto &time : policyTimeVec) {
378 ASSERT_LE(time, NSEC_PER_YEAR);
379 if (time > UINT32_MAX) policyFoundLargeValue = true;
380 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700381 }
382 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800383 // UINT32_MAX nanoseconds is less than 5 seconds, so if every part of our pipeline is using
384 // uint64_t as expected, we should have some times higher than that.
385 ASSERT_TRUE(activeFoundLargeValue);
386 ASSERT_TRUE(policyFoundLargeValue);
Connor O'Brien26de80f2019-06-11 13:49:19 -0700387 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700388}
389
390TEST(TimeInStateTest, AllUidTimesConsistent) {
391 auto tisMap = getUidsCpuFreqTimes();
392 ASSERT_TRUE(tisMap.has_value());
393
394 auto concurrentMap = getUidsConcurrentTimes();
395 ASSERT_TRUE(concurrentMap.has_value());
396
397 ASSERT_EQ(tisMap->size(), concurrentMap->size());
398 for (const auto &kv : *tisMap) {
399 uint32_t uid = kv.first;
400 auto times = kv.second;
401 ASSERT_NE(concurrentMap->find(uid), concurrentMap->end());
402
403 auto concurrentTimes = (*concurrentMap)[uid];
404 ASSERT_NO_FATAL_FAILURE(TestUidTimesConsistent(times, concurrentTimes));
405 }
406}
407
Connor O'Brien57337192018-11-20 12:49:16 -0800408TEST(TimeInStateTest, RemoveUid) {
Connor O'Briena178a732019-06-05 18:27:47 -0700409 uint32_t uid = 0;
410 {
411 // Find an unused UID
412 auto times = getUidsCpuFreqTimes();
413 ASSERT_TRUE(times.has_value());
414 ASSERT_FALSE(times->empty());
415 for (const auto &kv : *times) uid = std::max(uid, kv.first);
416 ++uid;
417 }
418 {
419 // Add a map entry for our fake UID by copying a real map entry
Connor O'Brien26de80f2019-06-11 13:49:19 -0700420 android::base::unique_fd fd{
421 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_time_in_state_map")};
Connor O'Briena178a732019-06-05 18:27:47 -0700422 ASSERT_GE(fd, 0);
423 time_key_t k;
424 ASSERT_FALSE(getFirstMapKey(fd, &k));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700425 std::vector<tis_val_t> vals(get_nprocs_conf());
Connor O'Brien1a180402019-06-07 16:39:49 -0700426 ASSERT_FALSE(findMapEntry(fd, &k, vals.data()));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700427 uint32_t copiedUid = k.uid;
Connor O'Briena178a732019-06-05 18:27:47 -0700428 k.uid = uid;
Connor O'Brien1a180402019-06-07 16:39:49 -0700429 ASSERT_FALSE(writeToMapEntry(fd, &k, vals.data(), BPF_NOEXIST));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700430
431 android::base::unique_fd fd2{
432 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")};
433 k.uid = copiedUid;
434 k.bucket = 0;
435 std::vector<concurrent_val_t> cvals(get_nprocs_conf());
436 ASSERT_FALSE(findMapEntry(fd2, &k, cvals.data()));
437 k.uid = uid;
438 ASSERT_FALSE(writeToMapEntry(fd2, &k, cvals.data(), BPF_NOEXIST));
Connor O'Briena178a732019-06-05 18:27:47 -0700439 }
440 auto times = getUidCpuFreqTimes(uid);
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700441 ASSERT_TRUE(times.has_value());
442 ASSERT_FALSE(times->empty());
Connor O'Brien57337192018-11-20 12:49:16 -0800443
Connor O'Brien26de80f2019-06-11 13:49:19 -0700444 auto concurrentTimes = getUidConcurrentTimes(0);
445 ASSERT_TRUE(concurrentTimes.has_value());
446 ASSERT_FALSE(concurrentTimes->active.empty());
447 ASSERT_FALSE(concurrentTimes->policy.empty());
448
Connor O'Brien57337192018-11-20 12:49:16 -0800449 uint64_t sum = 0;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700450 for (size_t i = 0; i < times->size(); ++i) {
451 for (auto x : (*times)[i]) sum += x;
Connor O'Brien57337192018-11-20 12:49:16 -0800452 }
453 ASSERT_GT(sum, (uint64_t)0);
454
Connor O'Brien26de80f2019-06-11 13:49:19 -0700455 uint64_t activeSum = 0;
456 for (size_t i = 0; i < concurrentTimes->active.size(); ++i) {
457 activeSum += concurrentTimes->active[i];
458 }
459 ASSERT_GT(activeSum, (uint64_t)0);
460
461 ASSERT_TRUE(clearUidTimes(uid));
Connor O'Brien57337192018-11-20 12:49:16 -0800462
Connor O'Briena178a732019-06-05 18:27:47 -0700463 auto allTimes = getUidsCpuFreqTimes();
464 ASSERT_TRUE(allTimes.has_value());
465 ASSERT_FALSE(allTimes->empty());
466 ASSERT_EQ(allTimes->find(uid), allTimes->end());
Connor O'Brien26de80f2019-06-11 13:49:19 -0700467
468 auto allConcurrentTimes = getUidsConcurrentTimes();
469 ASSERT_TRUE(allConcurrentTimes.has_value());
470 ASSERT_FALSE(allConcurrentTimes->empty());
471 ASSERT_EQ(allConcurrentTimes->find(uid), allConcurrentTimes->end());
Connor O'Brien57337192018-11-20 12:49:16 -0800472}
473
Connor O'Brien8f296eb2019-10-01 17:58:38 -0700474TEST(TimeInStateTest, GetCpuFreqs) {
475 auto freqs = getCpuFreqs();
476 ASSERT_TRUE(freqs.has_value());
477
478 auto times = getUidCpuFreqTimes(0);
479 ASSERT_TRUE(times.has_value());
480
481 ASSERT_EQ(freqs->size(), times->size());
482 for (size_t i = 0; i < freqs->size(); ++i) EXPECT_EQ((*freqs)[i].size(), (*times)[i].size());
483}
484
Connor O'Brien57337192018-11-20 12:49:16 -0800485} // namespace bpf
486} // namespace android