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