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 #include <bpf_timeinstate.h>
21 
22 #include <dirent.h>
23 #include <errno.h>
24 #include <inttypes.h>
25 #include <sys/sysinfo.h>
26 
27 #include <mutex>
28 #include <numeric>
29 #include <optional>
30 #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 
44 using android::base::StringPrintf;
45 using android::base::unique_fd;
46 
47 namespace android {
48 namespace bpf {
49 
50 static std::mutex gInitializedMutex;
51 static bool gInitialized = false;
52 static std::mutex gTrackingMutex;
53 static bool gTracking = false;
54 static uint32_t gNPolicies = 0;
55 static uint32_t gNCpus = 0;
56 static std::vector<std::vector<uint32_t>> gPolicyFreqs;
57 static std::vector<std::vector<uint32_t>> gPolicyCpus;
58 static std::set<uint32_t> gAllFreqs;
59 static unique_fd gTisMapFd;
60 static unique_fd gConcurrentMapFd;
61 static unique_fd gUidLastUpdateMapFd;
62 
readNumbersFromFile(const std::string & path)63 static std::optional<std::vector<uint32_t>> readNumbersFromFile(const std::string &path) {
64     std::string data;
65 
66     if (!android::base::ReadFileToString(path, &data)) return {};
67 
68     auto strings = android::base::Split(data, " \n");
69     std::vector<uint32_t> ret;
70     for (const auto &s : strings) {
71         if (s.empty()) continue;
72         uint32_t n;
73         if (!android::base::ParseUint(s, &n)) return {};
74         ret.emplace_back(n);
75     }
76     return ret;
77 }
78 
isPolicyFile(const struct dirent * d)79 static int isPolicyFile(const struct dirent *d) {
80     return android::base::StartsWith(d->d_name, "policy");
81 }
82 
comparePolicyFiles(const struct dirent ** d1,const struct dirent ** d2)83 static int comparePolicyFiles(const struct dirent **d1, const struct dirent **d2) {
84     uint32_t policyN1, policyN2;
85     if (sscanf((*d1)->d_name, "policy%" SCNu32 "", &policyN1) != 1 ||
86         sscanf((*d2)->d_name, "policy%" SCNu32 "", &policyN2) != 1)
87         return 0;
88     return policyN1 - policyN2;
89 }
90 
initGlobals()91 static bool initGlobals() {
92     std::lock_guard<std::mutex> guard(gInitializedMutex);
93     if (gInitialized) return true;
94 
95     gNCpus = get_nprocs_conf();
96 
97     struct dirent **dirlist;
98     const char basepath[] = "/sys/devices/system/cpu/cpufreq";
99     int ret = scandir(basepath, &dirlist, isPolicyFile, comparePolicyFiles);
100     if (ret == -1) return false;
101     gNPolicies = ret;
102 
103     std::vector<std::string> policyFileNames;
104     for (uint32_t i = 0; i < gNPolicies; ++i) {
105         policyFileNames.emplace_back(dirlist[i]->d_name);
106         free(dirlist[i]);
107     }
108     free(dirlist);
109 
110     for (const auto &policy : policyFileNames) {
111         std::vector<uint32_t> freqs;
112         for (const auto &name : {"available", "boost"}) {
113             std::string path =
114                     StringPrintf("%s/%s/scaling_%s_frequencies", basepath, policy.c_str(), name);
115             auto nums = readNumbersFromFile(path);
116             if (!nums) continue;
117             freqs.insert(freqs.end(), nums->begin(), nums->end());
118         }
119         if (freqs.empty()) return false;
120         std::sort(freqs.begin(), freqs.end());
121         gPolicyFreqs.emplace_back(freqs);
122 
123         for (auto freq : freqs) gAllFreqs.insert(freq);
124 
125         std::string path = StringPrintf("%s/%s/%s", basepath, policy.c_str(), "related_cpus");
126         auto cpus = readNumbersFromFile(path);
127         if (!cpus) return false;
128         gPolicyCpus.emplace_back(*cpus);
129     }
130 
131     gTisMapFd = unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_time_in_state_map")};
132     if (gTisMapFd < 0) return false;
133 
134     gConcurrentMapFd =
135             unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_concurrent_times_map")};
136     if (gConcurrentMapFd < 0) return false;
137 
138     gUidLastUpdateMapFd =
139             unique_fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_last_update_map")};
140     if (gUidLastUpdateMapFd < 0) return false;
141 
142     gInitialized = true;
143     return true;
144 }
145 
attachTracepointProgram(const std::string & eventType,const std::string & eventName)146 static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) {
147     std::string path = StringPrintf(BPF_FS_PATH "prog_time_in_state_tracepoint_%s_%s",
148                                     eventType.c_str(), eventName.c_str());
149     int prog_fd = retrieveProgram(path.c_str());
150     if (prog_fd < 0) return false;
151     return bpf_attach_tracepoint(prog_fd, eventType.c_str(), eventName.c_str()) >= 0;
152 }
153 
getPolicyFreqIdx(uint32_t policy)154 static std::optional<uint32_t> getPolicyFreqIdx(uint32_t policy) {
155     auto path = StringPrintf("/sys/devices/system/cpu/cpufreq/policy%u/scaling_cur_freq",
156                              gPolicyCpus[policy][0]);
157     auto freqVec = readNumbersFromFile(path);
158     if (!freqVec.has_value() || freqVec->size() != 1) return {};
159     for (uint32_t idx = 0; idx < gPolicyFreqs[policy].size(); ++idx) {
160         if ((*freqVec)[0] == gPolicyFreqs[policy][idx]) return idx + 1;
161     }
162     return {};
163 }
164 
165 // Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes.
166 // Returns true on success, false otherwise.
167 // Tracking is active only once a live process has successfully called this function; if the calling
168 // process dies then it must be called again to resume tracking.
169 // This function should *not* be called while tracking is already active; doing so is unnecessary
170 // and can lead to accounting errors.
startTrackingUidTimes()171 bool startTrackingUidTimes() {
172     std::lock_guard<std::mutex> guard(gTrackingMutex);
173     if (!initGlobals()) return false;
174     if (gTracking) return true;
175 
176     unique_fd cpuPolicyFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_cpu_policy_map"));
177     if (cpuPolicyFd < 0) return false;
178 
179     for (uint32_t i = 0; i < gPolicyCpus.size(); ++i) {
180         for (auto &cpu : gPolicyCpus[i]) {
181             if (writeToMapEntry(cpuPolicyFd, &cpu, &i, BPF_ANY)) return false;
182         }
183     }
184 
185     unique_fd freqToIdxFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_freq_to_idx_map"));
186     if (freqToIdxFd < 0) return false;
187     freq_idx_key_t key;
188     for (uint32_t i = 0; i < gNPolicies; ++i) {
189         key.policy = i;
190         for (uint32_t j = 0; j < gPolicyFreqs[i].size(); ++j) {
191             key.freq = gPolicyFreqs[i][j];
192             // Start indexes at 1 so that uninitialized state is distinguishable from lowest freq.
193             // The uid_times map still uses 0-based indexes, and the sched_switch program handles
194             // conversion between them, so this does not affect our map reading code.
195             uint32_t idx = j + 1;
196             if (writeToMapEntry(freqToIdxFd, &key, &idx, BPF_ANY)) return false;
197         }
198     }
199 
200     unique_fd cpuLastUpdateFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_cpu_last_update_map"));
201     if (cpuLastUpdateFd < 0) return false;
202     std::vector<uint64_t> zeros(get_nprocs_conf(), 0);
203     uint32_t zero = 0;
204     if (writeToMapEntry(cpuLastUpdateFd, &zero, zeros.data(), BPF_ANY)) return false;
205 
206     unique_fd nrActiveFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_nr_active_map"));
207     if (nrActiveFd < 0) return false;
208     if (writeToMapEntry(nrActiveFd, &zero, &zero, BPF_ANY)) return false;
209 
210     unique_fd policyNrActiveFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_policy_nr_active_map"));
211     if (policyNrActiveFd < 0) return false;
212     for (uint32_t i = 0; i < gNPolicies; ++i) {
213         if (writeToMapEntry(policyNrActiveFd, &i, &zero, BPF_ANY)) return false;
214     }
215 
216     unique_fd policyFreqIdxFd(mapRetrieveWO(BPF_FS_PATH "map_time_in_state_policy_freq_idx_map"));
217     if (policyFreqIdxFd < 0) return false;
218     for (uint32_t i = 0; i < gNPolicies; ++i) {
219         auto freqIdx = getPolicyFreqIdx(i);
220         if (!freqIdx.has_value()) return false;
221         if (writeToMapEntry(policyFreqIdxFd, &i, &(*freqIdx), BPF_ANY)) return false;
222     }
223 
224     gTracking = attachTracepointProgram("sched", "sched_switch") &&
225             attachTracepointProgram("power", "cpu_frequency");
226     return gTracking;
227 }
228 
getCpuFreqs()229 std::optional<std::vector<std::vector<uint32_t>>> getCpuFreqs() {
230     if (!gInitialized && !initGlobals()) return {};
231     return gPolicyFreqs;
232 }
233 
234 // Retrieve the times in ns that uid spent running at each CPU frequency.
235 // Return contains no value on error, otherwise it contains a vector of vectors using the format:
236 // [[t0_0, t0_1, ...],
237 //  [t1_0, t1_1, ...], ...]
238 // where ti_j is the ns that uid spent running on the ith cluster at that cluster's jth lowest freq.
getUidCpuFreqTimes(uint32_t uid)239 std::optional<std::vector<std::vector<uint64_t>>> getUidCpuFreqTimes(uint32_t uid) {
240     if (!gInitialized && !initGlobals()) return {};
241 
242     std::vector<std::vector<uint64_t>> out;
243     uint32_t maxFreqCount = 0;
244     for (const auto &freqList : gPolicyFreqs) {
245         if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size();
246         out.emplace_back(freqList.size(), 0);
247     }
248 
249     std::vector<tis_val_t> vals(gNCpus);
250     time_key_t key = {.uid = uid};
251     for (uint32_t i = 0; i <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++i) {
252         key.bucket = i;
253         if (findMapEntry(gTisMapFd, &key, vals.data())) {
254             if (errno != ENOENT) return {};
255             continue;
256         }
257 
258         auto offset = i * FREQS_PER_ENTRY;
259         auto nextOffset = (i + 1) * FREQS_PER_ENTRY;
260         for (uint32_t j = 0; j < gNPolicies; ++j) {
261             if (offset >= gPolicyFreqs[j].size()) continue;
262             auto begin = out[j].begin() + offset;
263             auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY : out[j].end();
264 
265             for (const auto &cpu : gPolicyCpus[j]) {
266                 std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus<uint64_t>());
267             }
268         }
269     }
270 
271     return out;
272 }
273 
uidUpdatedSince(uint32_t uid,uint64_t lastUpdate,uint64_t * newLastUpdate)274 static std::optional<bool> uidUpdatedSince(uint32_t uid, uint64_t lastUpdate,
275                                            uint64_t *newLastUpdate) {
276     uint64_t uidLastUpdate;
277     if (findMapEntry(gUidLastUpdateMapFd, &uid, &uidLastUpdate)) return {};
278     // Updates that occurred during the previous read may have been missed. To mitigate
279     // this, don't ignore entries updated up to 1s before *lastUpdate
280     constexpr uint64_t NSEC_PER_SEC = 1000000000;
281     if (uidLastUpdate + NSEC_PER_SEC < lastUpdate) return false;
282     if (uidLastUpdate > *newLastUpdate) *newLastUpdate = uidLastUpdate;
283     return true;
284 }
285 
286 // Retrieve the times in ns that each uid spent running at each CPU freq.
287 // Return contains no value on error, otherwise it contains a map from uids to vectors of vectors
288 // using the format:
289 // { uid0 -> [[t0_0_0, t0_0_1, ...], [t0_1_0, t0_1_1, ...], ...],
290 //   uid1 -> [[t1_0_0, t1_0_1, ...], [t1_1_0, t1_1_1, ...], ...], ... }
291 // where ti_j_k is the ns uid i spent running on the jth cluster at the cluster's kth lowest freq.
292 std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>>
getUidsCpuFreqTimes()293 getUidsCpuFreqTimes() {
294     return getUidsUpdatedCpuFreqTimes(nullptr);
295 }
296 
297 // Retrieve the times in ns that each uid spent running at each CPU freq, excluding UIDs that have
298 // not run since before lastUpdate.
299 // Return format is the same as getUidsCpuFreqTimes()
300 std::optional<std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>>>
getUidsUpdatedCpuFreqTimes(uint64_t * lastUpdate)301 getUidsUpdatedCpuFreqTimes(uint64_t *lastUpdate) {
302     if (!gInitialized && !initGlobals()) return {};
303     time_key_t key, prevKey;
304     std::unordered_map<uint32_t, std::vector<std::vector<uint64_t>>> map;
305     if (getFirstMapKey(gTisMapFd, &key)) {
306         if (errno == ENOENT) return map;
307         return std::nullopt;
308     }
309 
310     std::vector<std::vector<uint64_t>> mapFormat;
311     for (const auto &freqList : gPolicyFreqs) mapFormat.emplace_back(freqList.size(), 0);
312 
313     uint64_t newLastUpdate = lastUpdate ? *lastUpdate : 0;
314     std::vector<tis_val_t> vals(gNCpus);
315     do {
316         if (lastUpdate) {
317             auto uidUpdated = uidUpdatedSince(key.uid, *lastUpdate, &newLastUpdate);
318             if (!uidUpdated.has_value()) return {};
319             if (!*uidUpdated) continue;
320         }
321         if (findMapEntry(gTisMapFd, &key, vals.data())) return {};
322         if (map.find(key.uid) == map.end()) map.emplace(key.uid, mapFormat);
323 
324         auto offset = key.bucket * FREQS_PER_ENTRY;
325         auto nextOffset = (key.bucket + 1) * FREQS_PER_ENTRY;
326         for (uint32_t i = 0; i < gNPolicies; ++i) {
327             if (offset >= gPolicyFreqs[i].size()) continue;
328             auto begin = map[key.uid][i].begin() + offset;
329             auto end = nextOffset < gPolicyFreqs[i].size() ? begin + FREQS_PER_ENTRY :
330                 map[key.uid][i].end();
331             for (const auto &cpu : gPolicyCpus[i]) {
332                 std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus<uint64_t>());
333             }
334         }
335         prevKey = key;
336     } while (prevKey = key, !getNextMapKey(gTisMapFd, &prevKey, &key));
337     if (errno != ENOENT) return {};
338     if (lastUpdate && newLastUpdate > *lastUpdate) *lastUpdate = newLastUpdate;
339     return map;
340 }
341 
verifyConcurrentTimes(const concurrent_time_t & ct)342 static bool verifyConcurrentTimes(const concurrent_time_t &ct) {
343     uint64_t activeSum = std::accumulate(ct.active.begin(), ct.active.end(), (uint64_t)0);
344     uint64_t policySum = 0;
345     for (const auto &vec : ct.policy) {
346         policySum += std::accumulate(vec.begin(), vec.end(), (uint64_t)0);
347     }
348     return activeSum == policySum;
349 }
350 
351 // Retrieve the times in ns that uid spent running concurrently with each possible number of other
352 // tasks on each cluster (policy times) and overall (active times).
353 // Return contains no value on error, otherwise it contains a concurrent_time_t with the format:
354 // {.active = [a0, a1, ...], .policy = [[p0_0, p0_1, ...], [p1_0, p1_1, ...], ...]}
355 // where ai is the ns spent running concurrently with tasks on i other cpus and pi_j is the ns spent
356 // running on the ith cluster, concurrently with tasks on j other cpus in the same cluster
getUidConcurrentTimes(uint32_t uid,bool retry)357 std::optional<concurrent_time_t> getUidConcurrentTimes(uint32_t uid, bool retry) {
358     if (!gInitialized && !initGlobals()) return {};
359     concurrent_time_t ret = {.active = std::vector<uint64_t>(gNCpus, 0)};
360     for (const auto &cpuList : gPolicyCpus) ret.policy.emplace_back(cpuList.size(), 0);
361     std::vector<concurrent_val_t> vals(gNCpus);
362     time_key_t key = {.uid = uid};
363     for (key.bucket = 0; key.bucket <= (gNCpus - 1) / CPUS_PER_ENTRY; ++key.bucket) {
364         if (findMapEntry(gConcurrentMapFd, &key, vals.data())) {
365             if (errno != ENOENT) return {};
366             continue;
367         }
368         auto offset = key.bucket * CPUS_PER_ENTRY;
369         auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY;
370 
371         auto activeBegin = ret.active.begin() + offset;
372         auto activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret.active.end();
373 
374         for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) {
375             std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin,
376                            std::plus<uint64_t>());
377         }
378 
379         for (uint32_t policy = 0; policy < gNPolicies; ++policy) {
380             if (offset >= gPolicyCpus[policy].size()) continue;
381             auto policyBegin = ret.policy[policy].begin() + offset;
382             auto policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY
383                                                                      : ret.policy[policy].end();
384 
385             for (const auto &cpu : gPolicyCpus[policy]) {
386                 std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin,
387                                std::plus<uint64_t>());
388             }
389         }
390     }
391     if (!verifyConcurrentTimes(ret) && retry)  return getUidConcurrentTimes(uid, false);
392     return ret;
393 }
394 
395 // Retrieve the times in ns that each uid spent running concurrently with each possible number of
396 // other tasks on each cluster (policy times) and overall (active times).
397 // Return contains no value on error, otherwise it contains a map from uids to concurrent_time_t's
398 // using the format:
399 // { uid0 -> {.active = [a0, a1, ...], .policy = [[p0_0, p0_1, ...], [p1_0, p1_1, ...], ...] }, ...}
400 // where ai is the ns spent running concurrently with tasks on i other cpus and pi_j is the ns spent
401 // running on the ith cluster, concurrently with tasks on j other cpus in the same cluster.
getUidsConcurrentTimes()402 std::optional<std::unordered_map<uint32_t, concurrent_time_t>> getUidsConcurrentTimes() {
403     return getUidsUpdatedConcurrentTimes(nullptr);
404 }
405 
406 // Retrieve the times in ns that each uid spent running concurrently with each possible number of
407 // other tasks on each cluster (policy times) and overall (active times), excluding UIDs that have
408 // not run since before lastUpdate.
409 // Return format is the same as getUidsConcurrentTimes()
getUidsUpdatedConcurrentTimes(uint64_t * lastUpdate)410 std::optional<std::unordered_map<uint32_t, concurrent_time_t>> getUidsUpdatedConcurrentTimes(
411         uint64_t *lastUpdate) {
412     if (!gInitialized && !initGlobals()) return {};
413     time_key_t key, prevKey;
414     std::unordered_map<uint32_t, concurrent_time_t> ret;
415     if (getFirstMapKey(gConcurrentMapFd, &key)) {
416         if (errno == ENOENT) return ret;
417         return {};
418     }
419 
420     concurrent_time_t retFormat = {.active = std::vector<uint64_t>(gNCpus, 0)};
421     for (const auto &cpuList : gPolicyCpus) retFormat.policy.emplace_back(cpuList.size(), 0);
422 
423     std::vector<concurrent_val_t> vals(gNCpus);
424     std::vector<uint64_t>::iterator activeBegin, activeEnd, policyBegin, policyEnd;
425 
426     uint64_t newLastUpdate = lastUpdate ? *lastUpdate : 0;
427     do {
428         if (lastUpdate) {
429             auto uidUpdated = uidUpdatedSince(key.uid, *lastUpdate, &newLastUpdate);
430             if (!uidUpdated.has_value()) return {};
431             if (!*uidUpdated) continue;
432         }
433         if (findMapEntry(gConcurrentMapFd, &key, vals.data())) return {};
434         if (ret.find(key.uid) == ret.end()) ret.emplace(key.uid, retFormat);
435 
436         auto offset = key.bucket * CPUS_PER_ENTRY;
437         auto nextOffset = (key.bucket + 1) * CPUS_PER_ENTRY;
438 
439         activeBegin = ret[key.uid].active.begin();
440         activeEnd = nextOffset < gNCpus ? activeBegin + CPUS_PER_ENTRY : ret[key.uid].active.end();
441 
442         for (uint32_t cpu = 0; cpu < gNCpus; ++cpu) {
443             std::transform(activeBegin, activeEnd, std::begin(vals[cpu].active), activeBegin,
444                            std::plus<uint64_t>());
445         }
446 
447         for (uint32_t policy = 0; policy < gNPolicies; ++policy) {
448             if (offset >= gPolicyCpus[policy].size()) continue;
449             policyBegin = ret[key.uid].policy[policy].begin() + offset;
450             policyEnd = nextOffset < gPolicyCpus[policy].size() ? policyBegin + CPUS_PER_ENTRY
451                                                                 : ret[key.uid].policy[policy].end();
452 
453             for (const auto &cpu : gPolicyCpus[policy]) {
454                 std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin,
455                                std::plus<uint64_t>());
456             }
457         }
458     } while (prevKey = key, !getNextMapKey(gConcurrentMapFd, &prevKey, &key));
459     if (errno != ENOENT) return {};
460     for (const auto &[key, value] : ret) {
461         if (!verifyConcurrentTimes(value)) {
462             auto val = getUidConcurrentTimes(key, false);
463             if (val.has_value()) ret[key] = val.value();
464         }
465     }
466     if (lastUpdate && newLastUpdate > *lastUpdate) *lastUpdate = newLastUpdate;
467     return ret;
468 }
469 
470 // Clear all time in state data for a given uid. Returns false on error, true otherwise.
471 // This is only suitable for clearing data when an app is uninstalled; if called on a UID with
472 // running tasks it will cause time in state vs. concurrent time totals to be inconsistent for that
473 // UID.
clearUidTimes(uint32_t uid)474 bool clearUidTimes(uint32_t uid) {
475     if (!gInitialized && !initGlobals()) return false;
476 
477     time_key_t key = {.uid = uid};
478 
479     uint32_t maxFreqCount = 0;
480     for (const auto &freqList : gPolicyFreqs) {
481         if (freqList.size() > maxFreqCount) maxFreqCount = freqList.size();
482     }
483 
484     tis_val_t zeros = {0};
485     std::vector<tis_val_t> vals(gNCpus, zeros);
486     for (key.bucket = 0; key.bucket <= (maxFreqCount - 1) / FREQS_PER_ENTRY; ++key.bucket) {
487         if (writeToMapEntry(gTisMapFd, &key, vals.data(), BPF_EXIST) && errno != ENOENT)
488             return false;
489         if (deleteMapEntry(gTisMapFd, &key) && errno != ENOENT) return false;
490     }
491 
492     concurrent_val_t czeros = {.policy = {0}, .active = {0}};
493     std::vector<concurrent_val_t> cvals(gNCpus, czeros);
494     for (key.bucket = 0; key.bucket <= (gNCpus - 1) / CPUS_PER_ENTRY; ++key.bucket) {
495         if (writeToMapEntry(gConcurrentMapFd, &key, cvals.data(), BPF_EXIST) && errno != ENOENT)
496             return false;
497         if (deleteMapEntry(gConcurrentMapFd, &key) && errno != ENOENT) return false;
498     }
499 
500     if (deleteMapEntry(gUidLastUpdateMapFd, &uid) && errno != ENOENT) return false;
501     return true;
502 }
503 
504 } // namespace bpf
505 } // namespace android
506