Loading cmds/statsd/Android.mk +11 −2 Original line number Diff line number Diff line Loading @@ -29,7 +29,8 @@ statsd_common_src := \ src/config/ConfigKey.cpp \ src/config/ConfigListener.cpp \ src/config/ConfigManager.cpp \ src/external/KernelWakelockPuller.cpp \ src/external/StatsCompanionServicePuller.cpp \ src/external/ResourcePowerManagerPuller.cpp \ src/external/StatsPullerManager.cpp \ src/logd/LogEvent.cpp \ src/logd/LogListener.cpp \ Loading Loading @@ -71,7 +72,15 @@ statsd_common_shared_libraries := \ libutils \ libservices \ libandroidfw \ libprotoutil libprotoutil \ libstatslog \ libhardware \ libhardware_legacy \ libhidlbase \ libhidltransport \ libhwbinder \ android.hardware.power@1.0 \ android.hardware.power@1.1 # ========= # statsd Loading cmds/statsd/src/StatsService.cpp +9 −6 Original line number Diff line number Diff line Loading @@ -373,13 +373,16 @@ status_t StatsService::cmd_print_uid_map(FILE* out) { status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) { int s = atoi(args[1].c_str()); auto stats = m_stats_puller_manager.Pull(s, time(nullptr)); vector<shared_ptr<LogEvent> > stats; if (mStatsPullerManager.Pull(s, &stats)) { for (const auto& it : stats) { fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); } fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); return NO_ERROR; } return UNKNOWN_ERROR; } Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version, const vector<String16>& app) { Loading Loading @@ -440,7 +443,7 @@ Status StatsService::informPollAlarmFired() { "Only system uid can call informPollAlarmFired"); } m_stats_puller_manager.OnAlarmFired(); mStatsPullerManager.OnAlarmFired(); if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded"); Loading cmds/statsd/src/StatsService.h +1 −1 Original line number Diff line number Diff line Loading @@ -157,7 +157,7 @@ private: /** * Fetches external metrics. */ StatsPullerManager& m_stats_puller_manager = StatsPullerManager::GetInstance(); StatsPullerManager& mStatsPullerManager = StatsPullerManager::GetInstance(); /** * Tracks the configurations that have been passed to statsd. Loading cmds/statsd/src/config/ConfigManager.cpp +7 −1 Original line number Diff line number Diff line Loading @@ -139,7 +139,7 @@ static StatsdConfig build_fake_config() { int UID_PROCESS_STATE_TAG_ID = 27; int UID_PROCESS_STATE_UID_KEY = 1; int KERNEL_WAKELOCK_TAG_ID = 41; int KERNEL_WAKELOCK_TAG_ID = 1004; int KERNEL_WAKELOCK_NAME_KEY = 4; // Count Screen ON events. Loading Loading @@ -301,6 +301,12 @@ static StatsdConfig build_fake_config() { keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY); keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE); // pulled events eventMatcher = config.add_log_entry_matcher(); eventMatcher->set_name("KERNEL_WAKELOCK"); simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID); // Conditions............. Condition* condition = config.add_condition(); condition->set_name("SCREEN_IS_ON"); Loading cmds/statsd/src/external/ResourcePowerManagerPuller.cpp 0 → 100644 +165 −0 Original line number Diff line number Diff line /* * Copyright (C) 2017 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 DEBUG true // STOPSHIP if true #include "Log.h" #include <android/hardware/power/1.0/IPower.h> #include <android/hardware/power/1.1/IPower.h> #include <fcntl.h> #include <hardware/power.h> #include <hardware_legacy/power.h> #include <inttypes.h> #include <semaphore.h> #include <stddef.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include "external/ResourcePowerManagerPuller.h" #include "external/StatsPuller.h" #include "logd/LogEvent.h" using android::hardware::hidl_vec; using android::hardware::power::V1_0::IPower; using android::hardware::power::V1_0::PowerStatePlatformSleepState; using android::hardware::power::V1_0::PowerStateVoter; using android::hardware::power::V1_0::Status; using android::hardware::power::V1_1::PowerStateSubsystem; using android::hardware::power::V1_1::PowerStateSubsystemSleepState; using android::hardware::Return; using android::hardware::Void; using std::make_shared; using std::shared_ptr; namespace android { namespace os { namespace statsd { sp<android::hardware::power::V1_0::IPower> gPowerHalV1_0 = nullptr; sp<android::hardware::power::V1_1::IPower> gPowerHalV1_1 = nullptr; std::mutex gPowerHalMutex; bool gPowerHalExists = true; static const int power_state_platform_sleep_state_tag = 1011; static const int power_state_voter_tag = 1012; static const int power_state_subsystem_state_tag = 1013; bool getPowerHal() { if (gPowerHalExists && gPowerHalV1_0 == nullptr) { gPowerHalV1_0 = android::hardware::power::V1_0::IPower::getService(); if (gPowerHalV1_0 != nullptr) { gPowerHalV1_1 = android::hardware::power::V1_1::IPower::castFrom(gPowerHalV1_0); ALOGI("Loaded power HAL service"); } else { ALOGW("Couldn't load power HAL service"); gPowerHalExists = false; } } return gPowerHalV1_0 != nullptr; } bool ResourcePowerManagerPuller::Pull(const int tagId, vector<shared_ptr<LogEvent>>* data) { std::lock_guard<std::mutex> lock(gPowerHalMutex); if (!getPowerHal()) { ALOGE("Power Hal not loaded"); return false; } data->clear(); Return<void> ret = gPowerHalV1_0->getPlatformLowPowerStats( [&data](hidl_vec<PowerStatePlatformSleepState> states, Status status) { if (status != Status::SUCCESS) return; for (size_t i = 0; i < states.size(); i++) { const PowerStatePlatformSleepState& state = states[i]; auto statePtr = make_shared<LogEvent>(power_state_platform_sleep_state_tag); auto elemList = statePtr->GetAndroidLogEventList(); *elemList << state.name; *elemList << state.residencyInMsecSinceBoot; *elemList << state.totalTransitions; *elemList << state.supportedOnlyInSuspend; data->push_back(statePtr); VLOG("powerstate: %s, %lld, %lld, %d", state.name.c_str(), (long long)state.residencyInMsecSinceBoot, (long long)state.totalTransitions, state.supportedOnlyInSuspend ? 1 : 0); for (auto voter : state.voters) { auto voterPtr = make_shared<LogEvent>(power_state_voter_tag); auto elemList = voterPtr->GetAndroidLogEventList(); *elemList << state.name; *elemList << voter.name; *elemList << voter.totalTimeInMsecVotedForSinceBoot; *elemList << voter.totalNumberOfTimesVotedSinceBoot; data->push_back(voterPtr); VLOG("powerstatevoter: %s, %s, %lld, %lld", state.name.c_str(), voter.name.c_str(), (long long)voter.totalTimeInMsecVotedForSinceBoot, (long long)voter.totalNumberOfTimesVotedSinceBoot); } } }); if (!ret.isOk()) { ALOGE("getLowPowerStats() failed: power HAL service not available"); gPowerHalV1_0 = nullptr; return false; } // Trying to cast to IPower 1.1, this will succeed only for devices supporting 1.1 sp<android::hardware::power::V1_1::IPower> gPowerHal_1_1 = android::hardware::power::V1_1::IPower::castFrom(gPowerHalV1_0); if (gPowerHal_1_1 != nullptr) { ret = gPowerHal_1_1->getSubsystemLowPowerStats( [&data](hidl_vec<PowerStateSubsystem> subsystems, Status status) { if (status != Status::SUCCESS) return; if (subsystems.size() > 0) { for (size_t i = 0; i < subsystems.size(); i++) { const PowerStateSubsystem& subsystem = subsystems[i]; for (size_t j = 0; j < subsystem.states.size(); j++) { const PowerStateSubsystemSleepState& state = subsystem.states[j]; auto subsystemStatePtr = make_shared<LogEvent>(power_state_subsystem_state_tag); auto elemList = subsystemStatePtr->GetAndroidLogEventList(); *elemList << subsystem.name; *elemList << state.name; *elemList << state.residencyInMsecSinceBoot; *elemList << state.totalTransitions; *elemList << state.lastEntryTimestampMs; *elemList << state.supportedOnlyInSuspend; data->push_back(subsystemStatePtr); VLOG("subsystemstate: %s, %s, %lld, %lld, %lld", subsystem.name.c_str(), state.name.c_str(), (long long)state.residencyInMsecSinceBoot, (long long)state.totalTransitions, (long long)state.lastEntryTimestampMs); } } } }); } return true; } } // namespace statsd } // namespace os } // namespace android Loading
cmds/statsd/Android.mk +11 −2 Original line number Diff line number Diff line Loading @@ -29,7 +29,8 @@ statsd_common_src := \ src/config/ConfigKey.cpp \ src/config/ConfigListener.cpp \ src/config/ConfigManager.cpp \ src/external/KernelWakelockPuller.cpp \ src/external/StatsCompanionServicePuller.cpp \ src/external/ResourcePowerManagerPuller.cpp \ src/external/StatsPullerManager.cpp \ src/logd/LogEvent.cpp \ src/logd/LogListener.cpp \ Loading Loading @@ -71,7 +72,15 @@ statsd_common_shared_libraries := \ libutils \ libservices \ libandroidfw \ libprotoutil libprotoutil \ libstatslog \ libhardware \ libhardware_legacy \ libhidlbase \ libhidltransport \ libhwbinder \ android.hardware.power@1.0 \ android.hardware.power@1.1 # ========= # statsd Loading
cmds/statsd/src/StatsService.cpp +9 −6 Original line number Diff line number Diff line Loading @@ -373,13 +373,16 @@ status_t StatsService::cmd_print_uid_map(FILE* out) { status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) { int s = atoi(args[1].c_str()); auto stats = m_stats_puller_manager.Pull(s, time(nullptr)); vector<shared_ptr<LogEvent> > stats; if (mStatsPullerManager.Pull(s, &stats)) { for (const auto& it : stats) { fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); } fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); return NO_ERROR; } return UNKNOWN_ERROR; } Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version, const vector<String16>& app) { Loading Loading @@ -440,7 +443,7 @@ Status StatsService::informPollAlarmFired() { "Only system uid can call informPollAlarmFired"); } m_stats_puller_manager.OnAlarmFired(); mStatsPullerManager.OnAlarmFired(); if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded"); Loading
cmds/statsd/src/StatsService.h +1 −1 Original line number Diff line number Diff line Loading @@ -157,7 +157,7 @@ private: /** * Fetches external metrics. */ StatsPullerManager& m_stats_puller_manager = StatsPullerManager::GetInstance(); StatsPullerManager& mStatsPullerManager = StatsPullerManager::GetInstance(); /** * Tracks the configurations that have been passed to statsd. Loading
cmds/statsd/src/config/ConfigManager.cpp +7 −1 Original line number Diff line number Diff line Loading @@ -139,7 +139,7 @@ static StatsdConfig build_fake_config() { int UID_PROCESS_STATE_TAG_ID = 27; int UID_PROCESS_STATE_UID_KEY = 1; int KERNEL_WAKELOCK_TAG_ID = 41; int KERNEL_WAKELOCK_TAG_ID = 1004; int KERNEL_WAKELOCK_NAME_KEY = 4; // Count Screen ON events. Loading Loading @@ -301,6 +301,12 @@ static StatsdConfig build_fake_config() { keyValueMatcher->mutable_key_matcher()->set_key(WAKE_LOCK_STATE_KEY); keyValueMatcher->set_eq_int(WAKE_LOCK_RELEASE_VALUE); // pulled events eventMatcher = config.add_log_entry_matcher(); eventMatcher->set_name("KERNEL_WAKELOCK"); simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); simpleLogEntryMatcher->set_tag(KERNEL_WAKELOCK_TAG_ID); // Conditions............. Condition* condition = config.add_condition(); condition->set_name("SCREEN_IS_ON"); Loading
cmds/statsd/src/external/ResourcePowerManagerPuller.cpp 0 → 100644 +165 −0 Original line number Diff line number Diff line /* * Copyright (C) 2017 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 DEBUG true // STOPSHIP if true #include "Log.h" #include <android/hardware/power/1.0/IPower.h> #include <android/hardware/power/1.1/IPower.h> #include <fcntl.h> #include <hardware/power.h> #include <hardware_legacy/power.h> #include <inttypes.h> #include <semaphore.h> #include <stddef.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include "external/ResourcePowerManagerPuller.h" #include "external/StatsPuller.h" #include "logd/LogEvent.h" using android::hardware::hidl_vec; using android::hardware::power::V1_0::IPower; using android::hardware::power::V1_0::PowerStatePlatformSleepState; using android::hardware::power::V1_0::PowerStateVoter; using android::hardware::power::V1_0::Status; using android::hardware::power::V1_1::PowerStateSubsystem; using android::hardware::power::V1_1::PowerStateSubsystemSleepState; using android::hardware::Return; using android::hardware::Void; using std::make_shared; using std::shared_ptr; namespace android { namespace os { namespace statsd { sp<android::hardware::power::V1_0::IPower> gPowerHalV1_0 = nullptr; sp<android::hardware::power::V1_1::IPower> gPowerHalV1_1 = nullptr; std::mutex gPowerHalMutex; bool gPowerHalExists = true; static const int power_state_platform_sleep_state_tag = 1011; static const int power_state_voter_tag = 1012; static const int power_state_subsystem_state_tag = 1013; bool getPowerHal() { if (gPowerHalExists && gPowerHalV1_0 == nullptr) { gPowerHalV1_0 = android::hardware::power::V1_0::IPower::getService(); if (gPowerHalV1_0 != nullptr) { gPowerHalV1_1 = android::hardware::power::V1_1::IPower::castFrom(gPowerHalV1_0); ALOGI("Loaded power HAL service"); } else { ALOGW("Couldn't load power HAL service"); gPowerHalExists = false; } } return gPowerHalV1_0 != nullptr; } bool ResourcePowerManagerPuller::Pull(const int tagId, vector<shared_ptr<LogEvent>>* data) { std::lock_guard<std::mutex> lock(gPowerHalMutex); if (!getPowerHal()) { ALOGE("Power Hal not loaded"); return false; } data->clear(); Return<void> ret = gPowerHalV1_0->getPlatformLowPowerStats( [&data](hidl_vec<PowerStatePlatformSleepState> states, Status status) { if (status != Status::SUCCESS) return; for (size_t i = 0; i < states.size(); i++) { const PowerStatePlatformSleepState& state = states[i]; auto statePtr = make_shared<LogEvent>(power_state_platform_sleep_state_tag); auto elemList = statePtr->GetAndroidLogEventList(); *elemList << state.name; *elemList << state.residencyInMsecSinceBoot; *elemList << state.totalTransitions; *elemList << state.supportedOnlyInSuspend; data->push_back(statePtr); VLOG("powerstate: %s, %lld, %lld, %d", state.name.c_str(), (long long)state.residencyInMsecSinceBoot, (long long)state.totalTransitions, state.supportedOnlyInSuspend ? 1 : 0); for (auto voter : state.voters) { auto voterPtr = make_shared<LogEvent>(power_state_voter_tag); auto elemList = voterPtr->GetAndroidLogEventList(); *elemList << state.name; *elemList << voter.name; *elemList << voter.totalTimeInMsecVotedForSinceBoot; *elemList << voter.totalNumberOfTimesVotedSinceBoot; data->push_back(voterPtr); VLOG("powerstatevoter: %s, %s, %lld, %lld", state.name.c_str(), voter.name.c_str(), (long long)voter.totalTimeInMsecVotedForSinceBoot, (long long)voter.totalNumberOfTimesVotedSinceBoot); } } }); if (!ret.isOk()) { ALOGE("getLowPowerStats() failed: power HAL service not available"); gPowerHalV1_0 = nullptr; return false; } // Trying to cast to IPower 1.1, this will succeed only for devices supporting 1.1 sp<android::hardware::power::V1_1::IPower> gPowerHal_1_1 = android::hardware::power::V1_1::IPower::castFrom(gPowerHalV1_0); if (gPowerHal_1_1 != nullptr) { ret = gPowerHal_1_1->getSubsystemLowPowerStats( [&data](hidl_vec<PowerStateSubsystem> subsystems, Status status) { if (status != Status::SUCCESS) return; if (subsystems.size() > 0) { for (size_t i = 0; i < subsystems.size(); i++) { const PowerStateSubsystem& subsystem = subsystems[i]; for (size_t j = 0; j < subsystem.states.size(); j++) { const PowerStateSubsystemSleepState& state = subsystem.states[j]; auto subsystemStatePtr = make_shared<LogEvent>(power_state_subsystem_state_tag); auto elemList = subsystemStatePtr->GetAndroidLogEventList(); *elemList << subsystem.name; *elemList << state.name; *elemList << state.residencyInMsecSinceBoot; *elemList << state.totalTransitions; *elemList << state.lastEntryTimestampMs; *elemList << state.supportedOnlyInSuspend; data->push_back(subsystemStatePtr); VLOG("subsystemstate: %s, %s, %lld, %lld, %lld", subsystem.name.c_str(), state.name.c_str(), (long long)state.residencyInMsecSinceBoot, (long long)state.totalTransitions, (long long)state.lastEntryTimestampMs); } } } }); } return true; } } // namespace statsd } // namespace os } // namespace android