| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 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 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 Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 36 | #include "ResourcePowerManagerPuller.h" |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 37 | #include "logd/LogEvent.h" |
| Chenjie Yu | e33bc3b | 2017-11-06 17:56:44 -0800 | [diff] [blame] | 38 | #include "statslog.h" |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 39 | |
| 40 | using android::hardware::hidl_vec; |
| 41 | using android::hardware::power::V1_0::IPower; |
| 42 | using android::hardware::power::V1_0::PowerStatePlatformSleepState; |
| 43 | using android::hardware::power::V1_0::PowerStateVoter; |
| 44 | using android::hardware::power::V1_0::Status; |
| 45 | using android::hardware::power::V1_1::PowerStateSubsystem; |
| 46 | using android::hardware::power::V1_1::PowerStateSubsystemSleepState; |
| 47 | using android::hardware::Return; |
| 48 | using android::hardware::Void; |
| 49 | |
| 50 | using std::make_shared; |
| 51 | using std::shared_ptr; |
| 52 | |
| 53 | namespace android { |
| 54 | namespace os { |
| 55 | namespace statsd { |
| 56 | |
| 57 | sp<android::hardware::power::V1_0::IPower> gPowerHalV1_0 = nullptr; |
| 58 | sp<android::hardware::power::V1_1::IPower> gPowerHalV1_1 = nullptr; |
| 59 | std::mutex gPowerHalMutex; |
| 60 | bool gPowerHalExists = true; |
| 61 | |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 62 | bool 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 Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 76 | ResourcePowerManagerPuller::ResourcePowerManagerPuller(int tagId) : StatsPuller(tagId) { |
| 77 | } |
| 78 | |
| 79 | bool ResourcePowerManagerPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) { |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 80 | std::lock_guard<std::mutex> lock(gPowerHalMutex); |
| 81 | |
| 82 | if (!getPowerHal()) { |
| 83 | ALOGE("Power Hal not loaded"); |
| 84 | return false; |
| 85 | } |
| 86 | |
| Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 87 | uint64_t timestamp = time(nullptr) * NS_PER_SEC; |
| 88 | |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 89 | data->clear(); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 90 | |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 91 | 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 Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 96 | if (status != Status::SUCCESS) return; |
| 97 | |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 98 | 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 Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | }); |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 129 | 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 Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | } // namespace statsd |
| 176 | } // namespace os |
| 177 | } // namespace android |