| 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 | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 118 | vector<size_t> sizes; | 
| Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 119 | auto map = getUidsCpuFreqTimes(); | 
|  | 120 | ASSERT_TRUE(map.has_value()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 121 |  | 
| Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 122 | ASSERT_FALSE(map->empty()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 123 |  | 
| Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 124 | auto firstEntry = map->begin()->second; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 125 | for (const auto &subEntry : firstEntry) sizes.emplace_back(subEntry.size()); | 
|  | 126 |  | 
| Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 127 | for (const auto &vec : *map) { | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 128 | ASSERT_EQ(vec.second.size(), sizes.size()); | 
|  | 129 | for (size_t i = 0; i < vec.second.size(); ++i) ASSERT_EQ(vec.second[i].size(), sizes[i]); | 
|  | 130 | } | 
|  | 131 | } | 
|  | 132 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 133 | TEST(TimeInStateTest, SingleAndAllUidTimeInStateConsistent) { | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 134 | auto map = getUidsCpuFreqTimes(); | 
|  | 135 | ASSERT_TRUE(map.has_value()); | 
|  | 136 | ASSERT_FALSE(map->empty()); | 
|  | 137 |  | 
|  | 138 | for (const auto &kv : *map) { | 
|  | 139 | uint32_t uid = kv.first; | 
|  | 140 | auto times1 = kv.second; | 
|  | 141 | auto times2 = getUidCpuFreqTimes(uid); | 
|  | 142 | ASSERT_TRUE(times2.has_value()); | 
|  | 143 |  | 
|  | 144 | ASSERT_EQ(times1.size(), times2->size()); | 
|  | 145 | for (uint32_t i = 0; i < times1.size(); ++i) { | 
|  | 146 | ASSERT_EQ(times1[i].size(), (*times2)[i].size()); | 
|  | 147 | for (uint32_t j = 0; j < times1[i].size(); ++j) { | 
|  | 148 | ASSERT_LE((*times2)[i][j] - times1[i][j], NSEC_PER_SEC); | 
|  | 149 | } | 
|  | 150 | } | 
|  | 151 | } | 
|  | 152 | } | 
|  | 153 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 154 | TEST(TimeInStateTest, AllUidConcurrentTimes) { | 
|  | 155 | auto map = getUidsConcurrentTimes(); | 
|  | 156 | ASSERT_TRUE(map.has_value()); | 
|  | 157 | ASSERT_FALSE(map->empty()); | 
|  | 158 |  | 
|  | 159 | auto firstEntry = map->begin()->second; | 
|  | 160 | for (const auto &kv : *map) { | 
|  | 161 | ASSERT_EQ(kv.second.active.size(), firstEntry.active.size()); | 
|  | 162 | ASSERT_EQ(kv.second.policy.size(), firstEntry.policy.size()); | 
|  | 163 | for (size_t i = 0; i < kv.second.policy.size(); ++i) { | 
|  | 164 | ASSERT_EQ(kv.second.policy[i].size(), firstEntry.policy[i].size()); | 
|  | 165 | } | 
|  | 166 | } | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | TEST(TimeInStateTest, SingleAndAllUidConcurrentTimesConsistent) { | 
|  | 170 | auto map = getUidsConcurrentTimes(); | 
|  | 171 | ASSERT_TRUE(map.has_value()); | 
|  | 172 | for (const auto &kv : *map) { | 
|  | 173 | uint32_t uid = kv.first; | 
|  | 174 | auto times1 = kv.second; | 
|  | 175 | auto times2 = getUidConcurrentTimes(uid); | 
|  | 176 | ASSERT_TRUE(times2.has_value()); | 
|  | 177 | for (uint32_t i = 0; i < times1.active.size(); ++i) { | 
|  | 178 | ASSERT_LE(times2->active[i] - times1.active[i], NSEC_PER_SEC); | 
|  | 179 | } | 
|  | 180 | for (uint32_t i = 0; i < times1.policy.size(); ++i) { | 
|  | 181 | for (uint32_t j = 0; j < times1.policy[i].size(); ++j) { | 
|  | 182 | ASSERT_LE(times2->policy[i][j] - times1.policy[i][j], NSEC_PER_SEC); | 
|  | 183 | } | 
|  | 184 | } | 
|  | 185 | } | 
|  | 186 | } | 
|  | 187 |  | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 188 | void TestCheckDelta(uint64_t before, uint64_t after) { | 
|  | 189 | // Times should never decrease | 
|  | 190 | ASSERT_LE(before, after); | 
|  | 191 | // UID can't have run for more than ~1s on each CPU | 
|  | 192 | ASSERT_LE(after - before, NSEC_PER_SEC * 2 * get_nprocs_conf()); | 
|  | 193 | } | 
|  | 194 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 195 | TEST(TimeInStateTest, AllUidTimeInStateMonotonic) { | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 196 | auto map1 = getUidsCpuFreqTimes(); | 
|  | 197 | ASSERT_TRUE(map1.has_value()); | 
|  | 198 | sleep(1); | 
|  | 199 | auto map2 = getUidsCpuFreqTimes(); | 
|  | 200 | ASSERT_TRUE(map2.has_value()); | 
|  | 201 |  | 
|  | 202 | for (const auto &kv : *map1) { | 
|  | 203 | uint32_t uid = kv.first; | 
|  | 204 | auto times = kv.second; | 
|  | 205 | ASSERT_NE(map2->find(uid), map2->end()); | 
|  | 206 | for (uint32_t policy = 0; policy < times.size(); ++policy) { | 
|  | 207 | for (uint32_t freqIdx = 0; freqIdx < times[policy].size(); ++freqIdx) { | 
|  | 208 | auto before = times[policy][freqIdx]; | 
|  | 209 | auto after = (*map2)[uid][policy][freqIdx]; | 
|  | 210 | ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after)); | 
|  | 211 | } | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 | } | 
|  | 215 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 216 | TEST(TimeInStateTest, AllUidConcurrentTimesMonotonic) { | 
|  | 217 | auto map1 = getUidsConcurrentTimes(); | 
|  | 218 | ASSERT_TRUE(map1.has_value()); | 
|  | 219 | ASSERT_FALSE(map1->empty()); | 
|  | 220 | sleep(1); | 
|  | 221 | auto map2 = getUidsConcurrentTimes(); | 
|  | 222 | ASSERT_TRUE(map2.has_value()); | 
|  | 223 | ASSERT_FALSE(map2->empty()); | 
|  | 224 |  | 
|  | 225 | for (const auto &kv : *map1) { | 
|  | 226 | uint32_t uid = kv.first; | 
|  | 227 | auto times = kv.second; | 
|  | 228 | ASSERT_NE(map2->find(uid), map2->end()); | 
|  | 229 | for (uint32_t i = 0; i < times.active.size(); ++i) { | 
|  | 230 | auto before = times.active[i]; | 
|  | 231 | auto after = (*map2)[uid].active[i]; | 
|  | 232 | ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after)); | 
|  | 233 | } | 
|  | 234 | for (uint32_t policy = 0; policy < times.policy.size(); ++policy) { | 
|  | 235 | for (uint32_t idx = 0; idx < times.policy[policy].size(); ++idx) { | 
|  | 236 | auto before = times.policy[policy][idx]; | 
|  | 237 | auto after = (*map2)[uid].policy[policy][idx]; | 
|  | 238 | ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after)); | 
|  | 239 | } | 
|  | 240 | } | 
|  | 241 | } | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | TEST(TimeInStateTest, AllUidTimeInStateSanityCheck) { | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 245 | auto map = getUidsCpuFreqTimes(); | 
|  | 246 | ASSERT_TRUE(map.has_value()); | 
|  | 247 |  | 
|  | 248 | bool foundLargeValue = false; | 
|  | 249 | for (const auto &kv : *map) { | 
|  | 250 | for (const auto &timeVec : kv.second) { | 
|  | 251 | for (const auto &time : timeVec) { | 
|  | 252 | ASSERT_LE(time, NSEC_PER_YEAR); | 
|  | 253 | if (time > UINT32_MAX) foundLargeValue = true; | 
|  | 254 | } | 
|  | 255 | } | 
|  | 256 | } | 
|  | 257 | // UINT32_MAX nanoseconds is less than 5 seconds, so if every part of our pipeline is using | 
|  | 258 | // uint64_t as expected, we should have some times higher than that. | 
|  | 259 | ASSERT_TRUE(foundLargeValue); | 
|  | 260 | } | 
|  | 261 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 262 | TEST(TimeInStateTest, AllUidConcurrentTimesSanityCheck) { | 
|  | 263 | auto concurrentMap = getUidsConcurrentTimes(); | 
|  | 264 | ASSERT_TRUE(concurrentMap); | 
|  | 265 |  | 
|  | 266 | bool activeFoundLargeValue = false; | 
|  | 267 | bool policyFoundLargeValue = false; | 
|  | 268 | for (const auto &kv : *concurrentMap) { | 
|  | 269 | for (const auto &time : kv.second.active) { | 
|  | 270 | ASSERT_LE(time, NSEC_PER_YEAR); | 
|  | 271 | if (time > UINT32_MAX) activeFoundLargeValue = true; | 
|  | 272 | } | 
|  | 273 | for (const auto &policyTimeVec : kv.second.policy) { | 
|  | 274 | for (const auto &time : policyTimeVec) { | 
|  | 275 | ASSERT_LE(time, NSEC_PER_YEAR); | 
|  | 276 | if (time > UINT32_MAX) policyFoundLargeValue = true; | 
|  | 277 | } | 
|  | 278 | } | 
|  | 279 | } | 
|  | 280 | // UINT32_MAX nanoseconds is less than 5 seconds, so if every part of our pipeline is using | 
|  | 281 | // uint64_t as expected, we should have some times higher than that. | 
|  | 282 | ASSERT_TRUE(activeFoundLargeValue); | 
|  | 283 | ASSERT_TRUE(policyFoundLargeValue); | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | TEST(TimeInStateTest, AllUidTimesConsistent) { | 
|  | 287 | auto tisMap = getUidsCpuFreqTimes(); | 
|  | 288 | ASSERT_TRUE(tisMap.has_value()); | 
|  | 289 |  | 
|  | 290 | auto concurrentMap = getUidsConcurrentTimes(); | 
|  | 291 | ASSERT_TRUE(concurrentMap.has_value()); | 
|  | 292 |  | 
|  | 293 | ASSERT_EQ(tisMap->size(), concurrentMap->size()); | 
|  | 294 | for (const auto &kv : *tisMap) { | 
|  | 295 | uint32_t uid = kv.first; | 
|  | 296 | auto times = kv.second; | 
|  | 297 | ASSERT_NE(concurrentMap->find(uid), concurrentMap->end()); | 
|  | 298 |  | 
|  | 299 | auto concurrentTimes = (*concurrentMap)[uid]; | 
|  | 300 | ASSERT_NO_FATAL_FAILURE(TestUidTimesConsistent(times, concurrentTimes)); | 
|  | 301 | } | 
|  | 302 | } | 
|  | 303 |  | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 304 | TEST(TimeInStateTest, RemoveUid) { | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 305 | uint32_t uid = 0; | 
|  | 306 | { | 
|  | 307 | // Find an unused UID | 
|  | 308 | auto times = getUidsCpuFreqTimes(); | 
|  | 309 | ASSERT_TRUE(times.has_value()); | 
|  | 310 | ASSERT_FALSE(times->empty()); | 
|  | 311 | for (const auto &kv : *times) uid = std::max(uid, kv.first); | 
|  | 312 | ++uid; | 
|  | 313 | } | 
|  | 314 | { | 
|  | 315 | // Add a map entry for our fake UID by copying a real map entry | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 316 | android::base::unique_fd fd{ | 
|  | 317 | 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] | 318 | ASSERT_GE(fd, 0); | 
|  | 319 | time_key_t k; | 
|  | 320 | ASSERT_FALSE(getFirstMapKey(fd, &k)); | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 321 | std::vector<tis_val_t> vals(get_nprocs_conf()); | 
| Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 322 | ASSERT_FALSE(findMapEntry(fd, &k, vals.data())); | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 323 | uint32_t copiedUid = k.uid; | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 324 | k.uid = uid; | 
| Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 325 | ASSERT_FALSE(writeToMapEntry(fd, &k, vals.data(), BPF_NOEXIST)); | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 326 |  | 
|  | 327 | android::base::unique_fd fd2{ | 
|  | 328 | bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")}; | 
|  | 329 | k.uid = copiedUid; | 
|  | 330 | k.bucket = 0; | 
|  | 331 | std::vector<concurrent_val_t> cvals(get_nprocs_conf()); | 
|  | 332 | ASSERT_FALSE(findMapEntry(fd2, &k, cvals.data())); | 
|  | 333 | k.uid = uid; | 
|  | 334 | ASSERT_FALSE(writeToMapEntry(fd2, &k, cvals.data(), BPF_NOEXIST)); | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 335 | } | 
|  | 336 | auto times = getUidCpuFreqTimes(uid); | 
| Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 337 | ASSERT_TRUE(times.has_value()); | 
|  | 338 | ASSERT_FALSE(times->empty()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 339 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 340 | auto concurrentTimes = getUidConcurrentTimes(0); | 
|  | 341 | ASSERT_TRUE(concurrentTimes.has_value()); | 
|  | 342 | ASSERT_FALSE(concurrentTimes->active.empty()); | 
|  | 343 | ASSERT_FALSE(concurrentTimes->policy.empty()); | 
|  | 344 |  | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 345 | uint64_t sum = 0; | 
| Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 346 | for (size_t i = 0; i < times->size(); ++i) { | 
|  | 347 | for (auto x : (*times)[i]) sum += x; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 348 | } | 
|  | 349 | ASSERT_GT(sum, (uint64_t)0); | 
|  | 350 |  | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 351 | uint64_t activeSum = 0; | 
|  | 352 | for (size_t i = 0; i < concurrentTimes->active.size(); ++i) { | 
|  | 353 | activeSum += concurrentTimes->active[i]; | 
|  | 354 | } | 
|  | 355 | ASSERT_GT(activeSum, (uint64_t)0); | 
|  | 356 |  | 
|  | 357 | ASSERT_TRUE(clearUidTimes(uid)); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 358 |  | 
| Connor O'Brien | a178a73 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 359 | auto allTimes = getUidsCpuFreqTimes(); | 
|  | 360 | ASSERT_TRUE(allTimes.has_value()); | 
|  | 361 | ASSERT_FALSE(allTimes->empty()); | 
|  | 362 | ASSERT_EQ(allTimes->find(uid), allTimes->end()); | 
| Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 363 |  | 
|  | 364 | auto allConcurrentTimes = getUidsConcurrentTimes(); | 
|  | 365 | ASSERT_TRUE(allConcurrentTimes.has_value()); | 
|  | 366 | ASSERT_FALSE(allConcurrentTimes->empty()); | 
|  | 367 | ASSERT_EQ(allConcurrentTimes->find(uid), allConcurrentTimes->end()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 368 | } | 
|  | 369 |  | 
| Connor O'Brien | 8f296eb | 2019-10-01 17:58:38 -0700 | [diff] [blame] | 370 | TEST(TimeInStateTest, GetCpuFreqs) { | 
|  | 371 | auto freqs = getCpuFreqs(); | 
|  | 372 | ASSERT_TRUE(freqs.has_value()); | 
|  | 373 |  | 
|  | 374 | auto times = getUidCpuFreqTimes(0); | 
|  | 375 | ASSERT_TRUE(times.has_value()); | 
|  | 376 |  | 
|  | 377 | ASSERT_EQ(freqs->size(), times->size()); | 
|  | 378 | for (size_t i = 0; i < freqs->size(); ++i) EXPECT_EQ((*freqs)[i].size(), (*times)[i].size()); | 
|  | 379 | } | 
|  | 380 |  | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 381 | } // namespace bpf | 
|  | 382 | } // namespace android |