summaryrefslogtreecommitdiff
path: root/powerstats/CpupmStateResidencyDataProvider.cpp
blob: 2adae5486d8931037bcc0a88817c8e5e2660b452 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright (C) 2023 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.
 */
#include "CpupmStateResidencyDataProvider.h"

#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/strings.h>

#include <string>
#include <utility>

using android::base::ParseUint;
using android::base::Split;
using android::base::StartsWith;
using android::base::Trim;

namespace aidl {
namespace android {
namespace hardware {
namespace power {
namespace stats {

CpupmStateResidencyDataProvider::CpupmStateResidencyDataProvider(
        const std::string &path,
        const Config &config,
        const std::string &sleepPath,
        const SleepConfig &sleepConfig)
    : mPath(std::move(path)),
      mConfig(std::move(config)),
      mSleepPath(std::move(sleepPath)),
      mSleepConfig(std::move(sleepConfig)) {}

int32_t CpupmStateResidencyDataProvider::matchState(char const *line) {
    for (int32_t i = 0; i < mConfig.states.size(); i++) {
        if (mConfig.states[i].second == Trim(std::string(line))) {
            return i;
        }
    }
    return -1;
}

int32_t CpupmStateResidencyDataProvider::matchEntity(char const *line) {
    for (int32_t i = 0; i < mConfig.entities.size(); i++) {
        if (StartsWith(Trim(std::string(line)), mConfig.entities[i].second)) {
            return i;
        }
    }
    return -1;
}

bool CpupmStateResidencyDataProvider::parseState(
        char const *line, uint64_t *duration, uint64_t *count) {
    std::vector<std::string> parts = Split(line, " ");
    if (parts.size() != 5) {
        return false;
    }
    if (!ParseUint(Trim(parts[1]), count)) {
        return false;
    }
    if (!ParseUint(Trim(parts[3]), duration)) {
        return false;
    }
    return true;
}

bool CpupmStateResidencyDataProvider::getStateResidencies(
        std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
    std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(mPath.c_str(), "r"), fclose);
    if (!fp) {
        PLOG(ERROR) << __func__ << ":Failed to open file " << mPath;
        return false;
    }

    std::unique_ptr<FILE, decltype(&fclose)> sleepFp(fopen(mSleepPath.c_str(), "r"), fclose);
    if (!sleepFp) {
        PLOG(ERROR) << __func__ << ":Failed to open file " << mSleepPath;
        return false;
    }

    for (int32_t i = 0; i < mConfig.entities.size(); i++) {
        std::vector<StateResidency> stateResidencies(mConfig.states.size());
        for (int32_t j = 0; j < stateResidencies.size(); j++) {
            stateResidencies[j].id = j;
        }
        residencies->emplace(mConfig.entities[i].first, stateResidencies);
    }

    size_t len = 0;
    char *line = nullptr;

    int32_t temp, entityIndex, stateId = -1;
    uint64_t duration, count, sleepDurationMs = 0;
    auto it = residencies->end();
    int32_t sleepIndex = 0;

    // Parse state for sleep duration
    while (getline(&line, &len, sleepFp.get()) != -1) {
        std::string trimedLine = Trim(std::string(line));
        if (StartsWith(trimedLine, mSleepConfig[sleepIndex])) {
            if (sleepIndex < mSleepConfig.size() - 1) {
                sleepIndex++;
                continue;
            } else {
                std::vector<std::string> parts = Split(trimedLine, " ");
                if (parts.size() == 2) {
                    ParseUint(parts[1], &sleepDurationMs);
                    sleepDurationMs /= NS_TO_MS;
                }
                break;
            }
        }
    }

    // Parse state for CPUPM entities
    while (getline(&line, &len, fp.get()) != -1) {
        temp = matchState(line);
        // Assign new id only when a new valid state is encountered.
        if (temp >= 0) {
            stateId = temp;
        }

        if (stateId < 0) continue;

        entityIndex = matchEntity(line);

        if (entityIndex < 0) continue;

        it = residencies->find(mConfig.entities[entityIndex].first);
        if (it != residencies->end()) {
            if (parseState(line, &duration, &count)) {
                it->second[stateId].totalTimeInStateMs = duration / US_TO_MS + sleepDurationMs;
                it->second[stateId].totalStateEntryCount = count;
            } else {
                LOG(ERROR) << "Failed to parse duration and count from [" << std::string(line)
                           << "]";
                return false;
            }
        }
    }

    free(line);

    return true;
}

std::unordered_map<std::string, std::vector<State>> CpupmStateResidencyDataProvider::getInfo() {
    std::unordered_map<std::string, std::vector<State>> info;
    for (auto const &entity : mConfig.entities) {
        std::vector<State> stateInfo(mConfig.states.size());
        int32_t stateId = 0;
        for (auto const &state : mConfig.states) {
            stateInfo[stateId] = State{
                .id = stateId,
                .name = state.first
            };
            stateId++;
        }
        info.emplace(entity.first, stateInfo);
    }
    return info;
}

}  // namespace stats
}  // namespace power
}  // namespace hardware
}  // namespace android
}  // namespace aidl