Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ab51dca9 authored by Connor O'Brien's avatar Connor O'Brien
Browse files

libtimeinstate: initialize current freq before starting tracking



Currently, our BPF programs start with no knowledge of current CPU
frequency and tracking cannot begin for a cluster until a frequency
transition happens & makes this information available. On cuttlefish,
CPU frequency never changes, so tracking never starts & cuttlefish
can't be used for testing this feature.
To resolve this, pass the BPF programs initial frequency information
read from the scaling_cur_freq sysfs nodes.

Test: libtimeinstate_test passes on cuttlefish
Test: confirm that uid_time_in_state BPF map contains data on
cuttlefish
Bug: 138317993
Signed-off-by: default avatarConnor O'Brien <connoro@google.com>

Change-Id: I5b24c1311e1b58b73c7e32f41c2d99c5ab344549
parent c6f093ab
Loading
Loading
Loading
Loading
+14 −1
Original line number Original line Diff line number Diff line
@@ -161,6 +161,17 @@ static bool attachTracepointProgram(const std::string &eventType, const std::str
    return bpf_attach_tracepoint(prog_fd, eventType.c_str(), eventName.c_str()) >= 0;
    return bpf_attach_tracepoint(prog_fd, eventType.c_str(), eventName.c_str()) >= 0;
}
}


static std::optional<uint32_t> getPolicyFreqIdx(uint32_t policy) {
    auto path = StringPrintf("/sys/devices/system/cpu/cpufreq/policy%u/scaling_cur_freq",
                             gPolicyCpus[policy][0]);
    auto freqVec = readNumbersFromFile(path);
    if (!freqVec.has_value() || freqVec->size() != 1) return {};
    for (uint32_t idx = 0; idx < gPolicyFreqs[policy].size(); ++idx) {
        if ((*freqVec)[0] == gPolicyFreqs[policy][idx]) return idx + 1;
    }
    return {};
}

// Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes.
// Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes.
// Returns true on success, false otherwise.
// Returns true on success, false otherwise.
// Tracking is active only once a live process has successfully called this function; if the calling
// Tracking is active only once a live process has successfully called this function; if the calling
@@ -215,7 +226,9 @@ bool startTrackingUidTimes() {
    unique_fd policyFreqIdxFd(bpf_obj_get_wronly(BPF_FS_PATH "map_time_in_state_policy_freq_idx_map"));
    unique_fd policyFreqIdxFd(bpf_obj_get_wronly(BPF_FS_PATH "map_time_in_state_policy_freq_idx_map"));
    if (policyFreqIdxFd < 0) return false;
    if (policyFreqIdxFd < 0) return false;
    for (uint32_t i = 0; i < gNPolicies; ++i) {
    for (uint32_t i = 0; i < gNPolicies; ++i) {
        if (writeToMapEntry(policyFreqIdxFd, &i, &zero, BPF_ANY)) return false;
        auto freqIdx = getPolicyFreqIdx(i);
        if (!freqIdx.has_value()) return false;
        if (writeToMapEntry(policyFreqIdxFd, &i, &(*freqIdx), BPF_ANY)) return false;
    }
    }


    gTracking = attachTracepointProgram("sched", "sched_switch") &&
    gTracking = attachTracepointProgram("sched", "sched_switch") &&