summaryrefslogtreecommitdiff
path: root/power-libperfmgr/aidl/UClampVoter.cpp
blob: 251f5410217d2996768d45bc5ba4bbdae69f3af6 (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
/*
 * Copyright 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 "UClampVoter.h"

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

static void confine(UclampRange *uclampRange, const VoteRange &vr,
                    std::chrono::steady_clock::time_point t) {
    if (!vr.isTimeInRange(t)) {
        return;
    }
    uclampRange->uclampMin = std::max(uclampRange->uclampMin, vr.uclampMin());
    uclampRange->uclampMax = std::min(uclampRange->uclampMax, vr.uclampMax());
}

VoteRange VoteRange::makeMinRange(int uclampMin, std::chrono::steady_clock::time_point startTime,
                                  std::chrono::nanoseconds durationNs) {
    VoteRange v(true, uclampMin, kUclampMax, startTime, durationNs);
    return v;
}

std::ostream &operator<<(std::ostream &o, const VoteRange &vr) {
    o << "[" << vr.uclampMin() << "," << vr.uclampMax() << "]";
    return o;
}

Votes::Votes() {}

void Votes::add(int voteId, const VoteRange &v) {
    mVotes[voteId] = v;
}

void Votes::updateDuration(int voteId, std::chrono::nanoseconds durationNs) {
    auto voteItr = mVotes.find(voteId);
    if (voteItr != mVotes.end()) {
        voteItr->second.updateDuration(durationNs);
    }
}

void Votes::getUclampRange(UclampRange *uclampRange,
                           std::chrono::steady_clock::time_point t) const {
    if (nullptr == uclampRange) {
        return;
    }
    for (const auto &v : mVotes) {
        confine(uclampRange, v.second, t);
    }
}

bool Votes::anyTimedOut(std::chrono::steady_clock::time_point t) const {
    for (const auto &v : mVotes) {
        if (!v.second.isTimeInRange(t)) {
            return true;
        }
    }
    return false;
}

bool Votes::allTimedOut(std::chrono::steady_clock::time_point t) const {
    for (const auto &v : mVotes) {
        if (v.second.isTimeInRange(t)) {
            return false;
        }
    }
    return true;
}

bool Votes::remove(int voteId) {
    auto itr = mVotes.find(voteId);
    if (itr == mVotes.end()) {
        return false;
    }
    mVotes.erase(itr);
    return true;
}

bool Votes::setUseVote(int voteId, bool active) {
    auto itr = mVotes.find(voteId);
    if (itr == mVotes.end()) {
        return false;
    }
    itr->second.setActive(active);
    return true;
}

size_t Votes::size() const {
    return mVotes.size();
}

bool Votes::voteIsActive(int voteId) {
    auto itr = mVotes.find(voteId);
    if (itr == mVotes.end()) {
        return false;
    }
    return itr->second.active();
}

std::chrono::steady_clock::time_point Votes::voteTimeout(int voteId) {
    auto itr = mVotes.find(voteId);
    if (itr == mVotes.end()) {
        return std::chrono::steady_clock::time_point{};
    }
    return itr->second.startTime() + itr->second.durationNs();
}

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