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" |
| 20 | |
| 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
| 23 | #include <inttypes.h> |
| 24 | |
| 25 | #include <mutex> |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 26 | #include <optional> |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 27 | #include <set> |
| 28 | #include <string> |
| 29 | #include <unordered_map> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include <android-base/file.h> |
| 33 | #include <android-base/parseint.h> |
| 34 | #include <android-base/stringprintf.h> |
| 35 | #include <android-base/strings.h> |
| 36 | #include <android-base/unique_fd.h> |
| 37 | #include <bpf/BpfMap.h> |
| 38 | #include <libbpf.h> |
| 39 | #include <log/log.h> |
| 40 | |
| 41 | #define BPF_FS_PATH "/sys/fs/bpf/" |
| 42 | |
| 43 | using android::base::StringPrintf; |
| 44 | using android::base::unique_fd; |
| 45 | |
| 46 | namespace android { |
| 47 | namespace bpf { |
| 48 | |
Connor O'Brien | d250acc | 2019-01-23 17:21:41 -0800 | [diff] [blame] | 49 | struct time_key_t { |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 50 | uint32_t uid; |
| 51 | uint32_t freq; |
Connor O'Brien | d250acc | 2019-01-23 17:21:41 -0800 | [diff] [blame] | 52 | }; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 53 | |
Connor O'Brien | d250acc | 2019-01-23 17:21:41 -0800 | [diff] [blame] | 54 | struct val_t { |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 55 | uint64_t ar[100]; |
Connor O'Brien | d250acc | 2019-01-23 17:21:41 -0800 | [diff] [blame] | 56 | }; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 57 | |
| 58 | static std::mutex gInitializedMutex; |
| 59 | static bool gInitialized = false; |
| 60 | static uint32_t gNPolicies = 0; |
| 61 | static std::vector<std::vector<uint32_t>> gPolicyFreqs; |
| 62 | static std::vector<std::vector<uint32_t>> gPolicyCpus; |
| 63 | static std::set<uint32_t> gAllFreqs; |
| 64 | static unique_fd gMapFd; |
| 65 | |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 66 | 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] | 67 | std::string data; |
| 68 | |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 69 | if (!android::base::ReadFileToString(path, &data)) return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 70 | |
| 71 | auto strings = android::base::Split(data, " \n"); |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 72 | std::vector<uint32_t> ret; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 73 | for (const auto &s : strings) { |
| 74 | if (s.empty()) continue; |
| 75 | uint32_t n; |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 76 | if (!android::base::ParseUint(s, &n)) return {}; |
| 77 | ret.emplace_back(n); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 78 | } |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 79 | return ret; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int isPolicyFile(const struct dirent *d) { |
| 83 | return android::base::StartsWith(d->d_name, "policy"); |
| 84 | } |
| 85 | |
| 86 | static int comparePolicyFiles(const struct dirent **d1, const struct dirent **d2) { |
| 87 | uint32_t policyN1, policyN2; |
| 88 | if (sscanf((*d1)->d_name, "policy%" SCNu32 "", &policyN1) != 1 || |
| 89 | sscanf((*d2)->d_name, "policy%" SCNu32 "", &policyN2) != 1) |
| 90 | return 0; |
| 91 | return policyN1 - policyN2; |
| 92 | } |
| 93 | |
| 94 | static bool initGlobals() { |
| 95 | std::lock_guard<std::mutex> guard(gInitializedMutex); |
| 96 | if (gInitialized) return true; |
| 97 | |
| 98 | struct dirent **dirlist; |
| 99 | const char basepath[] = "/sys/devices/system/cpu/cpufreq"; |
| 100 | int ret = scandir(basepath, &dirlist, isPolicyFile, comparePolicyFiles); |
| 101 | if (ret == -1) return false; |
| 102 | gNPolicies = ret; |
| 103 | |
| 104 | std::vector<std::string> policyFileNames; |
| 105 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 106 | policyFileNames.emplace_back(dirlist[i]->d_name); |
| 107 | free(dirlist[i]); |
| 108 | } |
| 109 | free(dirlist); |
| 110 | |
| 111 | for (const auto &policy : policyFileNames) { |
| 112 | std::vector<uint32_t> freqs; |
| 113 | for (const auto &name : {"available", "boost"}) { |
| 114 | std::string path = |
| 115 | StringPrintf("%s/%s/scaling_%s_frequencies", basepath, policy.c_str(), name); |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 116 | auto nums = readNumbersFromFile(path); |
| 117 | if (!nums) return false; |
| 118 | freqs.insert(freqs.end(), nums->begin(), nums->end()); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 119 | } |
| 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 | 4b9c498 | 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 | cf9bc5f | 2019-06-06 17:29:18 -0700 | [diff] [blame] | 131 | 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] | 132 | if (gMapFd < 0) return false; |
| 133 | |
| 134 | gInitialized = true; |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) { |
| 139 | std::string path = StringPrintf(BPF_FS_PATH "prog_time_in_state_tracepoint_%s_%s", |
| 140 | eventType.c_str(), eventName.c_str()); |
| 141 | int prog_fd = bpf_obj_get(path.c_str()); |
Connor O'Brien | d250acc | 2019-01-23 17:21:41 -0800 | [diff] [blame] | 142 | if (prog_fd < 0) return false; |
| 143 | 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] | 144 | } |
| 145 | |
| 146 | // Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes. |
| 147 | // Returns true on success, false otherwise. |
| 148 | // Tracking is active only once a live process has successfully called this function; if the calling |
| 149 | // process dies then it must be called again to resume tracking. |
| 150 | // This function should *not* be called while tracking is already active; doing so is unnecessary |
| 151 | // and can lead to accounting errors. |
| 152 | bool startTrackingUidCpuFreqTimes() { |
| 153 | return attachTracepointProgram("sched", "sched_switch") && |
| 154 | attachTracepointProgram("power", "cpu_frequency"); |
| 155 | } |
| 156 | |
| 157 | // 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^] | 158 | // 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] | 159 | // [[t0_0, t0_1, ...], |
| 160 | // [t1_0, t1_1, ...], ...] |
| 161 | // 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^] | 162 | std::optional<std::vector<std::vector<uint64_t>>> getUidCpuFreqTimes(uint32_t uid) { |
| 163 | if (!gInitialized && !initGlobals()) return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 164 | time_key_t key = {.uid = uid, .freq = 0}; |
| 165 | |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 166 | std::vector<std::vector<uint64_t>> out(gNPolicies); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 167 | std::vector<uint32_t> idxs(gNPolicies, 0); |
| 168 | |
| 169 | val_t value; |
| 170 | for (uint32_t freq : gAllFreqs) { |
| 171 | key.freq = freq; |
| 172 | int ret = findMapEntry(gMapFd, &key, &value); |
| 173 | if (ret) { |
| 174 | if (errno == ENOENT) |
| 175 | memset(&value.ar, 0, sizeof(value.ar)); |
| 176 | else |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 177 | return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 178 | } |
| 179 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 180 | if (idxs[i] == gPolicyFreqs[i].size() || freq != gPolicyFreqs[i][idxs[i]]) continue; |
| 181 | uint64_t time = 0; |
| 182 | for (uint32_t cpu : gPolicyCpus[i]) time += value.ar[cpu]; |
| 183 | idxs[i] += 1; |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 184 | out[i].emplace_back(time); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 188 | return out; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // 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^] | 192 | // Return contains no value on error, otherwise it contains a map from uids to vectors of vectors |
| 193 | // using the format: |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 194 | // { uid0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...], |
| 195 | // uid1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... } |
| 196 | // 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^] | 197 | std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>> |
| 198 | getUidsCpuFreqTimes() { |
| 199 | if (!gInitialized && !initGlobals()) return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 200 | |
Connor O'Brien | cf9bc5f | 2019-06-06 17:29:18 -0700 | [diff] [blame] | 201 | int fd = bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_times_map"); |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 202 | if (fd < 0) return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 203 | BpfMap<time_key_t, val_t> m(fd); |
| 204 | |
| 205 | std::vector<std::unordered_map<uint32_t, uint32_t>> policyFreqIdxs; |
| 206 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
| 207 | std::unordered_map<uint32_t, uint32_t> freqIdxs; |
| 208 | for (size_t j = 0; j < gPolicyFreqs[i].size(); ++j) freqIdxs[gPolicyFreqs[i][j]] = j; |
| 209 | policyFreqIdxs.emplace_back(freqIdxs); |
| 210 | } |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 211 | std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>> map; |
| 212 | auto fn = [&map, &policyFreqIdxs](const time_key_t &key, const val_t &val, |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 213 | const BpfMap<time_key_t, val_t> &) { |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 214 | if (map.find(key.uid) == map.end()) { |
| 215 | map[key.uid].resize(gNPolicies); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 216 | for (uint32_t i = 0; i < gNPolicies; ++i) { |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 217 | map[key.uid][i].resize(gPolicyFreqs[i].size(), 0); |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 218 | } |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | for (size_t policy = 0; policy < gNPolicies; ++policy) { |
| 222 | for (const auto &cpu : gPolicyCpus[policy]) { |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 223 | auto freqIdx = policyFreqIdxs[policy][key.freq]; |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 224 | map[key.uid][policy][freqIdx] += val.ar[cpu]; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | return android::netdutils::status::ok; |
| 228 | }; |
Connor O'Brien | 4b9c498 | 2019-06-05 18:03:12 -0700 | [diff] [blame^] | 229 | if (isOk(m.iterateWithValue(fn))) return map; |
| 230 | return {}; |
Connor O'Brien | 5733719 | 2018-11-20 12:49:16 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Clear all time in state data for a given uid. Returns false on error, true otherwise. |
| 234 | bool clearUidCpuFreqTimes(uint32_t uid) { |
| 235 | if (!gInitialized && !initGlobals()) return false; |
| 236 | time_key_t key = {.uid = uid, .freq = 0}; |
| 237 | |
| 238 | std::vector<uint32_t> idxs(gNPolicies, 0); |
| 239 | for (auto freq : gAllFreqs) { |
| 240 | key.freq = freq; |
| 241 | if (deleteMapEntry(gMapFd, &key) && errno != ENOENT) return false; |
| 242 | } |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | } // namespace bpf |
| 247 | } // namespace android |