Migrate HealthHal to StatsPullAtomService

Bug: 148618655

Test: atest HostAtomTests
Test: adb shell cmd stats pull-source 10019
Test: adb shell cmd stats pull-source 10020
Test: adb shell cmd stats pull-source 10030
Test: adb shell cmd stats pull-source 10045
Change-Id: I10a0682828b086751b90c3cc133fb51c34cbea23
This commit is contained in:
Jeffrey Huang
2020-01-30 14:03:31 -08:00
parent 9a3c1b6e1b
commit 43f4d26d41
6 changed files with 77 additions and 184 deletions

View File

@@ -69,7 +69,6 @@ cc_defaults {
"src/external/PowerStatsPuller.cpp",
"src/external/PullResultReceiver.cpp",
"src/external/puller_util.cpp",
"src/external/ResourceHealthManagerPuller.cpp",
"src/external/StatsCallbackPuller.cpp",
"src/external/StatsPuller.cpp",
"src/external/StatsPullerManager.cpp",
@@ -127,7 +126,6 @@ cc_defaults {
"android.hardware.power@1.1",
"libbase",
"libcutils",
"libhealthhalutils",
"liblog",
"libplatformprotos",
"libprotoutil",
@@ -136,7 +134,6 @@ cc_defaults {
"libsysutils",
],
shared_libs: [
"android.hardware.health@2.0",
"libbinder",
"libgraphicsenv",
"libhidlbase",

View File

@@ -411,8 +411,8 @@ message Atom {
CpuActiveTime cpu_active_time = 10016 [(module) = "framework"];
CpuClusterTime cpu_cluster_time = 10017 [(module) = "framework"];
DiskSpace disk_space = 10018 [deprecated=true];
RemainingBatteryCapacity remaining_battery_capacity = 10019;
FullBatteryCapacity full_battery_capacity = 10020;
RemainingBatteryCapacity remaining_battery_capacity = 10019 [(module) = "framework"];
FullBatteryCapacity full_battery_capacity = 10020 [(module) = "framework"];
Temperature temperature = 10021 [(module) = "framework"];
BinderCalls binder_calls = 10022 [(module) = "framework"];
BinderCallsExceptions binder_calls_exceptions = 10023 [(module) = "framework"];
@@ -422,7 +422,7 @@ message Atom {
AppSize app_size = 10027 [(module) = "framework"];
CategorySize category_size = 10028 [(module) = "framework"];
ProcStats proc_stats = 10029 [(module) = "framework"];
BatteryVoltage battery_voltage = 10030;
BatteryVoltage battery_voltage = 10030 [(module) = "framework"];
NumFingerprintsEnrolled num_fingerprints_enrolled = 10031 [(module) = "framework"];
DiskIo disk_io = 10032 [(module) = "framework"];
PowerProfile power_profile = 10033 [(module) = "framework"];
@@ -438,7 +438,7 @@ message Atom {
ProcessMemoryHighWaterMark process_memory_high_water_mark = 10042 [(module) = "framework"];
BatteryLevel battery_level = 10043 [(module) = "framework"];
BuildInformation build_information = 10044 [(module) = "framework"];
BatteryCycleCount battery_cycle_count = 10045;
BatteryCycleCount battery_cycle_count = 10045 [(module) = "framework"];
DebugElapsedClock debug_elapsed_clock = 10046 [(module) = "framework"];
DebugFailingElapsedClock debug_failing_elapsed_clock = 10047 [(module) = "framework"];
NumFacesEnrolled num_faces_enrolled = 10048 [(module) = "framework"];

View File

@@ -1,117 +0,0 @@
/*
* Copyright (C) 2018 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 false // STOPSHIP if true
#include "Log.h"
#include <android/hardware/health/2.0/IHealth.h>
#include <healthhalutils/HealthHalUtils.h>
#include "external/ResourceHealthManagerPuller.h"
#include "external/StatsPuller.h"
#include "ResourceHealthManagerPuller.h"
#include "logd/LogEvent.h"
#include "stats_log_util.h"
#include "statslog.h"
using android::hardware::hidl_vec;
using android::hardware::Return;
using android::hardware::Void;
using android::hardware::health::V2_0::get_health_service;
using android::hardware::health::V2_0::HealthInfo;
using android::hardware::health::V2_0::IHealth;
using android::hardware::health::V2_0::Result;
using std::make_shared;
using std::shared_ptr;
namespace android {
namespace os {
namespace statsd {
sp<android::hardware::health::V2_0::IHealth> gHealthHal = nullptr;
bool getHealthHal() {
if (gHealthHal == nullptr) {
gHealthHal = get_health_service();
}
return gHealthHal != nullptr;
}
ResourceHealthManagerPuller::ResourceHealthManagerPuller(int tagId) : StatsPuller(tagId) {
}
// TODO(b/110565992): add other health atoms (eg. Temperature).
bool ResourceHealthManagerPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
if (!getHealthHal()) {
ALOGE("Health Hal not loaded");
return false;
}
int64_t wallClockTimestampNs = getWallClockNs();
int64_t elapsedTimestampNs = getElapsedRealtimeNs();
data->clear();
bool result_success = true;
// Get the data from the Health HAL (hardware/interfaces/health/1.0/types.hal).
Return<void> ret = gHealthHal->getHealthInfo([&](Result r, HealthInfo v) {
if (r != Result::SUCCESS) {
result_success = false;
return;
}
if (mTagId == android::util::REMAINING_BATTERY_CAPACITY) {
auto ptr = make_shared<LogEvent>(android::util::REMAINING_BATTERY_CAPACITY,
wallClockTimestampNs, elapsedTimestampNs);
ptr->write(v.legacy.batteryChargeCounter);
ptr->init();
data->push_back(ptr);
} else if (mTagId == android::util::FULL_BATTERY_CAPACITY) {
auto ptr = make_shared<LogEvent>(android::util::FULL_BATTERY_CAPACITY,
wallClockTimestampNs, elapsedTimestampNs);
ptr->write(v.legacy.batteryFullCharge);
ptr->init();
data->push_back(ptr);
} else if (mTagId == android::util::BATTERY_VOLTAGE) {
auto ptr = make_shared<LogEvent>(android::util::BATTERY_VOLTAGE, wallClockTimestampNs,
elapsedTimestampNs);
ptr->write(v.legacy.batteryVoltage);
ptr->init();
data->push_back(ptr);
} else if (mTagId == android::util::BATTERY_CYCLE_COUNT) {
auto ptr = make_shared<LogEvent>(android::util::BATTERY_CYCLE_COUNT,
wallClockTimestampNs, elapsedTimestampNs);
ptr->write(v.legacy.batteryCycleCount);
ptr->init();
data->push_back(ptr);
} else {
ALOGE("Unsupported tag in ResourceHealthManagerPuller: %d", mTagId);
}
});
if (!result_success || !ret.isOk()) {
ALOGE("getHealthHal() failed: health HAL service not available. Description: %s",
ret.description().c_str());
if (!ret.isOk() && ret.isDeadObject()) {
gHealthHal = nullptr;
}
return false;
}
return true;
}
} // namespace statsd
} // namespace os
} // namespace android

View File

@@ -1,39 +0,0 @@
/*
* Copyright (C) 2018 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.
*/
#pragma once
#include <utils/String16.h>
#include "StatsPuller.h"
namespace android {
namespace os {
namespace statsd {
/**
* Reads Ihealth.hal
*/
class ResourceHealthManagerPuller : public StatsPuller {
public:
explicit ResourceHealthManagerPuller(int tagId);
private:
bool PullInternal(vector<std::shared_ptr<LogEvent>>* data) override;
};
} // namespace statsd
} // namespace os
} // namespace android

View File

@@ -34,7 +34,6 @@
#include "../statscompanion_util.h"
#include "GpuStatsPuller.h"
#include "PowerStatsPuller.h"
#include "ResourceHealthManagerPuller.h"
#include "StatsCallbackPuller.h"
#include "SubsystemSleepStatePuller.h"
#include "TrainInfoPuller.h"
@@ -62,22 +61,6 @@ StatsPullerManager::StatsPullerManager()
// on_device_power_measurement
{{.atomTag = android::util::ON_DEVICE_POWER_MEASUREMENT}, new PowerStatsPuller()},
// remaining_battery_capacity
{{.atomTag = android::util::REMAINING_BATTERY_CAPACITY},
new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)},
// full_battery_capacity
{{.atomTag = android::util::FULL_BATTERY_CAPACITY},
new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)},
// battery_voltage
{{.atomTag = android::util::BATTERY_VOLTAGE},
new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)},
// battery_cycle_count
{{.atomTag = android::util::BATTERY_CYCLE_COUNT},
new ResourceHealthManagerPuller(android::util::BATTERY_CYCLE_COUNT)},
// TrainInfo.
{{.atomTag = android::util::TRAIN_INFO}, new TrainInfoPuller()},

View File

@@ -378,7 +378,11 @@ public class StatsPullAtomService extends SystemService {
case FrameworkStatsLog.DANGEROUS_PERMISSION_STATE_SAMPLED:
return pullDangerousPermissionState(atomTag, data);
case FrameworkStatsLog.BATTERY_LEVEL:
return pullBatteryLevel(atomTag, data);
case FrameworkStatsLog.REMAINING_BATTERY_CAPACITY:
case FrameworkStatsLog.FULL_BATTERY_CAPACITY:
case FrameworkStatsLog.BATTERY_VOLTAGE:
case FrameworkStatsLog.BATTERY_CYCLE_COUNT:
return pullHealthHal(atomTag, data);
default:
throw new UnsupportedOperationException("Unknown tagId=" + atomTag);
}
@@ -534,6 +538,10 @@ public class StatsPullAtomService extends SystemService {
registerDangerousPermissionState();
registerDangerousPermissionStateSampled();
registerBatteryLevel();
registerRemainingBatteryCapacity();
registerFullBatteryCapacity();
registerBatteryVoltage();
registerBatteryCycleCount();
}
private INetworkStatsService getINetworkStatsService() {
@@ -2986,20 +2994,81 @@ public class StatsPullAtomService extends SystemService {
);
}
int pullBatteryLevel(int atomTag, List<StatsEvent> pulledData) {
private void registerRemainingBatteryCapacity() {
int tagId = FrameworkStatsLog.REMAINING_BATTERY_CAPACITY;
mStatsManager.registerPullAtomCallback(
tagId,
null, // use default PullAtomMetadata values
BackgroundThread.getExecutor(),
mStatsCallbackImpl
);
}
private void registerFullBatteryCapacity() {
int tagId = FrameworkStatsLog.FULL_BATTERY_CAPACITY;
mStatsManager.registerPullAtomCallback(
tagId,
null, // use default PullAtomMetadata values
BackgroundThread.getExecutor(),
mStatsCallbackImpl
);
}
private void registerBatteryVoltage() {
int tagId = FrameworkStatsLog.BATTERY_VOLTAGE;
mStatsManager.registerPullAtomCallback(
tagId,
null, // use default PullAtomMetadata values
BackgroundThread.getExecutor(),
mStatsCallbackImpl
);
}
private void registerBatteryCycleCount() {
int tagId = FrameworkStatsLog.BATTERY_CYCLE_COUNT;
mStatsManager.registerPullAtomCallback(
tagId,
null, // use default PullAtomMetadata values
BackgroundThread.getExecutor(),
mStatsCallbackImpl
);
}
int pullHealthHal(int atomTag, List<StatsEvent> pulledData) {
IHealth healthService = mHealthService.getLastService();
if (healthService == null) {
return StatsManager.PULL_SKIP;
}
try {
healthService.getHealthInfo((result, value) -> {
int pulledValue;
switch(atomTag) {
case FrameworkStatsLog.BATTERY_LEVEL:
pulledValue = value.legacy.batteryLevel;
break;
case FrameworkStatsLog.REMAINING_BATTERY_CAPACITY:
pulledValue = value.legacy.batteryChargeCounter;
break;
case FrameworkStatsLog.FULL_BATTERY_CAPACITY:
pulledValue = value.legacy.batteryFullCharge;
break;
case FrameworkStatsLog.BATTERY_VOLTAGE:
pulledValue = value.legacy.batteryVoltage;
break;
case FrameworkStatsLog.BATTERY_CYCLE_COUNT:
pulledValue = value.legacy.batteryCycleCount;
break;
default:
throw new IllegalStateException("Invalid atomTag in healthHal puller: "
+ atomTag);
}
StatsEvent e = StatsEvent.newBuilder()
.setAtomId(atomTag)
.writeInt(value.legacy.batteryLevel)
.writeInt(pulledValue)
.build();
pulledData.add(e);
});
} catch (RemoteException e) {
} catch (RemoteException | IllegalStateException e) {
return StatsManager.PULL_SKIP;
}
return StatsManager.PULL_SUCCESS;