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