Dump top distinct tag counts per uid

Sample Output:
NetworkStatsFactory logs:
  Top distinct tag counts in UidTagSets:
    1000=3
    10115=2
    1073=2
    1051=1
    10037=1
    10113=1
    0=1

Test: manual
Bug: 364353020
Change-Id: I1707d28efc991b083d360ff479216e1480ab0e7a
This commit is contained in:
Junyu Lai 2025-03-11 18:34:13 +08:00
commit 8fb3d765fc
2 changed files with 42 additions and 0 deletions

View file

@ -28,7 +28,9 @@ import android.net.UnderlyingNetworkInfo;
import android.os.ServiceSpecificException;
import android.os.SystemClock;
import android.util.ArraySet;
import android.util.IndentingPrintWriter;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
@ -40,6 +42,9 @@ import com.android.server.connectivity.InterfaceTracker;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@ -76,6 +81,7 @@ public class NetworkStatsFactory {
static final String CONFIG_PER_UID_TAG_THROTTLING_THRESHOLD =
"per_uid_tag_throttling_threshold";
private static final int DEFAULT_TAGS_PER_UID_THRESHOLD = 1000;
private static final int DUMP_TAGS_PER_UID_COUNT = 20;
private final boolean mSupportPerUidTagThrottling;
private final int mPerUidTagThrottlingThreshold;
@ -387,4 +393,34 @@ public class NetworkStatsFactory {
pe.initCause(cause);
return pe;
}
/**
* Dump the contents of NetworkStatsFactory.
*/
public void dump(IndentingPrintWriter pw) {
dumpUidTagSets(pw);
}
private void dumpUidTagSets(IndentingPrintWriter pw) {
pw.println("Top distinct tag counts in UidTagSets:");
pw.increaseIndent();
final List<Pair<Integer, Integer>> countForUidList = new ArrayList<>();
synchronized (mPersistentDataLock) {
for (int i = 0; i < mUidTagSets.size(); i++) {
final Pair<Integer, Integer> countForUid =
new Pair<>(mUidTagSets.keyAt(i), mUidTagSets.valueAt(i).size());
countForUidList.add(countForUid);
}
}
Collections.sort(countForUidList,
(entry1, entry2) -> Integer.compare(entry2.second, entry1.second));
final int dumpSize = Math.min(countForUidList.size(), DUMP_TAGS_PER_UID_COUNT);
for (int j = 0; j < dumpSize; j++) {
final Pair<Integer, Integer> entry = countForUidList.get(j);
pw.print(entry.first);
pw.print("=");
pw.println(entry.second);
}
pw.decreaseIndent();
}
}

View file

@ -3228,6 +3228,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
pw.increaseIndent();
mSkDestroyListener.dump(pw);
pw.decreaseIndent();
pw.println();
pw.println("NetworkStatsFactory logs:");
pw.increaseIndent();
mStatsFactory.dump(pw);
pw.decreaseIndent();
}
}