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