Pipe potential wakeup events from sensor service

Sensor events from wakeup sensors can cause CPU wakeups. Exposing an
aidl method to receive notification of such events from sensor service
native code.

Creating a helper method to retreive a Sensor object given a sensor
handle in SensorManager.
These notifications will be used to attribute to a wakeup in
CpuWakeupStats in a subsequent change.

Test: Manually trigger sensor events and check logcat.

Bug: 275436924
Change-Id: Ia16fa43dcd82e9a869db3b510292694a51e2b995
This commit is contained in:
Suprabh Shukla
2023-04-04 03:34:47 -07:00
parent 7b888dcc01
commit 2c31d40dce
4 changed files with 50 additions and 1 deletions

View File

@@ -449,6 +449,27 @@ public abstract class SensorManager {
return list;
}
/**
* Returns the {@link Sensor} object identified by the given sensor handle.
*
* The raw sensor handle integer is an implementation detail and as such this method should only
* be used by internal system components.
*
* @param sensorHandle The integer handle uniquely identifying the sensor.
* @return A Sensor object identified by the given {@code sensorHandle}, if such a sensor
* exists, {@code null} otherwise.
*
* @hide
*/
public @Nullable Sensor getSensorByHandle(int sensorHandle) {
for (final Sensor sensor : getFullSensorList()) {
if (sensor.getHandle() == sensorHandle) {
return sensor;
}
}
return null;
}
/**
* Use this method to get a list of available dynamic sensors of a certain type.
* Make multiple calls to get sensors of different types or use

View File

@@ -220,6 +220,12 @@ public class SystemSensorManager extends SensorManager {
return fullList;
}
/** @hide */
@Override
public Sensor getSensorByHandle(int sensorHandle) {
return mHandleToSensor.get(sensorHandle);
}
/** @hide */
@Override
protected List<Sensor> getFullDynamicSensorList() {

View File

@@ -63,6 +63,7 @@ interface IBatteryStats {
void noteResetCamera();
@EnforcePermission("UPDATE_DEVICE_STATS")
void noteResetFlashlight();
void noteWakeupSensorEvent(long elapsedNanos, int uid, int handle);
// Remaining methods are only used in Java.
@EnforcePermission("BATTERY_STATS")

View File

@@ -37,6 +37,8 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.power.stats.PowerEntity;
import android.hardware.power.stats.State;
import android.hardware.power.stats.StateResidency;
@@ -149,7 +151,6 @@ public final class BatteryStatsService extends IBatteryStats.Stub
private final PowerProfile mPowerProfile;
final BatteryStatsImpl mStats;
@GuardedBy("mWakeupStats")
final CpuWakeupStats mCpuWakeupStats;
private final BatteryUsageStatsStore mBatteryUsageStatsStore;
private final BatteryStatsImpl.UserInfoProvider mUserManagerUserInfoProvider;
@@ -1262,6 +1263,26 @@ public final class BatteryStatsService extends IBatteryStats.Stub
null, sensor, FrameworkStatsLog.SENSOR_STATE_CHANGED__STATE__ON);
}
@Override
public void noteWakeupSensorEvent(long elapsedNanos, int uid, int sensorHandle) {
final int callingUid = Binder.getCallingUid();
if (callingUid != Process.SYSTEM_UID) {
throw new SecurityException("Calling uid " + callingUid + " is not system uid");
}
final SensorManager sm = mContext.getSystemService(SensorManager.class);
final Sensor sensor = sm.getSensorByHandle(sensorHandle);
if (sensor == null) {
Slog.w(TAG, "Unknown sensor handle " + sensorHandle
+ " received in noteWakeupSensorEvent");
return;
}
Slog.i(TAG, "Sensor " + sensor + " wakeup event at " + elapsedNanos + " sent to uid "
+ uid);
// TODO (b/275436924): Remove log and pipe to CpuWakeupStats for wakeup attribution
// This method should return as quickly as possible. Use mHandler#post to do longer work.
}
@Override
@EnforcePermission(UPDATE_DEVICE_STATS)
public void noteStopSensor(final int uid, final int sensor) {