blob: ca427f60f9c28e6cdb7f91a81b3bc7a34fa0bf60 [file] [log] [blame]
Darren Hsu391f6012022-04-27 01:10:12 +08001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "AocTimedStateResidencyDataProvider.h"
18
19#include <android-base/logging.h>
20#include <chrono>
21
22namespace aidl {
23namespace android {
24namespace hardware {
25namespace power {
26namespace stats {
27
28AocTimedStateResidencyDataProvider::AocTimedStateResidencyDataProvider(
29 std::vector<std::pair<std::string, std::string>> ids,
30 std::vector<std::pair<std::string, std::string>> states,
31 const uint64_t timeoutMillis)
32 : AocStateResidencyDataProvider(ids, states) {
33 static const uint64_t DEFAULT_MAX_TIME_PER_STATE_MILLIS = 120;
34
35 mTimeoutMillis =
36 timeoutMillis == 0 ? DEFAULT_MAX_TIME_PER_STATE_MILLIS * states.size() : timeoutMillis;
37
38 mAsyncThread = std::thread(&AocTimedStateResidencyDataProvider::getStateResidenciesAsync, this);
39}
40
41bool AocTimedStateResidencyDataProvider::getStateResidencies(
42 std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
43 bool ret = true;
44 std::unique_lock<std::mutex> statusLock(mStatusMutex);
45
46 if (mAsyncStatus != COMPLETED) {
47 LOG(ERROR) << "The async thread is not ready: " << mAsyncStatus;
48 return false;
49 }
50
51 mStateResidencies.clear();
52
53 mAsyncStatus = RUN;
54 mRunCond.notify_one();
55
56 auto timeout = std::chrono::steady_clock::now() + std::chrono::milliseconds(mTimeoutMillis);
57 auto isCompleted =
58 mCompletedCond.wait_until(statusLock, timeout, [this]{ return mAsyncStatus == COMPLETED; });
59
60 if (isCompleted) {
61 for (const auto &residency : mStateResidencies) {
62 residencies->emplace(residency.first, residency.second);
63 }
64 } else {
65 LOG(ERROR) << __func__ << " for AoC timed out: " << mTimeoutMillis << " ms";
66 ret = false;
67 }
68
69 return ret;
70}
71
72void AocTimedStateResidencyDataProvider::getStateResidenciesAsync() {
73 std::unique_lock<std::mutex> statusLock(mStatusMutex);
74
75 mAsyncStatus = COMPLETED;
76
77 while (1) {
78 mRunCond.wait(statusLock, [this]{ return mAsyncStatus == RUN; });
79
80 mAsyncStatus = RUNNING;
81 statusLock.unlock();
82
83 // States from the same power entity are merged.
84 for (const auto &providerList : mProviders) {
85 int32_t stateId = 0;
86 std::string curEntity = providerList.first;
87 std::vector<StateResidency> stateResidencies;
88
89 // Iterate over each provider in the providerList, appending each of the states
90 for (const auto &provider : providerList.second) {
91 std::unordered_map<std::string, std::vector<StateResidency>> residency;
92 provider->getStateResidencies(&residency);
93
94 // Each provider should only return data for curEntity but checking anyway
95 if (residency.find(curEntity) != residency.end()) {
96 for (auto &r : residency.at(curEntity)) {
97 /*
98 * Modifying stateId here because we are stitching together infos from
99 * multiple GenericStateResidencyDataProviders. stateId must be modified
100 * to maintain uniqueness for a given entity
101 */
102 r.id = stateId++;
103 stateResidencies.push_back(r);
104 }
105 }
106 }
107 mStateResidencies.emplace(curEntity, stateResidencies);
108 }
109
110 statusLock.lock();
111 mAsyncStatus = COMPLETED;
112 mCompletedCond.notify_one();
113 } // while loop
114}
115
116} // namespace stats
117} // namespace power
118} // namespace hardware
119} // namespace android
120} // namespace aidl