diff options
5 files changed, 323 insertions, 0 deletions
diff --git a/core/java/android/os/connectivity/CellularBatteryStats.aidl b/core/java/android/os/connectivity/CellularBatteryStats.aidl new file mode 100644 index 000000000000..ca0a585932f1 --- /dev/null +++ b/core/java/android/os/connectivity/CellularBatteryStats.aidl @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2016 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. + */ + +package android.os.connectivity; + +/** {@hide} */ +parcelable CellularBatteryStats;
\ No newline at end of file diff --git a/core/java/android/os/connectivity/CellularBatteryStats.java b/core/java/android/os/connectivity/CellularBatteryStats.java new file mode 100644 index 000000000000..2593c85cff12 --- /dev/null +++ b/core/java/android/os/connectivity/CellularBatteryStats.java @@ -0,0 +1,242 @@ +/* + * Copyright (C) 2016 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. + */ +package android.os.connectivity; + +import android.os.BatteryStats; +import android.os.Parcel; +import android.os.Parcelable; + +import android.telephony.ModemActivityInfo; +import android.telephony.SignalStrength; + +import java.util.Arrays; + +/** + * API for Cellular power stats + * + * @hide + */ +public final class CellularBatteryStats implements Parcelable { + + private long mLoggingDurationMs; + private long mKernelActiveTimeMs; + private long mNumPacketsTx; + private long mNumBytesTx; + private long mNumPacketsRx; + private long mNumBytesRx; + private long mSleepTimeMs; + private long mIdleTimeMs; + private long mRxTimeMs; + private long mEnergyConsumedMaMs; + private long[] mTimeInRatMs; + private long[] mTimeInRxSignalStrengthLevelMs; + private long[] mTxTimeMs; + + public static final Parcelable.Creator<CellularBatteryStats> CREATOR = new + Parcelable.Creator<CellularBatteryStats>() { + public CellularBatteryStats createFromParcel(Parcel in) { + return new CellularBatteryStats(in); + } + + public CellularBatteryStats[] newArray(int size) { + return new CellularBatteryStats[size]; + } + }; + + public CellularBatteryStats() { + initialize(); + } + + public void writeToParcel(Parcel out, int flags) { + out.writeLong(mLoggingDurationMs); + out.writeLong(mKernelActiveTimeMs); + out.writeLong(mNumPacketsTx); + out.writeLong(mNumBytesTx); + out.writeLong(mNumPacketsRx); + out.writeLong(mNumBytesRx); + out.writeLong(mSleepTimeMs); + out.writeLong(mIdleTimeMs); + out.writeLong(mRxTimeMs); + out.writeLong(mEnergyConsumedMaMs); + out.writeLongArray(mTimeInRatMs); + out.writeLongArray(mTimeInRxSignalStrengthLevelMs); + out.writeLongArray(mTxTimeMs); + } + + public void readFromParcel(Parcel in) { + mLoggingDurationMs = in.readLong(); + mKernelActiveTimeMs = in.readLong(); + mNumPacketsTx = in.readLong(); + mNumBytesTx = in.readLong(); + mNumPacketsRx = in.readLong(); + mNumBytesRx = in.readLong(); + mSleepTimeMs = in.readLong(); + mIdleTimeMs = in.readLong(); + mRxTimeMs = in.readLong(); + mEnergyConsumedMaMs = in.readLong(); + in.readLongArray(mTimeInRatMs); + in.readLongArray(mTimeInRxSignalStrengthLevelMs); + in.readLongArray(mTxTimeMs); + } + + public long getLoggingDurationMs() { + return mLoggingDurationMs; + } + + public long getKernelActiveTimeMs() { + return mKernelActiveTimeMs; + } + + public long getNumPacketsTx() { + return mNumPacketsTx; + } + + public long getNumBytesTx() { + return mNumBytesTx; + } + + public long getNumPacketsRx() { + return mNumPacketsRx; + } + + public long getNumBytesRx() { + return mNumBytesRx; + } + + public long getSleepTimeMs() { + return mSleepTimeMs; + } + + public long getIdleTimeMs() { + return mIdleTimeMs; + } + + public long getRxTimeMs() { + return mRxTimeMs; + } + + public long getEnergyConsumedMaMs() { + return mEnergyConsumedMaMs; + } + + public long[] getTimeInRatMs() { + return mTimeInRatMs; + } + + public long[] getTimeInRxSignalStrengthLevelMs() { + return mTimeInRxSignalStrengthLevelMs; + } + + public long[] getTxTimeMs() { + return mTxTimeMs; + } + + public void setLoggingDurationMs(long t) { + mLoggingDurationMs = t; + return; + } + + public void setKernelActiveTimeMs(long t) { + mKernelActiveTimeMs = t; + return; + } + + public void setNumPacketsTx(long n) { + mNumPacketsTx = n; + return; + } + + public void setNumBytesTx(long b) { + mNumBytesTx = b; + return; + } + + public void setNumPacketsRx(long n) { + mNumPacketsRx = n; + return; + } + + public void setNumBytesRx(long b) { + mNumBytesRx = b; + return; + } + + public void setSleepTimeMs(long t) { + mSleepTimeMs = t; + return; + } + + public void setIdleTimeMs(long t) { + mIdleTimeMs = t; + return; + } + + public void setRxTimeMs(long t) { + mRxTimeMs = t; + return; + } + + public void setEnergyConsumedMaMs(long e) { + mEnergyConsumedMaMs = e; + return; + } + + public void setTimeInRatMs(long[] t) { + mTimeInRatMs = Arrays.copyOfRange(t, 0, + Math.min(t.length, BatteryStats.NUM_DATA_CONNECTION_TYPES)); + return; + } + + public void setTimeInRxSignalStrengthLevelMs(long[] t) { + mTimeInRxSignalStrengthLevelMs = Arrays.copyOfRange(t, 0, + Math.min(t.length, SignalStrength.NUM_SIGNAL_STRENGTH_BINS)); + return; + } + + public void setTxTimeMs(long[] t) { + mTxTimeMs = Arrays.copyOfRange(t, 0, Math.min(t.length, ModemActivityInfo.TX_POWER_LEVELS)); + return; + } + + public int describeContents() { + return 0; + } + + private CellularBatteryStats(Parcel in) { + initialize(); + readFromParcel(in); + } + + private void initialize() { + mLoggingDurationMs = 0; + mKernelActiveTimeMs = 0; + mNumPacketsTx = 0; + mNumBytesTx = 0; + mNumPacketsRx = 0; + mNumBytesRx = 0; + mSleepTimeMs = 0; + mIdleTimeMs = 0; + mRxTimeMs = 0; + mEnergyConsumedMaMs = 0; + mTimeInRatMs = new long[BatteryStats.NUM_DATA_CONNECTION_TYPES]; + Arrays.fill(mTimeInRatMs, 0); + mTimeInRxSignalStrengthLevelMs = new long[SignalStrength.NUM_SIGNAL_STRENGTH_BINS]; + Arrays.fill(mTimeInRxSignalStrengthLevelMs, 0); + mTxTimeMs = new long[ModemActivityInfo.TX_POWER_LEVELS]; + Arrays.fill(mTxTimeMs, 0); + return; + } +}
\ No newline at end of file diff --git a/core/java/com/android/internal/app/IBatteryStats.aidl b/core/java/com/android/internal/app/IBatteryStats.aidl index f40523162078..ab118ba45514 100644 --- a/core/java/com/android/internal/app/IBatteryStats.aidl +++ b/core/java/com/android/internal/app/IBatteryStats.aidl @@ -22,6 +22,7 @@ import android.bluetooth.BluetoothActivityEnergyInfo; import android.net.wifi.WifiActivityEnergyInfo; import android.os.ParcelFileDescriptor; import android.os.WorkSource; +import android.os.connectivity.CellularBatteryStats; import android.os.health.HealthStatsParceler; import android.telephony.DataConnectionRealTimeInfo; import android.telephony.ModemActivityInfo; @@ -134,6 +135,9 @@ interface IBatteryStats { void noteResetBleScan(); void noteBleScanResults(in WorkSource ws, int numNewResults); + /** {@hide} */ + CellularBatteryStats getCellularBatteryStats(); + HealthStatsParceler takeUidSnapshot(int uid); HealthStatsParceler[] takeUidSnapshots(in int[] uid); diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java index 6510a70c0cf6..6f6eaaa59d17 100644 --- a/core/java/com/android/internal/os/BatteryStatsImpl.java +++ b/core/java/com/android/internal/os/BatteryStatsImpl.java @@ -30,6 +30,7 @@ import android.net.wifi.WifiManager; import android.os.BatteryManager; import android.os.BatteryStats; import android.os.Build; +import android.os.connectivity.CellularBatteryStats; import android.os.FileUtils; import android.os.Handler; import android.os.IBatteryPropertiesRegistrar; @@ -11599,6 +11600,51 @@ public class BatteryStatsImpl extends BatteryStats { return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000; } + /*@hide */ + public CellularBatteryStats getCellularBatteryStats() { + CellularBatteryStats s = new CellularBatteryStats(); + final int which = STATS_SINCE_CHARGED; + final long rawRealTime = SystemClock.elapsedRealtime() * 1000; + final ControllerActivityCounter counter = getModemControllerActivity(); + final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which); + final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which); + final long energyConsumedMaMs = counter.getPowerCounter().getCountLocked(which); + long[] timeInRatMs = new long[BatteryStats.NUM_DATA_CONNECTION_TYPES]; + for (int i = 0; i < timeInRatMs.length; i++) { + timeInRatMs[i] = getPhoneDataConnectionTime(i, rawRealTime, which) / 1000; + } + long[] timeInRxSignalStrengthLevelMs = new long[SignalStrength.NUM_SIGNAL_STRENGTH_BINS]; + for (int i = 0; i < timeInRxSignalStrengthLevelMs.length; i++) { + timeInRxSignalStrengthLevelMs[i] + = getPhoneSignalStrengthTime(i, rawRealTime, which) / 1000; + } + long[] txTimeMs = new long[Math.min(ModemActivityInfo.TX_POWER_LEVELS, + counter.getTxTimeCounters().length)]; + long totalTxTimeMs = 0; + for (int i = 0; i < txTimeMs.length; i++) { + txTimeMs[i] = counter.getTxTimeCounters()[i].getCountLocked(which); + totalTxTimeMs += txTimeMs[i]; + } + final long totalControllerActivityTimeMs + = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which) / 1000; + final long sleepTimeMs + = totalControllerActivityTimeMs - (idleTimeMs + rxTimeMs + totalTxTimeMs); + s.setLoggingDurationMs(computeBatteryRealtime(rawRealTime, which) / 1000); + s.setKernelActiveTimeMs(getMobileRadioActiveTime(rawRealTime, which) / 1000); + s.setNumPacketsTx(getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which)); + s.setNumBytesTx(getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which)); + s.setNumPacketsRx(getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which)); + s.setNumBytesRx(getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which)); + s.setSleepTimeMs(sleepTimeMs); + s.setIdleTimeMs(idleTimeMs); + s.setRxTimeMs(rxTimeMs); + s.setEnergyConsumedMaMs(energyConsumedMaMs); + s.setTimeInRatMs(timeInRatMs); + s.setTimeInRxSignalStrengthLevelMs(timeInRxSignalStrengthLevelMs); + s.setTxTimeMs(txTimeMs); + return s; + } + @Override public LevelStepTracker getChargeLevelStepTracker() { return mChargeStepTracker; diff --git a/services/core/java/com/android/server/am/BatteryStatsService.java b/services/core/java/com/android/server/am/BatteryStatsService.java index 93fb3e3e3e6e..1fcd47da4e9b 100644 --- a/services/core/java/com/android/server/am/BatteryStatsService.java +++ b/services/core/java/com/android/server/am/BatteryStatsService.java @@ -38,6 +38,7 @@ import android.os.SystemClock; import android.os.UserHandle; import android.os.UserManagerInternal; import android.os.WorkSource; +import android.os.connectivity.CellularBatteryStats; import android.os.health.HealthStatsParceler; import android.os.health.HealthStatsWriter; import android.os.health.UidHealthStats; @@ -1405,6 +1406,16 @@ public final class BatteryStatsService extends IBatteryStats.Stub } /** + * Gets a snapshot of cellular stats + * @hide + */ + public CellularBatteryStats getCellularBatteryStats() { + synchronized (mStats) { + return mStats.getCellularBatteryStats(); + } + } + + /** * Gets a snapshot of the system health for a particular uid. */ @Override |