blob: 2e29fb04abfafd3d66ab02e64e2f467f5f909430 [file] [log] [blame]
Chenjie Yu5305e1d2017-10-31 13:49:36 -07001/*
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 true // 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/ResourcePowerManagerPuller.h"
34#include "external/StatsPuller.h"
35
Chenjie Yub038b702017-12-18 15:15:34 -080036#include "ResourcePowerManagerPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070037#include "logd/LogEvent.h"
Chenjie Yue33bc3b2017-11-06 17:56:44 -080038#include "statslog.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070039
40using android::hardware::hidl_vec;
41using android::hardware::power::V1_0::IPower;
42using android::hardware::power::V1_0::PowerStatePlatformSleepState;
43using android::hardware::power::V1_0::PowerStateVoter;
44using android::hardware::power::V1_0::Status;
45using android::hardware::power::V1_1::PowerStateSubsystem;
46using android::hardware::power::V1_1::PowerStateSubsystemSleepState;
47using android::hardware::Return;
48using android::hardware::Void;
49
50using std::make_shared;
51using std::shared_ptr;
52
53namespace android {
54namespace os {
55namespace statsd {
56
57sp<android::hardware::power::V1_0::IPower> gPowerHalV1_0 = nullptr;
58sp<android::hardware::power::V1_1::IPower> gPowerHalV1_1 = nullptr;
59std::mutex gPowerHalMutex;
60bool gPowerHalExists = true;
61
Chenjie Yu5305e1d2017-10-31 13:49:36 -070062bool getPowerHal() {
63 if (gPowerHalExists && gPowerHalV1_0 == nullptr) {
64 gPowerHalV1_0 = android::hardware::power::V1_0::IPower::getService();
65 if (gPowerHalV1_0 != nullptr) {
66 gPowerHalV1_1 = android::hardware::power::V1_1::IPower::castFrom(gPowerHalV1_0);
67 ALOGI("Loaded power HAL service");
68 } else {
69 ALOGW("Couldn't load power HAL service");
70 gPowerHalExists = false;
71 }
72 }
73 return gPowerHalV1_0 != nullptr;
74}
75
Chenjie Yub038b702017-12-18 15:15:34 -080076ResourcePowerManagerPuller::ResourcePowerManagerPuller(int tagId) : StatsPuller(tagId) {
77}
78
79bool ResourcePowerManagerPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -070080 std::lock_guard<std::mutex> lock(gPowerHalMutex);
81
82 if (!getPowerHal()) {
83 ALOGE("Power Hal not loaded");
84 return false;
85 }
86
Yao Chen93fe3a32017-11-02 13:52:59 -070087 uint64_t timestamp = time(nullptr) * NS_PER_SEC;
88
Chenjie Yu5305e1d2017-10-31 13:49:36 -070089 data->clear();
Chenjie Yu5305e1d2017-10-31 13:49:36 -070090
Chenjie Yub038b702017-12-18 15:15:34 -080091 Return<void> ret;
92 if (mTagId == android::util::PLATFORM_SLEEP_STATE ||
93 mTagId == android::util::SLEEP_STATE_VOTER) {
94 ret = gPowerHalV1_0->getPlatformLowPowerStats(
95 [&data, timestamp](hidl_vec<PowerStatePlatformSleepState> states, Status status) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -070096 if (status != Status::SUCCESS) return;
97
Chenjie Yub038b702017-12-18 15:15:34 -080098 for (size_t i = 0; i < states.size(); i++) {
99 const PowerStatePlatformSleepState& state = states[i];
100
101 auto statePtr = make_shared<LogEvent>(android::util::PLATFORM_SLEEP_STATE,
102 timestamp);
103 statePtr->write(state.name);
104 statePtr->write(state.residencyInMsecSinceBoot);
105 statePtr->write(state.totalTransitions);
106 statePtr->write(state.supportedOnlyInSuspend);
107 statePtr->init();
108 data->push_back(statePtr);
109 VLOG("powerstate: %s, %lld, %lld, %d", state.name.c_str(),
110 (long long)state.residencyInMsecSinceBoot,
111 (long long)state.totalTransitions,
112 state.supportedOnlyInSuspend ? 1 : 0);
113 for (auto voter : state.voters) {
114 auto voterPtr = make_shared<LogEvent>(android::util::SLEEP_STATE_VOTER,
115 timestamp);
116 voterPtr->write(state.name);
117 voterPtr->write(voter.name);
118 voterPtr->write(voter.totalTimeInMsecVotedForSinceBoot);
119 voterPtr->write(voter.totalNumberOfTimesVotedSinceBoot);
120 voterPtr->init();
121 data->push_back(voterPtr);
122 VLOG("powerstatevoter: %s, %s, %lld, %lld", state.name.c_str(),
123 voter.name.c_str(),
124 (long long)voter.totalTimeInMsecVotedForSinceBoot,
125 (long long)voter.totalNumberOfTimesVotedSinceBoot);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700126 }
127 }
128 });
Chenjie Yub038b702017-12-18 15:15:34 -0800129 if (!ret.isOk()) {
130 ALOGE("getLowPowerStats() failed: power HAL service not available");
131 gPowerHalV1_0 = nullptr;
132 return false;
133 }
134 }
135
136 if (mTagId == android::util::SUBSYSTEM_SLEEP_STATE) {
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, timestamp](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, timestamp);
153 subsystemStatePtr->write(subsystem.name);
154 subsystemStatePtr->write(state.name);
155 subsystemStatePtr->write(state.residencyInMsecSinceBoot);
156 subsystemStatePtr->write(state.totalTransitions);
157 subsystemStatePtr->write(state.lastEntryTimestampMs);
158 subsystemStatePtr->write(state.supportedOnlyInSuspend);
159 subsystemStatePtr->init();
160 data->push_back(subsystemStatePtr);
161 VLOG("subsystemstate: %s, %s, %lld, %lld, %lld",
162 subsystem.name.c_str(), state.name.c_str(),
163 (long long)state.residencyInMsecSinceBoot,
164 (long long)state.totalTransitions,
165 (long long)state.lastEntryTimestampMs);
166 }
167 }
168 }
169 });
170 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700171 }
172 return true;
173}
174
175} // namespace statsd
176} // namespace os
177} // namespace android