Loading libs/cputimeinstate/cputimeinstate.cpp +17 −2 Original line number Diff line number Diff line Loading @@ -155,10 +155,14 @@ static bool initGlobals() { return true; } static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) { static int retrieveProgramFd(const std::string &eventType, const std::string &eventName) { std::string path = StringPrintf(BPF_FS_PATH "prog_time_in_state_tracepoint_%s_%s", eventType.c_str(), eventName.c_str()); int prog_fd = retrieveProgram(path.c_str()); return retrieveProgram(path.c_str()); } static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) { int prog_fd = retrieveProgramFd(eventType, eventName); if (prog_fd < 0) return false; return bpf_attach_tracepoint(prog_fd, eventType.c_str(), eventName.c_str()) >= 0; } Loading @@ -174,6 +178,17 @@ static std::optional<uint32_t> getPolicyFreqIdx(uint32_t policy) { return {}; } // Check if tracking is expected to work without activating it. bool isTrackingUidTimesSupported() { auto freqs = getCpuFreqs(); if (!freqs || freqs->empty()) return false; if (gTracking) return true; if (retrieveProgramFd("sched", "sched_switch") < 0) return false; if (retrieveProgramFd("power", "cpu_frequency") < 0) return false; if (retrieveProgramFd("sched", "sched_process_free") < 0) return false; return true; } // Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes. // Returns true on success, false otherwise. // Tracking is active only once a live process has successfully called this function; if the calling Loading libs/cputimeinstate/cputimeinstate.h +1 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ namespace android { namespace bpf { bool isTrackingUidTimesSupported(); bool startTrackingUidTimes(); std::optional<std::vector<std::vector<uint64_t>>> getTotalCpuFreqTimes(); std::optional<std::vector<std::vector<uint64_t>>> getUidCpuFreqTimes(uint32_t uid); Loading libs/cputimeinstate/testtimeinstate.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -40,6 +40,11 @@ static constexpr uint64_t NSEC_PER_YEAR = NSEC_PER_SEC * 60 * 60 * 24 * 365; using std::vector; TEST(TimeInStateTest, IsTrackingSupported) { isTrackingUidTimesSupported(); SUCCEED(); } TEST(TimeInStateTest, TotalTimeInState) { auto times = getTotalCpuFreqTimes(); ASSERT_TRUE(times.has_value()); Loading Loading
libs/cputimeinstate/cputimeinstate.cpp +17 −2 Original line number Diff line number Diff line Loading @@ -155,10 +155,14 @@ static bool initGlobals() { return true; } static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) { static int retrieveProgramFd(const std::string &eventType, const std::string &eventName) { std::string path = StringPrintf(BPF_FS_PATH "prog_time_in_state_tracepoint_%s_%s", eventType.c_str(), eventName.c_str()); int prog_fd = retrieveProgram(path.c_str()); return retrieveProgram(path.c_str()); } static bool attachTracepointProgram(const std::string &eventType, const std::string &eventName) { int prog_fd = retrieveProgramFd(eventType, eventName); if (prog_fd < 0) return false; return bpf_attach_tracepoint(prog_fd, eventType.c_str(), eventName.c_str()) >= 0; } Loading @@ -174,6 +178,17 @@ static std::optional<uint32_t> getPolicyFreqIdx(uint32_t policy) { return {}; } // Check if tracking is expected to work without activating it. bool isTrackingUidTimesSupported() { auto freqs = getCpuFreqs(); if (!freqs || freqs->empty()) return false; if (gTracking) return true; if (retrieveProgramFd("sched", "sched_switch") < 0) return false; if (retrieveProgramFd("power", "cpu_frequency") < 0) return false; if (retrieveProgramFd("sched", "sched_process_free") < 0) return false; return true; } // Start tracking and aggregating data to be reported by getUidCpuFreqTimes and getUidsCpuFreqTimes. // Returns true on success, false otherwise. // Tracking is active only once a live process has successfully called this function; if the calling Loading
libs/cputimeinstate/cputimeinstate.h +1 −0 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ namespace android { namespace bpf { bool isTrackingUidTimesSupported(); bool startTrackingUidTimes(); std::optional<std::vector<std::vector<uint64_t>>> getTotalCpuFreqTimes(); std::optional<std::vector<std::vector<uint64_t>>> getUidCpuFreqTimes(uint32_t uid); Loading
libs/cputimeinstate/testtimeinstate.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -40,6 +40,11 @@ static constexpr uint64_t NSEC_PER_YEAR = NSEC_PER_SEC * 60 * 60 * 24 * 365; using std::vector; TEST(TimeInStateTest, IsTrackingSupported) { isTrackingUidTimesSupported(); SUCCEED(); } TEST(TimeInStateTest, TotalTimeInState) { auto times = getTotalCpuFreqTimes(); ASSERT_TRUE(times.has_value()); Loading