blob: d25b2e9d6fba9e5eda97ff2b9877ea3c3205aa56 [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
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -070022#include <pthread.h>
23#include <semaphore.h>
Connor O'Brien26de80f2019-06-11 13:49:19 -070024#include <numeric>
Connor O'Brien57337192018-11-20 12:49:16 -080025#include <unordered_map>
26#include <vector>
27
28#include <gtest/gtest.h>
29
Connor O'Briena178a732019-06-05 18:27:47 -070030#include <android-base/unique_fd.h>
31#include <bpf/BpfMap.h>
Connor O'Brien57337192018-11-20 12:49:16 -080032#include <cputimeinstate.h>
Connor O'Briena178a732019-06-05 18:27:47 -070033#include <libbpf.h>
Connor O'Brien57337192018-11-20 12:49:16 -080034
35namespace android {
36namespace bpf {
37
Connor O'Briena178a732019-06-05 18:27:47 -070038static constexpr uint64_t NSEC_PER_SEC = 1000000000;
39static constexpr uint64_t NSEC_PER_YEAR = NSEC_PER_SEC * 60 * 60 * 24 * 365;
40
Connor O'Brien57337192018-11-20 12:49:16 -080041using std::vector;
42
Rafal Slawik27c48db2021-01-05 19:10:07 +000043TEST(TimeInStateTest, TotalTimeInState) {
44 auto times = getTotalCpuFreqTimes();
45 ASSERT_TRUE(times.has_value());
46 EXPECT_FALSE(times->empty());
47}
48
Connor O'Brien26de80f2019-06-11 13:49:19 -070049TEST(TimeInStateTest, SingleUidTimeInState) {
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070050 auto times = getUidCpuFreqTimes(0);
51 ASSERT_TRUE(times.has_value());
52 EXPECT_FALSE(times->empty());
Connor O'Brien57337192018-11-20 12:49:16 -080053}
54
Connor O'Brien26de80f2019-06-11 13:49:19 -070055TEST(TimeInStateTest, SingleUidConcurrentTimes) {
56 auto concurrentTimes = getUidConcurrentTimes(0);
57 ASSERT_TRUE(concurrentTimes.has_value());
58 ASSERT_FALSE(concurrentTimes->active.empty());
59 ASSERT_FALSE(concurrentTimes->policy.empty());
60
61 uint64_t policyEntries = 0;
62 for (const auto &policyTimeVec : concurrentTimes->policy) policyEntries += policyTimeVec.size();
63 ASSERT_EQ(concurrentTimes->active.size(), policyEntries);
64}
65
66static void TestConcurrentTimesConsistent(const struct concurrent_time_t &concurrentTime) {
67 size_t maxPolicyCpus = 0;
68 for (const auto &vec : concurrentTime.policy) {
69 maxPolicyCpus = std::max(maxPolicyCpus, vec.size());
70 }
71 uint64_t policySum = 0;
72 for (size_t i = 0; i < maxPolicyCpus; ++i) {
73 for (const auto &vec : concurrentTime.policy) {
74 if (i < vec.size()) policySum += vec[i];
75 }
76 ASSERT_LE(concurrentTime.active[i], policySum);
77 policySum -= concurrentTime.active[i];
78 }
79 policySum = 0;
80 for (size_t i = 0; i < concurrentTime.active.size(); ++i) {
81 for (const auto &vec : concurrentTime.policy) {
82 if (i < vec.size()) policySum += vec[vec.size() - 1 - i];
83 }
84 auto activeSum = concurrentTime.active[concurrentTime.active.size() - 1 - i];
85 // This check is slightly flaky because we may read a map entry in the middle of an update
86 // when active times have been updated but policy times have not. This happens infrequently
87 // and can be distinguished from more serious bugs by re-running the test: if the underlying
88 // data itself is inconsistent, the test will fail every time.
89 ASSERT_LE(activeSum, policySum);
90 policySum -= activeSum;
91 }
92}
93
94static void TestUidTimesConsistent(const std::vector<std::vector<uint64_t>> &timeInState,
95 const struct concurrent_time_t &concurrentTime) {
96 ASSERT_NO_FATAL_FAILURE(TestConcurrentTimesConsistent(concurrentTime));
97 ASSERT_EQ(timeInState.size(), concurrentTime.policy.size());
98 uint64_t policySum = 0;
99 for (uint32_t i = 0; i < timeInState.size(); ++i) {
100 uint64_t tisSum =
101 std::accumulate(timeInState[i].begin(), timeInState[i].end(), (uint64_t)0);
102 uint64_t concurrentSum = std::accumulate(concurrentTime.policy[i].begin(),
103 concurrentTime.policy[i].end(), (uint64_t)0);
104 if (tisSum < concurrentSum)
105 ASSERT_LE(concurrentSum - tisSum, NSEC_PER_SEC);
106 else
107 ASSERT_LE(tisSum - concurrentSum, NSEC_PER_SEC);
108 policySum += concurrentSum;
109 }
110 uint64_t activeSum = std::accumulate(concurrentTime.active.begin(), concurrentTime.active.end(),
111 (uint64_t)0);
112 EXPECT_EQ(activeSum, policySum);
113}
114
115TEST(TimeInStateTest, SingleUidTimesConsistent) {
116 auto times = getUidCpuFreqTimes(0);
117 ASSERT_TRUE(times.has_value());
118
119 auto concurrentTimes = getUidConcurrentTimes(0);
120 ASSERT_TRUE(concurrentTimes.has_value());
121
122 ASSERT_NO_FATAL_FAILURE(TestUidTimesConsistent(*times, *concurrentTimes));
123}
124
125TEST(TimeInStateTest, AllUidTimeInState) {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800126 uint64_t zero = 0;
127 auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)};
128 for (const auto &map : maps) {
129 ASSERT_TRUE(map.has_value());
Connor O'Brien57337192018-11-20 12:49:16 -0800130
Connor O'Brien2a716a42020-01-31 18:51:56 -0800131 ASSERT_FALSE(map->empty());
Connor O'Brien57337192018-11-20 12:49:16 -0800132
Connor O'Brien2a716a42020-01-31 18:51:56 -0800133 vector<size_t> sizes;
134 auto firstEntry = map->begin()->second;
135 for (const auto &subEntry : firstEntry) sizes.emplace_back(subEntry.size());
Connor O'Brien57337192018-11-20 12:49:16 -0800136
Connor O'Brien2a716a42020-01-31 18:51:56 -0800137 for (const auto &vec : *map) {
138 ASSERT_EQ(vec.second.size(), sizes.size());
139 for (size_t i = 0; i < vec.second.size(); ++i) ASSERT_EQ(vec.second[i].size(), sizes[i]);
140 }
141 }
142}
143
144void TestCheckUpdate(const std::vector<std::vector<uint64_t>> &before,
145 const std::vector<std::vector<uint64_t>> &after) {
146 ASSERT_EQ(before.size(), after.size());
147 uint64_t sumBefore = 0, sumAfter = 0;
148 for (size_t i = 0; i < before.size(); ++i) {
149 ASSERT_EQ(before[i].size(), after[i].size());
150 for (size_t j = 0; j < before[i].size(); ++j) {
151 // Times should never decrease
152 ASSERT_LE(before[i][j], after[i][j]);
153 }
154 sumBefore += std::accumulate(before[i].begin(), before[i].end(), (uint64_t)0);
155 sumAfter += std::accumulate(after[i].begin(), after[i].end(), (uint64_t)0);
156 }
157 ASSERT_LE(sumBefore, sumAfter);
158 ASSERT_LE(sumAfter - sumBefore, NSEC_PER_SEC);
159}
160
161TEST(TimeInStateTest, AllUidUpdatedTimeInState) {
162 uint64_t lastUpdate = 0;
163 auto map1 = getUidsUpdatedCpuFreqTimes(&lastUpdate);
164 ASSERT_TRUE(map1.has_value());
165 ASSERT_FALSE(map1->empty());
166 ASSERT_NE(lastUpdate, (uint64_t)0);
167 uint64_t oldLastUpdate = lastUpdate;
168
169 // Sleep briefly to trigger a context switch, ensuring we see at least one update.
170 struct timespec ts;
171 ts.tv_sec = 0;
172 ts.tv_nsec = 1000000;
173 nanosleep (&ts, NULL);
174
175 auto map2 = getUidsUpdatedCpuFreqTimes(&lastUpdate);
176 ASSERT_TRUE(map2.has_value());
177 ASSERT_FALSE(map2->empty());
178 ASSERT_NE(lastUpdate, oldLastUpdate);
179
180 bool someUidsExcluded = false;
181 for (const auto &[uid, v] : *map1) {
182 if (map2->find(uid) == map2->end()) {
183 someUidsExcluded = true;
184 break;
185 }
186 }
187 ASSERT_TRUE(someUidsExcluded);
188
189 for (const auto &[uid, newTimes] : *map2) {
190 ASSERT_NE(map1->find(uid), map1->end());
191 ASSERT_NO_FATAL_FAILURE(TestCheckUpdate((*map1)[uid], newTimes));
Connor O'Brien57337192018-11-20 12:49:16 -0800192 }
193}
194
Rafal Slawik27c48db2021-01-05 19:10:07 +0000195TEST(TimeInStateTest, TotalAndAllUidTimeInStateConsistent) {
196 auto allUid = getUidsCpuFreqTimes();
197 auto total = getTotalCpuFreqTimes();
198
199 ASSERT_TRUE(allUid.has_value() && total.has_value());
200
201 // Check the number of policies.
202 ASSERT_EQ(allUid->at(0).size(), total->size());
203
204 for (uint32_t policyIdx = 0; policyIdx < total->size(); ++policyIdx) {
205 std::vector<uint64_t> totalTimes = total->at(policyIdx);
206 uint32_t totalFreqsCount = totalTimes.size();
207 std::vector<uint64_t> allUidTimes(totalFreqsCount, 0);
208 for (auto const &[uid, uidTimes]: *allUid) {
209 for (uint32_t freqIdx = 0; freqIdx < uidTimes[policyIdx].size(); ++freqIdx) {
210 allUidTimes[std::min(freqIdx, totalFreqsCount - 1)] += uidTimes[policyIdx][freqIdx];
211 }
212 }
213
214 for (uint32_t freqIdx = 0; freqIdx < totalFreqsCount; ++freqIdx) {
215 ASSERT_LE(allUidTimes[freqIdx], totalTimes[freqIdx]);
216 }
217 }
218}
219
Connor O'Brien26de80f2019-06-11 13:49:19 -0700220TEST(TimeInStateTest, SingleAndAllUidTimeInStateConsistent) {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800221 uint64_t zero = 0;
222 auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)};
223 for (const auto &map : maps) {
224 ASSERT_TRUE(map.has_value());
225 ASSERT_FALSE(map->empty());
Connor O'Briena178a732019-06-05 18:27:47 -0700226
Connor O'Brien2a716a42020-01-31 18:51:56 -0800227 for (const auto &kv : *map) {
228 uint32_t uid = kv.first;
229 auto times1 = kv.second;
230 auto times2 = getUidCpuFreqTimes(uid);
231 ASSERT_TRUE(times2.has_value());
Connor O'Briena178a732019-06-05 18:27:47 -0700232
Connor O'Brien2a716a42020-01-31 18:51:56 -0800233 ASSERT_EQ(times1.size(), times2->size());
234 for (uint32_t i = 0; i < times1.size(); ++i) {
235 ASSERT_EQ(times1[i].size(), (*times2)[i].size());
236 for (uint32_t j = 0; j < times1[i].size(); ++j) {
237 ASSERT_LE((*times2)[i][j] - times1[i][j], NSEC_PER_SEC);
238 }
Connor O'Briena178a732019-06-05 18:27:47 -0700239 }
240 }
241 }
242}
243
Connor O'Brien26de80f2019-06-11 13:49:19 -0700244TEST(TimeInStateTest, AllUidConcurrentTimes) {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800245 uint64_t zero = 0;
246 auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)};
247 for (const auto &map : maps) {
248 ASSERT_TRUE(map.has_value());
249 ASSERT_FALSE(map->empty());
Connor O'Brien26de80f2019-06-11 13:49:19 -0700250
Connor O'Brien2a716a42020-01-31 18:51:56 -0800251 auto firstEntry = map->begin()->second;
252 for (const auto &kv : *map) {
253 ASSERT_EQ(kv.second.active.size(), firstEntry.active.size());
254 ASSERT_EQ(kv.second.policy.size(), firstEntry.policy.size());
255 for (size_t i = 0; i < kv.second.policy.size(); ++i) {
256 ASSERT_EQ(kv.second.policy[i].size(), firstEntry.policy[i].size());
257 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700258 }
259 }
260}
261
Connor O'Brien2a716a42020-01-31 18:51:56 -0800262TEST(TimeInStateTest, AllUidUpdatedConcurrentTimes) {
263 uint64_t lastUpdate = 0;
264 auto map1 = getUidsUpdatedConcurrentTimes(&lastUpdate);
265 ASSERT_TRUE(map1.has_value());
266 ASSERT_FALSE(map1->empty());
267 ASSERT_NE(lastUpdate, (uint64_t)0);
268
269 // Sleep briefly to trigger a context switch, ensuring we see at least one update.
270 struct timespec ts;
271 ts.tv_sec = 0;
272 ts.tv_nsec = 1000000;
273 nanosleep (&ts, NULL);
274
275 uint64_t oldLastUpdate = lastUpdate;
276 auto map2 = getUidsUpdatedConcurrentTimes(&lastUpdate);
277 ASSERT_TRUE(map2.has_value());
278 ASSERT_FALSE(map2->empty());
279 ASSERT_NE(lastUpdate, oldLastUpdate);
280
281 bool someUidsExcluded = false;
282 for (const auto &[uid, v] : *map1) {
283 if (map2->find(uid) == map2->end()) {
284 someUidsExcluded = true;
285 break;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700286 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800287 }
288 ASSERT_TRUE(someUidsExcluded);
289
290 for (const auto &[uid, newTimes] : *map2) {
291 ASSERT_NE(map1->find(uid), map1->end());
292 ASSERT_NO_FATAL_FAILURE(TestCheckUpdate({(*map1)[uid].active},{newTimes.active}));
293 ASSERT_NO_FATAL_FAILURE(TestCheckUpdate((*map1)[uid].policy, newTimes.policy));
294 }
295}
296
297TEST(TimeInStateTest, SingleAndAllUidConcurrentTimesConsistent) {
298 uint64_t zero = 0;
299 auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)};
300 for (const auto &map : maps) {
301 ASSERT_TRUE(map.has_value());
302 for (const auto &kv : *map) {
303 uint32_t uid = kv.first;
304 auto times1 = kv.second;
305 auto times2 = getUidConcurrentTimes(uid);
306 ASSERT_TRUE(times2.has_value());
307 for (uint32_t i = 0; i < times1.active.size(); ++i) {
308 ASSERT_LE(times2->active[i] - times1.active[i], NSEC_PER_SEC);
309 }
310 for (uint32_t i = 0; i < times1.policy.size(); ++i) {
311 for (uint32_t j = 0; j < times1.policy[i].size(); ++j) {
312 ASSERT_LE(times2->policy[i][j] - times1.policy[i][j], NSEC_PER_SEC);
313 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700314 }
315 }
316 }
317}
318
Connor O'Briena178a732019-06-05 18:27:47 -0700319void TestCheckDelta(uint64_t before, uint64_t after) {
320 // Times should never decrease
321 ASSERT_LE(before, after);
322 // UID can't have run for more than ~1s on each CPU
323 ASSERT_LE(after - before, NSEC_PER_SEC * 2 * get_nprocs_conf());
324}
325
Rafal Slawik27c48db2021-01-05 19:10:07 +0000326TEST(TimeInStateTest, TotalTimeInStateMonotonic) {
327 auto before = getTotalCpuFreqTimes();
328 ASSERT_TRUE(before.has_value());
329 sleep(1);
330 auto after = getTotalCpuFreqTimes();
331 ASSERT_TRUE(after.has_value());
332
333 for (uint32_t policyIdx = 0; policyIdx < after->size(); ++policyIdx) {
334 auto timesBefore = before->at(policyIdx);
335 auto timesAfter = after->at(policyIdx);
336 for (uint32_t freqIdx = 0; freqIdx < timesAfter.size(); ++freqIdx) {
337 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(timesBefore[freqIdx], timesAfter[freqIdx]));
338 }
339 }
340}
341
Connor O'Brien26de80f2019-06-11 13:49:19 -0700342TEST(TimeInStateTest, AllUidTimeInStateMonotonic) {
Connor O'Briena178a732019-06-05 18:27:47 -0700343 auto map1 = getUidsCpuFreqTimes();
344 ASSERT_TRUE(map1.has_value());
345 sleep(1);
346 auto map2 = getUidsCpuFreqTimes();
347 ASSERT_TRUE(map2.has_value());
348
349 for (const auto &kv : *map1) {
350 uint32_t uid = kv.first;
351 auto times = kv.second;
352 ASSERT_NE(map2->find(uid), map2->end());
353 for (uint32_t policy = 0; policy < times.size(); ++policy) {
354 for (uint32_t freqIdx = 0; freqIdx < times[policy].size(); ++freqIdx) {
355 auto before = times[policy][freqIdx];
356 auto after = (*map2)[uid][policy][freqIdx];
357 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
358 }
359 }
360 }
361}
362
Connor O'Brien26de80f2019-06-11 13:49:19 -0700363TEST(TimeInStateTest, AllUidConcurrentTimesMonotonic) {
364 auto map1 = getUidsConcurrentTimes();
365 ASSERT_TRUE(map1.has_value());
366 ASSERT_FALSE(map1->empty());
367 sleep(1);
368 auto map2 = getUidsConcurrentTimes();
369 ASSERT_TRUE(map2.has_value());
370 ASSERT_FALSE(map2->empty());
371
372 for (const auto &kv : *map1) {
373 uint32_t uid = kv.first;
374 auto times = kv.second;
375 ASSERT_NE(map2->find(uid), map2->end());
376 for (uint32_t i = 0; i < times.active.size(); ++i) {
377 auto before = times.active[i];
378 auto after = (*map2)[uid].active[i];
379 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
380 }
381 for (uint32_t policy = 0; policy < times.policy.size(); ++policy) {
382 for (uint32_t idx = 0; idx < times.policy[policy].size(); ++idx) {
383 auto before = times.policy[policy][idx];
384 auto after = (*map2)[uid].policy[policy][idx];
385 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
386 }
387 }
388 }
389}
390
391TEST(TimeInStateTest, AllUidTimeInStateSanityCheck) {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800392 uint64_t zero = 0;
393 auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)};
394 for (const auto &map : maps) {
395 ASSERT_TRUE(map.has_value());
Connor O'Briena178a732019-06-05 18:27:47 -0700396
Connor O'Brien2a716a42020-01-31 18:51:56 -0800397 bool foundLargeValue = false;
398 for (const auto &kv : *map) {
399 for (const auto &timeVec : kv.second) {
400 for (const auto &time : timeVec) {
401 ASSERT_LE(time, NSEC_PER_YEAR);
402 if (time > UINT32_MAX) foundLargeValue = true;
403 }
Connor O'Briena178a732019-06-05 18:27:47 -0700404 }
405 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800406 // UINT32_MAX nanoseconds is less than 5 seconds, so if every part of our pipeline is using
407 // uint64_t as expected, we should have some times higher than that.
408 ASSERT_TRUE(foundLargeValue);
Connor O'Briena178a732019-06-05 18:27:47 -0700409 }
Connor O'Briena178a732019-06-05 18:27:47 -0700410}
411
Connor O'Brien26de80f2019-06-11 13:49:19 -0700412TEST(TimeInStateTest, AllUidConcurrentTimesSanityCheck) {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800413 uint64_t zero = 0;
414 auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)};
415 for (const auto &concurrentMap : maps) {
416 ASSERT_TRUE(concurrentMap);
Connor O'Brien26de80f2019-06-11 13:49:19 -0700417
Connor O'Brien2a716a42020-01-31 18:51:56 -0800418 bool activeFoundLargeValue = false;
419 bool policyFoundLargeValue = false;
420 for (const auto &kv : *concurrentMap) {
421 for (const auto &time : kv.second.active) {
Connor O'Brien26de80f2019-06-11 13:49:19 -0700422 ASSERT_LE(time, NSEC_PER_YEAR);
Connor O'Brien2a716a42020-01-31 18:51:56 -0800423 if (time > UINT32_MAX) activeFoundLargeValue = true;
424 }
425 for (const auto &policyTimeVec : kv.second.policy) {
426 for (const auto &time : policyTimeVec) {
427 ASSERT_LE(time, NSEC_PER_YEAR);
428 if (time > UINT32_MAX) policyFoundLargeValue = true;
429 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700430 }
431 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800432 // UINT32_MAX nanoseconds is less than 5 seconds, so if every part of our pipeline is using
433 // uint64_t as expected, we should have some times higher than that.
434 ASSERT_TRUE(activeFoundLargeValue);
435 ASSERT_TRUE(policyFoundLargeValue);
Connor O'Brien26de80f2019-06-11 13:49:19 -0700436 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700437}
438
Connor O'Brien597f6372020-10-20 14:48:40 -0700439TEST(TimeInStateTest, AllUidConcurrentTimesFailsOnInvalidBucket) {
440 uint32_t uid = 0;
441 {
442 // Find an unused UID
443 auto map = getUidsConcurrentTimes();
444 ASSERT_TRUE(map.has_value());
445 ASSERT_FALSE(map->empty());
446 for (const auto &kv : *map) uid = std::max(uid, kv.first);
447 ++uid;
448 }
449 android::base::unique_fd fd{
450 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")};
451 ASSERT_GE(fd, 0);
452 uint32_t nCpus = get_nprocs_conf();
453 uint32_t maxBucket = (nCpus - 1) / CPUS_PER_ENTRY;
454 time_key_t key = {.uid = uid, .bucket = maxBucket + 1};
455 std::vector<concurrent_val_t> vals(nCpus);
456 ASSERT_FALSE(writeToMapEntry(fd, &key, vals.data(), BPF_NOEXIST));
457 EXPECT_FALSE(getUidsConcurrentTimes().has_value());
458 ASSERT_FALSE(deleteMapEntry(fd, &key));
459}
460
Connor O'Brien26de80f2019-06-11 13:49:19 -0700461TEST(TimeInStateTest, AllUidTimesConsistent) {
462 auto tisMap = getUidsCpuFreqTimes();
463 ASSERT_TRUE(tisMap.has_value());
464
465 auto concurrentMap = getUidsConcurrentTimes();
466 ASSERT_TRUE(concurrentMap.has_value());
467
468 ASSERT_EQ(tisMap->size(), concurrentMap->size());
469 for (const auto &kv : *tisMap) {
470 uint32_t uid = kv.first;
471 auto times = kv.second;
472 ASSERT_NE(concurrentMap->find(uid), concurrentMap->end());
473
474 auto concurrentTimes = (*concurrentMap)[uid];
475 ASSERT_NO_FATAL_FAILURE(TestUidTimesConsistent(times, concurrentTimes));
476 }
477}
478
Connor O'Brien57337192018-11-20 12:49:16 -0800479TEST(TimeInStateTest, RemoveUid) {
Connor O'Briena178a732019-06-05 18:27:47 -0700480 uint32_t uid = 0;
481 {
482 // Find an unused UID
483 auto times = getUidsCpuFreqTimes();
484 ASSERT_TRUE(times.has_value());
485 ASSERT_FALSE(times->empty());
486 for (const auto &kv : *times) uid = std::max(uid, kv.first);
487 ++uid;
488 }
489 {
490 // Add a map entry for our fake UID by copying a real map entry
Connor O'Brien26de80f2019-06-11 13:49:19 -0700491 android::base::unique_fd fd{
492 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_time_in_state_map")};
Connor O'Briena178a732019-06-05 18:27:47 -0700493 ASSERT_GE(fd, 0);
494 time_key_t k;
495 ASSERT_FALSE(getFirstMapKey(fd, &k));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700496 std::vector<tis_val_t> vals(get_nprocs_conf());
Connor O'Brien1a180402019-06-07 16:39:49 -0700497 ASSERT_FALSE(findMapEntry(fd, &k, vals.data()));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700498 uint32_t copiedUid = k.uid;
Connor O'Briena178a732019-06-05 18:27:47 -0700499 k.uid = uid;
Connor O'Brien1a180402019-06-07 16:39:49 -0700500 ASSERT_FALSE(writeToMapEntry(fd, &k, vals.data(), BPF_NOEXIST));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700501
502 android::base::unique_fd fd2{
503 bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")};
504 k.uid = copiedUid;
505 k.bucket = 0;
506 std::vector<concurrent_val_t> cvals(get_nprocs_conf());
507 ASSERT_FALSE(findMapEntry(fd2, &k, cvals.data()));
508 k.uid = uid;
509 ASSERT_FALSE(writeToMapEntry(fd2, &k, cvals.data(), BPF_NOEXIST));
Connor O'Briena178a732019-06-05 18:27:47 -0700510 }
511 auto times = getUidCpuFreqTimes(uid);
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700512 ASSERT_TRUE(times.has_value());
513 ASSERT_FALSE(times->empty());
Connor O'Brien57337192018-11-20 12:49:16 -0800514
Connor O'Brien26de80f2019-06-11 13:49:19 -0700515 auto concurrentTimes = getUidConcurrentTimes(0);
516 ASSERT_TRUE(concurrentTimes.has_value());
517 ASSERT_FALSE(concurrentTimes->active.empty());
518 ASSERT_FALSE(concurrentTimes->policy.empty());
519
Connor O'Brien57337192018-11-20 12:49:16 -0800520 uint64_t sum = 0;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700521 for (size_t i = 0; i < times->size(); ++i) {
522 for (auto x : (*times)[i]) sum += x;
Connor O'Brien57337192018-11-20 12:49:16 -0800523 }
524 ASSERT_GT(sum, (uint64_t)0);
525
Connor O'Brien26de80f2019-06-11 13:49:19 -0700526 uint64_t activeSum = 0;
527 for (size_t i = 0; i < concurrentTimes->active.size(); ++i) {
528 activeSum += concurrentTimes->active[i];
529 }
530 ASSERT_GT(activeSum, (uint64_t)0);
531
532 ASSERT_TRUE(clearUidTimes(uid));
Connor O'Brien57337192018-11-20 12:49:16 -0800533
Connor O'Briena178a732019-06-05 18:27:47 -0700534 auto allTimes = getUidsCpuFreqTimes();
535 ASSERT_TRUE(allTimes.has_value());
536 ASSERT_FALSE(allTimes->empty());
537 ASSERT_EQ(allTimes->find(uid), allTimes->end());
Connor O'Brien26de80f2019-06-11 13:49:19 -0700538
539 auto allConcurrentTimes = getUidsConcurrentTimes();
540 ASSERT_TRUE(allConcurrentTimes.has_value());
541 ASSERT_FALSE(allConcurrentTimes->empty());
542 ASSERT_EQ(allConcurrentTimes->find(uid), allConcurrentTimes->end());
Connor O'Brien57337192018-11-20 12:49:16 -0800543}
544
Connor O'Brien8f296eb2019-10-01 17:58:38 -0700545TEST(TimeInStateTest, GetCpuFreqs) {
546 auto freqs = getCpuFreqs();
547 ASSERT_TRUE(freqs.has_value());
548
549 auto times = getUidCpuFreqTimes(0);
550 ASSERT_TRUE(times.has_value());
551
552 ASSERT_EQ(freqs->size(), times->size());
553 for (size_t i = 0; i < freqs->size(); ++i) EXPECT_EQ((*freqs)[i].size(), (*times)[i].size());
554}
555
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700556uint64_t timeNanos() {
557 struct timespec spec;
558 clock_gettime(CLOCK_MONOTONIC, &spec);
559 return spec.tv_sec * 1000000000 + spec.tv_nsec;
560}
561
562// Keeps CPU busy with some number crunching
563void useCpu() {
564 long sum = 0;
565 for (int i = 0; i < 100000; i++) {
566 sum *= i;
567 }
568}
569
570sem_t pingsem, pongsem;
571
572void *testThread(void *) {
573 for (int i = 0; i < 10; i++) {
574 sem_wait(&pingsem);
575 useCpu();
576 sem_post(&pongsem);
577 }
578 return nullptr;
579}
580
581TEST(TimeInStateTest, GetAggregatedTaskCpuFreqTimes) {
582 uint64_t startTimeNs = timeNanos();
583
584 sem_init(&pingsem, 0, 1);
585 sem_init(&pongsem, 0, 0);
586
587 pthread_t thread;
588 ASSERT_EQ(pthread_create(&thread, NULL, &testThread, NULL), 0);
589
590 // This process may have been running for some time, so when we start tracking
591 // CPU time, the very first switch may include the accumulated time.
592 // Yield the remainder of this timeslice to the newly created thread.
593 sem_wait(&pongsem);
594 sem_post(&pingsem);
595
596 pid_t tgid = getpid();
597 startTrackingProcessCpuTimes(tgid);
598
599 pid_t tid = pthread_gettid_np(thread);
600 startAggregatingTaskCpuTimes(tid, 42);
601
602 // Play ping-pong with the other thread to ensure that both threads get
603 // some CPU time.
604 for (int i = 0; i < 9; i++) {
605 sem_wait(&pongsem);
606 useCpu();
607 sem_post(&pingsem);
608 }
609
610 pthread_join(thread, NULL);
611
612 std::optional<std::unordered_map<uint16_t, std::vector<std::vector<uint64_t>>>> optionalMap =
613 getAggregatedTaskCpuFreqTimes(tgid, {0, 42});
614 ASSERT_TRUE(optionalMap);
615
616 std::unordered_map<uint16_t, std::vector<std::vector<uint64_t>>> map = *optionalMap;
617 ASSERT_EQ(map.size(), 2u);
618
619 uint64_t testDurationNs = timeNanos() - startTimeNs;
620 for (auto pair : map) {
621 uint16_t aggregationKey = pair.first;
622 ASSERT_TRUE(aggregationKey == 0 || aggregationKey == 42);
623
624 std::vector<std::vector<uint64_t>> timesInState = pair.second;
625 uint64_t totalCpuTime = 0;
626 for (size_t i = 0; i < timesInState.size(); i++) {
627 for (size_t j = 0; j < timesInState[i].size(); j++) {
628 totalCpuTime += timesInState[i][j];
629 }
630 }
631 ASSERT_GT(totalCpuTime, 0ul);
632 ASSERT_LE(totalCpuTime, testDurationNs);
633 }
634}
635
Connor O'Brien57337192018-11-20 12:49:16 -0800636} // namespace bpf
637} // namespace android