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