blob: 706704ad345857ed8439b834d56135753994925b [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;
Rafal Slawik27c48db2021-01-05 19:10:07 +000059static unique_fd gTisTotalMapFd;
Connor O'Brien26de80f2019-06-11 13:49:19 -070060static unique_fd gTisMapFd;
61static unique_fd gConcurrentMapFd;
Connor O'Brien2a716a42020-01-31 18:51:56 -080062static unique_fd gUidLastUpdateMapFd;
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -070063static unique_fd gPidTisMapFd;
Connor O'Brien57337192018-11-20 12:49:16 -080064
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070065static std::optional<std::vector<uint32_t>> readNumbersFromFile(const std::string &path) {
Connor O'Brien57337192018-11-20 12:49:16 -080066 std::string data;
67
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070068 if (!android::base::ReadFileToString(path, &data)) return {};
Connor O'Brien57337192018-11-20 12:49:16 -080069
70 auto strings = android::base::Split(data, " \n");
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070071 std::vector<uint32_t> ret;
Connor O'Brien57337192018-11-20 12:49:16 -080072 for (const auto &s : strings) {
73 if (s.empty()) continue;
74 uint32_t n;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070075 if (!android::base::ParseUint(s, &n)) return {};
76 ret.emplace_back(n);
Connor O'Brien57337192018-11-20 12:49:16 -080077 }
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070078 return ret;
Connor O'Brien57337192018-11-20 12:49:16 -080079}
80
81static int isPolicyFile(const struct dirent *d) {
82 return android::base::StartsWith(d->d_name, "policy");
83}
84
85static int comparePolicyFiles(const struct dirent **d1, const struct dirent **d2) {
86 uint32_t policyN1, policyN2;
87 if (sscanf((*d1)->d_name, "policy%" SCNu32 "", &policyN1) != 1 ||
88 sscanf((*d2)->d_name, "policy%" SCNu32 "", &policyN2) != 1)
89 return 0;
90 return policyN1 - policyN2;
91}
92
93static bool initGlobals() {
94 std::lock_guard<std::mutex> guard(gInitializedMutex);
95 if (gInitialized) return true;
96
Connor O'Brien1a180402019-06-07 16:39:49 -070097 gNCpus = get_nprocs_conf();
98
Connor O'Brien57337192018-11-20 12:49:16 -080099 struct dirent **dirlist;
100 const char basepath[] = "/sys/devices/system/cpu/cpufreq";
101 int ret = scandir(basepath, &dirlist, isPolicyFile, comparePolicyFiles);
Peter Collingbournea5ca7662021-01-26 11:56:58 -0800102 if (ret == -1 || ret == 0) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800103 gNPolicies = ret;
104
105 std::vector<std::string> policyFileNames;
106 for (uint32_t i = 0; i < gNPolicies; ++i) {
107 policyFileNames.emplace_back(dirlist[i]->d_name);
108 free(dirlist[i]);
109 }
110 free(dirlist);
111
112 for (const auto &policy : policyFileNames) {
113 std::vector<uint32_t> freqs;
114 for (const auto &name : {"available", "boost"}) {
115 std::string path =
116 StringPrintf("%s/%s/scaling_%s_frequencies", basepath, policy.c_str(), name);
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700117 auto nums = readNumbersFromFile(path);
Connor O'Brienb8fe0772019-09-11 18:09:28 -0700118 if (!nums) continue;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700119 freqs.insert(freqs.end(), nums->begin(), nums->end());
Connor O'Brien57337192018-11-20 12:49:16 -0800120 }
Connor O'Brienb8fe0772019-09-11 18:09:28 -0700121 if (freqs.empty()) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800122 std::sort(freqs.begin(), freqs.end());
123 gPolicyFreqs.emplace_back(freqs);
124
125 for (auto freq : freqs) gAllFreqs.insert(freq);
126
Connor O'Brien57337192018-11-20 12:49:16 -0800127 std::string path = StringPrintf("%s/%s/%s", basepath, policy.c_str(), "related_cpus");
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700128 auto cpus = readNumbersFromFile(path);
129 if (!cpus) return false;
130 gPolicyCpus.emplace_back(*cpus);
Connor O'Brien57337192018-11-20 12:49:16 -0800131 }
132
Rafal Slawik27c48db2021-01-05 19:10:07 +0000133 gTisTotalMapFd =
Ken Chen9c34e712022-07-10 19:05:39 +0800134 unique_fd{bpf_obj_get(BPF_FS_PATH "map_timeInState_total_time_in_state_map")};
Rafal Slawik27c48db2021-01-05 19:10:07 +0000135 if (gTisTotalMapFd < 0) return false;
136
Ken Chen9c34e712022-07-10 19:05:39 +0800137 gTisMapFd = unique_fd{bpf_obj_get(BPF_FS_PATH "map_timeInState_uid_time_in_state_map")};
Connor O'Brien26de80f2019-06-11 13:49:19 -0700138 if (gTisMapFd < 0) return false;
139
140 gConcurrentMapFd =
Ken Chen9c34e712022-07-10 19:05:39 +0800141 unique_fd{bpf_obj_get(BPF_FS_PATH "map_timeInState_uid_concurrent_times_map")};
Connor O'Brien26de80f2019-06-11 13:49:19 -0700142 if (gConcurrentMapFd < 0) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800143
Connor O'Brien2a716a42020-01-31 18:51:56 -0800144 gUidLastUpdateMapFd =
Ken Chen9c34e712022-07-10 19:05:39 +0800145 unique_fd{bpf_obj_get(BPF_FS_PATH "map_timeInState_uid_last_update_map")};
Connor O'Brien2a716a42020-01-31 18:51:56 -0800146 if (gUidLastUpdateMapFd < 0) return false;
147
Ken Chen9c34e712022-07-10 19:05:39 +0800148 gPidTisMapFd = unique_fd{mapRetrieveRO(BPF_FS_PATH "map_timeInState_pid_time_in_state_map")};
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700149 if (gPidTisMapFd < 0) return false;
150
Ken Chen9c34e712022-07-10 19:05:39 +0800151 unique_fd trackedPidMapFd(mapRetrieveWO(BPF_FS_PATH "map_timeInState_pid_tracked_map"));
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700152 if (trackedPidMapFd < 0) return false;
153
Connor O'Brien57337192018-11-20 12:49:16 -0800154 gInitialized = true;
155 return true;
156}
157
Rafal Slawik45caa842021-01-29 12:25:56 +0000158static int retrieveProgramFd(const std::string &eventType, const std::string &eventName) {
Ken Chen9c34e712022-07-10 19:05:39 +0800159 std::string path = StringPrintf(BPF_FS_PATH "prog_timeInState_tracepoint_%s_%s",
Connor O'Brien57337192018-11-20 12:49:16 -0800160 eventType.c_str(), eventName.c_str());
Rafal Slawik45caa842021-01-29 12:25:56 +0000161 return retrieveProgram(path.c_str());
162}
163
164static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) {
165 int prog_fd = retrieveProgramFd(eventType, eventName);
Connor O'Briend250acc2019-01-23 17:21:41 -0800166 if (prog_fd < 0) return false;
167 return bpf_attach_tracepoint(prog_fd, eventType.c_str(), eventName.c_str()) >= 0;
Connor O'Brien57337192018-11-20 12:49:16 -0800168}
169
Connor O'Brienab51dca2020-02-19 14:11:45 -0800170static std::optional<uint32_t> getPolicyFreqIdx(uint32_t policy) {
171 auto path = StringPrintf("/sys/devices/system/cpu/cpufreq/policy%u/scaling_cur_freq",
172 gPolicyCpus[policy][0]);
173 auto freqVec = readNumbersFromFile(path);
174 if (!freqVec.has_value() || freqVec->size() != 1) return {};
175 for (uint32_t idx = 0; idx < gPolicyFreqs[policy].size(); ++idx) {
176 if ((*freqVec)[0] == gPolicyFreqs[policy][idx]) return idx + 1;
177 }
178 return {};
179}
180
Rafal Slawik45caa842021-01-29 12:25:56 +0000181// Check if tracking is expected to work without activating it.
182bool isTrackingUidTimesSupported() {
183 auto freqs = getCpuFreqs();
184 if (!freqs || freqs->empty()) return false;
185 if (gTracking) return true;
186 if (retrieveProgramFd("sched", "sched_switch") < 0) return false;
187 if (retrieveProgramFd("power", "cpu_frequency") < 0) return false;
188 if (retrieveProgramFd("sched", "sched_process_free") < 0) return false;
189 return true;
190}
191
Connor O'Brien57337192018-11-20 12:49:16 -0800192// Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes.
193// Returns true on success, false otherwise.
194// Tracking is active only once a live process has successfully called this function; if the calling
195// process dies then it must be called again to resume tracking.
196// This function should *not* be called while tracking is already active; doing so is unnecessary
197// and can lead to accounting errors.
Connor O'Brien26de80f2019-06-11 13:49:19 -0700198bool startTrackingUidTimes() {
Connor O'Brienb0491f82020-01-09 17:10:19 -0800199 std::lock_guard<std::mutex> guard(gTrackingMutex);
Connor O'Brien57b75dc2019-06-06 17:48:20 -0700200 if (!initGlobals()) return false;
Connor O'Brienb0491f82020-01-09 17:10:19 -0800201 if (gTracking) return true;
Connor O'Brien57b75dc2019-06-06 17:48:20 -0700202
Ken Chen9c34e712022-07-10 19:05:39 +0800203 unique_fd cpuPolicyFd(mapRetrieveWO(BPF_FS_PATH "map_timeInState_cpu_policy_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700204 if (cpuPolicyFd < 0) return false;
Connor O'Brien57b75dc2019-06-06 17:48:20 -0700205
206 for (uint32_t i = 0; i < gPolicyCpus.size(); ++i) {
207 for (auto &cpu : gPolicyCpus[i]) {
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700208 if (writeToMapEntry(cpuPolicyFd, &cpu, &i, BPF_ANY)) return false;
Connor O'Brien57b75dc2019-06-06 17:48:20 -0700209 }
210 }
211
Ken Chen9c34e712022-07-10 19:05:39 +0800212 unique_fd freqToIdxFd(mapRetrieveWO(BPF_FS_PATH "map_timeInState_freq_to_idx_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700213 if (freqToIdxFd < 0) return false;
Connor O'Brien1a180402019-06-07 16:39:49 -0700214 freq_idx_key_t key;
215 for (uint32_t i = 0; i < gNPolicies; ++i) {
216 key.policy = i;
217 for (uint32_t j = 0; j < gPolicyFreqs[i].size(); ++j) {
218 key.freq = gPolicyFreqs[i][j];
219 // Start indexes at 1 so that uninitialized state is distinguishable from lowest freq.
220 // The uid_times map still uses 0-based indexes, and the sched_switch program handles
221 // conversion between them, so this does not affect our map reading code.
222 uint32_t idx = j + 1;
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700223 if (writeToMapEntry(freqToIdxFd, &key, &idx, BPF_ANY)) return false;
Connor O'Brien1a180402019-06-07 16:39:49 -0700224 }
225 }
226
Ken Chen9c34e712022-07-10 19:05:39 +0800227 unique_fd cpuLastUpdateFd(mapRetrieveWO(BPF_FS_PATH "map_timeInState_cpu_last_update_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700228 if (cpuLastUpdateFd < 0) return false;
229 std::vector<uint64_t> zeros(get_nprocs_conf(), 0);
230 uint32_t zero = 0;
231 if (writeToMapEntry(cpuLastUpdateFd, &zero, zeros.data(), BPF_ANY)) return false;
232
Ken Chen9c34e712022-07-10 19:05:39 +0800233 unique_fd nrActiveFd(mapRetrieveWO(BPF_FS_PATH "map_timeInState_nr_active_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700234 if (nrActiveFd < 0) return false;
235 if (writeToMapEntry(nrActiveFd, &zero, &zero, BPF_ANY)) return false;
236
Ken Chen9c34e712022-07-10 19:05:39 +0800237 unique_fd policyNrActiveFd(mapRetrieveWO(BPF_FS_PATH "map_timeInState_policy_nr_active_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700238 if (policyNrActiveFd < 0) return false;
239 for (uint32_t i = 0; i < gNPolicies; ++i) {
240 if (writeToMapEntry(policyNrActiveFd, &i, &zero, BPF_ANY)) return false;
241 }
242
Ken Chen9c34e712022-07-10 19:05:39 +0800243 unique_fd policyFreqIdxFd(mapRetrieveWO(BPF_FS_PATH "map_timeInState_policy_freq_idx_map"));
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700244 if (policyFreqIdxFd < 0) return false;
245 for (uint32_t i = 0; i < gNPolicies; ++i) {
Connor O'Brienab51dca2020-02-19 14:11:45 -0800246 auto freqIdx = getPolicyFreqIdx(i);
247 if (!freqIdx.has_value()) return false;
248 if (writeToMapEntry(policyFreqIdxFd, &i, &(*freqIdx), BPF_ANY)) return false;
Connor O'Brien3fc2cb72019-08-21 12:08:39 -0700249 }
250
Connor O'Brienb0491f82020-01-09 17:10:19 -0800251 gTracking = attachTracepointProgram("sched", "sched_switch") &&
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700252 attachTracepointProgram("power", "cpu_frequency") &&
253 attachTracepointProgram("sched", "sched_process_free");
Connor O'Brienb0491f82020-01-09 17:10:19 -0800254 return gTracking;
Connor O'Brien57337192018-11-20 12:49:16 -0800255}
256
Connor O'Brien8f296eb2019-10-01 17:58:38 -0700257std::optional<std::vector<std::vector<uint32_t>>> getCpuFreqs() {
258 if (!gInitialized && !initGlobals()) return {};
259 return gPolicyFreqs;
260}
261
Rafal Slawik27c48db2021-01-05 19:10:07 +0000262std::optional<std::vector<std::vector<uint64_t>>> getTotalCpuFreqTimes() {
263 if (!gInitialized && !initGlobals()) return {};
264
265 std::vector<std::vector<uint64_t>> out;
266 uint32_t maxFreqCount = 0;
267 for (const auto &freqList : gPolicyFreqs) {
268 if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size();
269 out.emplace_back(freqList.size(), 0);
270 }
271
272 std::vector<uint64_t> vals(gNCpus);
273 const uint32_t freqCount = maxFreqCount <= MAX_FREQS_FOR_TOTAL ? maxFreqCount :
274 MAX_FREQS_FOR_TOTAL;
275 for (uint32_t freqIdx = 0; freqIdx < freqCount; ++freqIdx) {
276 if (findMapEntry(gTisTotalMapFd, &freqIdx, vals.data())) return {};
277 for (uint32_t policyIdx = 0; policyIdx < gNPolicies; ++policyIdx) {
278 if (freqIdx >= gPolicyFreqs[policyIdx].size()) continue;
279 for (const auto &cpu : gPolicyCpus[policyIdx]) {
280 out[policyIdx][freqIdx] += vals[cpu];
281 }
282 }
283 }
284
285 return out;
286}
Connor O'Brien26de80f2019-06-11 13:49:19 -0700287// Retrieve the times in ns that uid spent running at each CPU frequency.
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700288// Return contains no value on error, otherwise it contains a vector of vectors using the format:
Connor O'Brien57337192018-11-20 12:49:16 -0800289// [[t0_0, t0_1, ...],
290// [t1_0, t1_1, ...], ...]
291// 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 -0700292std::optional<std::vector<std::vector<uint64_t>>> getUidCpuFreqTimes(uint32_t uid) {
293 if (!gInitialized && !initGlobals()) return {};
Connor O'Brien57337192018-11-20 12:49:16 -0800294
Connor O'Brien1a180402019-06-07 16:39:49 -0700295 std::vector<std::vector<uint64_t>> out;
296 uint32_t maxFreqCount = 0;
297 for (const auto &freqList : gPolicyFreqs) {
298 if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size();
299 out.emplace_back(freqList.size(), 0);
300 }
Connor O'Brien57337192018-11-20 12:49:16 -0800301
Connor O'Brien26de80f2019-06-11 13:49:19 -0700302 std::vector<tis_val_t> vals(gNCpus);
Connor O'Brien1a180402019-06-07 16:39:49 -0700303 for (uint32_t i = 0; i <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++i) {
Connor O'Briend4b1ec02022-04-26 18:50:26 -0700304 const time_key_t key = {.uid = uid, .bucket = i};
Connor O'Brien26de80f2019-06-11 13:49:19 -0700305 if (findMapEntry(gTisMapFd, &key, vals.data())) {
Connor O'Briend4b1ec02022-04-26 18:50:26 -0700306 time_key_t tmpKey;
307 if (errno != ENOENT || getFirstMapKey(gTisMapFd, &tmpKey)) return {};
Connor O'Brien1a180402019-06-07 16:39:49 -0700308 continue;
Connor O'Brien57337192018-11-20 12:49:16 -0800309 }
Connor O'Brien1a180402019-06-07 16:39:49 -0700310
311 auto offset = i * FREQS_PER_ENTRY;
312 auto nextOffset = (i + 1) * FREQS_PER_ENTRY;
313 for (uint32_t j = 0; j < gNPolicies; ++j) {
314 if (offset >= gPolicyFreqs[j].size()) continue;
315 auto begin = out[j].begin() + offset;
316 auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY : out[j].end();
317
318 for (const auto &cpu : gPolicyCpus[j]) {
319 std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus<uint64_t>());
Connor O'Brienc92ef102019-07-24 15:42:11 -0700320 }
Connor O'Brien57337192018-11-20 12:49:16 -0800321 }
322 }
323
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700324 return out;
Connor O'Brien57337192018-11-20 12:49:16 -0800325}
326
Connor O'Brien2a716a42020-01-31 18:51:56 -0800327static std::optional<bool> uidUpdatedSince(uint32_t uid, uint64_t lastUpdate,
328 uint64_t *newLastUpdate) {
329 uint64_t uidLastUpdate;
330 if (findMapEntry(gUidLastUpdateMapFd, &uid, &uidLastUpdate)) return {};
331 // Updates that occurred during the previous read may have been missed. To mitigate
332 // this, don't ignore entries updated up to 1s before *lastUpdate
333 constexpr uint64_t NSEC_PER_SEC = 1000000000;
334 if (uidLastUpdate + NSEC_PER_SEC < lastUpdate) return false;
335 if (uidLastUpdate > *newLastUpdate) *newLastUpdate = uidLastUpdate;
336 return true;
337}
338
Connor O'Brien26de80f2019-06-11 13:49:19 -0700339// Retrieve the times in ns that each uid spent running at each CPU freq.
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700340// Return contains no value on error, otherwise it contains a map from uids to vectors of vectors
341// using the format:
Connor O'Brien57337192018-11-20 12:49:16 -0800342// { uid0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...],
343// uid1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... }
344// 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 -0700345std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>>
346getUidsCpuFreqTimes() {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800347 return getUidsUpdatedCpuFreqTimes(nullptr);
348}
349
350// Retrieve the times in ns that each uid spent running at each CPU freq, excluding UIDs that have
351// not run since before lastUpdate.
352// Return format is the same as getUidsCpuFreqTimes()
353std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>>
354getUidsUpdatedCpuFreqTimes(uint64_t *lastUpdate) {
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700355 if (!gInitialized && !initGlobals()) return {};
Connor O'Brien1a180402019-06-07 16:39:49 -0700356 time_key_t key, prevKey;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700357 std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>> map;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700358 if (getFirstMapKey(gTisMapFd, &key)) {
Connor O'Brien1a180402019-06-07 16:39:49 -0700359 if (errno == ENOENT) return map;
360 return std::nullopt;
361 }
362
363 std::vector<std::vector<uint64_t>> mapFormat;
364 for (const auto &freqList : gPolicyFreqs) mapFormat.emplace_back(freqList.size(), 0);
365
Connor O'Brien2a716a42020-01-31 18:51:56 -0800366 uint64_t newLastUpdate = lastUpdate ? *lastUpdate : 0;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700367 std::vector<tis_val_t> vals(gNCpus);
Connor O'Brien1a180402019-06-07 16:39:49 -0700368 do {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800369 if (lastUpdate) {
370 auto uidUpdated = uidUpdatedSince(key.uid, *lastUpdate, &newLastUpdate);
371 if (!uidUpdated.has_value()) return {};
372 if (!*uidUpdated) continue;
373 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700374 if (findMapEntry(gTisMapFd, &key, vals.data())) return {};
Connor O'Brien1a180402019-06-07 16:39:49 -0700375 if (map.find(key.uid) == map.end()) map.emplace(key.uid, mapFormat);
376
377 auto offset = key.bucket * FREQS_PER_ENTRY;
378 auto nextOffset = (key.bucket + 1) * FREQS_PER_ENTRY;
379 for (uint32_t i = 0; i < gNPolicies; ++i) {
380 if (offset >= gPolicyFreqs[i].size()) continue;
381 auto begin = map[key.uid][i].begin() + offset;
382 auto end = nextOffset < gPolicyFreqs[i].size() ? begin + FREQS_PER_ENTRY :
383 map[key.uid][i].end();
384 for (const auto &cpu : gPolicyCpus[i]) {
385 std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus<uint64_t>());
Connor O'Brien57337192018-11-20 12:49:16 -0800386 }
Connor O'Brien57337192018-11-20 12:49:16 -0800387 }
Connor O'Brien1a180402019-06-07 16:39:49 -0700388 prevKey = key;
Connor O'Brien2a716a42020-01-31 18:51:56 -0800389 } while (prevKey = key, !getNextMapKey(gTisMapFd, &prevKey, &key));
Connor O'Brien1a180402019-06-07 16:39:49 -0700390 if (errno != ENOENT) return {};
Connor O'Brien2a716a42020-01-31 18:51:56 -0800391 if (lastUpdate && newLastUpdate > *lastUpdate) *lastUpdate = newLastUpdate;
Connor O'Brien1a180402019-06-07 16:39:49 -0700392 return map;
Connor O'Brien57337192018-11-20 12:49:16 -0800393}
394
Connor O'Brien26de80f2019-06-11 13:49:19 -0700395static bool verifyConcurrentTimes(const concurrent_time_t &ct) {
396 uint64_t activeSum = std::accumulate(ct.active.begin(), ct.active.end(), (uint64_t)0);
397 uint64_t policySum = 0;
398 for (const auto &vec : ct.policy) {
399 policySum += std::accumulate(vec.begin(), vec.end(), (uint64_t)0);
400 }
401 return activeSum == policySum;
402}
403
404// Retrieve the times in ns that uid spent running concurrently with each possible number of other
405// tasks on each cluster (policy times) and overall (active times).
406// Return contains no value on error, otherwise it contains a concurrent_time_t with the format:
407// {.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<concurrent_time_t> getUidConcurrentTimes(uint32_t uid, bool retry) {
411 if (!gInitialized && !initGlobals()) return {};
412 concurrent_time_t ret = {.active = std::vector<uint64_t>(gNCpus, 0)};
413 for (const auto &cpuList : gPolicyCpus) ret.policy.emplace_back(cpuList.size(), 0);
414 std::vector<concurrent_val_t> vals(gNCpus);
Connor O'Briend4b1ec02022-04-26 18:50:26 -0700415 for (uint32_t i = 0; i <= (gNCpus - 1) / CPUS_PER_ENTRY; ++i) {
416 const time_key_t key = {.uid = uid, .bucket = i};
Connor O'Brien26de80f2019-06-11 13:49:19 -0700417 if (findMapEntry(gConcurrentMapFd, &key, vals.data())) {
Connor O'Briend4b1ec02022-04-26 18:50:26 -0700418 time_key_t tmpKey;
419 if (errno != ENOENT || getFirstMapKey(gConcurrentMapFd, &tmpKey)) return {};
Connor O'Brien26de80f2019-06-11 13:49:19 -0700420 continue;
421 }
422 auto offset = key.bucket * CPUS_PER_ENTRY;
423 auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY;
424
425 auto activeBegin = ret.active.begin() + offset;
426 auto activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret.active.end();
427
428 for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) {
429 std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin,
430 std::plus<uint64_t>());
431 }
432
433 for (uint32_t policy = 0; policy < gNPolicies; ++policy) {
434 if (offset >= gPolicyCpus[policy].size()) continue;
435 auto policyBegin = ret.policy[policy].begin() + offset;
436 auto policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY
437 : ret.policy[policy].end();
438
439 for (const auto &cpu : gPolicyCpus[policy]) {
440 std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin,
441 std::plus<uint64_t>());
442 }
443 }
444 }
445 if (!verifyConcurrentTimes(ret) && retry) return getUidConcurrentTimes(uid, false);
446 return ret;
447}
448
449// Retrieve the times in ns that each uid spent running concurrently with each possible number of
450// other tasks on each cluster (policy times) and overall (active times).
451// Return contains no value on error, otherwise it contains a map from uids to concurrent_time_t's
452// using the format:
453// { uid0 -> {.active = [a0, a1, ...], .policy = [[p0_0, p0_1, ...], [p1_0, p1_1, ...], ...] }, ...}
454// where ai is the ns spent running concurrently with tasks on i other cpus and pi_j is the ns spent
455// running on the ith cluster, concurrently with tasks on j other cpus in the same cluster.
456std::optional<std::unordered_map<uint32_t, concurrent_time_t>> getUidsConcurrentTimes() {
Connor O'Brien2a716a42020-01-31 18:51:56 -0800457 return getUidsUpdatedConcurrentTimes(nullptr);
458}
459
460// Retrieve the times in ns that each uid spent running concurrently with each possible number of
461// other tasks on each cluster (policy times) and overall (active times), excluding UIDs that have
462// not run since before lastUpdate.
463// Return format is the same as getUidsConcurrentTimes()
464std::optional<std::unordered_map<uint32_t, concurrent_time_t>> getUidsUpdatedConcurrentTimes(
465 uint64_t *lastUpdate) {
Connor O'Brien26de80f2019-06-11 13:49:19 -0700466 if (!gInitialized && !initGlobals()) return {};
467 time_key_t key, prevKey;
468 std::unordered_map<uint32_t, concurrent_time_t> ret;
469 if (getFirstMapKey(gConcurrentMapFd, &key)) {
470 if (errno == ENOENT) return ret;
471 return {};
472 }
473
474 concurrent_time_t retFormat = {.active = std::vector<uint64_t>(gNCpus, 0)};
475 for (const auto &cpuList : gPolicyCpus) retFormat.policy.emplace_back(cpuList.size(), 0);
476
477 std::vector<concurrent_val_t> vals(gNCpus);
478 std::vector<uint64_t>::iterator activeBegin, activeEnd, policyBegin, policyEnd;
479
Connor O'Brien2a716a42020-01-31 18:51:56 -0800480 uint64_t newLastUpdate = lastUpdate ? *lastUpdate : 0;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700481 do {
Connor O'Brien597f6372020-10-20 14:48:40 -0700482 if (key.bucket > (gNCpus - 1) / CPUS_PER_ENTRY) return {};
Connor O'Brien2a716a42020-01-31 18:51:56 -0800483 if (lastUpdate) {
484 auto uidUpdated = uidUpdatedSince(key.uid, *lastUpdate, &newLastUpdate);
485 if (!uidUpdated.has_value()) return {};
486 if (!*uidUpdated) continue;
487 }
Connor O'Brien26de80f2019-06-11 13:49:19 -0700488 if (findMapEntry(gConcurrentMapFd, &key, vals.data())) return {};
489 if (ret.find(key.uid) == ret.end()) ret.emplace(key.uid, retFormat);
490
491 auto offset = key.bucket * CPUS_PER_ENTRY;
492 auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY;
493
494 activeBegin = ret[key.uid].active.begin();
495 activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret[key.uid].active.end();
496
497 for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) {
498 std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin,
499 std::plus<uint64_t>());
500 }
501
502 for (uint32_t policy = 0; policy < gNPolicies; ++policy) {
503 if (offset >= gPolicyCpus[policy].size()) continue;
504 policyBegin = ret[key.uid].policy[policy].begin() + offset;
505 policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY
506 : ret[key.uid].policy[policy].end();
507
508 for (const auto &cpu : gPolicyCpus[policy]) {
509 std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin,
510 std::plus<uint64_t>());
511 }
512 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800513 } while (prevKey = key, !getNextMapKey(gConcurrentMapFd, &prevKey, &key));
Connor O'Brien26de80f2019-06-11 13:49:19 -0700514 if (errno != ENOENT) return {};
515 for (const auto &[key, value] : ret) {
516 if (!verifyConcurrentTimes(value)) {
517 auto val = getUidConcurrentTimes(key, false);
518 if (val.has_value()) ret[key] = val.value();
519 }
520 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800521 if (lastUpdate && newLastUpdate > *lastUpdate) *lastUpdate = newLastUpdate;
Connor O'Brien26de80f2019-06-11 13:49:19 -0700522 return ret;
523}
524
Connor O'Brien57337192018-11-20 12:49:16 -0800525// Clear all time in state data for a given uid. Returns false on error, true otherwise.
Connor O'Brien26de80f2019-06-11 13:49:19 -0700526// This is only suitable for clearing data when an app is uninstalled; if called on a UID with
527// running tasks it will cause time in state vs. concurrent time totals to be inconsistent for that
528// UID.
529bool clearUidTimes(uint32_t uid) {
Connor O'Brien57337192018-11-20 12:49:16 -0800530 if (!gInitialized && !initGlobals()) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800531
Connor O'Brien1a180402019-06-07 16:39:49 -0700532 time_key_t key = {.uid = uid};
533
534 uint32_t maxFreqCount = 0;
535 for (const auto &freqList : gPolicyFreqs) {
536 if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size();
537 }
538
Connor O'Brien26de80f2019-06-11 13:49:19 -0700539 tis_val_t zeros = {0};
540 std::vector<tis_val_t> vals(gNCpus, zeros);
Connor O'Brien1a180402019-06-07 16:39:49 -0700541 for (key.bucket = 0; key.bucket <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++key.bucket) {
Connor O'Brien26de80f2019-06-11 13:49:19 -0700542 if (writeToMapEntry(gTisMapFd, &key, vals.data(), BPF_EXIST) && errno != ENOENT)
543 return false;
544 if (deleteMapEntry(gTisMapFd, &key) && errno != ENOENT) return false;
545 }
546
Nick Desaulniers54891cd2019-11-19 09:31:05 -0800547 concurrent_val_t czeros = { .active = {0}, .policy = {0}, };
Connor O'Brien26de80f2019-06-11 13:49:19 -0700548 std::vector<concurrent_val_t> cvals(gNCpus, czeros);
549 for (key.bucket = 0; key.bucket <= (gNCpus - 1) / CPUS_PER_ENTRY; ++key.bucket) {
550 if (writeToMapEntry(gConcurrentMapFd, &key, cvals.data(), BPF_EXIST) && errno != ENOENT)
551 return false;
552 if (deleteMapEntry(gConcurrentMapFd, &key) && errno != ENOENT) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800553 }
Connor O'Brien2a716a42020-01-31 18:51:56 -0800554
555 if (deleteMapEntry(gUidLastUpdateMapFd, &uid) && errno != ENOENT) return false;
Connor O'Brien57337192018-11-20 12:49:16 -0800556 return true;
557}
558
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700559bool startTrackingProcessCpuTimes(pid_t pid) {
560 if (!gInitialized && !initGlobals()) return false;
561
562 unique_fd trackedPidHashMapFd(
Ken Chen9c34e712022-07-10 19:05:39 +0800563 mapRetrieveWO(BPF_FS_PATH "map_timeInState_pid_tracked_hash_map"));
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700564 if (trackedPidHashMapFd < 0) return false;
565
Ken Chen9c34e712022-07-10 19:05:39 +0800566 unique_fd trackedPidMapFd(mapRetrieveWO(BPF_FS_PATH "map_timeInState_pid_tracked_map"));
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700567 if (trackedPidMapFd < 0) return false;
568
569 for (uint32_t index = 0; index < MAX_TRACKED_PIDS; index++) {
570 // Find first available [index, pid] entry in the pid_tracked_hash_map map
571 if (writeToMapEntry(trackedPidHashMapFd, &index, &pid, BPF_NOEXIST) != 0) {
572 if (errno != EEXIST) {
573 return false;
574 }
575 continue; // This index is already taken
576 }
577
578 tracked_pid_t tracked_pid = {.pid = pid, .state = TRACKED_PID_STATE_ACTIVE};
579 if (writeToMapEntry(trackedPidMapFd, &index, &tracked_pid, BPF_ANY) != 0) {
580 return false;
581 }
582 return true;
583 }
584 return false;
585}
586
587// Marks the specified task identified by its PID (aka TID) for CPU time-in-state tracking
588// aggregated with other tasks sharing the same TGID and aggregation key.
589bool startAggregatingTaskCpuTimes(pid_t pid, uint16_t aggregationKey) {
590 if (!gInitialized && !initGlobals()) return false;
591
592 unique_fd taskAggregationMapFd(
Ken Chen9c34e712022-07-10 19:05:39 +0800593 mapRetrieveWO(BPF_FS_PATH "map_timeInState_pid_task_aggregation_map"));
Dmitri Plotnikov2677dba2020-10-17 21:06:55 -0700594 if (taskAggregationMapFd < 0) return false;
595
596 return writeToMapEntry(taskAggregationMapFd, &pid, &aggregationKey, BPF_ANY) == 0;
597}
598
599// Retrieves the times in ns that each thread spent running at each CPU freq, aggregated by
600// aggregation key.
601// Return contains no value on error, otherwise it contains a map from aggregation keys
602// to vectors of vectors using the format:
603// { aggKey0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...],
604// aggKey1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... }
605// where ti_j_k is the ns tid i spent running on the jth cluster at the cluster's kth lowest freq.
606std::optional<std::unordered_map<uint16_t, std::vector<std::vector<uint64_t>>>>
607getAggregatedTaskCpuFreqTimes(pid_t tgid, const std::vector<uint16_t> &aggregationKeys) {
608 if (!gInitialized && !initGlobals()) return {};
609
610 uint32_t maxFreqCount = 0;
611 std::vector<std::vector<uint64_t>> mapFormat;
612 for (const auto &freqList : gPolicyFreqs) {
613 if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size();
614 mapFormat.emplace_back(freqList.size(), 0);
615 }
616
617 bool dataCollected = false;
618 std::unordered_map<uint16_t, std::vector<std::vector<uint64_t>>> map;
619 std::vector<tis_val_t> vals(gNCpus);
620 for (uint16_t aggregationKey : aggregationKeys) {
621 map.emplace(aggregationKey, mapFormat);
622
623 aggregated_task_tis_key_t key{.tgid = tgid, .aggregation_key = aggregationKey};
624 for (key.bucket = 0; key.bucket <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++key.bucket) {
625 if (findMapEntry(gPidTisMapFd, &key, vals.data()) != 0) {
626 if (errno != ENOENT) {
627 return {};
628 }
629 continue;
630 } else {
631 dataCollected = true;
632 }
633
634 // Combine data by aggregating time-in-state data grouped by CPU cluster aka policy.
635 uint32_t offset = key.bucket * FREQS_PER_ENTRY;
636 uint32_t nextOffset = offset + FREQS_PER_ENTRY;
637 for (uint32_t j = 0; j < gNPolicies; ++j) {
638 if (offset >= gPolicyFreqs[j].size()) continue;
639 auto begin = map[key.aggregation_key][j].begin() + offset;
640 auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY
641 : map[key.aggregation_key][j].end();
642 for (const auto &cpu : gPolicyCpus[j]) {
643 std::transform(begin, end, std::begin(vals[cpu].ar), begin,
644 std::plus<uint64_t>());
645 }
646 }
647 }
648 }
649
650 if (!dataCollected) {
651 // Check if eBPF is supported on this device. If it is, gTisMap should not be empty.
652 time_key_t key;
653 if (getFirstMapKey(gTisMapFd, &key) != 0) {
654 return {};
655 }
656 }
657 return map;
658}
659
Connor O'Brien57337192018-11-20 12:49:16 -0800660} // namespace bpf
661} // namespace android