blob: 0d5f4129355288f82764cbb2814594c8a5121a8f [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
Connor O'Brien597f6372020-10-20 14:48:40 -0700390TEST(TimeInStateTest, AllUidConcurrentTimesFailsOnInvalidBucket) {
391 uint32_t uid = 0;
392 {
393 // Find an unused UID
394 auto map = getUidsConcurrentTimes();
395 ASSERT_TRUE(map.has_value());
396 ASSERT_FALSE(map->empty());
397 for (const auto &kv : *map) uid = std::max(uid, kv.first);
398 ++uid;
399 }
400 android::base::unique_fd fd{
401 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")};
402 ASSERT_GE(fd, 0);
403 uint32_t nCpus = get_nprocs_conf();
404 uint32_t maxBucket = (nCpus - 1) / CPUS_PER_ENTRY;
405 time_key_t key = {.uid = uid, .bucket = maxBucket + 1};
406 std::vector<concurrent_val_t> vals(nCpus);
407 ASSERT_FALSE(writeToMapEntry(fd, &key, vals.data(), BPF_NOEXIST));
408 EXPECT_FALSE(getUidsConcurrentTimes().has_value());
409 ASSERT_FALSE(deleteMapEntry(fd, &key));
410}
411
Connor O'Brien26de80f2019-06-11 13:49:19 -0700412TEST(TimeInStateTest, AllUidTimesConsistent) {
413 auto tisMap = getUidsCpuFreqTimes();
414 ASSERT_TRUE(tisMap.has_value());
415
416 auto concurrentMap = getUidsConcurrentTimes();
417 ASSERT_TRUE(concurrentMap.has_value());
418
419 ASSERT_EQ(tisMap->size(), concurrentMap->size());
420 for (const auto &kv : *tisMap) {
421 uint32_t uid = kv.first;
422 auto times = kv.second;
423 ASSERT_NE(concurrentMap->find(uid), concurrentMap->end());
424
425 auto concurrentTimes = (*concurrentMap)[uid];
426 ASSERT_NO_FATAL_FAILURE(TestUidTimesConsistent(times, concurrentTimes));
427 }
428}
429
Connor O'Brien57337192018-11-20 12:49:16 -0800430TEST(TimeInStateTest, RemoveUid) {
Connor O'Briena178a732019-06-05 18:27:47 -0700431 uint32_t uid = 0;
432 {
433 // Find an unused UID
434 auto times = getUidsCpuFreqTimes();
435 ASSERT_TRUE(times.has_value());
436 ASSERT_FALSE(times->empty());
437 for (const auto &kv : *times) uid = std::max(uid, kv.first);
438 ++uid;
439 }
440 {
441 // Add a map entry for our fake UID by copying a real map entry
Connor O'Brien26de80f2019-06-11 13:49:19 -0700442 android::base::unique_fd fd{
443 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_time_in_state_map")};
Connor O'Briena178a732019-06-05 18:27:47 -0700444 ASSERT_GE(fd, 0);
445 time_key_t k;
446 ASSERT_FALSE(getFirstMapKey(fd, &k));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700447 std::vector<tis_val_t> vals(get_nprocs_conf());
Connor O'Brien1a180402019-06-07 16:39:49 -0700448 ASSERT_FALSE(findMapEntry(fd, &k, vals.data()));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700449 uint32_t copiedUid = k.uid;
Connor O'Briena178a732019-06-05 18:27:47 -0700450 k.uid = uid;
Connor O'Brien1a180402019-06-07 16:39:49 -0700451 ASSERT_FALSE(writeToMapEntry(fd, &k, vals.data(), BPF_NOEXIST));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700452
453 android::base::unique_fd fd2{
454 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")};
455 k.uid = copiedUid;
456 k.bucket = 0;
457 std::vector<concurrent_val_t> cvals(get_nprocs_conf());
458 ASSERT_FALSE(findMapEntry(fd2, &k, cvals.data()));
459 k.uid = uid;
460 ASSERT_FALSE(writeToMapEntry(fd2, &k, cvals.data(), BPF_NOEXIST));
Connor O'Briena178a732019-06-05 18:27:47 -0700461 }
462 auto times = getUidCpuFreqTimes(uid);
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700463 ASSERT_TRUE(times.has_value());
464 ASSERT_FALSE(times->empty());
Connor O'Brien57337192018-11-20 12:49:16 -0800465
Connor O'Brien26de80f2019-06-11 13:49:19 -0700466 auto concurrentTimes = getUidConcurrentTimes(0);
467 ASSERT_TRUE(concurrentTimes.has_value());
468 ASSERT_FALSE(concurrentTimes->active.empty());
469 ASSERT_FALSE(concurrentTimes->policy.empty());
470
Connor O'Brien57337192018-11-20 12:49:16 -0800471 uint64_t sum = 0;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700472 for (size_t i = 0; i < times->size(); ++i) {
473 for (auto x : (*times)[i]) sum += x;
Connor O'Brien57337192018-11-20 12:49:16 -0800474 }
475 ASSERT_GT(sum, (uint64_t)0);
476
Connor O'Brien26de80f2019-06-11 13:49:19 -0700477 uint64_t activeSum = 0;
478 for (size_t i = 0; i < concurrentTimes->active.size(); ++i) {
479 activeSum += concurrentTimes->active[i];
480 }
481 ASSERT_GT(activeSum, (uint64_t)0);
482
483 ASSERT_TRUE(clearUidTimes(uid));
Connor O'Brien57337192018-11-20 12:49:16 -0800484
Connor O'Briena178a732019-06-05 18:27:47 -0700485 auto allTimes = getUidsCpuFreqTimes();
486 ASSERT_TRUE(allTimes.has_value());
487 ASSERT_FALSE(allTimes->empty());
488 ASSERT_EQ(allTimes->find(uid), allTimes->end());
Connor O'Brien26de80f2019-06-11 13:49:19 -0700489
490 auto allConcurrentTimes = getUidsConcurrentTimes();
491 ASSERT_TRUE(allConcurrentTimes.has_value());
492 ASSERT_FALSE(allConcurrentTimes->empty());
493 ASSERT_EQ(allConcurrentTimes->find(uid), allConcurrentTimes->end());
Connor O'Brien57337192018-11-20 12:49:16 -0800494}
495
Connor O'Brien8f296eb2019-10-01 17:58:38 -0700496TEST(TimeInStateTest, GetCpuFreqs) {
497 auto freqs = getCpuFreqs();
498 ASSERT_TRUE(freqs.has_value());
499
500 auto times = getUidCpuFreqTimes(0);
501 ASSERT_TRUE(times.has_value());
502
503 ASSERT_EQ(freqs->size(), times->size());
504 for (size_t i = 0; i < freqs->size(); ++i) EXPECT_EQ((*freqs)[i].size(), (*times)[i].size());
505}
506
Connor O'Brien57337192018-11-20 12:49:16 -0800507} // namespace bpf
508} // namespace android