Loading services/core/java/com/android/server/stats/pull/StatsPullAtomService.java +45 −5 Original line number Diff line number Diff line Loading @@ -61,6 +61,7 @@ import static com.android.internal.util.FrameworkStatsLog.TIME_ZONE_DETECTOR_STA import static com.android.internal.util.FrameworkStatsLog.TIME_ZONE_DETECTOR_STATE__DETECTION_MODE__UNKNOWN; import static com.android.server.am.MemoryStatUtil.readMemoryStatFromFilesystem; import static com.android.server.stats.Flags.addMobileBytesTransferByProcStatePuller; import static com.android.server.stats.Flags.statsPullNetworkStatsManagerInitOrderFix; import static com.android.server.stats.pull.IonMemoryUtil.readProcessSystemIonHeapSizesFromDebugfs; import static com.android.server.stats.pull.IonMemoryUtil.readSystemIonHeapSizeFromDebugfs; import static com.android.server.stats.pull.ProcfsMemoryUtil.getProcessCmdlines; Loading Loading @@ -355,7 +356,17 @@ public class StatsPullAtomService extends SystemService { private TelephonyManager mTelephony; private UwbManager mUwbManager; private SubscriptionManager mSubscriptionManager; private NetworkStatsManager mNetworkStatsManager; /** * NetworkStatsManager initialization happens from one thread before any worker thread * is going to access the networkStatsManager instance: * - @initNetworkStatsManager() - initialization happens no worker thread to access are * active yet * - @initAndRegisterNetworkStatsPullers Network stats dependant pullers can only be * initialized after service is ready. Worker thread is spawn here only after the * initialization is completed in a thread safe way (no async access expected) */ private NetworkStatsManager mNetworkStatsManager = null; @GuardedBy("mKernelWakelockLock") private KernelWakelockReader mKernelWakelockReader; Loading Loading @@ -420,6 +431,12 @@ public class StatsPullAtomService extends SystemService { public static final boolean ENABLE_MOBILE_DATA_STATS_AGGREGATED_PULLER = addMobileBytesTransferByProcStatePuller(); /** * Whether or not to enable the mNetworkStatsManager initialization order fix */ private static final boolean ENABLE_NETWORK_STATS_MANAGER_INIT_ORDER_FIX = statsPullNetworkStatsManagerInitOrderFix(); // Puller locks private final Object mDataBytesTransferLock = new Object(); private final Object mBluetoothBytesTransferLock = new Object(); Loading Loading @@ -824,6 +841,9 @@ public class StatsPullAtomService extends SystemService { }); } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { BackgroundThread.getHandler().post(() -> { if (ENABLE_NETWORK_STATS_MANAGER_INIT_ORDER_FIX) { initNetworkStatsManager(); } // Network stats related pullers can only be initialized after service is ready. initAndRegisterNetworkStatsPullers(); // For services that are not ready at boot phase PHASE_SYSTEM_SERVICES_READY Loading @@ -843,7 +863,9 @@ public class StatsPullAtomService extends SystemService { mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); mStatsSubscriptionsListener = new StatsSubscriptionsListener(mSubscriptionManager); mStorageManager = (StorageManager) mContext.getSystemService(StorageManager.class); mNetworkStatsManager = mContext.getSystemService(NetworkStatsManager.class); if (!ENABLE_NETWORK_STATS_MANAGER_INIT_ORDER_FIX) { initNetworkStatsManager(); } // Initialize DiskIO mStoragedUidIoStatsReader = new StoragedUidIoStatsReader(); Loading Loading @@ -1019,6 +1041,24 @@ public class StatsPullAtomService extends SystemService { } } /** * Calling getNetworkStatsManager() before PHASE_THIRD_PARTY_APPS_CAN_START is unexpected * Callers use before PHASE_THIRD_PARTY_APPS_CAN_START stage is not legit */ @NonNull private NetworkStatsManager getNetworkStatsManager() { if (ENABLE_NETWORK_STATS_MANAGER_INIT_ORDER_FIX) { if (mNetworkStatsManager == null) { throw new IllegalStateException("NetworkStatsManager is not ready"); } } return mNetworkStatsManager; } private void initNetworkStatsManager() { mNetworkStatsManager = mContext.getSystemService(NetworkStatsManager.class); } private void initAndRegisterNetworkStatsPullers() { if (DEBUG) { Slog.d(TAG, "Registering NetworkStats pullers with statsd"); Loading Loading @@ -1514,11 +1554,11 @@ public class StatsPullAtomService extends SystemService { // I/O and also block main thread when polling. // Consider making perfd queries NetworkStatsService directly. if (template.getMatchRule() == MATCH_WIFI && template.getSubscriberIds().isEmpty()) { mNetworkStatsManager.forceUpdate(); getNetworkStatsManager().forceUpdate(); } final android.app.usage.NetworkStats queryNonTaggedStats = mNetworkStatsManager.querySummary( getNetworkStatsManager().querySummary( template, currentTimeInMillis - elapsedMillisSinceBoot - bucketDuration, currentTimeInMillis); Loading @@ -1528,7 +1568,7 @@ public class StatsPullAtomService extends SystemService { if (!includeTags) return nonTaggedStats; final android.app.usage.NetworkStats queryTaggedStats = mNetworkStatsManager.queryTaggedSummary(template, getNetworkStatsManager().queryTaggedSummary(template, currentTimeInMillis - elapsedMillisSinceBoot - bucketDuration, currentTimeInMillis); final NetworkStats taggedStats = Loading services/core/java/com/android/server/stats/stats_flags.aconfig +9 −1 Original line number Diff line number Diff line Loading @@ -8,3 +8,11 @@ flag { bug: "309512867" is_fixed_read_only: true } flag { name: "stats_pull_network_stats_manager_init_order_fix" namespace: "statsd" description: "Fix the mNetworkStatsManager initialization order" bug: "331989853" is_fixed_read_only: true } Loading
services/core/java/com/android/server/stats/pull/StatsPullAtomService.java +45 −5 Original line number Diff line number Diff line Loading @@ -61,6 +61,7 @@ import static com.android.internal.util.FrameworkStatsLog.TIME_ZONE_DETECTOR_STA import static com.android.internal.util.FrameworkStatsLog.TIME_ZONE_DETECTOR_STATE__DETECTION_MODE__UNKNOWN; import static com.android.server.am.MemoryStatUtil.readMemoryStatFromFilesystem; import static com.android.server.stats.Flags.addMobileBytesTransferByProcStatePuller; import static com.android.server.stats.Flags.statsPullNetworkStatsManagerInitOrderFix; import static com.android.server.stats.pull.IonMemoryUtil.readProcessSystemIonHeapSizesFromDebugfs; import static com.android.server.stats.pull.IonMemoryUtil.readSystemIonHeapSizeFromDebugfs; import static com.android.server.stats.pull.ProcfsMemoryUtil.getProcessCmdlines; Loading Loading @@ -355,7 +356,17 @@ public class StatsPullAtomService extends SystemService { private TelephonyManager mTelephony; private UwbManager mUwbManager; private SubscriptionManager mSubscriptionManager; private NetworkStatsManager mNetworkStatsManager; /** * NetworkStatsManager initialization happens from one thread before any worker thread * is going to access the networkStatsManager instance: * - @initNetworkStatsManager() - initialization happens no worker thread to access are * active yet * - @initAndRegisterNetworkStatsPullers Network stats dependant pullers can only be * initialized after service is ready. Worker thread is spawn here only after the * initialization is completed in a thread safe way (no async access expected) */ private NetworkStatsManager mNetworkStatsManager = null; @GuardedBy("mKernelWakelockLock") private KernelWakelockReader mKernelWakelockReader; Loading Loading @@ -420,6 +431,12 @@ public class StatsPullAtomService extends SystemService { public static final boolean ENABLE_MOBILE_DATA_STATS_AGGREGATED_PULLER = addMobileBytesTransferByProcStatePuller(); /** * Whether or not to enable the mNetworkStatsManager initialization order fix */ private static final boolean ENABLE_NETWORK_STATS_MANAGER_INIT_ORDER_FIX = statsPullNetworkStatsManagerInitOrderFix(); // Puller locks private final Object mDataBytesTransferLock = new Object(); private final Object mBluetoothBytesTransferLock = new Object(); Loading Loading @@ -824,6 +841,9 @@ public class StatsPullAtomService extends SystemService { }); } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { BackgroundThread.getHandler().post(() -> { if (ENABLE_NETWORK_STATS_MANAGER_INIT_ORDER_FIX) { initNetworkStatsManager(); } // Network stats related pullers can only be initialized after service is ready. initAndRegisterNetworkStatsPullers(); // For services that are not ready at boot phase PHASE_SYSTEM_SERVICES_READY Loading @@ -843,7 +863,9 @@ public class StatsPullAtomService extends SystemService { mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); mStatsSubscriptionsListener = new StatsSubscriptionsListener(mSubscriptionManager); mStorageManager = (StorageManager) mContext.getSystemService(StorageManager.class); mNetworkStatsManager = mContext.getSystemService(NetworkStatsManager.class); if (!ENABLE_NETWORK_STATS_MANAGER_INIT_ORDER_FIX) { initNetworkStatsManager(); } // Initialize DiskIO mStoragedUidIoStatsReader = new StoragedUidIoStatsReader(); Loading Loading @@ -1019,6 +1041,24 @@ public class StatsPullAtomService extends SystemService { } } /** * Calling getNetworkStatsManager() before PHASE_THIRD_PARTY_APPS_CAN_START is unexpected * Callers use before PHASE_THIRD_PARTY_APPS_CAN_START stage is not legit */ @NonNull private NetworkStatsManager getNetworkStatsManager() { if (ENABLE_NETWORK_STATS_MANAGER_INIT_ORDER_FIX) { if (mNetworkStatsManager == null) { throw new IllegalStateException("NetworkStatsManager is not ready"); } } return mNetworkStatsManager; } private void initNetworkStatsManager() { mNetworkStatsManager = mContext.getSystemService(NetworkStatsManager.class); } private void initAndRegisterNetworkStatsPullers() { if (DEBUG) { Slog.d(TAG, "Registering NetworkStats pullers with statsd"); Loading Loading @@ -1514,11 +1554,11 @@ public class StatsPullAtomService extends SystemService { // I/O and also block main thread when polling. // Consider making perfd queries NetworkStatsService directly. if (template.getMatchRule() == MATCH_WIFI && template.getSubscriberIds().isEmpty()) { mNetworkStatsManager.forceUpdate(); getNetworkStatsManager().forceUpdate(); } final android.app.usage.NetworkStats queryNonTaggedStats = mNetworkStatsManager.querySummary( getNetworkStatsManager().querySummary( template, currentTimeInMillis - elapsedMillisSinceBoot - bucketDuration, currentTimeInMillis); Loading @@ -1528,7 +1568,7 @@ public class StatsPullAtomService extends SystemService { if (!includeTags) return nonTaggedStats; final android.app.usage.NetworkStats queryTaggedStats = mNetworkStatsManager.queryTaggedSummary(template, getNetworkStatsManager().queryTaggedSummary(template, currentTimeInMillis - elapsedMillisSinceBoot - bucketDuration, currentTimeInMillis); final NetworkStats taggedStats = Loading
services/core/java/com/android/server/stats/stats_flags.aconfig +9 −1 Original line number Diff line number Diff line Loading @@ -8,3 +8,11 @@ flag { bug: "309512867" is_fixed_read_only: true } flag { name: "stats_pull_network_stats_manager_init_order_fix" namespace: "statsd" description: "Fix the mNetworkStatsManager initialization order" bug: "331989853" is_fixed_read_only: true }