| Steven Moreland | 7253041 | 2020-01-13 16:13:58 -0800 | [diff] [blame] | 1 | /* | 
|  | 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'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 17 |  | 
| Connor O'Brien | d65f2a0 | 2019-08-28 16:15:38 -0700 | [diff] [blame] | 18 | #include <bpf_timeinstate.h> | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | #include <sys/sysinfo.h> | 
|  | 21 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 22 | #include <numeric> | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 23 | #include <unordered_map> | 
|  | 24 | #include <vector> | 
|  | 25 |  | 
|  | 26 | #include <gtest/gtest.h> | 
|  | 27 |  | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 28 | #include <android-base/unique_fd.h> | 
|  | 29 | #include <bpf/BpfMap.h> | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 30 | #include <cputimeinstate.h> | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 31 | #include <libbpf.h> | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 32 |  | 
|  | 33 | namespace android { | 
|  | 34 | namespace bpf { | 
|  | 35 |  | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 36 | static constexpr uint64_t NSEC_PER_SEC = 1000000000; | 
|  | 37 | static constexpr uint64_t NSEC_PER_YEAR = NSEC_PER_SEC * 60 * 60 * 24 * 365; | 
|  | 38 |  | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 39 | using std::vector; | 
|  | 40 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 41 | TEST(TimeInStateTest, SingleUidTimeInState) { | 
| Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 42 | auto times = getUidCpuFreqTimes(0); | 
|  | 43 | ASSERT_TRUE(times.has_value()); | 
|  | 44 | EXPECT_FALSE(times->empty()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 47 | TEST(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 |  | 
|  | 58 | static 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 |  | 
|  | 86 | static 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 |  | 
|  | 107 | TEST(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 |  | 
|  | 117 | TEST(TimeInStateTest, AllUidTimeInState) { | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 118 | uint64_t zero = 0; | 
|  | 119 | auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)}; | 
|  | 120 | for (const auto &map : maps) { | 
|  | 121 | ASSERT_TRUE(map.has_value()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 122 |  | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 123 | ASSERT_FALSE(map->empty()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 124 |  | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 125 | vector<size_t> sizes; | 
|  | 126 | auto firstEntry = map->begin()->second; | 
|  | 127 | for (const auto &subEntry : firstEntry) sizes.emplace_back(subEntry.size()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 128 |  | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 129 | 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 |  | 
|  | 136 | void 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 |  | 
|  | 153 | TEST(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'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 184 | } | 
|  | 185 | } | 
|  | 186 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 187 | TEST(TimeInStateTest, SingleAndAllUidTimeInStateConsistent) { | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 188 | 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'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 193 |  | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 194 | 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'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 199 |  | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 200 | 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'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 206 | } | 
|  | 207 | } | 
|  | 208 | } | 
|  | 209 | } | 
|  | 210 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 211 | TEST(TimeInStateTest, AllUidConcurrentTimes) { | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 212 | 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'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 217 |  | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 218 | 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'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 225 | } | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 |  | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 229 | TEST(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'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 253 | } | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 254 | } | 
|  | 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 |  | 
|  | 264 | TEST(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'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 281 | } | 
|  | 282 | } | 
|  | 283 | } | 
|  | 284 | } | 
|  | 285 |  | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 286 | void 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'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 293 | TEST(TimeInStateTest, AllUidTimeInStateMonotonic) { | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 294 | 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'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 314 | TEST(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 |  | 
|  | 342 | TEST(TimeInStateTest, AllUidTimeInStateSanityCheck) { | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 343 | uint64_t zero = 0; | 
|  | 344 | auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)}; | 
|  | 345 | for (const auto &map : maps) { | 
|  | 346 | ASSERT_TRUE(map.has_value()); | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 347 |  | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 348 | 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'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 355 | } | 
|  | 356 | } | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 357 | // 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'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 360 | } | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 363 | TEST(TimeInStateTest, AllUidConcurrentTimesSanityCheck) { | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 364 | uint64_t zero = 0; | 
|  | 365 | auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)}; | 
|  | 366 | for (const auto &concurrentMap : maps) { | 
|  | 367 | ASSERT_TRUE(concurrentMap); | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 368 |  | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 369 | bool activeFoundLargeValue = false; | 
|  | 370 | bool policyFoundLargeValue = false; | 
|  | 371 | for (const auto &kv : *concurrentMap) { | 
|  | 372 | for (const auto &time : kv.second.active) { | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 373 | ASSERT_LE(time, NSEC_PER_YEAR); | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 374 | 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'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 381 | } | 
|  | 382 | } | 
| Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 383 | // 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'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 387 | } | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 388 | } | 
|  | 389 |  | 
|  | 390 | TEST(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'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 408 | TEST(TimeInStateTest, RemoveUid) { | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 409 | 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'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 420 | android::base::unique_fd fd{ | 
|  | 421 | bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_time_in_state_map")}; | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 422 | ASSERT_GE(fd, 0); | 
|  | 423 | time_key_t k; | 
|  | 424 | ASSERT_FALSE(getFirstMapKey(fd, &k)); | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 425 | std::vector<tis_val_t> vals(get_nprocs_conf()); | 
| Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 426 | ASSERT_FALSE(findMapEntry(fd, &k, vals.data())); | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 427 | uint32_t copiedUid = k.uid; | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 428 | k.uid = uid; | 
| Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 429 | ASSERT_FALSE(writeToMapEntry(fd, &k, vals.data(), BPF_NOEXIST)); | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 430 |  | 
|  | 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'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 439 | } | 
|  | 440 | auto times = getUidCpuFreqTimes(uid); | 
| Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 441 | ASSERT_TRUE(times.has_value()); | 
|  | 442 | ASSERT_FALSE(times->empty()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 443 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 444 | 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'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 449 | uint64_t sum = 0; | 
| Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 450 | for (size_t i = 0; i < times->size(); ++i) { | 
|  | 451 | for (auto x : (*times)[i]) sum += x; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 452 | } | 
|  | 453 | ASSERT_GT(sum, (uint64_t)0); | 
|  | 454 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 455 | 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'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 462 |  | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 463 | auto allTimes = getUidsCpuFreqTimes(); | 
|  | 464 | ASSERT_TRUE(allTimes.has_value()); | 
|  | 465 | ASSERT_FALSE(allTimes->empty()); | 
|  | 466 | ASSERT_EQ(allTimes->find(uid), allTimes->end()); | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 467 |  | 
|  | 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'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 472 | } | 
|  | 473 |  | 
| Connor O'Brien | 8f296eb | 2019-10-01 17:58:38 -0700 | [diff] [blame] | 474 | TEST(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'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 485 | } // namespace bpf | 
|  | 486 | } // namespace android |