blob: 4209dc5c6b2eb23b7a56c54e4936c77ac3419202 [file] [log] [blame]
Connor O'Brien57337192018-11-20 12:49:16 -08001/*
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'Briend65f2a02019-08-28 16:15:38 -070020#include <bpf_timeinstate.h>
Connor O'Brien57337192018-11-20 12:49:16 -080021
22#include <dirent.h>
23#include <errno.h>
24#include <inttypes.h>
Connor O'Brienc92ef102019-07-24 15:42:11 -070025#include <sys/sysinfo.h>
Connor O'Brien57337192018-11-20 12:49:16 -080026
27#include <mutex>
Connor O'Brien26de80f2019-06-11 13:49:19 -070028#include <numeric>
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070029#include <optional>
Connor O'Brien57337192018-11-20 12:49:16 -080030#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'Brien57337192018-11-20 12:49:16 -080044using android::base::StringPrintf;
45using android::base::unique_fd;
46
47namespace android {
48namespace bpf {
49
Connor O'Brien57337192018-11-20 12:49:16 -080050static std::mutex gInitializedMutex;
51static bool gInitialized = false;
Connor O'Brienb0491f82020-01-09 17:10:19 -080052static std::mutex gTrackingMutex;
53static bool gTracking = false;
Connor O'Brien57337192018-11-20 12:49:16 -080054static uint32_t gNPolicies = 0;
Connor O'Brien1a180402019-06-07 16:39:49 -070055static uint32_t gNCpus = 0;
Connor O'Brien57337192018-11-20 12:49:16 -080056static std::vector<std::vector<uint32_t>> gPolicyFreqs;
57static std::vector<std::vector<uint32_t>> gPolicyCpus;
58static std::set<uint32_t> gAllFreqs;
Connor O'Brien26de80f2019-06-11 13:49:19 -070059static unique_fd gTisMapFd;
60static unique_fd gConcurrentMapFd;
Connor O'Brien2a716a42020-01-31 18:51:56 -080061static unique_fd gUidLastUpdateMapFd;
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -070062static unique_fd gPidTisMapFd;
Connor O'Brien57337192018-11-20 12:49:16 -080063
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070064static std::optional<std::vector<uint32_t>> readNumbersFromFile(const std::string &path) {
Connor O'Brien57337192018-11-20 12:49:16 -080065 std::string data;
66
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070067 if (!android::base::ReadFileToString(path, &data)) return {};
Connor O'Brien57337192018-11-20 12:49:16 -080068
69 auto strings = android::base::Split(data, " \n");
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070070 std::vector<uint32_t> ret;
Connor O'Brien57337192018-11-20 12:49:16 -080071 for (const auto &s : strings) {
72 if (s.empty()) continue;
73 uint32_t n;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070074 if (!android::base::ParseUint(s, &n)) return {};
75 ret.emplace_back(n);
Connor O'Brien57337192018-11-20 12:49:16 -080076 }
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070077 return ret;
Connor O'Brien57337192018-11-20 12:49:16 -080078}
79
80static int isPolicyFile(const struct dirent *d) {
81 return android::base::StartsWith(d->d_name, "policy");
82}
83
84static 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
92static bool initGlobals() {
93 std::lock_guard<std::mutex> guard(gInitializedMutex);
94 if (gInitialized) return true;
95
Connor O'Brien1a180402019-06-07 16:39:49 -070096 gNCpus = get_nprocs_conf();
97
Connor O'Brien57337192018-11-20 12:49:16 -080098 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'Brienf03b6ae2019-06-05 18:03:12 -0700116 auto nums = readNumbersFromFile(path);
Connor O'Brienb8fe0772019-09-11 18:09:28 -0700117 if (!nums) continue;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700118 freqs.insert(freqs.end(), nums->begin(), nums->end());
Connor O'Brien57337192018-11-20 12:49:16 -0800119 }
Connor O'Brienb8fe0772019-09-11 18:09:28 -0700120 if (freqs.empty()) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800121 std::sort(freqs.begin(), freqs.end());
122 gPolicyFreqs.emplace_back(freqs);
123
124 for (auto freq : freqs) gAllFreqs.insert(freq);
125
Connor O'Brien57337192018-11-20 12:49:16 -0800126 std::string path = StringPrintf("%s/%s/%s", basepath, policy.c_str(), "related_cpus");
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700127 auto cpus = readNumbersFromFile(path);
128 if (!cpus) return false;
129 gPolicyCpus.emplace_back(*cpus);
Connor O'Brien57337192018-11-20 12:49:16 -0800130 }
131
Connor O'Brien26de80f2019-06-11 13:49:19 -0700132 gTisMapFd = unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_time_in_state_map")};
133 if (gTisMapFd < 0) return false;
134
135 gConcurrentMapFd =
136 unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")};
137 if (gConcurrentMapFd < 0) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800138
Connor O'Brien2a716a42020-01-31 18:51:56 -0800139 gUidLastUpdateMapFd =
140 unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_last_update_map")};
141 if (gUidLastUpdateMapFd < 0) return false;
142
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700143 gPidTisMapFd = unique_fd{mapRetrieveRO(BPF_FS_PATH "map_time_in_state_pid_time_in_state_map")};
144 if (gPidTisMapFd < 0) return false;
145
146 unique_fd trackedPidMapFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_pid_tracked_map"));
147 if (trackedPidMapFd < 0) return false;
148
Connor O'Brien57337192018-11-20 12:49:16 -0800149 gInitialized = true;
150 return true;
151}
152
153static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) {
154 std::string path = StringPrintf(BPF_FS_PATH "prog_time_in_state_tracepoint_%s_%s",
155 eventType.c_str(), eventName.c_str());
Maciej Żenczykowskiaf073cc2020-06-16 21:45:21 -0700156 int prog_fd = retrieveProgram(path.c_str());
Connor O'Briend250acc2019-01-23 17:21:41 -0800157 if (prog_fd < 0) return false;
158 return bpf_attach_tracepoint(prog_fd, eventType.c_str(), eventName.c_str()) >= 0;
Connor O'Brien57337192018-11-20 12:49:16 -0800159}
160
Connor O'Brienab51dca2020-02-19 14:11:45 -0800161static std::optional<uint32_t> getPolicyFreqIdx(uint32_t policy) {
162 auto path = StringPrintf("/sys/devices/system/cpu/cpufreq/policy%u/scaling_cur_freq",
163 gPolicyCpus[policy][0]);
164 auto freqVec = readNumbersFromFile(path);
165 if (!freqVec.has_value() || freqVec->size() != 1) return {};
166 for (uint32_t idx = 0; idx < gPolicyFreqs[policy].size(); ++idx) {
167 if ((*freqVec)[0] == gPolicyFreqs[policy][idx]) return idx + 1;
168 }
169 return {};
170}
171
Connor O'Brien57337192018-11-20 12:49:16 -0800172// Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes.
173// Returns true on success, false otherwise.
174// Tracking is active only once a live process has successfully called this function; if the calling
175// process dies then it must be called again to resume tracking.
176// This function should *not* be called while tracking is already active; doing so is unnecessary
177// and can lead to accounting errors.
Connor O'Brien26de80f2019-06-11 13:49:19 -0700178bool startTrackingUidTimes() {
Connor O'Brienb0491f82020-01-09 17:10:19 -0800179 std::lock_guard<std::mutex> guard(gTrackingMutex);
Connor O'Brien57b75dc2019-06-06 17:48:20 -0700180 if (!initGlobals()) return false;
Connor O'Brienb0491f82020-01-09 17:10:19 -0800181 if (gTracking) return true;
Connor O'Brien57b75dc2019-06-06 17:48:20 -0700182
Maciej Żenczykowskiaf073cc2020-06-16 21:45:21 -0700183 unique_fd cpuPolicyFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_cpu_policy_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700184 if (cpuPolicyFd < 0) return false;
Connor O'Brien57b75dc2019-06-06 17:48:20 -0700185
186 for (uint32_t i = 0; i < gPolicyCpus.size(); ++i) {
187 for (auto &cpu : gPolicyCpus[i]) {
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700188 if (writeToMapEntry(cpuPolicyFd, &cpu, &i, BPF_ANY)) return false;
Connor O'Brien57b75dc2019-06-06 17:48:20 -0700189 }
190 }
191
Maciej Żenczykowskiaf073cc2020-06-16 21:45:21 -0700192 unique_fd freqToIdxFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_freq_to_idx_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700193 if (freqToIdxFd < 0) return false;
Connor O'Brien1a180402019-06-07 16:39:49 -0700194 freq_idx_key_t key;
195 for (uint32_t i = 0; i < gNPolicies; ++i) {
196 key.policy = i;
197 for (uint32_t j = 0; j < gPolicyFreqs[i].size(); ++j) {
198 key.freq = gPolicyFreqs[i][j];
199 // Start indexes at 1 so that uninitialized state is distinguishable from lowest freq.
200 // The uid_times map still uses 0-based indexes, and the sched_switch program handles
201 // conversion between them, so this does not affect our map reading code.
202 uint32_t idx = j + 1;
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700203 if (writeToMapEntry(freqToIdxFd, &key, &idx, BPF_ANY)) return false;
Connor O'Brien1a180402019-06-07 16:39:49 -0700204 }
205 }
206
Maciej Żenczykowskiaf073cc2020-06-16 21:45:21 -0700207 unique_fd cpuLastUpdateFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_cpu_last_update_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700208 if (cpuLastUpdateFd < 0) return false;
209 std::vector<uint64_t> zeros(get_nprocs_conf(), 0);
210 uint32_t zero = 0;
211 if (writeToMapEntry(cpuLastUpdateFd, &zero, zeros.data(), BPF_ANY)) return false;
212
Maciej Żenczykowskiaf073cc2020-06-16 21:45:21 -0700213 unique_fd nrActiveFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_nr_active_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700214 if (nrActiveFd < 0) return false;
215 if (writeToMapEntry(nrActiveFd, &zero, &zero, BPF_ANY)) return false;
216
Maciej Żenczykowskiaf073cc2020-06-16 21:45:21 -0700217 unique_fd policyNrActiveFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_policy_nr_active_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700218 if (policyNrActiveFd < 0) return false;
219 for (uint32_t i = 0; i < gNPolicies; ++i) {
220 if (writeToMapEntry(policyNrActiveFd, &i, &zero, BPF_ANY)) return false;
221 }
222
Maciej Żenczykowskiaf073cc2020-06-16 21:45:21 -0700223 unique_fd policyFreqIdxFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_policy_freq_idx_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700224 if (policyFreqIdxFd < 0) return false;
225 for (uint32_t i = 0; i < gNPolicies; ++i) {
Connor O'Brienab51dca2020-02-19 14:11:45 -0800226 auto freqIdx = getPolicyFreqIdx(i);
227 if (!freqIdx.has_value()) return false;
228 if (writeToMapEntry(policyFreqIdxFd, &i, &(*freqIdx), BPF_ANY)) return false;
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700229 }
230
Connor O'Brienb0491f82020-01-09 17:10:19 -0800231 gTracking = attachTracepointProgram("sched", "sched_switch") &&
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700232 attachTracepointProgram("power", "cpu_frequency") &&
233 attachTracepointProgram("sched", "sched_process_free");
Connor O'Brienb0491f82020-01-09 17:10:19 -0800234 return gTracking;
Connor O'Brien57337192018-11-20 12:49:16 -0800235}
236
Connor O'Brien8f296eb2019-10-01 17:58:38 -0700237std::optional<std::vector<std::vector<uint32_t>>> getCpuFreqs() {
238 if (!gInitialized && !initGlobals()) return {};
239 return gPolicyFreqs;
240}
241
Connor O'Brien26de80f2019-06-11 13:49:19 -0700242// Retrieve the times in ns that uid spent running at each CPU frequency.
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700243// Return contains no value on error, otherwise it contains a vector of vectors using the format:
Connor O'Brien57337192018-11-20 12:49:16 -0800244// [[t0_0, t0_1, ...],
245// [t1_0, t1_1, ...], ...]
246// where ti_j is the ns that uid spent running on the ith cluster at that cluster's jth lowest freq.
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700247std::optional<std::vector<std::vector<uint64_t>>> getUidCpuFreqTimes(uint32_t uid) {
248 if (!gInitialized && !initGlobals()) return {};
Connor O'Brien57337192018-11-20 12:49:16 -0800249
Connor O'Brien1a180402019-06-07 16:39:49 -0700250 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 }
Connor O'Brien57337192018-11-20 12:49:16 -0800256
Connor O'Brien26de80f2019-06-11 13:49:19 -0700257 std::vector<tis_val_t> vals(gNCpus);
Connor O'Brien1a180402019-06-07 16:39:49 -0700258 time_key_t key = {.uid = uid};
259 for (uint32_t i = 0; i <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++i) {
260 key.bucket = i;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700261 if (findMapEntry(gTisMapFd, &key, vals.data())) {
Connor O'Brienb83af342020-08-14 13:13:37 -0700262 if (errno != ENOENT || getFirstMapKey(gTisMapFd, &key)) return {};
Connor O'Brien1a180402019-06-07 16:39:49 -0700263 continue;
Connor O'Brien57337192018-11-20 12:49:16 -0800264 }
Connor O'Brien1a180402019-06-07 16:39:49 -0700265
266 auto offset = i * FREQS_PER_ENTRY;
267 auto nextOffset = (i + 1) * FREQS_PER_ENTRY;
268 for (uint32_t j = 0; j < gNPolicies; ++j) {
269 if (offset >= gPolicyFreqs[j].size()) continue;
270 auto begin = out[j].begin() + offset;
271 auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY : out[j].end();
272
273 for (const auto &cpu : gPolicyCpus[j]) {
274 std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus<uint64_t>());
Connor O'Brienc92ef102019-07-24 15:42:11 -0700275 }
Connor O'Brien57337192018-11-20 12:49:16 -0800276 }
277 }
278
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700279 return out;
Connor O'Brien57337192018-11-20 12:49:16 -0800280}
281
Connor O'Brien2a716a42020-01-31 18:51:56 -0800282static std::optional<bool> uidUpdatedSince(uint32_t uid, uint64_t lastUpdate,
283 uint64_t *newLastUpdate) {
284 uint64_t uidLastUpdate;
285 if (findMapEntry(gUidLastUpdateMapFd, &uid, &uidLastUpdate)) return {};
286 // Updates that occurred during the previous read may have been missed. To mitigate
287 // this, don't ignore entries updated up to 1s before *lastUpdate
288 constexpr uint64_t NSEC_PER_SEC = 1000000000;
289 if (uidLastUpdate + NSEC_PER_SEC < lastUpdate) return false;
290 if (uidLastUpdate > *newLastUpdate) *newLastUpdate = uidLastUpdate;
291 return true;
292}
293
Connor O'Brien26de80f2019-06-11 13:49:19 -0700294// Retrieve the times in ns that each uid spent running at each CPU freq.
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700295// Return contains no value on error, otherwise it contains a map from uids to vectors of vectors
296// using the format:
Connor O'Brien57337192018-11-20 12:49:16 -0800297// { uid0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...],
298// uid1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... }
299// where ti_j_k is the ns uid i spent running on the jth cluster at the cluster's kth lowest freq.
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700300std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>>
301getUidsCpuFreqTimes() {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800302 return getUidsUpdatedCpuFreqTimes(nullptr);
303}
304
305// Retrieve the times in ns that each uid spent running at each CPU freq, excluding UIDs that have
306// not run since before lastUpdate.
307// Return format is the same as getUidsCpuFreqTimes()
308std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>>
309getUidsUpdatedCpuFreqTimes(uint64_t *lastUpdate) {
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700310 if (!gInitialized && !initGlobals()) return {};
Connor O'Brien1a180402019-06-07 16:39:49 -0700311 time_key_t key, prevKey;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700312 std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>> map;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700313 if (getFirstMapKey(gTisMapFd, &key)) {
Connor O'Brien1a180402019-06-07 16:39:49 -0700314 if (errno == ENOENT) return map;
315 return std::nullopt;
316 }
317
318 std::vector<std::vector<uint64_t>> mapFormat;
319 for (const auto &freqList : gPolicyFreqs) mapFormat.emplace_back(freqList.size(), 0);
320
Connor O'Brien2a716a42020-01-31 18:51:56 -0800321 uint64_t newLastUpdate = lastUpdate ? *lastUpdate : 0;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700322 std::vector<tis_val_t> vals(gNCpus);
Connor O'Brien1a180402019-06-07 16:39:49 -0700323 do {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800324 if (lastUpdate) {
325 auto uidUpdated = uidUpdatedSince(key.uid, *lastUpdate, &newLastUpdate);
326 if (!uidUpdated.has_value()) return {};
327 if (!*uidUpdated) continue;
328 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700329 if (findMapEntry(gTisMapFd, &key, vals.data())) return {};
Connor O'Brien1a180402019-06-07 16:39:49 -0700330 if (map.find(key.uid) == map.end()) map.emplace(key.uid, mapFormat);
331
332 auto offset = key.bucket * FREQS_PER_ENTRY;
333 auto nextOffset = (key.bucket + 1) * FREQS_PER_ENTRY;
334 for (uint32_t i = 0; i < gNPolicies; ++i) {
335 if (offset >= gPolicyFreqs[i].size()) continue;
336 auto begin = map[key.uid][i].begin() + offset;
337 auto end = nextOffset < gPolicyFreqs[i].size() ? begin + FREQS_PER_ENTRY :
338 map[key.uid][i].end();
339 for (const auto &cpu : gPolicyCpus[i]) {
340 std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus<uint64_t>());
Connor O'Brien57337192018-11-20 12:49:16 -0800341 }
Connor O'Brien57337192018-11-20 12:49:16 -0800342 }
Connor O'Brien1a180402019-06-07 16:39:49 -0700343 prevKey = key;
Connor O'Brien2a716a42020-01-31 18:51:56 -0800344 } while (prevKey = key, !getNextMapKey(gTisMapFd, &prevKey, &key));
Connor O'Brien1a180402019-06-07 16:39:49 -0700345 if (errno != ENOENT) return {};
Connor O'Brien2a716a42020-01-31 18:51:56 -0800346 if (lastUpdate && newLastUpdate > *lastUpdate) *lastUpdate = newLastUpdate;
Connor O'Brien1a180402019-06-07 16:39:49 -0700347 return map;
Connor O'Brien57337192018-11-20 12:49:16 -0800348}
349
Connor O'Brien26de80f2019-06-11 13:49:19 -0700350static bool verifyConcurrentTimes(const concurrent_time_t &ct) {
351 uint64_t activeSum = std::accumulate(ct.active.begin(), ct.active.end(), (uint64_t)0);
352 uint64_t policySum = 0;
353 for (const auto &vec : ct.policy) {
354 policySum += std::accumulate(vec.begin(), vec.end(), (uint64_t)0);
355 }
356 return activeSum == policySum;
357}
358
359// Retrieve the times in ns that uid spent running concurrently with each possible number of other
360// tasks on each cluster (policy times) and overall (active times).
361// Return contains no value on error, otherwise it contains a concurrent_time_t with the format:
362// {.active = [a0, a1, ...], .policy = [[p0_0, p0_1, ...], [p1_0, p1_1, ...], ...]}
363// where ai is the ns spent running concurrently with tasks on i other cpus and pi_j is the ns spent
364// running on the ith cluster, concurrently with tasks on j other cpus in the same cluster
365std::optional<concurrent_time_t> getUidConcurrentTimes(uint32_t uid, bool retry) {
366 if (!gInitialized && !initGlobals()) return {};
367 concurrent_time_t ret = {.active = std::vector<uint64_t>(gNCpus, 0)};
368 for (const auto &cpuList : gPolicyCpus) ret.policy.emplace_back(cpuList.size(), 0);
369 std::vector<concurrent_val_t> vals(gNCpus);
370 time_key_t key = {.uid = uid};
371 for (key.bucket = 0; key.bucket <= (gNCpus - 1) / CPUS_PER_ENTRY; ++key.bucket) {
372 if (findMapEntry(gConcurrentMapFd, &key, vals.data())) {
Connor O'Brienb83af342020-08-14 13:13:37 -0700373 if (errno != ENOENT || getFirstMapKey(gConcurrentMapFd, &key)) return {};
Connor O'Brien26de80f2019-06-11 13:49:19 -0700374 continue;
375 }
376 auto offset = key.bucket * CPUS_PER_ENTRY;
377 auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY;
378
379 auto activeBegin = ret.active.begin() + offset;
380 auto activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret.active.end();
381
382 for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) {
383 std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin,
384 std::plus<uint64_t>());
385 }
386
387 for (uint32_t policy = 0; policy < gNPolicies; ++policy) {
388 if (offset >= gPolicyCpus[policy].size()) continue;
389 auto policyBegin = ret.policy[policy].begin() + offset;
390 auto policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY
391 : ret.policy[policy].end();
392
393 for (const auto &cpu : gPolicyCpus[policy]) {
394 std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin,
395 std::plus<uint64_t>());
396 }
397 }
398 }
399 if (!verifyConcurrentTimes(ret) && retry) return getUidConcurrentTimes(uid, false);
400 return ret;
401}
402
403// Retrieve the times in ns that each uid spent running concurrently with each possible number of
404// other tasks on each cluster (policy times) and overall (active times).
405// Return contains no value on error, otherwise it contains a map from uids to concurrent_time_t's
406// using the format:
407// { uid0 -> {.active = [a0, a1, ...], .policy = [[p0_0, p0_1, ...], [p1_0, p1_1, ...], ...] }, ...}
408// where ai is the ns spent running concurrently with tasks on i other cpus and pi_j is the ns spent
409// running on the ith cluster, concurrently with tasks on j other cpus in the same cluster.
410std::optional<std::unordered_map<uint32_t, concurrent_time_t>> getUidsConcurrentTimes() {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800411 return getUidsUpdatedConcurrentTimes(nullptr);
412}
413
414// Retrieve the times in ns that each uid spent running concurrently with each possible number of
415// other tasks on each cluster (policy times) and overall (active times), excluding UIDs that have
416// not run since before lastUpdate.
417// Return format is the same as getUidsConcurrentTimes()
418std::optional<std::unordered_map<uint32_t, concurrent_time_t>> getUidsUpdatedConcurrentTimes(
419 uint64_t *lastUpdate) {
Connor O'Brien26de80f2019-06-11 13:49:19 -0700420 if (!gInitialized && !initGlobals()) return {};
421 time_key_t key, prevKey;
422 std::unordered_map<uint32_t, concurrent_time_t> ret;
423 if (getFirstMapKey(gConcurrentMapFd, &key)) {
424 if (errno == ENOENT) return ret;
425 return {};
426 }
427
428 concurrent_time_t retFormat = {.active = std::vector<uint64_t>(gNCpus, 0)};
429 for (const auto &cpuList : gPolicyCpus) retFormat.policy.emplace_back(cpuList.size(), 0);
430
431 std::vector<concurrent_val_t> vals(gNCpus);
432 std::vector<uint64_t>::iterator activeBegin, activeEnd, policyBegin, policyEnd;
433
Connor O'Brien2a716a42020-01-31 18:51:56 -0800434 uint64_t newLastUpdate = lastUpdate ? *lastUpdate : 0;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700435 do {
Connor O'Brien597f6372020-10-20 14:48:40 -0700436 if (key.bucket > (gNCpus - 1) / CPUS_PER_ENTRY) return {};
Connor O'Brien2a716a42020-01-31 18:51:56 -0800437 if (lastUpdate) {
438 auto uidUpdated = uidUpdatedSince(key.uid, *lastUpdate, &newLastUpdate);
439 if (!uidUpdated.has_value()) return {};
440 if (!*uidUpdated) continue;
441 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700442 if (findMapEntry(gConcurrentMapFd, &key, vals.data())) return {};
443 if (ret.find(key.uid) == ret.end()) ret.emplace(key.uid, retFormat);
444
445 auto offset = key.bucket * CPUS_PER_ENTRY;
446 auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY;
447
448 activeBegin = ret[key.uid].active.begin();
449 activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret[key.uid].active.end();
450
451 for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) {
452 std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin,
453 std::plus<uint64_t>());
454 }
455
456 for (uint32_t policy = 0; policy < gNPolicies; ++policy) {
457 if (offset >= gPolicyCpus[policy].size()) continue;
458 policyBegin = ret[key.uid].policy[policy].begin() + offset;
459 policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY
460 : ret[key.uid].policy[policy].end();
461
462 for (const auto &cpu : gPolicyCpus[policy]) {
463 std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin,
464 std::plus<uint64_t>());
465 }
466 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800467 } while (prevKey = key, !getNextMapKey(gConcurrentMapFd, &prevKey, &key));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700468 if (errno != ENOENT) return {};
469 for (const auto &[key, value] : ret) {
470 if (!verifyConcurrentTimes(value)) {
471 auto val = getUidConcurrentTimes(key, false);
472 if (val.has_value()) ret[key] = val.value();
473 }
474 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800475 if (lastUpdate && newLastUpdate > *lastUpdate) *lastUpdate = newLastUpdate;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700476 return ret;
477}
478
Connor O'Brien57337192018-11-20 12:49:16 -0800479// Clear all time in state data for a given uid. Returns false on error, true otherwise.
Connor O'Brien26de80f2019-06-11 13:49:19 -0700480// This is only suitable for clearing data when an app is uninstalled; if called on a UID with
481// running tasks it will cause time in state vs. concurrent time totals to be inconsistent for that
482// UID.
483bool clearUidTimes(uint32_t uid) {
Connor O'Brien57337192018-11-20 12:49:16 -0800484 if (!gInitialized && !initGlobals()) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800485
Connor O'Brien1a180402019-06-07 16:39:49 -0700486 time_key_t key = {.uid = uid};
487
488 uint32_t maxFreqCount = 0;
489 for (const auto &freqList : gPolicyFreqs) {
490 if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size();
491 }
492
Connor O'Brien26de80f2019-06-11 13:49:19 -0700493 tis_val_t zeros = {0};
494 std::vector<tis_val_t> vals(gNCpus, zeros);
Connor O'Brien1a180402019-06-07 16:39:49 -0700495 for (key.bucket = 0; key.bucket <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++key.bucket) {
Connor O'Brien26de80f2019-06-11 13:49:19 -0700496 if (writeToMapEntry(gTisMapFd, &key, vals.data(), BPF_EXIST) && errno != ENOENT)
497 return false;
498 if (deleteMapEntry(gTisMapFd, &key) && errno != ENOENT) return false;
499 }
500
Nick Desaulniers54891cd2019-11-19 09:31:05 -0800501 concurrent_val_t czeros = { .active = {0}, .policy = {0}, };
Connor O'Brien26de80f2019-06-11 13:49:19 -0700502 std::vector<concurrent_val_t> cvals(gNCpus, czeros);
503 for (key.bucket = 0; key.bucket <= (gNCpus - 1) / CPUS_PER_ENTRY; ++key.bucket) {
504 if (writeToMapEntry(gConcurrentMapFd, &key, cvals.data(), BPF_EXIST) && errno != ENOENT)
505 return false;
506 if (deleteMapEntry(gConcurrentMapFd, &key) && errno != ENOENT) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800507 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800508
509 if (deleteMapEntry(gUidLastUpdateMapFd, &uid) && errno != ENOENT) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800510 return true;
511}
512
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700513bool startTrackingProcessCpuTimes(pid_t pid) {
514 if (!gInitialized && !initGlobals()) return false;
515
516 unique_fd trackedPidHashMapFd(
517 mapRetrieveWO(BPF_FS_PATH "map_time_in_state_pid_tracked_hash_map"));
518 if (trackedPidHashMapFd < 0) return false;
519
520 unique_fd trackedPidMapFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_pid_tracked_map"));
521 if (trackedPidMapFd < 0) return false;
522
523 for (uint32_t index = 0; index < MAX_TRACKED_PIDS; index++) {
524 // Find first available [index, pid] entry in the pid_tracked_hash_map map
525 if (writeToMapEntry(trackedPidHashMapFd, &index, &pid, BPF_NOEXIST) != 0) {
526 if (errno != EEXIST) {
527 return false;
528 }
529 continue; // This index is already taken
530 }
531
532 tracked_pid_t tracked_pid = {.pid = pid, .state = TRACKED_PID_STATE_ACTIVE};
533 if (writeToMapEntry(trackedPidMapFd, &index, &tracked_pid, BPF_ANY) != 0) {
534 return false;
535 }
536 return true;
537 }
538 return false;
539}
540
541// Marks the specified task identified by its PID (aka TID) for CPU time-in-state tracking
542// aggregated with other tasks sharing the same TGID and aggregation key.
543bool startAggregatingTaskCpuTimes(pid_t pid, uint16_t aggregationKey) {
544 if (!gInitialized && !initGlobals()) return false;
545
546 unique_fd taskAggregationMapFd(
547 mapRetrieveWO(BPF_FS_PATH "map_time_in_state_pid_task_aggregation_map"));
548 if (taskAggregationMapFd < 0) return false;
549
550 return writeToMapEntry(taskAggregationMapFd, &pid, &aggregationKey, BPF_ANY) == 0;
551}
552
553// Retrieves the times in ns that each thread spent running at each CPU freq, aggregated by
554// aggregation key.
555// Return contains no value on error, otherwise it contains a map from aggregation keys
556// to vectors of vectors using the format:
557// { aggKey0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...],
558// aggKey1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... }
559// where ti_j_k is the ns tid i spent running on the jth cluster at the cluster's kth lowest freq.
560std::optional<std::unordered_map<uint16_t, std::vector<std::vector<uint64_t>>>>
561getAggregatedTaskCpuFreqTimes(pid_t tgid, const std::vector<uint16_t> &aggregationKeys) {
562 if (!gInitialized && !initGlobals()) return {};
563
564 uint32_t maxFreqCount = 0;
565 std::vector<std::vector<uint64_t>> mapFormat;
566 for (const auto &freqList : gPolicyFreqs) {
567 if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size();
568 mapFormat.emplace_back(freqList.size(), 0);
569 }
570
571 bool dataCollected = false;
572 std::unordered_map<uint16_t, std::vector<std::vector<uint64_t>>> map;
573 std::vector<tis_val_t> vals(gNCpus);
574 for (uint16_t aggregationKey : aggregationKeys) {
575 map.emplace(aggregationKey, mapFormat);
576
577 aggregated_task_tis_key_t key{.tgid = tgid, .aggregation_key = aggregationKey};
578 for (key.bucket = 0; key.bucket <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++key.bucket) {
579 if (findMapEntry(gPidTisMapFd, &key, vals.data()) != 0) {
580 if (errno != ENOENT) {
581 return {};
582 }
583 continue;
584 } else {
585 dataCollected = true;
586 }
587
588 // Combine data by aggregating time-in-state data grouped by CPU cluster aka policy.
589 uint32_t offset = key.bucket * FREQS_PER_ENTRY;
590 uint32_t nextOffset = offset + FREQS_PER_ENTRY;
591 for (uint32_t j = 0; j < gNPolicies; ++j) {
592 if (offset >= gPolicyFreqs[j].size()) continue;
593 auto begin = map[key.aggregation_key][j].begin() + offset;
594 auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY
595 : map[key.aggregation_key][j].end();
596 for (const auto &cpu : gPolicyCpus[j]) {
597 std::transform(begin, end, std::begin(vals[cpu].ar), begin,
598 std::plus<uint64_t>());
599 }
600 }
601 }
602 }
603
604 if (!dataCollected) {
605 // Check if eBPF is supported on this device. If it is, gTisMap should not be empty.
606 time_key_t key;
607 if (getFirstMapKey(gTisMapFd, &key) != 0) {
608 return {};
609 }
610 }
611 return map;
612}
613
Connor O'Brien57337192018-11-20 12:49:16 -0800614} // namespace bpf
615} // namespace android