Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3dbc13a9 authored by Bookatz's avatar Bookatz
Browse files

Statsd: change power units from double to int64

Changes the units for the following atoms from mAh to nAs:

DeviceCalculatedPowerUse
DeviceCalculatedPowerBlameUid
DeviceCalculatedPowerBlameOther

and changes them from floats to int64s.

int64 is better supported in statsd. In particular, Anomaly Detection
currently works only for int64, not floats.

The loss in precision here should be minimal or none. These numbers come
from BatteryStats, which calculates them as follows:

BatteryStats reports time in microseconds (us) as a long.
PowerProfile reports current in mA as a double.
  On the power_profile.xml files I have seen, they only have three
  decimal places of precision. So uA precision should suffice.

Thus, the absolute smallest unit of charge BatteryStats can therefore
measure (assuming three digits in the PowerProfile) is nAs. Given that our
measurements are over much longer periods of time than a microsecond, we
should be very safe.

In terms of max value: a phone battery is typically around
4000mAh  ~ 10^13 nAs << max_int64 by many orders of magnitude.

Bug: 119111972

Test: cts-tradefed run cts-dev -m CtsStatsdHostTestCases -t android.cts.statsd.atom.UidAtomTests#testDeviceCalculatedPowerUse
Test: cts-tradefed run cts-dev -m CtsStatsdHostTestCases -t android.cts.statsd.atom.UidAtomTests#testDeviceCalculatedPowerBlameUid

Test: cts-tradefed run cts-dev -m CtsStatsdHostTestCases -t android.cts.statsd.validation.BatteryStatsValidationTests#testPowerUse
Test: cts-tradefed run cts-dev -m CtsStatsdHostTestCases -t android.cts.statsd.validation.BatteryStatsValidationTests#testPowerBlameUid

Change-Id: I1cfd0a05717d7d357b43dd2408c85096599516c7
parent aa63fe56
Loading
Loading
Loading
Loading
+10 −10
Original line number Original line Diff line number Diff line
@@ -3474,10 +3474,10 @@ message BuildInformation {
 * Pulls on-device BatteryStats power use calculations for the overall device.
 * Pulls on-device BatteryStats power use calculations for the overall device.
 */
 */
message DeviceCalculatedPowerUse {
message DeviceCalculatedPowerUse {
    // Power used by the device in mAh, as computed by BatteryStats, since BatteryStats last reset
    // Power used by the device in nAs (i.e. nanocoulombs (nC)), as computed by BatteryStats, since
    // (i.e. roughly since device was last significantly charged).
    // BatteryStats last reset (i.e. roughly since device was last significantly charged).
    // Currently, this is BatteryStatsHelper.getComputedPower() (not getTotalPower()).
    // Currently, this is from BatteryStatsHelper.getComputedPower() (not getTotalPower()).
    optional float computed_power_milli_amp_hours = 1;
    optional int64 computed_power_nano_amp_secs = 1;
}
}


/**
/**
@@ -3489,9 +3489,9 @@ message DeviceCalculatedPowerBlameUid {
    // Uid being blamed. Note: isolated uids have already been mapped to host uid.
    // Uid being blamed. Note: isolated uids have already been mapped to host uid.
    optional int32 uid = 1 [(is_uid) = true];
    optional int32 uid = 1 [(is_uid) = true];


    // Power used by this uid in mAh, as computed by BatteryStats, since BatteryStats last reset
    // Power used by this uid in nAs (i.e. nanocoulombs (nC)), as computed by BatteryStats, since
    // (i.e. roughly since device was last significantly charged).
    // BatteryStats last reset (i.e. roughly since device was last significantly charged).
    optional float power_milli_amp_hours = 2;
    optional int64 power_nano_amp_secs = 2;
}
}


/**
/**
@@ -3525,9 +3525,9 @@ message DeviceCalculatedPowerBlameOther {
    }
    }
    optional DrainType drain_type = 1;
    optional DrainType drain_type = 1;


    // Power used by this item in mAh, as computed by BatteryStats, since BatteryStats last reset
    // Power used by this item in nAs (i.e. nanocoulombs (nC)), as computed by BatteryStats, since
    // (i.e. roughly since device was last significantly charged).
    // BatteryStats last reset (i.e. roughly since device was last significantly charged).
    optional float power_milli_amp_hours = 2;
    optional int64 power_nano_amp_secs = 2;
}
}


/**
/**
+8 −3
Original line number Original line Diff line number Diff line
@@ -1563,11 +1563,16 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        return mBatteryStatsHelper;
        return mBatteryStatsHelper;
    }
    }


    private long milliAmpHrsToNanoAmpSecs(double mAh) {
        final long MILLI_AMP_HR_TO_NANO_AMP_SECS = 1_000_000L * 3600L;
        return (long) (mAh * MILLI_AMP_HR_TO_NANO_AMP_SECS + 0.5);
    }

    private void pullDeviceCalculatedPowerUse(int tagId,
    private void pullDeviceCalculatedPowerUse(int tagId,
            long elapsedNanos, final long wallClockNanos, List<StatsLogEventWrapper> pulledData) {
            long elapsedNanos, final long wallClockNanos, List<StatsLogEventWrapper> pulledData) {
        BatteryStatsHelper bsHelper = getBatteryStatsHelper();
        BatteryStatsHelper bsHelper = getBatteryStatsHelper();
        StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
        StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
        e.writeFloat((float) bsHelper.getComputedPower());
        e.writeLong(milliAmpHrsToNanoAmpSecs(bsHelper.getComputedPower()));
        pulledData.add(e);
        pulledData.add(e);
    }
    }


@@ -1583,7 +1588,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            }
            }
            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
            e.writeInt(bs.uidObj.getUid());
            e.writeInt(bs.uidObj.getUid());
            e.writeFloat((float) bs.totalPowerMah);
            e.writeLong(milliAmpHrsToNanoAmpSecs(bs.totalPowerMah));
            pulledData.add(e);
            pulledData.add(e);
        }
        }
    }
    }
@@ -1603,7 +1608,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            }
            }
            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
            e.writeInt(bs.drainType.ordinal());
            e.writeInt(bs.drainType.ordinal());
            e.writeFloat((float) bs.totalPowerMah);
            e.writeLong(milliAmpHrsToNanoAmpSecs(bs.totalPowerMah));
            pulledData.add(e);
            pulledData.add(e);
        }
        }
    }
    }