Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | #define LOG_TAG "libtimeinstate" |
| 18 | |
| 19 | #include "cputimeinstate.h" |
Connor O'Brien | d65f2a0 | 2019-08-28 16:15:38 -0700 | [diff] [blame^] | 20 | #include <bpf_timeinstate.h> |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 21 | |
| 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
| 24 | #include <inttypes.h> |
Connor O'Brien | c92ef10 | 2019-07-24 15:42:11 -0700 | [diff] [blame] | 25 | #include <sys/sysinfo.h> |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 26 | |
| 27 | #include <mutex> |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 28 | #include <numeric> |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 29 | #include <optional> |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 30 | #include <set> |
| 31 | #include <string> |
| 32 | #include <unordered_map> |
| 33 | #include <vector> |
| 34 | |
| 35 | #include <android-base/file.h> |
| 36 | #include <android-base/parseint.h> |
| 37 | #include <android-base/stringprintf.h> |
| 38 | #include <android-base/strings.h> |
| 39 | #include <android-base/unique_fd.h> |
| 40 | #include <bpf/BpfMap.h> |
| 41 | #include <libbpf.h> |
| 42 | #include <log/log.h> |
| 43 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 44 | using android::base::StringPrintf; |
| 45 | using android::base::unique_fd; |
| 46 | |
| 47 | namespace android { |
| 48 | namespace bpf { |
| 49 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 50 | static std::mutex gInitializedMutex; |
| 51 | static bool gInitialized = false; |
| 52 | static uint32_t gNPolicies = 0; |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 53 | static uint32_t gNCpus = 0; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 54 | static std::vector<std::vector<uint32_t>> gPolicyFreqs; |
| 55 | static std::vector<std::vector<uint32_t>> gPolicyCpus; |
| 56 | static std::set<uint32_t> gAllFreqs; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 57 | static unique_fd gTisMapFd; |
| 58 | static unique_fd gConcurrentMapFd; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 59 | |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 60 | static std::optional<std::vector<uint32_t>> readNumbersFromFile(const std::string &path) { |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 61 | std::string data; |
| 62 | |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 63 | if (!android::base::ReadFileToString(path, &data)) return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 64 | |
| 65 | auto strings = android::base::Split(data, " \n"); |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 66 | std::vector<uint32_t> ret; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 67 | for (const auto &s : strings) { |
| 68 | if (s.empty()) continue; |
| 69 | uint32_t n; |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 70 | if (!android::base::ParseUint(s, &n)) return {}; |
| 71 | ret.emplace_back(n); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 72 | } |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 73 | return ret; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int isPolicyFile(const struct dirent *d) { |
| 77 | return android::base::StartsWith(d->d_name, "policy"); |
| 78 | } |
| 79 | |
| 80 | static int comparePolicyFiles(const struct dirent **d1, const struct dirent **d2) { |
| 81 | uint32_t policyN1, policyN2; |
| 82 | if (sscanf((*d1)->d_name, "policy%" SCNu32 "", &policyN1) != 1 || |
| 83 | sscanf((*d2)->d_name, "policy%" SCNu32 "", &policyN2) != 1) |
| 84 | return 0; |
| 85 | return policyN1 - policyN2; |
| 86 | } |
| 87 | |
| 88 | static bool initGlobals() { |
| 89 | std::lock_guard<std::mutex> guard(gInitializedMutex); |
| 90 | if (gInitialized) return true; |
| 91 | |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 92 | gNCpus = get_nprocs_conf(); |
| 93 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 94 | struct dirent **dirlist; |
| 95 | const char basepath[] = "/sys/devices/system/cpu/cpufreq"; |
| 96 | int ret = scandir(basepath, &dirlist, isPolicyFile, comparePolicyFiles); |
| 97 | if (ret == -1) return false; |
| 98 | gNPolicies = ret; |
| 99 | |
| 100 | std::vector<std::string> policyFileNames; |
| 101 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 102 | policyFileNames.emplace_back(dirlist[i]->d_name); |
| 103 | free(dirlist[i]); |
| 104 | } |
| 105 | free(dirlist); |
| 106 | |
| 107 | for (const auto &policy : policyFileNames) { |
| 108 | std::vector<uint32_t> freqs; |
| 109 | for (const auto &name : {"available", "boost"}) { |
| 110 | std::string path = |
| 111 | StringPrintf("%s/%s/scaling_%s_frequencies", basepath, policy.c_str(), name); |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 112 | auto nums = readNumbersFromFile(path); |
| 113 | if (!nums) return false; |
| 114 | freqs.insert(freqs.end(), nums->begin(), nums->end()); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 115 | } |
| 116 | std::sort(freqs.begin(), freqs.end()); |
| 117 | gPolicyFreqs.emplace_back(freqs); |
| 118 | |
| 119 | for (auto freq : freqs) gAllFreqs.insert(freq); |
| 120 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 121 | std::string path = StringPrintf("%s/%s/%s", basepath, policy.c_str(), "related_cpus"); |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 122 | auto cpus = readNumbersFromFile(path); |
| 123 | if (!cpus) return false; |
| 124 | gPolicyCpus.emplace_back(*cpus); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 127 | gTisMapFd = unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_time_in_state_map")}; |
| 128 | if (gTisMapFd < 0) return false; |
| 129 | |
| 130 | gConcurrentMapFd = |
| 131 | unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")}; |
| 132 | if (gConcurrentMapFd < 0) return false; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 133 | |
| 134 | gInitialized = true; |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) { |
| 139 | std::string path = StringPrintf(BPF_FS_PATH "prog_time_in_state_tracepoint_%s_%s", |
| 140 | eventType.c_str(), eventName.c_str()); |
| 141 | int prog_fd = bpf_obj_get(path.c_str()); |
Connor O'Brien | d250acc | 2019-01-23 17:21:41 -0800 | [diff] [blame] | 142 | if (prog_fd < 0) return false; |
| 143 | return bpf_attach_tracepoint(prog_fd, eventType.c_str(), eventName.c_str()) >= 0; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes. |
| 147 | // Returns true on success, false otherwise. |
| 148 | // Tracking is active only once a live process has successfully called this function; if the calling |
| 149 | // process dies then it must be called again to resume tracking. |
| 150 | // This function should *not* be called while tracking is already active; doing so is unnecessary |
| 151 | // and can lead to accounting errors. |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 152 | bool startTrackingUidTimes() { |
Connor O'Brien | 57b75dc | 2019-06-06 17:48:20 -0700 | [diff] [blame] | 153 | if (!initGlobals()) return false; |
| 154 | |
| 155 | unique_fd fd(bpf_obj_get(BPF_FS_PATH "map_time_in_state_cpu_policy_map")); |
| 156 | if (fd < 0) return false; |
| 157 | |
| 158 | for (uint32_t i = 0; i < gPolicyCpus.size(); ++i) { |
| 159 | for (auto &cpu : gPolicyCpus[i]) { |
| 160 | if (writeToMapEntry(fd, &cpu, &i, BPF_ANY)) return false; |
| 161 | } |
| 162 | } |
| 163 | |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 164 | unique_fd fd2(bpf_obj_get(BPF_FS_PATH "map_time_in_state_freq_to_idx_map")); |
| 165 | if (fd2 < 0) return false; |
| 166 | freq_idx_key_t key; |
| 167 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 168 | key.policy = i; |
| 169 | for (uint32_t j = 0; j < gPolicyFreqs[i].size(); ++j) { |
| 170 | key.freq = gPolicyFreqs[i][j]; |
| 171 | // Start indexes at 1 so that uninitialized state is distinguishable from lowest freq. |
| 172 | // The uid_times map still uses 0-based indexes, and the sched_switch program handles |
| 173 | // conversion between them, so this does not affect our map reading code. |
| 174 | uint32_t idx = j + 1; |
| 175 | if (writeToMapEntry(fd2, &key, &idx, BPF_ANY)) return false; |
| 176 | } |
| 177 | } |
| 178 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 179 | return attachTracepointProgram("sched", "sched_switch") && |
| 180 | attachTracepointProgram("power", "cpu_frequency"); |
| 181 | } |
| 182 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 183 | // Retrieve the times in ns that uid spent running at each CPU frequency. |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 184 | // Return contains no value on error, otherwise it contains a vector of vectors using the format: |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 185 | // [[t0_0, t0_1, ...], |
| 186 | // [t1_0, t1_1, ...], ...] |
| 187 | // where ti_j is the ns that uid spent running on the ith cluster at that cluster's jth lowest freq. |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 188 | std::optional<std::vector<std::vector<uint64_t>>> getUidCpuFreqTimes(uint32_t uid) { |
| 189 | if (!gInitialized && !initGlobals()) return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 190 | |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 191 | std::vector<std::vector<uint64_t>> out; |
| 192 | uint32_t maxFreqCount = 0; |
| 193 | for (const auto &freqList : gPolicyFreqs) { |
| 194 | if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size(); |
| 195 | out.emplace_back(freqList.size(), 0); |
| 196 | } |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 197 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 198 | std::vector<tis_val_t> vals(gNCpus); |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 199 | time_key_t key = {.uid = uid}; |
| 200 | for (uint32_t i = 0; i <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++i) { |
| 201 | key.bucket = i; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 202 | if (findMapEntry(gTisMapFd, &key, vals.data())) { |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 203 | if (errno != ENOENT) return {}; |
| 204 | continue; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 205 | } |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 206 | |
| 207 | auto offset = i * FREQS_PER_ENTRY; |
| 208 | auto nextOffset = (i + 1) * FREQS_PER_ENTRY; |
| 209 | for (uint32_t j = 0; j < gNPolicies; ++j) { |
| 210 | if (offset >= gPolicyFreqs[j].size()) continue; |
| 211 | auto begin = out[j].begin() + offset; |
| 212 | auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY : out[j].end(); |
| 213 | |
| 214 | for (const auto &cpu : gPolicyCpus[j]) { |
| 215 | std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus<uint64_t>()); |
Connor O'Brien | c92ef10 | 2019-07-24 15:42:11 -0700 | [diff] [blame] | 216 | } |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 220 | return out; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 223 | // Retrieve the times in ns that each uid spent running at each CPU freq. |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 224 | // Return contains no value on error, otherwise it contains a map from uids to vectors of vectors |
| 225 | // using the format: |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 226 | // { uid0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...], |
| 227 | // uid1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... } |
| 228 | // where ti_j_k is the ns uid i spent running on the jth cluster at the cluster's kth lowest freq. |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 229 | std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>> |
| 230 | getUidsCpuFreqTimes() { |
| 231 | if (!gInitialized && !initGlobals()) return {}; |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 232 | time_key_t key, prevKey; |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 233 | std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>> map; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 234 | if (getFirstMapKey(gTisMapFd, &key)) { |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 235 | if (errno == ENOENT) return map; |
| 236 | return std::nullopt; |
| 237 | } |
| 238 | |
| 239 | std::vector<std::vector<uint64_t>> mapFormat; |
| 240 | for (const auto &freqList : gPolicyFreqs) mapFormat.emplace_back(freqList.size(), 0); |
| 241 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 242 | std::vector<tis_val_t> vals(gNCpus); |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 243 | do { |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 244 | if (findMapEntry(gTisMapFd, &key, vals.data())) return {}; |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 245 | if (map.find(key.uid) == map.end()) map.emplace(key.uid, mapFormat); |
| 246 | |
| 247 | auto offset = key.bucket * FREQS_PER_ENTRY; |
| 248 | auto nextOffset = (key.bucket + 1) * FREQS_PER_ENTRY; |
| 249 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 250 | if (offset >= gPolicyFreqs[i].size()) continue; |
| 251 | auto begin = map[key.uid][i].begin() + offset; |
| 252 | auto end = nextOffset < gPolicyFreqs[i].size() ? begin + FREQS_PER_ENTRY : |
| 253 | map[key.uid][i].end(); |
| 254 | for (const auto &cpu : gPolicyCpus[i]) { |
| 255 | std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus<uint64_t>()); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 256 | } |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 257 | } |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 258 | prevKey = key; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 259 | } while (!getNextMapKey(gTisMapFd, &prevKey, &key)); |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 260 | if (errno != ENOENT) return {}; |
| 261 | return map; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 264 | static bool verifyConcurrentTimes(const concurrent_time_t &ct) { |
| 265 | uint64_t activeSum = std::accumulate(ct.active.begin(), ct.active.end(), (uint64_t)0); |
| 266 | uint64_t policySum = 0; |
| 267 | for (const auto &vec : ct.policy) { |
| 268 | policySum += std::accumulate(vec.begin(), vec.end(), (uint64_t)0); |
| 269 | } |
| 270 | return activeSum == policySum; |
| 271 | } |
| 272 | |
| 273 | // Retrieve the times in ns that uid spent running concurrently with each possible number of other |
| 274 | // tasks on each cluster (policy times) and overall (active times). |
| 275 | // Return contains no value on error, otherwise it contains a concurrent_time_t with the format: |
| 276 | // {.active = [a0, a1, ...], .policy = [[p0_0, p0_1, ...], [p1_0, p1_1, ...], ...]} |
| 277 | // where ai is the ns spent running concurrently with tasks on i other cpus and pi_j is the ns spent |
| 278 | // running on the ith cluster, concurrently with tasks on j other cpus in the same cluster |
| 279 | std::optional<concurrent_time_t> getUidConcurrentTimes(uint32_t uid, bool retry) { |
| 280 | if (!gInitialized && !initGlobals()) return {}; |
| 281 | concurrent_time_t ret = {.active = std::vector<uint64_t>(gNCpus, 0)}; |
| 282 | for (const auto &cpuList : gPolicyCpus) ret.policy.emplace_back(cpuList.size(), 0); |
| 283 | std::vector<concurrent_val_t> vals(gNCpus); |
| 284 | time_key_t key = {.uid = uid}; |
| 285 | for (key.bucket = 0; key.bucket <= (gNCpus - 1) / CPUS_PER_ENTRY; ++key.bucket) { |
| 286 | if (findMapEntry(gConcurrentMapFd, &key, vals.data())) { |
| 287 | if (errno != ENOENT) return {}; |
| 288 | continue; |
| 289 | } |
| 290 | auto offset = key.bucket * CPUS_PER_ENTRY; |
| 291 | auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY; |
| 292 | |
| 293 | auto activeBegin = ret.active.begin() + offset; |
| 294 | auto activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret.active.end(); |
| 295 | |
| 296 | for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) { |
| 297 | std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin, |
| 298 | std::plus<uint64_t>()); |
| 299 | } |
| 300 | |
| 301 | for (uint32_t policy = 0; policy < gNPolicies; ++policy) { |
| 302 | if (offset >= gPolicyCpus[policy].size()) continue; |
| 303 | auto policyBegin = ret.policy[policy].begin() + offset; |
| 304 | auto policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY |
| 305 | : ret.policy[policy].end(); |
| 306 | |
| 307 | for (const auto &cpu : gPolicyCpus[policy]) { |
| 308 | std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin, |
| 309 | std::plus<uint64_t>()); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | if (!verifyConcurrentTimes(ret) && retry) return getUidConcurrentTimes(uid, false); |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | // Retrieve the times in ns that each uid spent running concurrently with each possible number of |
| 318 | // other tasks on each cluster (policy times) and overall (active times). |
| 319 | // Return contains no value on error, otherwise it contains a map from uids to concurrent_time_t's |
| 320 | // using the format: |
| 321 | // { uid0 -> {.active = [a0, a1, ...], .policy = [[p0_0, p0_1, ...], [p1_0, p1_1, ...], ...] }, ...} |
| 322 | // where ai is the ns spent running concurrently with tasks on i other cpus and pi_j is the ns spent |
| 323 | // running on the ith cluster, concurrently with tasks on j other cpus in the same cluster. |
| 324 | std::optional<std::unordered_map<uint32_t, concurrent_time_t>> getUidsConcurrentTimes() { |
| 325 | if (!gInitialized && !initGlobals()) return {}; |
| 326 | time_key_t key, prevKey; |
| 327 | std::unordered_map<uint32_t, concurrent_time_t> ret; |
| 328 | if (getFirstMapKey(gConcurrentMapFd, &key)) { |
| 329 | if (errno == ENOENT) return ret; |
| 330 | return {}; |
| 331 | } |
| 332 | |
| 333 | concurrent_time_t retFormat = {.active = std::vector<uint64_t>(gNCpus, 0)}; |
| 334 | for (const auto &cpuList : gPolicyCpus) retFormat.policy.emplace_back(cpuList.size(), 0); |
| 335 | |
| 336 | std::vector<concurrent_val_t> vals(gNCpus); |
| 337 | std::vector<uint64_t>::iterator activeBegin, activeEnd, policyBegin, policyEnd; |
| 338 | |
| 339 | do { |
| 340 | if (findMapEntry(gConcurrentMapFd, &key, vals.data())) return {}; |
| 341 | if (ret.find(key.uid) == ret.end()) ret.emplace(key.uid, retFormat); |
| 342 | |
| 343 | auto offset = key.bucket * CPUS_PER_ENTRY; |
| 344 | auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY; |
| 345 | |
| 346 | activeBegin = ret[key.uid].active.begin(); |
| 347 | activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret[key.uid].active.end(); |
| 348 | |
| 349 | for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) { |
| 350 | std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin, |
| 351 | std::plus<uint64_t>()); |
| 352 | } |
| 353 | |
| 354 | for (uint32_t policy = 0; policy < gNPolicies; ++policy) { |
| 355 | if (offset >= gPolicyCpus[policy].size()) continue; |
| 356 | policyBegin = ret[key.uid].policy[policy].begin() + offset; |
| 357 | policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY |
| 358 | : ret[key.uid].policy[policy].end(); |
| 359 | |
| 360 | for (const auto &cpu : gPolicyCpus[policy]) { |
| 361 | std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin, |
| 362 | std::plus<uint64_t>()); |
| 363 | } |
| 364 | } |
| 365 | prevKey = key; |
| 366 | } while (!getNextMapKey(gConcurrentMapFd, &prevKey, &key)); |
| 367 | if (errno != ENOENT) return {}; |
| 368 | for (const auto &[key, value] : ret) { |
| 369 | if (!verifyConcurrentTimes(value)) { |
| 370 | auto val = getUidConcurrentTimes(key, false); |
| 371 | if (val.has_value()) ret[key] = val.value(); |
| 372 | } |
| 373 | } |
| 374 | return ret; |
| 375 | } |
| 376 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 377 | // Clear all time in state data for a given uid. Returns false on error, true otherwise. |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 378 | // This is only suitable for clearing data when an app is uninstalled; if called on a UID with |
| 379 | // running tasks it will cause time in state vs. concurrent time totals to be inconsistent for that |
| 380 | // UID. |
| 381 | bool clearUidTimes(uint32_t uid) { |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 382 | if (!gInitialized && !initGlobals()) return false; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 383 | |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 384 | time_key_t key = {.uid = uid}; |
| 385 | |
| 386 | uint32_t maxFreqCount = 0; |
| 387 | for (const auto &freqList : gPolicyFreqs) { |
| 388 | if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size(); |
| 389 | } |
| 390 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 391 | tis_val_t zeros = {0}; |
| 392 | std::vector<tis_val_t> vals(gNCpus, zeros); |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 393 | for (key.bucket = 0; key.bucket <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++key.bucket) { |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 394 | if (writeToMapEntry(gTisMapFd, &key, vals.data(), BPF_EXIST) && errno != ENOENT) |
| 395 | return false; |
| 396 | if (deleteMapEntry(gTisMapFd, &key) && errno != ENOENT) return false; |
| 397 | } |
| 398 | |
| 399 | concurrent_val_t czeros = {.policy = {0}, .active = {0}}; |
| 400 | std::vector<concurrent_val_t> cvals(gNCpus, czeros); |
| 401 | for (key.bucket = 0; key.bucket <= (gNCpus - 1) / CPUS_PER_ENTRY; ++key.bucket) { |
| 402 | if (writeToMapEntry(gConcurrentMapFd, &key, cvals.data(), BPF_EXIST) && errno != ENOENT) |
| 403 | return false; |
| 404 | if (deleteMapEntry(gConcurrentMapFd, &key) && errno != ENOENT) return false; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 405 | } |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | } // namespace bpf |
| 410 | } // namespace android |