summaryrefslogtreecommitdiff
path: root/power-libperfmgr/adaptivecpu/KernelCpuFeatureReader.cpp
blob: 087f03d91918d34499be6fc483b0157087aaa18d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "powerhal-adaptivecpu"
#define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)

#include "KernelCpuFeatureReader.h"

#include <android-base/logging.h>
#include <utils/Trace.h>

#include <ostream>

namespace aidl {
namespace google {
namespace hardware {
namespace power {
namespace impl {
namespace pixel {

constexpr std::string_view kKernelFilePath("/proc/vendor_sched/acpu_stats");
constexpr size_t kReadBufferSize = sizeof(acpu_stats) * NUM_CPU_CORES;

bool KernelCpuFeatureReader::Init() {
    ATRACE_CALL();
    if (!OpenStatsFile(&mStatsFile)) {
        return false;
    }
    return ReadStats(&mPreviousStats, &mPreviousReadTime);
}

bool KernelCpuFeatureReader::GetRecentCpuFeatures(
        std::array<double, NUM_CPU_POLICIES> *cpuPolicyAverageFrequencyHz,
        std::array<double, NUM_CPU_CORES> *cpuCoreIdleTimesPercentage) {
    ATRACE_CALL();
    std::array<acpu_stats, NUM_CPU_CORES> stats;
    std::chrono::nanoseconds readTime;
    if (!ReadStats(&stats, &readTime)) {
        return false;
    }
    const std::chrono::nanoseconds timeDelta = readTime - mPreviousReadTime;

    for (size_t i = 0; i < NUM_CPU_POLICIES; i++) {
        // acpu_stats has data per-CPU, but frequency data is equivalent for all CPUs in a policy.
        // So, we only read the first CPU in each policy.
        const size_t statsIdx = CPU_POLICY_INDICES[i];
        if (stats[statsIdx].weighted_sum_freq < mPreviousStats[statsIdx].weighted_sum_freq) {
            LOG(WARNING) << "New weighted_sum_freq is less than old: new="
                         << stats[statsIdx].weighted_sum_freq
                         << ", old=" << mPreviousStats[statsIdx].weighted_sum_freq;
            mPreviousStats[statsIdx].weighted_sum_freq = stats[statsIdx].weighted_sum_freq;
        }
        (*cpuPolicyAverageFrequencyHz)[i] =
                static_cast<double>(stats[statsIdx].weighted_sum_freq -
                                    mPreviousStats[statsIdx].weighted_sum_freq) /
                timeDelta.count();
    }
    for (size_t i = 0; i < NUM_CPU_CORES; i++) {
        if (stats[i].total_idle_time_ns < mPreviousStats[i].total_idle_time_ns) {
            LOG(WARNING) << "New total_idle_time_ns is less than old: new="
                         << stats[i].total_idle_time_ns
                         << ", old=" << mPreviousStats[i].total_idle_time_ns;
            mPreviousStats[i].total_idle_time_ns = stats[i].total_idle_time_ns;
        }
        (*cpuCoreIdleTimesPercentage)[i] =
                static_cast<double>(stats[i].total_idle_time_ns -
                                    mPreviousStats[i].total_idle_time_ns) /
                timeDelta.count();
    }

    mPreviousStats = stats;
    mPreviousReadTime = readTime;
    return true;
}

bool KernelCpuFeatureReader::OpenStatsFile(std::unique_ptr<std::istream> *file) {
    ATRACE_CALL();
    return mFilesystem->ReadFileStream(kKernelFilePath.data(), file);
}

bool KernelCpuFeatureReader::ReadStats(std::array<acpu_stats, NUM_CPU_CORES> *stats,
                                       std::chrono::nanoseconds *readTime) {
    ATRACE_CALL();
    *readTime = mTimeSource->GetKernelTime();
    if (!mFilesystem->ResetFileStream(mStatsFile)) {
        return false;
    }
    char buffer[kReadBufferSize];
    {
        ATRACE_NAME("read");
        if (!mStatsFile->read(buffer, kReadBufferSize).good()) {
            LOG(ERROR) << "Failed to read stats file";
            return false;
        }
    }
    const size_t bytesRead = mStatsFile->gcount();
    if (bytesRead != kReadBufferSize) {
        LOG(ERROR) << "Didn't read full data: expected=" << kReadBufferSize
                   << ", actual=" << bytesRead;
        return false;
    }
    const auto kernelStructs = reinterpret_cast<acpu_stats *>(buffer);
    std::copy(kernelStructs, kernelStructs + NUM_CPU_CORES, stats->begin());
    return true;
}

void KernelCpuFeatureReader::DumpToStream(std::ostream &stream) const {
    ATRACE_CALL();
    stream << "CPU features from acpu_stats:\n";
    for (size_t i = 0; i < NUM_CPU_CORES; i++) {
        stream << "- CPU " << i << ": weighted_sum_freq=" << mPreviousStats[i].weighted_sum_freq
               << ", total_idle_time_ns=" << mPreviousStats[i].total_idle_time_ns << "\n";
    }
    stream << "- Last read time: " << mPreviousReadTime.count() << "ns\n";
}

}  // namespace pixel
}  // namespace impl
}  // namespace power
}  // namespace hardware
}  // namespace google
}  // namespace aidl