From bbd443fd6c2abb3e01dd0d4870a432421012ee54 Mon Sep 17 00:00:00 2001 From: Himanshu Jakhmola Date: Mon, 13 Mar 2023 16:00:47 +0530 Subject: Use cpu_number to index mapping Change applicable when compiling data from vals policywise. Instead of using cpu_number as index to access vals, use cpu_number to index mapping to get index for accessing vals for a cpu. CRs-Fixed: 3413580 Bug: 280947753 Test: CtsStatsdAtomHostTestCases.android.cts.statsdatom.cpu.CpuStatsTests#testCpuCyclesPerUidCluster Change-Id: Id17661c50bda4cd4aecee6c7658e97c718dd4ec4 --- libs/cputimeinstate/cputimeinstate.cpp | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/libs/cputimeinstate/cputimeinstate.cpp b/libs/cputimeinstate/cputimeinstate.cpp index 706704ad34..4a7bd36202 100644 --- a/libs/cputimeinstate/cputimeinstate.cpp +++ b/libs/cputimeinstate/cputimeinstate.cpp @@ -55,6 +55,7 @@ static uint32_t gNPolicies = 0; static uint32_t gNCpus = 0; static std::vector> gPolicyFreqs; static std::vector> gPolicyCpus; +static std::vector gCpuIndexMap; static std::set gAllFreqs; static unique_fd gTisTotalMapFd; static unique_fd gTisMapFd; @@ -108,7 +109,7 @@ static bool initGlobals() { free(dirlist[i]); } free(dirlist); - + uint32_t max_cpu_number = 0; for (const auto &policy : policyFileNames) { std::vector freqs; for (const auto &name : {"available", "boost"}) { @@ -127,8 +128,19 @@ static bool initGlobals() { std::string path = StringPrintf("%s/%s/%s", basepath, policy.c_str(), "related_cpus"); auto cpus = readNumbersFromFile(path); if (!cpus) return false; + for (auto cpu : *cpus) { + if(cpu > max_cpu_number) + max_cpu_number = cpu; + } gPolicyCpus.emplace_back(*cpus); } + gCpuIndexMap = std::vector(max_cpu_number+1, -1); + uint32_t cpuorder = 0; + for (const auto &cpuList : gPolicyCpus) { + for (auto cpu : cpuList) { + gCpuIndexMap[cpu] = cpuorder++; + } + } gTisTotalMapFd = unique_fd{bpf_obj_get(BPF_FS_PATH "map_timeInState_total_time_in_state_map")}; @@ -277,7 +289,7 @@ std::optional>> getTotalCpuFreqTimes() { for (uint32_t policyIdx = 0; policyIdx < gNPolicies; ++policyIdx) { if (freqIdx >= gPolicyFreqs[policyIdx].size()) continue; for (const auto &cpu : gPolicyCpus[policyIdx]) { - out[policyIdx][freqIdx] += vals[cpu]; + out[policyIdx][freqIdx] += vals[gCpuIndexMap[cpu]]; } } } @@ -316,7 +328,8 @@ std::optional>> getUidCpuFreqTimes(uint32_t ui auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY : out[j].end(); for (const auto &cpu : gPolicyCpus[j]) { - std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus()); + std::transform(begin, end, std::begin(vals[gCpuIndexMap[cpu]].ar), begin, + std::plus()); } } } @@ -382,7 +395,8 @@ getUidsUpdatedCpuFreqTimes(uint64_t *lastUpdate) { auto end = nextOffset < gPolicyFreqs[i].size() ? begin + FREQS_PER_ENTRY : map[key.uid][i].end(); for (const auto &cpu : gPolicyCpus[i]) { - std::transform(begin, end, std::begin(vals[cpu].ar), begin, std::plus()); + std::transform(begin, end, std::begin(vals[gCpuIndexMap[cpu]].ar), begin, + std::plus()); } } prevKey = key; @@ -437,8 +451,8 @@ std::optional getUidConcurrentTimes(uint32_t uid, bool retry) : ret.policy[policy].end(); for (const auto &cpu : gPolicyCpus[policy]) { - std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin, - std::plus()); + std::transform(policyBegin, policyEnd, std::begin(vals[gCpuIndexMap[cpu]].policy), + policyBegin, std::plus()); } } } @@ -506,8 +520,8 @@ std::optional> getUidsUpdatedCon : ret[key.uid].policy[policy].end(); for (const auto &cpu : gPolicyCpus[policy]) { - std::transform(policyBegin, policyEnd, std::begin(vals[cpu].policy), policyBegin, - std::plus()); + std::transform(policyBegin, policyEnd, std::begin(vals[gCpuIndexMap[cpu]].policy), + policyBegin, std::plus()); } } } while (prevKey = key, !getNextMapKey(gConcurrentMapFd, &prevKey, &key)); @@ -640,7 +654,7 @@ getAggregatedTaskCpuFreqTimes(pid_t tgid, const std::vector &aggregati auto end = nextOffset < gPolicyFreqs[j].size() ? begin + FREQS_PER_ENTRY : map[key.aggregation_key][j].end(); for (const auto &cpu : gPolicyCpus[j]) { - std::transform(begin, end, std::begin(vals[cpu].ar), begin, + std::transform(begin, end, std::begin(vals[gCpuIndexMap[cpu]].ar), begin, std::plus()); } } -- cgit v1.2.3-59-g8ed1b