| 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 | ff7bf70 | 2019-06-05 18:27:47 -0700 | [diff] [blame] | 20 | #include "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 | 6c501ea | 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 | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 28 | #include <optional> | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 29 | #include <set> | 
|  | 30 | #include <string> | 
|  | 31 | #include <unordered_map> | 
|  | 32 | #include <vector> | 
|  | 33 |  | 
|  | 34 | #include <android-base/file.h> | 
|  | 35 | #include <android-base/parseint.h> | 
|  | 36 | #include <android-base/stringprintf.h> | 
|  | 37 | #include <android-base/strings.h> | 
|  | 38 | #include <android-base/unique_fd.h> | 
|  | 39 | #include <bpf/BpfMap.h> | 
|  | 40 | #include <libbpf.h> | 
|  | 41 | #include <log/log.h> | 
|  | 42 |  | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 43 | using android::base::StringPrintf; | 
|  | 44 | using android::base::unique_fd; | 
|  | 45 |  | 
|  | 46 | namespace android { | 
|  | 47 | namespace bpf { | 
|  | 48 |  | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 49 | static std::mutex gInitializedMutex; | 
|  | 50 | static bool gInitialized = false; | 
|  | 51 | static uint32_t gNPolicies = 0; | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 52 | static uint32_t gNCpus = 0; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 53 | static std::vector<std::vector<uint32_t>> gPolicyFreqs; | 
|  | 54 | static std::vector<std::vector<uint32_t>> gPolicyCpus; | 
|  | 55 | static std::set<uint32_t> gAllFreqs; | 
|  | 56 | static unique_fd gMapFd; | 
|  | 57 |  | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 58 | 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] | 59 | std::string data; | 
|  | 60 |  | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 61 | if (!android::base::ReadFileToString(path, &data)) return {}; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 62 |  | 
|  | 63 | auto strings = android::base::Split(data, " \n"); | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 64 | std::vector<uint32_t> ret; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 65 | for (const auto &s : strings) { | 
|  | 66 | if (s.empty()) continue; | 
|  | 67 | uint32_t n; | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 68 | if (!android::base::ParseUint(s, &n)) return {}; | 
|  | 69 | ret.emplace_back(n); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 70 | } | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 71 | return ret; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
|  | 74 | static int isPolicyFile(const struct dirent *d) { | 
|  | 75 | return android::base::StartsWith(d->d_name, "policy"); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | static int comparePolicyFiles(const struct dirent **d1, const struct dirent **d2) { | 
|  | 79 | uint32_t policyN1, policyN2; | 
|  | 80 | if (sscanf((*d1)->d_name, "policy%" SCNu32 "", &policyN1) != 1 || | 
|  | 81 | sscanf((*d2)->d_name, "policy%" SCNu32 "", &policyN2) != 1) | 
|  | 82 | return 0; | 
|  | 83 | return policyN1 - policyN2; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | static bool initGlobals() { | 
|  | 87 | std::lock_guard<std::mutex> guard(gInitializedMutex); | 
|  | 88 | if (gInitialized) return true; | 
|  | 89 |  | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 90 | gNCpus = get_nprocs_conf(); | 
|  | 91 |  | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 92 | struct dirent **dirlist; | 
|  | 93 | const char basepath[] = "/sys/devices/system/cpu/cpufreq"; | 
|  | 94 | int ret = scandir(basepath, &dirlist, isPolicyFile, comparePolicyFiles); | 
|  | 95 | if (ret == -1) return false; | 
|  | 96 | gNPolicies = ret; | 
|  | 97 |  | 
|  | 98 | std::vector<std::string> policyFileNames; | 
|  | 99 | for (uint32_t i = 0; i < gNPolicies; ++i) { | 
|  | 100 | policyFileNames.emplace_back(dirlist[i]->d_name); | 
|  | 101 | free(dirlist[i]); | 
|  | 102 | } | 
|  | 103 | free(dirlist); | 
|  | 104 |  | 
|  | 105 | for (const auto &policy : policyFileNames) { | 
|  | 106 | std::vector<uint32_t> freqs; | 
|  | 107 | for (const auto &name : {"available", "boost"}) { | 
|  | 108 | std::string path = | 
|  | 109 | StringPrintf("%s/%s/scaling_%s_frequencies", basepath, policy.c_str(), name); | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 110 | auto nums = readNumbersFromFile(path); | 
|  | 111 | if (!nums) return false; | 
|  | 112 | freqs.insert(freqs.end(), nums->begin(), nums->end()); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 113 | } | 
|  | 114 | std::sort(freqs.begin(), freqs.end()); | 
|  | 115 | gPolicyFreqs.emplace_back(freqs); | 
|  | 116 |  | 
|  | 117 | for (auto freq : freqs) gAllFreqs.insert(freq); | 
|  | 118 |  | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 119 | std::string path = StringPrintf("%s/%s/%s", basepath, policy.c_str(), "related_cpus"); | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 120 | auto cpus = readNumbersFromFile(path); | 
|  | 121 | if (!cpus) return false; | 
|  | 122 | gPolicyCpus.emplace_back(*cpus); | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
| Connor O'Brien | cf9bc5f | 2019-06-06 17:29:18 -0700 | [diff] [blame] | 125 | gMapFd = unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_times_map")}; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 126 | if (gMapFd < 0) return false; | 
|  | 127 |  | 
|  | 128 | gInitialized = true; | 
|  | 129 | return true; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) { | 
|  | 133 | std::string path = StringPrintf(BPF_FS_PATH "prog_time_in_state_tracepoint_%s_%s", | 
|  | 134 | eventType.c_str(), eventName.c_str()); | 
|  | 135 | int prog_fd = bpf_obj_get(path.c_str()); | 
| Connor O'Brien | d250acc | 2019-01-23 17:21:41 -0800 | [diff] [blame] | 136 | if (prog_fd < 0) return false; | 
|  | 137 | 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] | 138 | } | 
|  | 139 |  | 
|  | 140 | // Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes. | 
|  | 141 | // Returns true on success, false otherwise. | 
|  | 142 | // Tracking is active only once a live process has successfully called this function; if the calling | 
|  | 143 | // process dies then it must be called again to resume tracking. | 
|  | 144 | // This function should *not* be called while tracking is already active; doing so is unnecessary | 
|  | 145 | // and can lead to accounting errors. | 
|  | 146 | bool startTrackingUidCpuFreqTimes() { | 
| Connor O'Brien | 9236e87 | 2019-06-06 17:48:20 -0700 | [diff] [blame] | 147 | if (!initGlobals()) return false; | 
|  | 148 |  | 
|  | 149 | unique_fd fd(bpf_obj_get(BPF_FS_PATH "map_time_in_state_cpu_policy_map")); | 
|  | 150 | if (fd < 0) return false; | 
|  | 151 |  | 
|  | 152 | for (uint32_t i = 0; i < gPolicyCpus.size(); ++i) { | 
|  | 153 | for (auto &cpu : gPolicyCpus[i]) { | 
|  | 154 | if (writeToMapEntry(fd, &cpu, &i, BPF_ANY)) return false; | 
|  | 155 | } | 
|  | 156 | } | 
|  | 157 |  | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 158 | unique_fd fd2(bpf_obj_get(BPF_FS_PATH "map_time_in_state_freq_to_idx_map")); | 
|  | 159 | if (fd2 < 0) return false; | 
|  | 160 | freq_idx_key_t key; | 
|  | 161 | for (uint32_t i = 0; i < gNPolicies; ++i) { | 
|  | 162 | key.policy = i; | 
|  | 163 | for (uint32_t j = 0; j < gPolicyFreqs[i].size(); ++j) { | 
|  | 164 | key.freq = gPolicyFreqs[i][j]; | 
|  | 165 | // Start indexes at 1 so that uninitialized state is distinguishable from lowest freq. | 
|  | 166 | // The uid_times map still uses 0-based indexes, and the sched_switch program handles | 
|  | 167 | // conversion between them, so this does not affect our map reading code. | 
|  | 168 | uint32_t idx = j + 1; | 
|  | 169 | if (writeToMapEntry(fd2, &key, &idx, BPF_ANY)) return false; | 
|  | 170 | } | 
|  | 171 | } | 
|  | 172 |  | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 173 | return attachTracepointProgram("sched", "sched_switch") && | 
|  | 174 | attachTracepointProgram("power", "cpu_frequency"); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | // Retrieve the times in ns that uid spent running at each CPU frequency and store in freqTimes. | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 178 | // 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] | 179 | // [[t0_0, t0_1, ...], | 
|  | 180 | //  [t1_0, t1_1, ...], ...] | 
|  | 181 | // where ti_j is the ns that uid spent running on the ith cluster at that cluster's jth lowest freq. | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 182 | std::optional<std::vector<std::vector<uint64_t>>> getUidCpuFreqTimes(uint32_t uid) { | 
|  | 183 | if (!gInitialized && !initGlobals()) return {}; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 184 |  | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 185 | std::vector<std::vector<uint64_t>> out; | 
|  | 186 | uint32_t maxFreqCount = 0; | 
|  | 187 | for (const auto &freqList : gPolicyFreqs) { | 
|  | 188 | if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size(); | 
|  | 189 | out.emplace_back(freqList.size(), 0); | 
|  | 190 | } | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 191 |  | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 192 | std::vector<val_t> vals(gNCpus); | 
|  | 193 | time_key_t key = {.uid = uid}; | 
|  | 194 | for (uint32_t i = 0; i <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++i) { | 
|  | 195 | key.bucket = i; | 
|  | 196 | if (findMapEntry(gMapFd, &key, vals.data())) { | 
|  | 197 | if (errno != ENOENT) return {}; | 
|  | 198 | continue; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 199 | } | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 200 |  | 
|  | 201 | auto offset = i * FREQS_PER_ENTRY; | 
|  | 202 | auto nextOffset = (i + 1) * FREQS_PER_ENTRY; | 
|  | 203 | for (uint32_t j = 0; j < gNPolicies; ++j) { | 
|  | 204 | if (offset >= gPolicyFreqs[j].size()) continue; | 
|  | 205 | auto begin = out[j].begin() + offset; | 
|  | 206 | auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY : out[j].end(); | 
|  | 207 |  | 
|  | 208 | for (const auto &cpu : gPolicyCpus[j]) { | 
|  | 209 | std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus<uint64_t>()); | 
| Connor O'Brien | 6c501ea | 2019-07-24 15:42:11 -0700 | [diff] [blame] | 210 | } | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 211 | } | 
|  | 212 | } | 
|  | 213 |  | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 214 | return out; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
|  | 217 | // Retrieve the times in ns that each uid spent running at each CPU freq and store in freqTimeMap. | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 218 | // Return contains no value on error, otherwise it contains a map from uids to vectors of vectors | 
|  | 219 | // using the format: | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 220 | // { uid0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...], | 
|  | 221 | //   uid1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... } | 
|  | 222 | // 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 | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 223 | std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>> | 
|  | 224 | getUidsCpuFreqTimes() { | 
|  | 225 | if (!gInitialized && !initGlobals()) return {}; | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 226 | time_key_t key, prevKey; | 
| Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame] | 227 | std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>> map; | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 228 | if (getFirstMapKey(gMapFd, &key)) { | 
|  | 229 | if (errno == ENOENT) return map; | 
|  | 230 | return std::nullopt; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | std::vector<std::vector<uint64_t>> mapFormat; | 
|  | 234 | for (const auto &freqList : gPolicyFreqs) mapFormat.emplace_back(freqList.size(), 0); | 
|  | 235 |  | 
|  | 236 | std::vector<val_t> vals(gNCpus); | 
|  | 237 | do { | 
|  | 238 | if (findMapEntry(gMapFd, &key, vals.data())) return {}; | 
|  | 239 | if (map.find(key.uid) == map.end()) map.emplace(key.uid, mapFormat); | 
|  | 240 |  | 
|  | 241 | auto offset = key.bucket * FREQS_PER_ENTRY; | 
|  | 242 | auto nextOffset = (key.bucket + 1) * FREQS_PER_ENTRY; | 
|  | 243 | for (uint32_t i = 0; i < gNPolicies; ++i) { | 
|  | 244 | if (offset >= gPolicyFreqs[i].size()) continue; | 
|  | 245 | auto begin = map[key.uid][i].begin() + offset; | 
|  | 246 | auto end = nextOffset < gPolicyFreqs[i].size() ? begin + FREQS_PER_ENTRY : | 
|  | 247 | map[key.uid][i].end(); | 
|  | 248 | for (const auto &cpu : gPolicyCpus[i]) { | 
|  | 249 | 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] | 250 | } | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 251 | } | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 252 | prevKey = key; | 
|  | 253 | } while (!getNextMapKey(gMapFd, &prevKey, &key)); | 
|  | 254 | if (errno != ENOENT) return {}; | 
|  | 255 | return map; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 256 | } | 
|  | 257 |  | 
|  | 258 | // Clear all time in state data for a given uid. Returns false on error, true otherwise. | 
|  | 259 | bool clearUidCpuFreqTimes(uint32_t uid) { | 
|  | 260 | if (!gInitialized && !initGlobals()) return false; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 261 |  | 
| Connor O'Brien | 16ab170 | 2019-06-07 16:39:49 -0700 | [diff] [blame] | 262 | time_key_t key = {.uid = uid}; | 
|  | 263 |  | 
|  | 264 | uint32_t maxFreqCount = 0; | 
|  | 265 | for (const auto &freqList : gPolicyFreqs) { | 
|  | 266 | if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size(); | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | val_t zeros = {0}; | 
|  | 270 | std::vector<val_t> vals(gNCpus, zeros); | 
|  | 271 | for (key.bucket = 0; key.bucket <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++key.bucket) { | 
| Connor O'Brien | 6c501ea | 2019-07-24 15:42:11 -0700 | [diff] [blame] | 272 | if (writeToMapEntry(gMapFd, &key, vals.data(), BPF_EXIST) && errno != ENOENT) return false; | 
| Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 273 | if (deleteMapEntry(gMapFd, &key) && errno != ENOENT) return false; | 
|  | 274 | } | 
|  | 275 | return true; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | } // namespace bpf | 
|  | 279 | } // namespace android |