summaryrefslogtreecommitdiff
path: root/powerstats/dataproviders/PixelStateResidencyDataProvider.cpp
blob: 3e2673973fbbe644281dee032c9b4ca1ea4ef76d (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
/*
 * Copyright (C) 2021 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 "android.hardware.power.stats-service.pixel"

#include <dataproviders/PixelStateResidencyDataProvider.h>

#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android/binder_status.h>

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

PixelStateResidencyDataProvider::PixelStateResidencyDataProvider() {}

void PixelStateResidencyDataProvider::addEntity(std::string name, std::vector<State> states) {
    std::lock_guard<std::mutex> lock(mLock);

    mEntries.emplace_back(name, states);
}

void PixelStateResidencyDataProvider::start() {
    binder_status_t status = AServiceManager_addService(this->asBinder().get(), kInstance.c_str());
    if (status != STATUS_OK) {
        LOG(ERROR) << "Failed to start " << kInstance;
    }
}

::ndk::ScopedAStatus PixelStateResidencyDataProvider::getStateResidenciesTimed(
        const Entry &entry, std::vector<StateResidency> *residency) {
    const uint64_t MAX_LATENCY_US = 2000;

    if (!entry.mCallback) {
        LOG(ERROR) << "callback for " << entry.mName << " is not registered";
        return ndk::ScopedAStatus::fromStatus(STATUS_UNEXPECTED_NULL);
    }

    struct timespec then;
    struct timespec now;

    clock_gettime(CLOCK_BOOTTIME, &then);
    ::ndk::ScopedAStatus status = entry.mCallback->getStateResidency(residency);
    clock_gettime(CLOCK_BOOTTIME, &now);

    uint64_t timeElapsedUs =
            ((now.tv_sec - then.tv_sec) * 1000000) + ((now.tv_nsec - then.tv_nsec) / 1000);
    if (timeElapsedUs > MAX_LATENCY_US) {
        LOG(WARNING) << "getStateResidency latency for " << entry.mName
                     << " exceeded time allowed: " << timeElapsedUs << "us";
    }

    return status;
}

bool PixelStateResidencyDataProvider::getStateResidencies(
        std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
    std::lock_guard<std::mutex> lock(mLock);

    size_t numResultsFound = 0;
    size_t numResults = mEntries.size();
    for (auto &entry : mEntries) {
        std::vector<StateResidency> residency;
        ::ndk::ScopedAStatus status = getStateResidenciesTimed(entry, &residency);

        if (!status.isOk()) {
            LOG(ERROR) << "getStateResidency for " << entry.mName << " failed";

            if (status.getStatus() == STATUS_DEAD_OBJECT) {
                LOG(ERROR) << "Unregistering dead callback for " << entry.mName;
                entry.mCallback = nullptr;
            }
        }
        if (!residency.empty()) {
            residencies->emplace(entry.mName, residency);
            numResultsFound++;
        }
    }

    return (numResultsFound == numResults);
}

std::unordered_map<std::string, std::vector<State>> PixelStateResidencyDataProvider::getInfo() {
    std::lock_guard<std::mutex> lock(mLock);

    std::unordered_map<std::string, std::vector<State>> ret;
    for (const auto &entry : mEntries) {
        ret.emplace(entry.mName, entry.mStates);
    }

    return ret;
}

::ndk::ScopedAStatus PixelStateResidencyDataProvider::registerCallback(
        const std::string &in_entityName,
        const std::shared_ptr<IPixelStateResidencyCallback> &in_cb) {
    std::lock_guard<std::mutex> lock(mLock);

    if (!in_cb) {
        return ndk::ScopedAStatus::fromStatus(STATUS_UNEXPECTED_NULL);
    }

    auto toRegister =
            std::find_if(mEntries.begin(), mEntries.end(),
                         [&in_entityName](const auto &it) { return it.mName == in_entityName; });

    if (toRegister == mEntries.end()) {
        LOG(ERROR) << __func__ << " Invalid entityName: " << in_entityName;
        return ::ndk::ScopedAStatus::fromStatus(STATUS_BAD_VALUE);
    }

    toRegister->mCallback = in_cb;

    LOG(INFO) << __func__ << ": Registered " << in_entityName;
    return ::ndk::ScopedAStatus::ok();
}

::ndk::ScopedAStatus PixelStateResidencyDataProvider::unregisterCallback(
        const std::shared_ptr<IPixelStateResidencyCallback> &in_cb) {
    std::lock_guard<std::mutex> lock(mLock);

    if (!in_cb) {
        return ndk::ScopedAStatus::fromStatus(STATUS_UNEXPECTED_NULL);
    }

    auto toRemove = std::find_if(mEntries.begin(), mEntries.end(), [&in_cb](const auto &it) {
        return it.mCallback->asBinder().get() == in_cb->asBinder().get();
    });

    if (toRemove == mEntries.end()) {
        return ndk::ScopedAStatus::fromStatus(STATUS_BAD_VALUE);
    }

    toRemove->mCallback = nullptr;

    return ::ndk::ScopedAStatus::ok();
}

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