Merge "BatteryStats: Acquire network stats without BatteryStatsImpl lock" into oc-dev

This commit is contained in:
Adam Lesinski
2017-05-31 22:36:55 +00:00
committed by Android (Google) Code Review
2 changed files with 452 additions and 427 deletions

View File

@@ -56,6 +56,7 @@ import android.util.LogWriter;
import android.util.LongSparseArray; import android.util.LongSparseArray;
import android.util.LongSparseLongArray; import android.util.LongSparseLongArray;
import android.util.MutableInt; import android.util.MutableInt;
import android.util.Pools;
import android.util.PrintWriterPrinter; import android.util.PrintWriterPrinter;
import android.util.Printer; import android.util.Printer;
import android.util.Slog; import android.util.Slog;
@@ -66,6 +67,7 @@ import android.util.TimeUtils;
import android.util.Xml; import android.util.Xml;
import android.view.Display; import android.view.Display;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.net.NetworkStatsFactory; import com.android.internal.net.NetworkStatsFactory;
import com.android.internal.util.ArrayUtils; import com.android.internal.util.ArrayUtils;
@@ -173,7 +175,6 @@ public class BatteryStatsImpl extends BatteryStats {
private final PlatformIdleStateCallback mPlatformIdleStateCallback; private final PlatformIdleStateCallback mPlatformIdleStateCallback;
final class MyHandler extends Handler { final class MyHandler extends Handler {
public MyHandler(Looper looper) { public MyHandler(Looper looper) {
super(looper, null, true); super(looper, null, true);
@@ -228,11 +229,11 @@ public class BatteryStatsImpl extends BatteryStats {
} }
public interface ExternalStatsSync { public interface ExternalStatsSync {
public static final int UPDATE_CPU = 0x01; int UPDATE_CPU = 0x01;
public static final int UPDATE_WIFI = 0x02; int UPDATE_WIFI = 0x02;
public static final int UPDATE_RADIO = 0x04; int UPDATE_RADIO = 0x04;
public static final int UPDATE_BT = 0x08; int UPDATE_BT = 0x08;
public static final int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT; int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
void scheduleSync(String reason, int flags); void scheduleSync(String reason, int flags);
void scheduleCpuSyncDueToRemovedUid(int uid); void scheduleCpuSyncDueToRemovedUid(int uid);
@@ -572,8 +573,6 @@ public class BatteryStatsImpl extends BatteryStats {
private int mMinLearnedBatteryCapacity = -1; private int mMinLearnedBatteryCapacity = -1;
private int mMaxLearnedBatteryCapacity = -1; private int mMaxLearnedBatteryCapacity = -1;
private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
private long[] mCpuFreqs; private long[] mCpuFreqs;
private PowerProfile mPowerProfile; private PowerProfile mPowerProfile;
@@ -637,19 +636,9 @@ public class BatteryStatsImpl extends BatteryStats {
private void init(Clocks clocks) { private void init(Clocks clocks) {
mClocks = clocks; mClocks = clocks;
mMobileNetworkStats = new NetworkStats[] {
new NetworkStats(mClocks.elapsedRealtime(), 50),
new NetworkStats(mClocks.elapsedRealtime(), 50),
new NetworkStats(mClocks.elapsedRealtime(), 50)
};
mWifiNetworkStats = new NetworkStats[] {
new NetworkStats(mClocks.elapsedRealtime(), 50),
new NetworkStats(mClocks.elapsedRealtime(), 50),
new NetworkStats(mClocks.elapsedRealtime(), 50)
};
} }
public static interface TimeBaseObs { public interface TimeBaseObs {
void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime); void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime); void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
} }
@@ -4200,7 +4189,10 @@ public class BatteryStatsImpl extends BatteryStats {
getUidStatsLocked(uid).noteMobileRadioApWakeupLocked(); getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
} }
public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) { /**
* Updates the radio power state and returns true if an external stats collection should occur.
*/
public boolean noteMobileRadioPowerStateLocked(int powerState, long timestampNs, int uid) {
final long elapsedRealtime = mClocks.elapsedRealtime(); final long elapsedRealtime = mClocks.elapsedRealtime();
final long uptime = mClocks.uptimeMillis(); final long uptime = mClocks.uptimeMillis();
if (mMobileRadioPowerState != powerState) { if (mMobileRadioPowerState != powerState) {
@@ -4237,13 +4229,15 @@ public class BatteryStatsImpl extends BatteryStats {
mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime); mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
} else { } else {
mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs); mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs); mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
// Tell the caller to collect radio network/power stats.
return true;
} }
} }
return false;
} }
public void notePowerSaveMode(boolean enabled) { public void notePowerSaveModeLocked(boolean enabled) {
if (mPowerSaveModeEnabled != enabled) { if (mPowerSaveModeEnabled != enabled) {
int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0; int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState; mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
@@ -5243,13 +5237,18 @@ public class BatteryStatsImpl extends BatteryStats {
public void noteNetworkInterfaceTypeLocked(String iface, int networkType) { public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
if (TextUtils.isEmpty(iface)) return; if (TextUtils.isEmpty(iface)) return;
synchronized (mModemNetworkLock) {
if (ConnectivityManager.isNetworkTypeMobile(networkType)) { if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
mMobileIfaces = includeInStringArray(mMobileIfaces, iface); mModemIfaces = includeInStringArray(mModemIfaces, iface);
if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces); if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mModemIfaces);
} else { } else {
mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface); mModemIfaces = excludeFromStringArray(mModemIfaces, iface);
if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces); if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mModemIfaces);
} }
}
synchronized (mWifiNetworkLock) {
if (ConnectivityManager.isNetworkTypeWifi(networkType)) { if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
mWifiIfaces = includeInStringArray(mWifiIfaces, iface); mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces); if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
@@ -5258,13 +5257,6 @@ public class BatteryStatsImpl extends BatteryStats {
if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces); if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
} }
} }
public void noteNetworkStatsEnabledLocked() {
// During device boot, qtaguid isn't enabled until after the inital
// loading of battery stats. Now that they're enabled, take our initial
// snapshot for future delta calculation.
updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
updateWifiStateLocked(null);
} }
@Override public long getScreenOnTime(long elapsedRealtimeUs, int which) { @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
@@ -8713,8 +8705,7 @@ public class BatteryStatsImpl extends BatteryStats {
mPlatformIdleStateCallback = null; mPlatformIdleStateCallback = null;
} }
public void setPowerProfile(PowerProfile profile) { public void setPowerProfileLocked(PowerProfile profile) {
synchronized (this) {
mPowerProfile = profile; mPowerProfile = profile;
// We need to initialize the KernelCpuSpeedReaders to read from // We need to initialize the KernelCpuSpeedReaders to read from
@@ -8735,13 +8726,12 @@ public class BatteryStatsImpl extends BatteryStats {
mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity(); mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
} }
} }
}
public void setCallback(BatteryCallback cb) { public void setCallback(BatteryCallback cb) {
mCallback = cb; mCallback = cb;
} }
public void setRadioScanningTimeout(long timeout) { public void setRadioScanningTimeoutLocked(long timeout) {
if (mPhoneSignalScanningTimer != null) { if (mPhoneSignalScanningTimer != null) {
mPhoneSignalScanningTimer.setTimeout(timeout); mPhoneSignalScanningTimer.setTimeout(timeout);
} }
@@ -9433,72 +9423,76 @@ public class BatteryStatsImpl extends BatteryStats {
} }
} }
private String[] mMobileIfaces = EmptyArray.STRING; private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
private final Pools.Pool<NetworkStats> mNetworkStatsPool = new Pools.SynchronizedPool<>(6);
private final Object mWifiNetworkLock = new Object();
@GuardedBy("mWifiNetworkLock")
private String[] mWifiIfaces = EmptyArray.STRING; private String[] mWifiIfaces = EmptyArray.STRING;
private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory(); @GuardedBy("mWifiNetworkLock")
private NetworkStats mLastWifiNetworkStats = new NetworkStats(0, -1);
private static final int NETWORK_STATS_LAST = 0; private final Object mModemNetworkLock = new Object();
private static final int NETWORK_STATS_NEXT = 1;
private static final int NETWORK_STATS_DELTA = 2;
private NetworkStats[] mMobileNetworkStats; @GuardedBy("mModemNetworkLock")
private NetworkStats[] mWifiNetworkStats; private String[] mModemIfaces = EmptyArray.STRING;
/** @GuardedBy("mModemNetworkLock")
* Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer private NetworkStats mLastModemNetworkStats = new NetworkStats(0, -1);
* as a buffer of NetworkStats objects to cycle through when computing deltas.
*/ private NetworkStats readNetworkStatsLocked(String[] ifaces) {
private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces, try {
NetworkStats[] networkStatsBuffer) if (!ArrayUtils.isEmpty(ifaces)) {
throws IOException { return mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED, NetworkStats.TAG_NONE, mNetworkStatsPool.acquire());
false)) {
return null;
} }
} catch (IOException e) {
final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, Slog.e(TAG, "failed to read network stats for ifaces: " + Arrays.toString(ifaces));
ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]); }
networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats, return null;
networkStatsBuffer[NETWORK_STATS_LAST], null, null,
networkStatsBuffer[NETWORK_STATS_DELTA]);
networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
networkStatsBuffer[NETWORK_STATS_LAST] = stats;
return networkStatsBuffer[NETWORK_STATS_DELTA];
} }
/** /**
* Distribute WiFi energy info and network traffic to apps. * Distribute WiFi energy info and network traffic to apps.
* @param info The energy information from the WiFi controller. * @param info The energy information from the WiFi controller.
*/ */
public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) { public void updateWifiState(@Nullable final WifiActivityEnergyInfo info) {
if (DEBUG_ENERGY) { if (DEBUG_ENERGY) {
Slog.d(TAG, "Updating wifi stats"); Slog.d(TAG, "Updating wifi stats: " + Arrays.toString(mWifiIfaces));
}
// Grab a separate lock to acquire the network stats, which may do I/O.
NetworkStats delta = null;
synchronized (mWifiNetworkLock) {
final NetworkStats latestStats = readNetworkStatsLocked(mWifiIfaces);
if (latestStats != null) {
delta = NetworkStats.subtract(latestStats, mLastWifiNetworkStats, null, null,
mNetworkStatsPool.acquire());
mNetworkStatsPool.release(mLastWifiNetworkStats);
mLastWifiNetworkStats = latestStats;
}
}
synchronized (this) {
if (!mOnBatteryInternal) {
if (delta != null) {
mNetworkStatsPool.release(delta);
}
return;
} }
final long elapsedRealtimeMs = mClocks.elapsedRealtime(); final long elapsedRealtimeMs = mClocks.elapsedRealtime();
NetworkStats delta = null;
try {
if (!ArrayUtils.isEmpty(mWifiIfaces)) {
delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
}
} catch (IOException e) {
Slog.wtf(TAG, "Failed to get wifi network stats", e);
return;
}
if (!mOnBatteryInternal) {
return;
}
SparseLongArray rxPackets = new SparseLongArray(); SparseLongArray rxPackets = new SparseLongArray();
SparseLongArray txPackets = new SparseLongArray(); SparseLongArray txPackets = new SparseLongArray();
long totalTxPackets = 0; long totalTxPackets = 0;
long totalRxPackets = 0; long totalRxPackets = 0;
if (delta != null) { if (delta != null) {
NetworkStats.Entry entry = new NetworkStats.Entry();
final int size = delta.size(); final int size = delta.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry); entry = delta.getValues(i, entry);
if (DEBUG_ENERGY) { if (DEBUG_ENERGY) {
Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
@@ -9550,6 +9544,8 @@ public class BatteryStatsImpl extends BatteryStats {
totalTxPackets += entry.txPackets; totalTxPackets += entry.txPackets;
} }
} }
mNetworkStatsPool.release(delta);
delta = null;
} }
if (info != null) { if (info != null) {
@@ -9591,11 +9587,13 @@ public class BatteryStatsImpl extends BatteryStats {
} }
if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) { if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > " Slog.d(TAG,
" !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
+ rxTimeMs + " ms). Normalizing scan time."); + rxTimeMs + " ms). Normalizing scan time.");
} }
if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) { if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > " Slog.d(TAG,
" !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
+ txTimeMs + " ms). Normalizing scan time."); + txTimeMs + " ms). Normalizing scan time.");
} }
@@ -9637,14 +9635,16 @@ public class BatteryStatsImpl extends BatteryStats {
ControllerActivityCounterImpl activityCounter = ControllerActivityCounterImpl activityCounter =
uid.getOrCreateWifiControllerActivityLocked(); uid.getOrCreateWifiControllerActivityLocked();
activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs); activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs); activityCounter.getTxTimeCounters()[0].addCountLocked(
scanTxTimeSinceMarkMs);
leftOverRxTimeMs -= scanRxTimeSinceMarkMs; leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
leftOverTxTimeMs -= scanTxTimeSinceMarkMs; leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
} }
// Distribute evenly the power consumed while Idle to each app holding a WiFi // Distribute evenly the power consumed while Idle to each app holding a WiFi
// lock. // lock.
final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked( final long wifiLockTimeSinceMarkMs =
uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
elapsedRealtimeMs * 1000) / 1000; elapsedRealtimeMs * 1000) / 1000;
if (wifiLockTimeSinceMarkMs > 0) { if (wifiLockTimeSinceMarkMs > 0) {
// Set the new mark so that next time we get new data since this point. // Set the new mark so that next time we get new data since this point.
@@ -9670,7 +9670,8 @@ public class BatteryStatsImpl extends BatteryStats {
// packets. // packets.
for (int i = 0; i < txPackets.size(); i++) { for (int i = 0; i < txPackets.size(); i++) {
final Uid uid = getUidStatsLocked(txPackets.keyAt(i)); final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets; final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs)
/ totalTxPackets;
if (DEBUG_ENERGY) { if (DEBUG_ENERGY) {
Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms"); Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
} }
@@ -9682,7 +9683,8 @@ public class BatteryStatsImpl extends BatteryStats {
// packets. // packets.
for (int i = 0; i < rxPackets.size(); i++) { for (int i = 0; i < rxPackets.size(); i++) {
final Uid uid = getUidStatsLocked(rxPackets.keyAt(i)); final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets; final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs)
/ totalRxPackets;
if (DEBUG_ENERGY) { if (DEBUG_ENERGY) {
Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms"); Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
} }
@@ -9692,10 +9694,13 @@ public class BatteryStatsImpl extends BatteryStats {
// Any left over power use will be picked up by the WiFi category in BatteryStatsHelper. // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
// Update WiFi controller stats. // Update WiFi controller stats.
mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis()); mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis()); mWifiActivity.getTxTimeCounters()[0].addCountLocked(
mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis()); info.getControllerTxTimeMillis());
mWifiActivity.getIdleTimeCounter().addCountLocked(
info.getControllerIdleTimeMillis());
// POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V. // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
final double opVolt = mPowerProfile.getAveragePower( final double opVolt = mPowerProfile.getAveragePower(
@@ -9703,7 +9708,8 @@ public class BatteryStatsImpl extends BatteryStats {
if (opVolt != 0) { if (opVolt != 0) {
// We store the power drain as mAms. // We store the power drain as mAms.
mWifiActivity.getPowerCounter().addCountLocked( mWifiActivity.getPowerCounter().addCountLocked(
(long)(info.getControllerEnergyUsed() / opVolt)); (long) (info.getControllerEnergyUsed() / opVolt));
}
} }
} }
} }
@@ -9711,26 +9717,32 @@ public class BatteryStatsImpl extends BatteryStats {
/** /**
* Distribute Cell radio energy info and network traffic to apps. * Distribute Cell radio energy info and network traffic to apps.
*/ */
public void updateMobileRadioStateLocked(final long elapsedRealtimeMs, public void updateMobileRadioState(@Nullable final ModemActivityInfo activityInfo) {
final ModemActivityInfo activityInfo) {
if (DEBUG_ENERGY) { if (DEBUG_ENERGY) {
Slog.d(TAG, "Updating mobile radio stats with " + activityInfo); Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
} }
// Grab a separate lock to acquire the network stats, which may do I/O.
NetworkStats delta = null; NetworkStats delta = null;
try { synchronized (mModemNetworkLock) {
if (!ArrayUtils.isEmpty(mMobileIfaces)) { final NetworkStats latestStats = readNetworkStatsLocked(mModemIfaces);
delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats); if (latestStats != null) {
delta = NetworkStats.subtract(latestStats, mLastModemNetworkStats, null, null,
mNetworkStatsPool.acquire());
mNetworkStatsPool.release(mLastModemNetworkStats);
mLastModemNetworkStats = latestStats;
} }
} catch (IOException e) {
Slog.wtf(TAG, "Failed to get mobile network stats", e);
return;
} }
synchronized (this) {
if (!mOnBatteryInternal) { if (!mOnBatteryInternal) {
if (delta != null) {
mNetworkStatsPool.release(delta);
}
return; return;
} }
final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked( long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
elapsedRealtimeMs * 1000); elapsedRealtimeMs * 1000);
mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs); mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
@@ -9738,9 +9750,10 @@ public class BatteryStatsImpl extends BatteryStats {
long totalRxPackets = 0; long totalRxPackets = 0;
long totalTxPackets = 0; long totalTxPackets = 0;
if (delta != null) { if (delta != null) {
NetworkStats.Entry entry = new NetworkStats.Entry();
final int size = delta.size(); final int size = delta.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry); entry = delta.getValues(i, entry);
if (entry.rxPackets == 0 && entry.txPackets == 0) { if (entry.rxPackets == 0 && entry.txPackets == 0) {
continue; continue;
} }
@@ -9755,8 +9768,10 @@ public class BatteryStatsImpl extends BatteryStats {
totalTxPackets += entry.txPackets; totalTxPackets += entry.txPackets;
final Uid u = getUidStatsLocked(mapUid(entry.uid)); final Uid u = getUidStatsLocked(mapUid(entry.uid));
u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets); u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets); entry.rxPackets);
u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
entry.txPackets);
if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA, u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
entry.rxBytes, entry.rxPackets); entry.rxBytes, entry.rxPackets);
@@ -9778,7 +9793,7 @@ public class BatteryStatsImpl extends BatteryStats {
long totalPackets = totalRxPackets + totalTxPackets; long totalPackets = totalRxPackets + totalTxPackets;
if (totalPackets > 0) { if (totalPackets > 0) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry); entry = delta.getValues(i, entry);
if (entry.rxPackets == 0 && entry.txPackets == 0) { if (entry.rxPackets == 0 && entry.txPackets == 0) {
continue; continue;
} }
@@ -9806,7 +9821,8 @@ public class BatteryStatsImpl extends BatteryStats {
if (totalTxPackets > 0 && entry.txPackets > 0) { if (totalTxPackets > 0 && entry.txPackets > 0) {
for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) { for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl]; long txMs =
entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
txMs /= totalTxPackets; txMs /= totalTxPackets;
activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs); activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
} }
@@ -9820,11 +9836,15 @@ public class BatteryStatsImpl extends BatteryStats {
mMobileRadioActiveUnknownTime.addCountLocked(radioTime); mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
mMobileRadioActiveUnknownCount.addCountLocked(1); mMobileRadioActiveUnknownCount.addCountLocked(1);
} }
mNetworkStatsPool.release(delta);
delta = null;
} }
if (activityInfo != null) { if (activityInfo != null) {
mHasModemReporting = true; mHasModemReporting = true;
mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis()); mModemActivity.getIdleTimeCounter().addCountLocked(
activityInfo.getIdleTimeMillis());
mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis()); mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) { for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
mModemActivity.getTxTimeCounters()[lvl] mModemActivity.getTxTimeCounters()[lvl]
@@ -9841,6 +9861,7 @@ public class BatteryStatsImpl extends BatteryStats {
} }
} }
} }
}
/** /**
* Distribute Bluetooth energy info and network traffic to apps. * Distribute Bluetooth energy info and network traffic to apps.

View File

@@ -17,6 +17,8 @@
package com.android.server.am; package com.android.server.am;
import static com.android.internal.os.BatteryStatsImpl.ExternalStatsSync.UPDATE_CPU; import static com.android.internal.os.BatteryStatsImpl.ExternalStatsSync.UPDATE_CPU;
import static com.android.internal.os.BatteryStatsImpl.ExternalStatsSync.UPDATE_RADIO;
import static com.android.internal.os.BatteryStatsImpl.ExternalStatsSync.UPDATE_WIFI;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.bluetooth.BluetoothActivityEnergyInfo; import android.bluetooth.BluetoothActivityEnergyInfo;
@@ -231,10 +233,12 @@ public final class BatteryStatsService extends IBatteryStats.Stub
public void publish(Context context) { public void publish(Context context) {
mContext = context; mContext = context;
mStats.setRadioScanningTimeout(mContext.getResources().getInteger( synchronized (mStats) {
mStats.setRadioScanningTimeoutLocked(mContext.getResources().getInteger(
com.android.internal.R.integer.config_radioScanningTimeout) com.android.internal.R.integer.config_radioScanningTimeout)
* 1000L); * 1000L);
mStats.setPowerProfile(new PowerProfile(context)); mStats.setPowerProfileLocked(new PowerProfile(context));
}
ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder()); ServiceManager.addService(BatteryStats.SERVICE_NAME, asBinder());
} }
@@ -245,9 +249,11 @@ public final class BatteryStatsService extends IBatteryStats.Stub
public void initPowerManagement() { public void initPowerManagement() {
final PowerManagerInternal powerMgr = LocalServices.getService(PowerManagerInternal.class); final PowerManagerInternal powerMgr = LocalServices.getService(PowerManagerInternal.class);
powerMgr.registerLowPowerModeObserver(this); powerMgr.registerLowPowerModeObserver(this);
mStats.notePowerSaveMode( synchronized (mStats) {
mStats.notePowerSaveModeLocked(
powerMgr.getLowPowerState(ServiceType.BATTERY_STATS) powerMgr.getLowPowerState(ServiceType.BATTERY_STATS)
.batterySaverEnabled); .batterySaverEnabled);
}
(new WakeupReasonThread()).start(); (new WakeupReasonThread()).start();
} }
@@ -280,7 +286,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
@Override @Override
public void onLowPowerModeChanged(PowerSaveState result) { public void onLowPowerModeChanged(PowerSaveState result) {
synchronized (mStats) { synchronized (mStats) {
mStats.notePowerSaveMode(result.batterySaverEnabled); mStats.notePowerSaveModeLocked(result.batterySaverEnabled);
} }
} }
@@ -606,8 +612,13 @@ public final class BatteryStatsService extends IBatteryStats.Stub
public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) { public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
enforceCallingPermission(); enforceCallingPermission();
boolean update;
synchronized (mStats) { synchronized (mStats) {
mStats.noteMobileRadioPowerState(powerState, timestampNs, uid); update = mStats.noteMobileRadioPowerStateLocked(powerState, timestampNs, uid);
}
if (update) {
mHandler.scheduleSync("modem-data", UPDATE_RADIO);
} }
} }
@@ -759,7 +770,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM) ? "active" powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM) ? "active"
: "inactive"; : "inactive";
mHandler.scheduleSync("wifi-data: " + type, mHandler.scheduleSync("wifi-data: " + type,
BatteryStatsImpl.ExternalStatsSync.UPDATE_WIFI); UPDATE_WIFI);
} }
mStats.noteWifiRadioPowerState(powerState, tsNanos, uid); mStats.noteWifiRadioPowerState(powerState, tsNanos, uid);
} }
@@ -917,9 +928,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
@Override @Override
public void noteNetworkStatsEnabled() { public void noteNetworkStatsEnabled() {
enforceCallingPermission(); enforceCallingPermission();
synchronized (mStats) { mHandler.scheduleSync("network-stats-enabled", UPDATE_RADIO | UPDATE_WIFI);
mStats.noteNetworkStatsEnabledLocked();
}
} }
@Override @Override
@@ -985,9 +994,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
return; return;
} }
synchronized (mStats) { mStats.updateWifiState(info);
mStats.updateWifiStateLocked(info);
}
} }
@Override @Override
@@ -1012,9 +1019,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
return; return;
} }
synchronized (mStats) { mStats.updateMobileRadioState(info);
mStats.updateMobileRadioStateLocked(SystemClock.elapsedRealtime(), info);
}
} }
public boolean isOnBattery() { public boolean isOnBattery() {
@@ -1489,7 +1494,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
return; return;
} }
if ((updateFlags & BatteryStatsImpl.ExternalStatsSync.UPDATE_WIFI) != 0) { if ((updateFlags & UPDATE_WIFI) != 0) {
if (mWifiManager == null) { if (mWifiManager == null) {
mWifiManager = IWifiManager.Stub.asInterface( mWifiManager = IWifiManager.Stub.asInterface(
ServiceManager.getService(Context.WIFI_SERVICE)); ServiceManager.getService(Context.WIFI_SERVICE));
@@ -1558,14 +1563,6 @@ public final class BatteryStatsService extends IBatteryStats.Stub
mStats.updateKernelWakelocksLocked(); mStats.updateKernelWakelocksLocked();
mStats.updateKernelMemoryBandwidthLocked(); mStats.updateKernelMemoryBandwidthLocked();
if (wifiInfo != null) {
if (wifiInfo.isValid()) {
mStats.updateWifiStateLocked(extractDelta(wifiInfo));
} else {
Slog.e(TAG, "wifi info is invalid: " + wifiInfo);
}
}
if (bluetoothInfo != null) { if (bluetoothInfo != null) {
if (bluetoothInfo.isValid()) { if (bluetoothInfo.isValid()) {
mStats.updateBluetoothStateLocked(bluetoothInfo); mStats.updateBluetoothStateLocked(bluetoothInfo);
@@ -1573,17 +1570,24 @@ public final class BatteryStatsService extends IBatteryStats.Stub
Slog.e(TAG, "bluetooth info is invalid: " + bluetoothInfo); Slog.e(TAG, "bluetooth info is invalid: " + bluetoothInfo);
} }
} }
}
if (wifiInfo != null) {
if (wifiInfo.isValid()) {
mStats.updateWifiState(extractDelta(wifiInfo));
} else {
Slog.e(TAG, "wifi info is invalid: " + wifiInfo);
}
}
if (modemInfo != null) { if (modemInfo != null) {
if (modemInfo.isValid()) { if (modemInfo.isValid()) {
mStats.updateMobileRadioStateLocked(SystemClock.elapsedRealtime(), mStats.updateMobileRadioState(modemInfo);
modemInfo);
} else { } else {
Slog.e(TAG, "modem info is invalid: " + modemInfo); Slog.e(TAG, "modem info is invalid: " + modemInfo);
} }
} }
} }
}
if (DBG) Slog.d(TAG, "end updateExternalStatsSync"); if (DBG) Slog.d(TAG, "end updateExternalStatsSync");
} }