1 /*
2 * Copyright (C) 2017 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 #define DEBUG false // STOPSHIP if true
18 #include "Log.h"
19
20 #include <android/hardware/power/1.0/IPower.h>
21 #include <android/hardware/power/1.1/IPower.h>
22 #include <fcntl.h>
23 #include <hardware/power.h>
24 #include <hardware_legacy/power.h>
25 #include <inttypes.h>
26 #include <semaphore.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include "external/SubsystemSleepStatePuller.h"
34 #include "external/StatsPuller.h"
35
36 #include "SubsystemSleepStatePuller.h"
37 #include "logd/LogEvent.h"
38 #include "statslog.h"
39 #include "stats_log_util.h"
40
41 using android::hardware::hidl_vec;
42 using android::hardware::power::V1_0::IPower;
43 using android::hardware::power::V1_0::PowerStatePlatformSleepState;
44 using android::hardware::power::V1_0::PowerStateVoter;
45 using android::hardware::power::V1_0::Status;
46 using android::hardware::power::V1_1::PowerStateSubsystem;
47 using android::hardware::power::V1_1::PowerStateSubsystemSleepState;
48 using android::hardware::Return;
49 using android::hardware::Void;
50
51 using std::make_shared;
52 using std::shared_ptr;
53
54 namespace android {
55 namespace os {
56 namespace statsd {
57
58 sp<android::hardware::power::V1_0::IPower> gPowerHalV1_0 = nullptr;
59 sp<android::hardware::power::V1_1::IPower> gPowerHalV1_1 = nullptr;
60 std::mutex gPowerHalMutex;
61 bool gPowerHalExists = true;
62
getPowerHal()63 bool getPowerHal() {
64 if (gPowerHalExists && gPowerHalV1_0 == nullptr) {
65 gPowerHalV1_0 = android::hardware::power::V1_0::IPower::getService();
66 if (gPowerHalV1_0 != nullptr) {
67 gPowerHalV1_1 = android::hardware::power::V1_1::IPower::castFrom(gPowerHalV1_0);
68 ALOGI("Loaded power HAL service");
69 } else {
70 ALOGW("Couldn't load power HAL service");
71 gPowerHalExists = false;
72 }
73 }
74 return gPowerHalV1_0 != nullptr;
75 }
76
SubsystemSleepStatePuller()77 SubsystemSleepStatePuller::SubsystemSleepStatePuller() : StatsPuller(android::util::SUBSYSTEM_SLEEP_STATE) {
78 }
79
PullInternal(vector<shared_ptr<LogEvent>> * data)80 bool SubsystemSleepStatePuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
81 std::lock_guard<std::mutex> lock(gPowerHalMutex);
82
83 if (!getPowerHal()) {
84 ALOGE("Power Hal not loaded");
85 return false;
86 }
87
88 int64_t wallClockTimestampNs = getWallClockNs();
89 int64_t elapsedTimestampNs = getElapsedRealtimeNs();
90
91 data->clear();
92
93 Return<void> ret;
94 ret = gPowerHalV1_0->getPlatformLowPowerStats(
95 [&data, wallClockTimestampNs, elapsedTimestampNs](hidl_vec<PowerStatePlatformSleepState> states, Status status) {
96 if (status != Status::SUCCESS) return;
97
98 for (size_t i = 0; i < states.size(); i++) {
99 const PowerStatePlatformSleepState& state = states[i];
100
101 auto statePtr = make_shared<LogEvent>(
102 android::util::SUBSYSTEM_SLEEP_STATE,
103 wallClockTimestampNs, elapsedTimestampNs);
104 statePtr->write(state.name);
105 statePtr->write("");
106 statePtr->write(state.totalTransitions);
107 statePtr->write(state.residencyInMsecSinceBoot);
108 statePtr->init();
109 data->push_back(statePtr);
110 VLOG("powerstate: %s, %lld, %lld, %d", state.name.c_str(),
111 (long long)state.residencyInMsecSinceBoot,
112 (long long)state.totalTransitions,
113 state.supportedOnlyInSuspend ? 1 : 0);
114 for (auto voter : state.voters) {
115 auto voterPtr = make_shared<LogEvent>(
116 android::util::SUBSYSTEM_SLEEP_STATE,
117 wallClockTimestampNs, elapsedTimestampNs);
118 voterPtr->write(state.name);
119 voterPtr->write(voter.name);
120 voterPtr->write(voter.totalNumberOfTimesVotedSinceBoot);
121 voterPtr->write(voter.totalTimeInMsecVotedForSinceBoot);
122 voterPtr->init();
123 data->push_back(voterPtr);
124 VLOG("powerstatevoter: %s, %s, %lld, %lld", state.name.c_str(),
125 voter.name.c_str(),
126 (long long)voter.totalTimeInMsecVotedForSinceBoot,
127 (long long)voter.totalNumberOfTimesVotedSinceBoot);
128 }
129 }
130 });
131 if (!ret.isOk()) {
132 ALOGE("getLowPowerStats() failed: power HAL service not available");
133 gPowerHalV1_0 = nullptr;
134 return false;
135 }
136
137 // Trying to cast to IPower 1.1, this will succeed only for devices supporting 1.1
138 sp<android::hardware::power::V1_1::IPower> gPowerHal_1_1 =
139 android::hardware::power::V1_1::IPower::castFrom(gPowerHalV1_0);
140 if (gPowerHal_1_1 != nullptr) {
141 ret = gPowerHal_1_1->getSubsystemLowPowerStats(
142 [&data, wallClockTimestampNs, elapsedTimestampNs](hidl_vec<PowerStateSubsystem> subsystems, Status status) {
143 if (status != Status::SUCCESS) return;
144
145 if (subsystems.size() > 0) {
146 for (size_t i = 0; i < subsystems.size(); i++) {
147 const PowerStateSubsystem& subsystem = subsystems[i];
148 for (size_t j = 0; j < subsystem.states.size(); j++) {
149 const PowerStateSubsystemSleepState& state =
150 subsystem.states[j];
151 auto subsystemStatePtr = make_shared<LogEvent>(
152 android::util::SUBSYSTEM_SLEEP_STATE,
153 wallClockTimestampNs, elapsedTimestampNs);
154 subsystemStatePtr->write(subsystem.name);
155 subsystemStatePtr->write(state.name);
156 subsystemStatePtr->write(state.totalTransitions);
157 subsystemStatePtr->write(state.residencyInMsecSinceBoot);
158 subsystemStatePtr->init();
159 data->push_back(subsystemStatePtr);
160 VLOG("subsystemstate: %s, %s, %lld, %lld, %lld",
161 subsystem.name.c_str(), state.name.c_str(),
162 (long long)state.residencyInMsecSinceBoot,
163 (long long)state.totalTransitions,
164 (long long)state.lastEntryTimestampMs);
165 }
166 }
167 }
168 });
169 }
170 return true;
171 }
172
173 } // namespace statsd
174 } // namespace os
175 } // namespace android
176