libtimeinstate: support cpufreq fast switching

In order to support kernels that use fast frequency switching, the
time_in_state BPF program needs to know which CPUs are members of
which cpufreq policies. Add logic to startTrackingUidCpuFreqTimes() to
write this information into a BPF map in order to make it available to
the BPF program.

Test: libtimeinstate_test passes
Change-Id: I47b38457766d21c2aa0879f156fc90757c0db705
Signed-off-by: Connor O'Brien <connoro@google.com>
Merged-In: I47b38457766d21c2aa0879f156fc90757c0db705
diff --git a/libs/cputimeinstate/cputimeinstate.cpp b/libs/cputimeinstate/cputimeinstate.cpp
index a02b285..6f50a1e 100644
--- a/libs/cputimeinstate/cputimeinstate.cpp
+++ b/libs/cputimeinstate/cputimeinstate.cpp
@@ -141,6 +141,17 @@
 // This function should *not* be called while tracking is already active; doing so is unnecessary
 // and can lead to accounting errors.
 bool startTrackingUidCpuFreqTimes() {
+    if (!initGlobals()) return false;
+
+    unique_fd fd(bpf_obj_get(BPF_FS_PATH "map_time_in_state_cpu_policy_map"));
+    if (fd < 0) return false;
+
+    for (uint32_t i = 0; i < gPolicyCpus.size(); ++i) {
+        for (auto &cpu : gPolicyCpus[i]) {
+            if (writeToMapEntry(fd, &cpu, &i, BPF_ANY)) return false;
+        }
+    }
+
     return attachTracepointProgram("sched", "sched_switch") &&
             attachTracepointProgram("power", "cpu_frequency");
 }