Merge "Update UidCounterSetMap directly from NetworkStatsService"
This commit is contained in:
@@ -60,7 +60,6 @@ import static android.text.format.DateUtils.SECOND_IN_MILLIS;
|
||||
import static com.android.net.module.util.NetworkCapabilitiesUtils.getDisplayTransport;
|
||||
import static com.android.net.module.util.NetworkStatsUtils.LIMIT_GLOBAL_ALERT;
|
||||
import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats;
|
||||
import static com.android.server.NetworkManagementSocketTagger.setKernelCounterSet;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
@@ -121,6 +120,7 @@ import android.provider.Settings;
|
||||
import android.provider.Settings.Global;
|
||||
import android.service.NetworkInterfaceProto;
|
||||
import android.service.NetworkStatsServiceDumpProto;
|
||||
import android.system.ErrnoException;
|
||||
import android.telephony.PhoneStateListener;
|
||||
import android.telephony.SubscriptionPlan;
|
||||
import android.text.TextUtils;
|
||||
@@ -139,10 +139,14 @@ import com.android.internal.util.FileRotator;
|
||||
import com.android.net.module.util.BaseNetdUnsolicitedEventListener;
|
||||
import com.android.net.module.util.BestClock;
|
||||
import com.android.net.module.util.BinderUtils;
|
||||
import com.android.net.module.util.BpfMap;
|
||||
import com.android.net.module.util.CollectionUtils;
|
||||
import com.android.net.module.util.IBpfMap;
|
||||
import com.android.net.module.util.LocationPermissionChecker;
|
||||
import com.android.net.module.util.NetworkStatsUtils;
|
||||
import com.android.net.module.util.PermissionUtils;
|
||||
import com.android.net.module.util.Struct.U32;
|
||||
import com.android.net.module.util.Struct.U8;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileDescriptor;
|
||||
@@ -207,6 +211,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
||||
private static final String NETSTATS_COMBINE_SUBTYPE_ENABLED =
|
||||
"netstats_combine_subtype_enabled";
|
||||
|
||||
// This is current path but may be changed soon.
|
||||
private static final String UID_COUNTERSET_MAP_PATH =
|
||||
"/sys/fs/bpf/map_netd_uid_counterset_map";
|
||||
|
||||
private final Context mContext;
|
||||
private final NetworkStatsFactory mStatsFactory;
|
||||
private final AlarmManager mAlarmManager;
|
||||
@@ -325,8 +333,14 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
||||
@GuardedBy("mStatsLock")
|
||||
private NetworkStatsCollection mXtStatsCached;
|
||||
|
||||
/** Current counter sets for each UID. */
|
||||
/**
|
||||
* Current counter sets for each UID.
|
||||
* TODO: maybe remove mActiveUidCounterSet and read UidCouneterSet value from mUidCounterSetMap
|
||||
* directly ? But if mActiveUidCounterSet would be accessed very frequently, maybe keep
|
||||
* mActiveUidCounterSet to avoid accessing kernel too frequently.
|
||||
*/
|
||||
private SparseIntArray mActiveUidCounterSet = new SparseIntArray();
|
||||
private final IBpfMap<U32, U8> mUidCounterSetMap;
|
||||
|
||||
/** Data layer operation counters for splicing into other structures. */
|
||||
private NetworkStats mUidOperations = new NetworkStats(0L, 10);
|
||||
@@ -459,6 +473,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
||||
mLocationPermissionChecker = mDeps.makeLocationPermissionChecker(mContext);
|
||||
mInterfaceMapUpdater = mDeps.makeBpfInterfaceMapUpdater(mContext, mHandler);
|
||||
mInterfaceMapUpdater.start();
|
||||
mUidCounterSetMap = mDeps.getUidCounterSetMap();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -520,6 +535,17 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
||||
@NonNull Context ctx, @NonNull Handler handler) {
|
||||
return new BpfInterfaceMapUpdater(ctx, handler);
|
||||
}
|
||||
|
||||
/** Get counter sets map for each UID. */
|
||||
public IBpfMap<U32, U8> getUidCounterSetMap() {
|
||||
try {
|
||||
return new BpfMap<U32, U8>(UID_COUNTERSET_MAP_PATH, BpfMap.BPF_F_RDWR,
|
||||
U32.class, U8.class);
|
||||
} catch (ErrnoException e) {
|
||||
Log.wtf(TAG, "Cannot create uid counter set map: " + e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1077,6 +1103,29 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
||||
}
|
||||
}
|
||||
|
||||
private void setKernelCounterSet(int uid, int set) {
|
||||
if (mUidCounterSetMap == null) {
|
||||
Log.wtf(TAG, "Fail to set UidCounterSet: Null bpf map");
|
||||
return;
|
||||
}
|
||||
|
||||
if (set == SET_DEFAULT) {
|
||||
try {
|
||||
mUidCounterSetMap.deleteEntry(new U32(uid));
|
||||
} catch (ErrnoException e) {
|
||||
Log.w(TAG, "UidCounterSetMap.deleteEntry(" + uid + ") failed with errno: " + e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
mUidCounterSetMap.updateEntry(new U32(uid), new U8((short) set));
|
||||
} catch (ErrnoException e) {
|
||||
Log.w(TAG, "UidCounterSetMap.updateEntry(" + uid + ", " + set
|
||||
+ ") failed with errno: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setUidForeground(int uid, boolean uidForeground) {
|
||||
PermissionUtils.enforceNetworkStackPermission(mContext);
|
||||
|
||||
Reference in New Issue
Block a user