Merge changes I044d0363,I0be3b802,Ib584ea57,I92291023 am: 10640379e4

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1888339

Change-Id: Ie6ea902d71f69457ddc85b5be41c9dbc7086dfbd
This commit is contained in:
Treehugger Robot
2021-11-18 08:12:33 +00:00
committed by Automerger Merge Worker
3 changed files with 16 additions and 127 deletions

View File

@@ -159,7 +159,7 @@ public class NetworkStatsFactory {
} }
public NetworkStatsFactory() { public NetworkStatsFactory() {
this(new File("/proc/"), new File("/sys/fs/bpf/map_netd_app_uid_stats_map").exists()); this(new File("/proc/"), true);
} }
@VisibleForTesting @VisibleForTesting

View File

@@ -215,8 +215,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
private final PowerManager.WakeLock mWakeLock; private final PowerManager.WakeLock mWakeLock;
private final boolean mUseBpfTrafficStats;
private final ContentObserver mContentObserver; private final ContentObserver mContentObserver;
private final ContentResolver mContentResolver; private final ContentResolver mContentResolver;
@@ -438,7 +436,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
mStatsObservers = Objects.requireNonNull(statsObservers, "missing NetworkStatsObservers"); mStatsObservers = Objects.requireNonNull(statsObservers, "missing NetworkStatsObservers");
mSystemDir = Objects.requireNonNull(systemDir, "missing systemDir"); mSystemDir = Objects.requireNonNull(systemDir, "missing systemDir");
mBaseDir = Objects.requireNonNull(baseDir, "missing baseDir"); mBaseDir = Objects.requireNonNull(baseDir, "missing baseDir");
mUseBpfTrafficStats = new File("/sys/fs/bpf/map_netd_app_uid_stats_map").exists();
mDeps = Objects.requireNonNull(deps, "missing Dependencies"); mDeps = Objects.requireNonNull(deps, "missing Dependencies");
final HandlerThread handlerThread = mDeps.makeHandlerThread(); final HandlerThread handlerThread = mDeps.makeHandlerThread();
@@ -1084,13 +1081,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
if (callingUid != android.os.Process.SYSTEM_UID && callingUid != uid) { if (callingUid != android.os.Process.SYSTEM_UID && callingUid != uid) {
return UNSUPPORTED; return UNSUPPORTED;
} }
return nativeGetUidStat(uid, type, checkBpfStatsEnable()); return nativeGetUidStat(uid, type);
} }
@Override @Override
public long getIfaceStats(@NonNull String iface, int type) { public long getIfaceStats(@NonNull String iface, int type) {
Objects.requireNonNull(iface); Objects.requireNonNull(iface);
long nativeIfaceStats = nativeGetIfaceStat(iface, type, checkBpfStatsEnable()); long nativeIfaceStats = nativeGetIfaceStat(iface, type);
if (nativeIfaceStats == -1) { if (nativeIfaceStats == -1) {
return nativeIfaceStats; return nativeIfaceStats;
} else { } else {
@@ -1104,7 +1101,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
@Override @Override
public long getTotalStats(int type) { public long getTotalStats(int type) {
long nativeTotalStats = nativeGetTotalStat(type, checkBpfStatsEnable()); long nativeTotalStats = nativeGetTotalStat(type);
if (nativeTotalStats == -1) { if (nativeTotalStats == -1) {
return nativeTotalStats; return nativeTotalStats;
} else { } else {
@@ -1137,10 +1134,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
} }
private boolean checkBpfStatsEnable() {
return mUseBpfTrafficStats;
}
/** /**
* Update {@link NetworkStatsRecorder} and {@link #mGlobalAlertBytes} to * Update {@link NetworkStatsRecorder} and {@link #mGlobalAlertBytes} to
* reflect current {@link #mPersistThreshold} value. Always defers to * reflect current {@link #mPersistThreshold} value. Always defers to
@@ -2249,7 +2242,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
} }
private static native long nativeGetTotalStat(int type, boolean useBpfStats); private static native long nativeGetTotalStat(int type);
private static native long nativeGetIfaceStat(String iface, int type, boolean useBpfStats); private static native long nativeGetIfaceStat(String iface, int type);
private static native long nativeGetUidStat(int uid, int type, boolean useBpfStats); private static native long nativeGetUidStat(int uid, int type);
} }

View File

@@ -38,9 +38,6 @@ using android::bpf::bpfGetIfaceStats;
namespace android { namespace android {
static const char* QTAGUID_IFACE_STATS = "/proc/net/xt_qtaguid/iface_stat_fmt";
static const char* QTAGUID_UID_STATS = "/proc/net/xt_qtaguid/stats";
// NOTE: keep these in sync with TrafficStats.java // NOTE: keep these in sync with TrafficStats.java
static const uint64_t UNKNOWN = -1; static const uint64_t UNKNOWN = -1;
@@ -72,102 +69,17 @@ static uint64_t getStatsType(Stats* stats, StatsType type) {
} }
} }
static int parseIfaceStats(const char* iface, Stats* stats) { static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type) {
FILE *fp = fopen(QTAGUID_IFACE_STATS, "r");
if (fp == NULL) {
return -1;
}
char buffer[384];
char cur_iface[32];
bool foundTcp = false;
uint64_t rxBytes, rxPackets, txBytes, txPackets, tcpRxPackets, tcpTxPackets;
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
int matched = sscanf(buffer, "%31s %" SCNu64 " %" SCNu64 " %" SCNu64
" %" SCNu64 " " "%*u %" SCNu64 " %*u %*u %*u %*u "
"%*u %" SCNu64 " %*u %*u %*u %*u", cur_iface, &rxBytes,
&rxPackets, &txBytes, &txPackets, &tcpRxPackets, &tcpTxPackets);
if (matched >= 5) {
if (matched == 7) {
foundTcp = true;
}
if (!iface || !strcmp(iface, cur_iface)) {
stats->rxBytes += rxBytes;
stats->rxPackets += rxPackets;
stats->txBytes += txBytes;
stats->txPackets += txPackets;
if (matched == 7) {
stats->tcpRxPackets += tcpRxPackets;
stats->tcpTxPackets += tcpTxPackets;
}
}
}
}
if (!foundTcp) {
stats->tcpRxPackets = UNKNOWN;
stats->tcpTxPackets = UNKNOWN;
}
if (fclose(fp) != 0) {
return -1;
}
return 0;
}
static int parseUidStats(const uint32_t uid, Stats* stats) {
FILE *fp = fopen(QTAGUID_UID_STATS, "r");
if (fp == NULL) {
return -1;
}
char buffer[384];
char iface[32];
uint32_t idx, cur_uid, set;
uint64_t tag, rxBytes, rxPackets, txBytes, txPackets;
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
if (sscanf(buffer,
"%" SCNu32 " %31s 0x%" SCNx64 " %u %u %" SCNu64 " %" SCNu64
" %" SCNu64 " %" SCNu64 "",
&idx, iface, &tag, &cur_uid, &set, &rxBytes, &rxPackets,
&txBytes, &txPackets) == 9) {
if (uid == cur_uid && tag == 0L) {
stats->rxBytes += rxBytes;
stats->rxPackets += rxPackets;
stats->txBytes += txBytes;
stats->txPackets += txPackets;
}
}
}
if (fclose(fp) != 0) {
return -1;
}
return 0;
}
static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type, jboolean useBpfStats) {
Stats stats = {}; Stats stats = {};
if (useBpfStats) {
if (bpfGetIfaceStats(NULL, &stats) == 0) { if (bpfGetIfaceStats(NULL, &stats) == 0) {
return getStatsType(&stats, (StatsType) type); return getStatsType(&stats, (StatsType) type);
} else { } else {
return UNKNOWN; return UNKNOWN;
} }
}
if (parseIfaceStats(NULL, &stats) == 0) {
return getStatsType(&stats, (StatsType) type);
} else {
return UNKNOWN;
}
} }
static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type, static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type) {
jboolean useBpfStats) {
ScopedUtfChars iface8(env, iface); ScopedUtfChars iface8(env, iface);
if (iface8.c_str() == NULL) { if (iface8.c_str() == NULL) {
return UNKNOWN; return UNKNOWN;
@@ -175,43 +87,27 @@ static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type,
Stats stats = {}; Stats stats = {};
if (useBpfStats) {
if (bpfGetIfaceStats(iface8.c_str(), &stats) == 0) { if (bpfGetIfaceStats(iface8.c_str(), &stats) == 0) {
return getStatsType(&stats, (StatsType) type); return getStatsType(&stats, (StatsType) type);
} else { } else {
return UNKNOWN; return UNKNOWN;
} }
}
if (parseIfaceStats(iface8.c_str(), &stats) == 0) {
return getStatsType(&stats, (StatsType) type);
} else {
return UNKNOWN;
}
} }
static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type, jboolean useBpfStats) { static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type) {
Stats stats = {}; Stats stats = {};
if (useBpfStats) {
if (bpfGetUidStats(uid, &stats) == 0) { if (bpfGetUidStats(uid, &stats) == 0) {
return getStatsType(&stats, (StatsType) type); return getStatsType(&stats, (StatsType) type);
} else { } else {
return UNKNOWN; return UNKNOWN;
} }
}
if (parseUidStats(uid, &stats) == 0) {
return getStatsType(&stats, (StatsType) type);
} else {
return UNKNOWN;
}
} }
static const JNINativeMethod gMethods[] = { static const JNINativeMethod gMethods[] = {
{"nativeGetTotalStat", "(IZ)J", (void*) getTotalStat}, {"nativeGetTotalStat", "(I)J", (void*)getTotalStat},
{"nativeGetIfaceStat", "(Ljava/lang/String;IZ)J", (void*) getIfaceStat}, {"nativeGetIfaceStat", "(Ljava/lang/String;I)J", (void*)getIfaceStat},
{"nativeGetUidStat", "(IIZ)J", (void*) getUidStat}, {"nativeGetUidStat", "(II)J", (void*)getUidStat},
}; };
int register_android_server_net_NetworkStatsService(JNIEnv* env) { int register_android_server_net_NetworkStatsService(JNIEnv* env) {