[SM06] Collect mobile NetworkStats metrics by collapsed rat type
Test: adb shell cmd stats pull-source 10000~10003
adb shell dumpsys netstats --uid
Bug: 129082217
Change-Id: I726e74f5c63a6ed456cb13ea259b58c7a33bec76
Merged-In: I726e74f5c63a6ed456cb13ea259b58c7a33bec76
(cherry picked from commit c82dfeb422)
This commit is contained in:
@@ -34,6 +34,7 @@ import static android.net.NetworkStats.ROAMING_NO;
|
||||
import static android.net.NetworkStats.ROAMING_YES;
|
||||
import static android.net.wifi.WifiInfo.sanitizeSsid;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.os.Parcel;
|
||||
@@ -52,6 +53,8 @@ import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -477,6 +480,32 @@ public class NetworkTemplate implements Parcelable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all supported collapsed RAT types that could be returned by
|
||||
* {@link #getCollapsedRatType(int)}.
|
||||
*/
|
||||
@NonNull
|
||||
public static final int[] getAllCollapsedRatTypes() {
|
||||
final int[] ratTypes = TelephonyManager.getAllNetworkTypes();
|
||||
final HashSet<Integer> collapsedRatTypes = new HashSet<>();
|
||||
for (final int ratType : ratTypes) {
|
||||
collapsedRatTypes.add(NetworkTemplate.getCollapsedRatType(ratType));
|
||||
}
|
||||
// Ensure that unknown type is returned.
|
||||
collapsedRatTypes.add(TelephonyManager.NETWORK_TYPE_UNKNOWN);
|
||||
return toIntArray(collapsedRatTypes);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static int[] toIntArray(@NonNull Collection<Integer> list) {
|
||||
final int[] array = new int[list.size()];
|
||||
int i = 0;
|
||||
for (final Integer item : list) {
|
||||
array[i++] = item;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if matches Wi-Fi network template.
|
||||
*/
|
||||
|
||||
@@ -21,6 +21,7 @@ import static android.app.AppOpsManager.OP_FLAG_TRUSTED_PROXY;
|
||||
import static android.app.usage.NetworkStatsManager.FLAG_AUGMENT_WITH_SUBSCRIPTION_PLAN;
|
||||
import static android.content.pm.PackageInfo.REQUESTED_PERMISSION_GRANTED;
|
||||
import static android.content.pm.PermissionInfo.PROTECTION_DANGEROUS;
|
||||
import static android.net.NetworkTemplate.getAllCollapsedRatTypes;
|
||||
import static android.os.Debug.getIonHeapsSizeKb;
|
||||
import static android.os.Process.getUidForPid;
|
||||
import static android.os.storage.VolumeInfo.TYPE_PRIVATE;
|
||||
@@ -718,7 +719,7 @@ public class StatsPullAtomService extends SystemService {
|
||||
final NetworkTemplate template = NetworkTemplate.buildTemplateWifiWildcard();
|
||||
final NetworkStats stats = getUidNetworkStatsSinceBoot(template, withFgbg);
|
||||
if (stats != null) {
|
||||
addNetworkStats(atomTag, pulledData, stats, withFgbg);
|
||||
addNetworkStats(atomTag, pulledData, stats, withFgbg, 0 /* ratType */);
|
||||
return StatsManager.PULL_SUCCESS;
|
||||
}
|
||||
return StatsManager.PULL_SKIP;
|
||||
@@ -726,17 +727,22 @@ public class StatsPullAtomService extends SystemService {
|
||||
|
||||
private int pullMobileBytesTransfer(
|
||||
int atomTag, @NonNull List<StatsEvent> pulledData, boolean withFgbg) {
|
||||
final NetworkTemplate template = NetworkTemplate.buildTemplateMobileWildcard();
|
||||
final NetworkStats stats = getUidNetworkStatsSinceBoot(template, withFgbg);
|
||||
if (stats != null) {
|
||||
addNetworkStats(atomTag, pulledData, stats, withFgbg);
|
||||
return StatsManager.PULL_SUCCESS;
|
||||
int ret = StatsManager.PULL_SKIP;
|
||||
for (final int ratType : getAllCollapsedRatTypes()) {
|
||||
final NetworkTemplate template =
|
||||
NetworkTemplate.buildTemplateMobileWithRatType(null, ratType);
|
||||
final NetworkStats stats = getUidNetworkStatsSinceBoot(template, withFgbg);
|
||||
if (stats != null) {
|
||||
addNetworkStats(atomTag, pulledData, stats, withFgbg, ratType);
|
||||
ret = StatsManager.PULL_SUCCESS; // If any of them is not null, then success.
|
||||
}
|
||||
}
|
||||
return StatsManager.PULL_SKIP;
|
||||
// If there is no data return PULL_SKIP to avoid wasting performance adding empty stats.
|
||||
return ret;
|
||||
}
|
||||
|
||||
private void addNetworkStats(int atomTag, @NonNull List<StatsEvent> ret,
|
||||
@NonNull NetworkStats stats, boolean withFgbg) {
|
||||
@NonNull NetworkStats stats, boolean withFgbg, int ratType) {
|
||||
int size = stats.size();
|
||||
final NetworkStats.Entry entry = new NetworkStats.Entry(); // For recycling
|
||||
for (int j = 0; j < size; j++) {
|
||||
@@ -751,6 +757,13 @@ public class StatsPullAtomService extends SystemService {
|
||||
e.writeLong(entry.rxPackets);
|
||||
e.writeLong(entry.txBytes);
|
||||
e.writeLong(entry.txPackets);
|
||||
switch (atomTag) {
|
||||
case FrameworkStatsLog.MOBILE_BYTES_TRANSFER:
|
||||
case FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG:
|
||||
e.writeInt(ratType);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
ret.add(e.build());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user