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