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; |
Rafal Slawik | 27c48db | 2021-01-05 19:10:07 +0000 | [diff] [blame^] | 59 | static unique_fd gTisTotalMapFd; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 60 | static unique_fd gTisMapFd; |
| 61 | static unique_fd gConcurrentMapFd; |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 62 | static unique_fd gUidLastUpdateMapFd; |
Dmitri Plotnikov | 2677dba | 2020-10-17 21:06:55 -0700 | [diff] [blame] | 63 | static unique_fd gPidTisMapFd; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 64 | |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 65 | 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] | 66 | std::string data; |
| 67 | |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 68 | if (!android::base::ReadFileToString(path, &data)) return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 69 | |
| 70 | auto strings = android::base::Split(data, " \n"); |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 71 | std::vector<uint32_t> ret; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 72 | for (const auto &s : strings) { |
| 73 | if (s.empty()) continue; |
| 74 | uint32_t n; |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 75 | if (!android::base::ParseUint(s, &n)) return {}; |
| 76 | ret.emplace_back(n); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 77 | } |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 78 | return ret; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static int isPolicyFile(const struct dirent *d) { |
| 82 | return android::base::StartsWith(d->d_name, "policy"); |
| 83 | } |
| 84 | |
| 85 | static int comparePolicyFiles(const struct dirent **d1, const struct dirent **d2) { |
| 86 | uint32_t policyN1, policyN2; |
| 87 | if (sscanf((*d1)->d_name, "policy%" SCNu32 "", &policyN1) != 1 || |
| 88 | sscanf((*d2)->d_name, "policy%" SCNu32 "", &policyN2) != 1) |
| 89 | return 0; |
| 90 | return policyN1 - policyN2; |
| 91 | } |
| 92 | |
| 93 | static bool initGlobals() { |
| 94 | std::lock_guard<std::mutex> guard(gInitializedMutex); |
| 95 | if (gInitialized) return true; |
| 96 | |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 97 | gNCpus = get_nprocs_conf(); |
| 98 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 99 | struct dirent **dirlist; |
| 100 | const char basepath[] = "/sys/devices/system/cpu/cpufreq"; |
| 101 | int ret = scandir(basepath, &dirlist, isPolicyFile, comparePolicyFiles); |
| 102 | if (ret == -1) return false; |
| 103 | gNPolicies = ret; |
| 104 | |
| 105 | std::vector<std::string> policyFileNames; |
| 106 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 107 | policyFileNames.emplace_back(dirlist[i]->d_name); |
| 108 | free(dirlist[i]); |
| 109 | } |
| 110 | free(dirlist); |
| 111 | |
| 112 | for (const auto &policy : policyFileNames) { |
| 113 | std::vector<uint32_t> freqs; |
| 114 | for (const auto &name : {"available", "boost"}) { |
| 115 | std::string path = |
| 116 | StringPrintf("%s/%s/scaling_%s_frequencies", basepath, policy.c_str(), name); |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 117 | auto nums = readNumbersFromFile(path); |
Connor O'Brien | b8fe077 | 2019-09-11 18:09:28 -0700 | [diff] [blame] | 118 | if (!nums) continue; |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 119 | freqs.insert(freqs.end(), nums->begin(), nums->end()); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 120 | } |
Connor O'Brien | b8fe077 | 2019-09-11 18:09:28 -0700 | [diff] [blame] | 121 | if (freqs.empty()) return false; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 122 | std::sort(freqs.begin(), freqs.end()); |
| 123 | gPolicyFreqs.emplace_back(freqs); |
| 124 | |
| 125 | for (auto freq : freqs) gAllFreqs.insert(freq); |
| 126 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 127 | 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] | 128 | auto cpus = readNumbersFromFile(path); |
| 129 | if (!cpus) return false; |
| 130 | gPolicyCpus.emplace_back(*cpus); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Rafal Slawik | 27c48db | 2021-01-05 19:10:07 +0000 | [diff] [blame^] | 133 | gTisTotalMapFd = |
| 134 | unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_total_time_in_state_map")}; |
| 135 | if (gTisTotalMapFd < 0) return false; |
| 136 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 137 | gTisMapFd = unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_time_in_state_map")}; |
| 138 | if (gTisMapFd < 0) return false; |
| 139 | |
| 140 | gConcurrentMapFd = |
| 141 | unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")}; |
| 142 | if (gConcurrentMapFd < 0) return false; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 143 | |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 144 | gUidLastUpdateMapFd = |
| 145 | unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_last_update_map")}; |
| 146 | if (gUidLastUpdateMapFd < 0) return false; |
| 147 | |
Dmitri Plotnikov | 2677dba | 2020-10-17 21:06:55 -0700 | [diff] [blame] | 148 | gPidTisMapFd = unique_fd{mapRetrieveRO(BPF_FS_PATH "map_time_in_state_pid_time_in_state_map")}; |
| 149 | if (gPidTisMapFd < 0) return false; |
| 150 | |
| 151 | unique_fd trackedPidMapFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_pid_tracked_map")); |
| 152 | if (trackedPidMapFd < 0) return false; |
| 153 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 154 | gInitialized = true; |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) { |
| 159 | std::string path = StringPrintf(BPF_FS_PATH "prog_time_in_state_tracepoint_%s_%s", |
| 160 | eventType.c_str(), eventName.c_str()); |
Maciej Żenczykowski | af073cc | 2020-06-16 21:45:21 -0700 | [diff] [blame] | 161 | int prog_fd = retrieveProgram(path.c_str()); |
Connor O'Brien | d250acc | 2019-01-23 17:21:41 -0800 | [diff] [blame] | 162 | if (prog_fd < 0) return false; |
| 163 | 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] | 164 | } |
| 165 | |
Connor O'Brien | ab51dca | 2020-02-19 14:11:45 -0800 | [diff] [blame] | 166 | static std::optional<uint32_t> getPolicyFreqIdx(uint32_t policy) { |
| 167 | auto path = StringPrintf("/sys/devices/system/cpu/cpufreq/policy%u/scaling_cur_freq", |
| 168 | gPolicyCpus[policy][0]); |
| 169 | auto freqVec = readNumbersFromFile(path); |
| 170 | if (!freqVec.has_value() || freqVec->size() != 1) return {}; |
| 171 | for (uint32_t idx = 0; idx < gPolicyFreqs[policy].size(); ++idx) { |
| 172 | if ((*freqVec)[0] == gPolicyFreqs[policy][idx]) return idx + 1; |
| 173 | } |
| 174 | return {}; |
| 175 | } |
| 176 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 177 | // Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes. |
| 178 | // Returns true on success, false otherwise. |
| 179 | // Tracking is active only once a live process has successfully called this function; if the calling |
| 180 | // process dies then it must be called again to resume tracking. |
| 181 | // This function should *not* be called while tracking is already active; doing so is unnecessary |
| 182 | // and can lead to accounting errors. |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 183 | bool startTrackingUidTimes() { |
Connor O'Brien | b0491f8 | 2020-01-09 17:10:19 -0800 | [diff] [blame] | 184 | std::lock_guard<std::mutex> guard(gTrackingMutex); |
Connor O'Brien | 57b75dc | 2019-06-06 17:48:20 -0700 | [diff] [blame] | 185 | if (!initGlobals()) return false; |
Connor O'Brien | b0491f8 | 2020-01-09 17:10:19 -0800 | [diff] [blame] | 186 | if (gTracking) return true; |
Connor O'Brien | 57b75dc | 2019-06-06 17:48:20 -0700 | [diff] [blame] | 187 | |
Maciej Żenczykowski | af073cc | 2020-06-16 21:45:21 -0700 | [diff] [blame] | 188 | 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] | 189 | if (cpuPolicyFd < 0) return false; |
Connor O'Brien | 57b75dc | 2019-06-06 17:48:20 -0700 | [diff] [blame] | 190 | |
| 191 | for (uint32_t i = 0; i < gPolicyCpus.size(); ++i) { |
| 192 | for (auto &cpu : gPolicyCpus[i]) { |
Connor O'Brien | 3fc2cb7 | 2019-08-21 12:08:39 -0700 | [diff] [blame] | 193 | if (writeToMapEntry(cpuPolicyFd, &cpu, &i, BPF_ANY)) return false; |
Connor O'Brien | 57b75dc | 2019-06-06 17:48:20 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Maciej Żenczykowski | af073cc | 2020-06-16 21:45:21 -0700 | [diff] [blame] | 197 | 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] | 198 | if (freqToIdxFd < 0) return false; |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 199 | freq_idx_key_t key; |
| 200 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 201 | key.policy = i; |
| 202 | for (uint32_t j = 0; j < gPolicyFreqs[i].size(); ++j) { |
| 203 | key.freq = gPolicyFreqs[i][j]; |
| 204 | // Start indexes at 1 so that uninitialized state is distinguishable from lowest freq. |
| 205 | // The uid_times map still uses 0-based indexes, and the sched_switch program handles |
| 206 | // conversion between them, so this does not affect our map reading code. |
| 207 | uint32_t idx = j + 1; |
Connor O'Brien | 3fc2cb7 | 2019-08-21 12:08:39 -0700 | [diff] [blame] | 208 | if (writeToMapEntry(freqToIdxFd, &key, &idx, BPF_ANY)) return false; |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Maciej Żenczykowski | af073cc | 2020-06-16 21:45:21 -0700 | [diff] [blame] | 212 | 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] | 213 | if (cpuLastUpdateFd < 0) return false; |
| 214 | std::vector<uint64_t> zeros(get_nprocs_conf(), 0); |
| 215 | uint32_t zero = 0; |
| 216 | if (writeToMapEntry(cpuLastUpdateFd, &zero, zeros.data(), BPF_ANY)) return false; |
| 217 | |
Maciej Żenczykowski | af073cc | 2020-06-16 21:45:21 -0700 | [diff] [blame] | 218 | 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] | 219 | if (nrActiveFd < 0) return false; |
| 220 | if (writeToMapEntry(nrActiveFd, &zero, &zero, BPF_ANY)) return false; |
| 221 | |
Maciej Żenczykowski | af073cc | 2020-06-16 21:45:21 -0700 | [diff] [blame] | 222 | 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] | 223 | if (policyNrActiveFd < 0) return false; |
| 224 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 225 | if (writeToMapEntry(policyNrActiveFd, &i, &zero, BPF_ANY)) return false; |
| 226 | } |
| 227 | |
Maciej Żenczykowski | af073cc | 2020-06-16 21:45:21 -0700 | [diff] [blame] | 228 | 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] | 229 | if (policyFreqIdxFd < 0) return false; |
| 230 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
Connor O'Brien | ab51dca | 2020-02-19 14:11:45 -0800 | [diff] [blame] | 231 | auto freqIdx = getPolicyFreqIdx(i); |
| 232 | if (!freqIdx.has_value()) return false; |
| 233 | if (writeToMapEntry(policyFreqIdxFd, &i, &(*freqIdx), BPF_ANY)) return false; |
Connor O'Brien | 3fc2cb7 | 2019-08-21 12:08:39 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Connor O'Brien | b0491f8 | 2020-01-09 17:10:19 -0800 | [diff] [blame] | 236 | gTracking = attachTracepointProgram("sched", "sched_switch") && |
Dmitri Plotnikov | 2677dba | 2020-10-17 21:06:55 -0700 | [diff] [blame] | 237 | attachTracepointProgram("power", "cpu_frequency") && |
| 238 | attachTracepointProgram("sched", "sched_process_free"); |
Connor O'Brien | b0491f8 | 2020-01-09 17:10:19 -0800 | [diff] [blame] | 239 | return gTracking; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Connor O'Brien | 8f296eb | 2019-10-01 17:58:38 -0700 | [diff] [blame] | 242 | std::optional<std::vector<std::vector<uint32_t>>> getCpuFreqs() { |
| 243 | if (!gInitialized && !initGlobals()) return {}; |
| 244 | return gPolicyFreqs; |
| 245 | } |
| 246 | |
Rafal Slawik | 27c48db | 2021-01-05 19:10:07 +0000 | [diff] [blame^] | 247 | std::optional<std::vector<std::vector<uint64_t>>> getTotalCpuFreqTimes() { |
| 248 | if (!gInitialized && !initGlobals()) return {}; |
| 249 | |
| 250 | std::vector<std::vector<uint64_t>> out; |
| 251 | uint32_t maxFreqCount = 0; |
| 252 | for (const auto &freqList : gPolicyFreqs) { |
| 253 | if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size(); |
| 254 | out.emplace_back(freqList.size(), 0); |
| 255 | } |
| 256 | |
| 257 | std::vector<uint64_t> vals(gNCpus); |
| 258 | const uint32_t freqCount = maxFreqCount <= MAX_FREQS_FOR_TOTAL ? maxFreqCount : |
| 259 | MAX_FREQS_FOR_TOTAL; |
| 260 | for (uint32_t freqIdx = 0; freqIdx < freqCount; ++freqIdx) { |
| 261 | if (findMapEntry(gTisTotalMapFd, &freqIdx, vals.data())) return {}; |
| 262 | for (uint32_t policyIdx = 0; policyIdx < gNPolicies; ++policyIdx) { |
| 263 | if (freqIdx >= gPolicyFreqs[policyIdx].size()) continue; |
| 264 | for (const auto &cpu : gPolicyCpus[policyIdx]) { |
| 265 | out[policyIdx][freqIdx] += vals[cpu]; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return out; |
| 271 | } |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 272 | // 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] | 273 | // 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] | 274 | // [[t0_0, t0_1, ...], |
| 275 | // [t1_0, t1_1, ...], ...] |
| 276 | // 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] | 277 | std::optional<std::vector<std::vector<uint64_t>>> getUidCpuFreqTimes(uint32_t uid) { |
| 278 | if (!gInitialized && !initGlobals()) return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 279 | |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 280 | std::vector<std::vector<uint64_t>> out; |
| 281 | uint32_t maxFreqCount = 0; |
| 282 | for (const auto &freqList : gPolicyFreqs) { |
| 283 | if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size(); |
| 284 | out.emplace_back(freqList.size(), 0); |
| 285 | } |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 286 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 287 | std::vector<tis_val_t> vals(gNCpus); |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 288 | time_key_t key = {.uid = uid}; |
| 289 | for (uint32_t i = 0; i <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++i) { |
| 290 | key.bucket = i; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 291 | if (findMapEntry(gTisMapFd, &key, vals.data())) { |
Connor O'Brien | b83af34 | 2020-08-14 13:13:37 -0700 | [diff] [blame] | 292 | if (errno != ENOENT || getFirstMapKey(gTisMapFd, &key)) return {}; |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 293 | continue; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 294 | } |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 295 | |
| 296 | auto offset = i * FREQS_PER_ENTRY; |
| 297 | auto nextOffset = (i + 1) * FREQS_PER_ENTRY; |
| 298 | for (uint32_t j = 0; j < gNPolicies; ++j) { |
| 299 | if (offset >= gPolicyFreqs[j].size()) continue; |
| 300 | auto begin = out[j].begin() + offset; |
| 301 | auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY : out[j].end(); |
| 302 | |
| 303 | for (const auto &cpu : gPolicyCpus[j]) { |
| 304 | 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] | 305 | } |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 309 | return out; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 312 | static std::optional<bool> uidUpdatedSince(uint32_t uid, uint64_t lastUpdate, |
| 313 | uint64_t *newLastUpdate) { |
| 314 | uint64_t uidLastUpdate; |
| 315 | if (findMapEntry(gUidLastUpdateMapFd, &uid, &uidLastUpdate)) return {}; |
| 316 | // Updates that occurred during the previous read may have been missed. To mitigate |
| 317 | // this, don't ignore entries updated up to 1s before *lastUpdate |
| 318 | constexpr uint64_t NSEC_PER_SEC = 1000000000; |
| 319 | if (uidLastUpdate + NSEC_PER_SEC < lastUpdate) return false; |
| 320 | if (uidLastUpdate > *newLastUpdate) *newLastUpdate = uidLastUpdate; |
| 321 | return true; |
| 322 | } |
| 323 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 324 | // 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] | 325 | // Return contains no value on error, otherwise it contains a map from uids to vectors of vectors |
| 326 | // using the format: |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 327 | // { uid0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...], |
| 328 | // uid1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... } |
| 329 | // 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] | 330 | std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>> |
| 331 | getUidsCpuFreqTimes() { |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 332 | return getUidsUpdatedCpuFreqTimes(nullptr); |
| 333 | } |
| 334 | |
| 335 | // Retrieve the times in ns that each uid spent running at each CPU freq, excluding UIDs that have |
| 336 | // not run since before lastUpdate. |
| 337 | // Return format is the same as getUidsCpuFreqTimes() |
| 338 | std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>> |
| 339 | getUidsUpdatedCpuFreqTimes(uint64_t *lastUpdate) { |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 340 | if (!gInitialized && !initGlobals()) return {}; |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 341 | time_key_t key, prevKey; |
Connor O'Brien | f03b6ae | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 342 | 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] | 343 | if (getFirstMapKey(gTisMapFd, &key)) { |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 344 | if (errno == ENOENT) return map; |
| 345 | return std::nullopt; |
| 346 | } |
| 347 | |
| 348 | std::vector<std::vector<uint64_t>> mapFormat; |
| 349 | for (const auto &freqList : gPolicyFreqs) mapFormat.emplace_back(freqList.size(), 0); |
| 350 | |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 351 | uint64_t newLastUpdate = lastUpdate ? *lastUpdate : 0; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 352 | std::vector<tis_val_t> vals(gNCpus); |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 353 | do { |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 354 | if (lastUpdate) { |
| 355 | auto uidUpdated = uidUpdatedSince(key.uid, *lastUpdate, &newLastUpdate); |
| 356 | if (!uidUpdated.has_value()) return {}; |
| 357 | if (!*uidUpdated) continue; |
| 358 | } |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 359 | if (findMapEntry(gTisMapFd, &key, vals.data())) return {}; |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 360 | if (map.find(key.uid) == map.end()) map.emplace(key.uid, mapFormat); |
| 361 | |
| 362 | auto offset = key.bucket * FREQS_PER_ENTRY; |
| 363 | auto nextOffset = (key.bucket + 1) * FREQS_PER_ENTRY; |
| 364 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 365 | if (offset >= gPolicyFreqs[i].size()) continue; |
| 366 | auto begin = map[key.uid][i].begin() + offset; |
| 367 | auto end = nextOffset < gPolicyFreqs[i].size() ? begin + FREQS_PER_ENTRY : |
| 368 | map[key.uid][i].end(); |
| 369 | for (const auto &cpu : gPolicyCpus[i]) { |
| 370 | 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] | 371 | } |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 372 | } |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 373 | prevKey = key; |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 374 | } while (prevKey = key, !getNextMapKey(gTisMapFd, &prevKey, &key)); |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 375 | if (errno != ENOENT) return {}; |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 376 | if (lastUpdate && newLastUpdate > *lastUpdate) *lastUpdate = newLastUpdate; |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 377 | return map; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 380 | static bool verifyConcurrentTimes(const concurrent_time_t &ct) { |
| 381 | uint64_t activeSum = std::accumulate(ct.active.begin(), ct.active.end(), (uint64_t)0); |
| 382 | uint64_t policySum = 0; |
| 383 | for (const auto &vec : ct.policy) { |
| 384 | policySum += std::accumulate(vec.begin(), vec.end(), (uint64_t)0); |
| 385 | } |
| 386 | return activeSum == policySum; |
| 387 | } |
| 388 | |
| 389 | // Retrieve the times in ns that uid spent running concurrently with each possible number of other |
| 390 | // tasks on each cluster (policy times) and overall (active times). |
| 391 | // Return contains no value on error, otherwise it contains a concurrent_time_t with the format: |
| 392 | // {.active = [a0, a1, ...], .policy = [[p0_0, p0_1, ...], [p1_0, p1_1, ...], ...]} |
| 393 | // where ai is the ns spent running concurrently with tasks on i other cpus and pi_j is the ns spent |
| 394 | // running on the ith cluster, concurrently with tasks on j other cpus in the same cluster |
| 395 | std::optional<concurrent_time_t> getUidConcurrentTimes(uint32_t uid, bool retry) { |
| 396 | if (!gInitialized && !initGlobals()) return {}; |
| 397 | concurrent_time_t ret = {.active = std::vector<uint64_t>(gNCpus, 0)}; |
| 398 | for (const auto &cpuList : gPolicyCpus) ret.policy.emplace_back(cpuList.size(), 0); |
| 399 | std::vector<concurrent_val_t> vals(gNCpus); |
| 400 | time_key_t key = {.uid = uid}; |
| 401 | for (key.bucket = 0; key.bucket <= (gNCpus - 1) / CPUS_PER_ENTRY; ++key.bucket) { |
| 402 | if (findMapEntry(gConcurrentMapFd, &key, vals.data())) { |
Connor O'Brien | b83af34 | 2020-08-14 13:13:37 -0700 | [diff] [blame] | 403 | if (errno != ENOENT || getFirstMapKey(gConcurrentMapFd, &key)) return {}; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 404 | continue; |
| 405 | } |
| 406 | auto offset = key.bucket * CPUS_PER_ENTRY; |
| 407 | auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY; |
| 408 | |
| 409 | auto activeBegin = ret.active.begin() + offset; |
| 410 | auto activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret.active.end(); |
| 411 | |
| 412 | for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) { |
| 413 | std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin, |
| 414 | std::plus<uint64_t>()); |
| 415 | } |
| 416 | |
| 417 | for (uint32_t policy = 0; policy < gNPolicies; ++policy) { |
| 418 | if (offset >= gPolicyCpus[policy].size()) continue; |
| 419 | auto policyBegin = ret.policy[policy].begin() + offset; |
| 420 | auto policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY |
| 421 | : ret.policy[policy].end(); |
| 422 | |
| 423 | for (const auto &cpu : gPolicyCpus[policy]) { |
| 424 | std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin, |
| 425 | std::plus<uint64_t>()); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | if (!verifyConcurrentTimes(ret) && retry) return getUidConcurrentTimes(uid, false); |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | // Retrieve the times in ns that each uid spent running concurrently with each possible number of |
| 434 | // other tasks on each cluster (policy times) and overall (active times). |
| 435 | // Return contains no value on error, otherwise it contains a map from uids to concurrent_time_t's |
| 436 | // using the format: |
| 437 | // { uid0 -> {.active = [a0, a1, ...], .policy = [[p0_0, p0_1, ...], [p1_0, p1_1, ...], ...] }, ...} |
| 438 | // where ai is the ns spent running concurrently with tasks on i other cpus and pi_j is the ns spent |
| 439 | // running on the ith cluster, concurrently with tasks on j other cpus in the same cluster. |
| 440 | std::optional<std::unordered_map<uint32_t, concurrent_time_t>> getUidsConcurrentTimes() { |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 441 | return getUidsUpdatedConcurrentTimes(nullptr); |
| 442 | } |
| 443 | |
| 444 | // Retrieve the times in ns that each uid spent running concurrently with each possible number of |
| 445 | // other tasks on each cluster (policy times) and overall (active times), excluding UIDs that have |
| 446 | // not run since before lastUpdate. |
| 447 | // Return format is the same as getUidsConcurrentTimes() |
| 448 | std::optional<std::unordered_map<uint32_t, concurrent_time_t>> getUidsUpdatedConcurrentTimes( |
| 449 | uint64_t *lastUpdate) { |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 450 | if (!gInitialized && !initGlobals()) return {}; |
| 451 | time_key_t key, prevKey; |
| 452 | std::unordered_map<uint32_t, concurrent_time_t> ret; |
| 453 | if (getFirstMapKey(gConcurrentMapFd, &key)) { |
| 454 | if (errno == ENOENT) return ret; |
| 455 | return {}; |
| 456 | } |
| 457 | |
| 458 | concurrent_time_t retFormat = {.active = std::vector<uint64_t>(gNCpus, 0)}; |
| 459 | for (const auto &cpuList : gPolicyCpus) retFormat.policy.emplace_back(cpuList.size(), 0); |
| 460 | |
| 461 | std::vector<concurrent_val_t> vals(gNCpus); |
| 462 | std::vector<uint64_t>::iterator activeBegin, activeEnd, policyBegin, policyEnd; |
| 463 | |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 464 | uint64_t newLastUpdate = lastUpdate ? *lastUpdate : 0; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 465 | do { |
Connor O'Brien | 597f637 | 2020-10-20 14:48:40 -0700 | [diff] [blame] | 466 | if (key.bucket > (gNCpus - 1) / CPUS_PER_ENTRY) return {}; |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 467 | if (lastUpdate) { |
| 468 | auto uidUpdated = uidUpdatedSince(key.uid, *lastUpdate, &newLastUpdate); |
| 469 | if (!uidUpdated.has_value()) return {}; |
| 470 | if (!*uidUpdated) continue; |
| 471 | } |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 472 | if (findMapEntry(gConcurrentMapFd, &key, vals.data())) return {}; |
| 473 | if (ret.find(key.uid) == ret.end()) ret.emplace(key.uid, retFormat); |
| 474 | |
| 475 | auto offset = key.bucket * CPUS_PER_ENTRY; |
| 476 | auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY; |
| 477 | |
| 478 | activeBegin = ret[key.uid].active.begin(); |
| 479 | activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret[key.uid].active.end(); |
| 480 | |
| 481 | for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) { |
| 482 | std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin, |
| 483 | std::plus<uint64_t>()); |
| 484 | } |
| 485 | |
| 486 | for (uint32_t policy = 0; policy < gNPolicies; ++policy) { |
| 487 | if (offset >= gPolicyCpus[policy].size()) continue; |
| 488 | policyBegin = ret[key.uid].policy[policy].begin() + offset; |
| 489 | policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY |
| 490 | : ret[key.uid].policy[policy].end(); |
| 491 | |
| 492 | for (const auto &cpu : gPolicyCpus[policy]) { |
| 493 | std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin, |
| 494 | std::plus<uint64_t>()); |
| 495 | } |
| 496 | } |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 497 | } while (prevKey = key, !getNextMapKey(gConcurrentMapFd, &prevKey, &key)); |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 498 | if (errno != ENOENT) return {}; |
| 499 | for (const auto &[key, value] : ret) { |
| 500 | if (!verifyConcurrentTimes(value)) { |
| 501 | auto val = getUidConcurrentTimes(key, false); |
| 502 | if (val.has_value()) ret[key] = val.value(); |
| 503 | } |
| 504 | } |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 505 | if (lastUpdate && newLastUpdate > *lastUpdate) *lastUpdate = newLastUpdate; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 506 | return ret; |
| 507 | } |
| 508 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 509 | // 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] | 510 | // This is only suitable for clearing data when an app is uninstalled; if called on a UID with |
| 511 | // running tasks it will cause time in state vs. concurrent time totals to be inconsistent for that |
| 512 | // UID. |
| 513 | bool clearUidTimes(uint32_t uid) { |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 514 | if (!gInitialized && !initGlobals()) return false; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 515 | |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 516 | time_key_t key = {.uid = uid}; |
| 517 | |
| 518 | uint32_t maxFreqCount = 0; |
| 519 | for (const auto &freqList : gPolicyFreqs) { |
| 520 | if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size(); |
| 521 | } |
| 522 | |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 523 | tis_val_t zeros = {0}; |
| 524 | std::vector<tis_val_t> vals(gNCpus, zeros); |
Connor O'Brien | 1a18040 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 525 | 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] | 526 | if (writeToMapEntry(gTisMapFd, &key, vals.data(), BPF_EXIST) && errno != ENOENT) |
| 527 | return false; |
| 528 | if (deleteMapEntry(gTisMapFd, &key) && errno != ENOENT) return false; |
| 529 | } |
| 530 | |
Nick Desaulniers | 54891cd | 2019-11-19 09:31:05 -0800 | [diff] [blame] | 531 | concurrent_val_t czeros = { .active = {0}, .policy = {0}, }; |
Connor O'Brien | 26de80f | 2019-06-11 13:49:19 -0700 | [diff] [blame] | 532 | std::vector<concurrent_val_t> cvals(gNCpus, czeros); |
| 533 | for (key.bucket = 0; key.bucket <= (gNCpus - 1) / CPUS_PER_ENTRY; ++key.bucket) { |
| 534 | if (writeToMapEntry(gConcurrentMapFd, &key, cvals.data(), BPF_EXIST) && errno != ENOENT) |
| 535 | return false; |
| 536 | if (deleteMapEntry(gConcurrentMapFd, &key) && errno != ENOENT) return false; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 537 | } |
Connor O'Brien | 2a716a4 | 2020-01-31 18:51:56 -0800 | [diff] [blame] | 538 | |
| 539 | if (deleteMapEntry(gUidLastUpdateMapFd, &uid) && errno != ENOENT) return false; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
Dmitri Plotnikov | 2677dba | 2020-10-17 21:06:55 -0700 | [diff] [blame] | 543 | bool startTrackingProcessCpuTimes(pid_t pid) { |
| 544 | if (!gInitialized && !initGlobals()) return false; |
| 545 | |
| 546 | unique_fd trackedPidHashMapFd( |
| 547 | mapRetrieveWO(BPF_FS_PATH "map_time_in_state_pid_tracked_hash_map")); |
| 548 | if (trackedPidHashMapFd < 0) return false; |
| 549 | |
| 550 | unique_fd trackedPidMapFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_pid_tracked_map")); |
| 551 | if (trackedPidMapFd < 0) return false; |
| 552 | |
| 553 | for (uint32_t index = 0; index < MAX_TRACKED_PIDS; index++) { |
| 554 | // Find first available [index, pid] entry in the pid_tracked_hash_map map |
| 555 | if (writeToMapEntry(trackedPidHashMapFd, &index, &pid, BPF_NOEXIST) != 0) { |
| 556 | if (errno != EEXIST) { |
| 557 | return false; |
| 558 | } |
| 559 | continue; // This index is already taken |
| 560 | } |
| 561 | |
| 562 | tracked_pid_t tracked_pid = {.pid = pid, .state = TRACKED_PID_STATE_ACTIVE}; |
| 563 | if (writeToMapEntry(trackedPidMapFd, &index, &tracked_pid, BPF_ANY) != 0) { |
| 564 | return false; |
| 565 | } |
| 566 | return true; |
| 567 | } |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | // Marks the specified task identified by its PID (aka TID) for CPU time-in-state tracking |
| 572 | // aggregated with other tasks sharing the same TGID and aggregation key. |
| 573 | bool startAggregatingTaskCpuTimes(pid_t pid, uint16_t aggregationKey) { |
| 574 | if (!gInitialized && !initGlobals()) return false; |
| 575 | |
| 576 | unique_fd taskAggregationMapFd( |
| 577 | mapRetrieveWO(BPF_FS_PATH "map_time_in_state_pid_task_aggregation_map")); |
| 578 | if (taskAggregationMapFd < 0) return false; |
| 579 | |
| 580 | return writeToMapEntry(taskAggregationMapFd, &pid, &aggregationKey, BPF_ANY) == 0; |
| 581 | } |
| 582 | |
| 583 | // Retrieves the times in ns that each thread spent running at each CPU freq, aggregated by |
| 584 | // aggregation key. |
| 585 | // Return contains no value on error, otherwise it contains a map from aggregation keys |
| 586 | // to vectors of vectors using the format: |
| 587 | // { aggKey0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...], |
| 588 | // aggKey1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... } |
| 589 | // where ti_j_k is the ns tid i spent running on the jth cluster at the cluster's kth lowest freq. |
| 590 | std::optional<std::unordered_map<uint16_t, std::vector<std::vector<uint64_t>>>> |
| 591 | getAggregatedTaskCpuFreqTimes(pid_t tgid, const std::vector<uint16_t> &aggregationKeys) { |
| 592 | if (!gInitialized && !initGlobals()) return {}; |
| 593 | |
| 594 | uint32_t maxFreqCount = 0; |
| 595 | std::vector<std::vector<uint64_t>> mapFormat; |
| 596 | for (const auto &freqList : gPolicyFreqs) { |
| 597 | if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size(); |
| 598 | mapFormat.emplace_back(freqList.size(), 0); |
| 599 | } |
| 600 | |
| 601 | bool dataCollected = false; |
| 602 | std::unordered_map<uint16_t, std::vector<std::vector<uint64_t>>> map; |
| 603 | std::vector<tis_val_t> vals(gNCpus); |
| 604 | for (uint16_t aggregationKey : aggregationKeys) { |
| 605 | map.emplace(aggregationKey, mapFormat); |
| 606 | |
| 607 | aggregated_task_tis_key_t key{.tgid = tgid, .aggregation_key = aggregationKey}; |
| 608 | for (key.bucket = 0; key.bucket <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++key.bucket) { |
| 609 | if (findMapEntry(gPidTisMapFd, &key, vals.data()) != 0) { |
| 610 | if (errno != ENOENT) { |
| 611 | return {}; |
| 612 | } |
| 613 | continue; |
| 614 | } else { |
| 615 | dataCollected = true; |
| 616 | } |
| 617 | |
| 618 | // Combine data by aggregating time-in-state data grouped by CPU cluster aka policy. |
| 619 | uint32_t offset = key.bucket * FREQS_PER_ENTRY; |
| 620 | uint32_t nextOffset = offset + FREQS_PER_ENTRY; |
| 621 | for (uint32_t j = 0; j < gNPolicies; ++j) { |
| 622 | if (offset >= gPolicyFreqs[j].size()) continue; |
| 623 | auto begin = map[key.aggregation_key][j].begin() + offset; |
| 624 | auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY |
| 625 | : map[key.aggregation_key][j].end(); |
| 626 | for (const auto &cpu : gPolicyCpus[j]) { |
| 627 | std::transform(begin, end, std::begin(vals[cpu].ar), begin, |
| 628 | std::plus<uint64_t>()); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | if (!dataCollected) { |
| 635 | // Check if eBPF is supported on this device. If it is, gTisMap should not be empty. |
| 636 | time_key_t key; |
| 637 | if (getFirstMapKey(gTisMapFd, &key) != 0) { |
| 638 | return {}; |
| 639 | } |
| 640 | } |
| 641 | return map; |
| 642 | } |
| 643 | |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 644 | } // namespace bpf |
| 645 | } // namespace android |