Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a267e48 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

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

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

Change-Id: Ie2f10aa2f1222022eb46aeb5b4a5c250053ac954
parents b7b98179 b74f5480
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -159,7 +159,7 @@ public class 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
+6 −13
Original line number Diff line number Diff line
@@ -215,8 +215,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {

    private final PowerManager.WakeLock mWakeLock;

    private final boolean mUseBpfTrafficStats;

    private final ContentObserver mContentObserver;
    private final ContentResolver mContentResolver;

@@ -438,7 +436,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        mStatsObservers = Objects.requireNonNull(statsObservers, "missing NetworkStatsObservers");
        mSystemDir = Objects.requireNonNull(systemDir, "missing systemDir");
        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");

        final HandlerThread handlerThread = mDeps.makeHandlerThread();
@@ -1084,13 +1081,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        if (callingUid != android.os.Process.SYSTEM_UID && callingUid != uid) {
            return UNSUPPORTED;
        }
        return nativeGetUidStat(uid, type, checkBpfStatsEnable());
        return nativeGetUidStat(uid, type);
    }

    @Override
    public long getIfaceStats(@NonNull String iface, int type) {
        Objects.requireNonNull(iface);
        long nativeIfaceStats = nativeGetIfaceStat(iface, type, checkBpfStatsEnable());
        long nativeIfaceStats = nativeGetIfaceStat(iface, type);
        if (nativeIfaceStats == -1) {
            return nativeIfaceStats;
        } else {
@@ -1104,7 +1101,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {

    @Override
    public long getTotalStats(int type) {
        long nativeTotalStats = nativeGetTotalStat(type, checkBpfStatsEnable());
        long nativeTotalStats = nativeGetTotalStat(type);
        if (nativeTotalStats == -1) {
            return nativeTotalStats;
        } else {
@@ -1137,10 +1134,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        }
    }

    private boolean checkBpfStatsEnable() {
        return mUseBpfTrafficStats;
    }

    /**
     * Update {@link NetworkStatsRecorder} and {@link #mGlobalAlertBytes} 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 nativeGetIfaceStat(String iface, int type, boolean useBpfStats);
    private static native long nativeGetUidStat(int uid, int type, boolean useBpfStats);
    private static native long nativeGetTotalStat(int type);
    private static native long nativeGetIfaceStat(String iface, int type);
    private static native long nativeGetUidStat(int uid, int type);
}
+9 −113
Original line number Diff line number Diff line
@@ -38,9 +38,6 @@ using android::bpf::bpfGetIfaceStats;

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
static const uint64_t UNKNOWN = -1;

@@ -72,86 +69,9 @@ static uint64_t getStatsType(Stats* stats, StatsType type) {
    }
}

static int parseIfaceStats(const char* iface, Stats* stats) {
    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) {
static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type) {
    Stats stats = {};

    if (useBpfStats) {
    if (bpfGetIfaceStats(NULL, &stats) == 0) {
        return getStatsType(&stats, (StatsType) type);
    } else {
@@ -159,15 +79,7 @@ static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type, jboolean useBpfS
    }
}

    if (parseIfaceStats(NULL, &stats) == 0) {
        return getStatsType(&stats, (StatsType) type);
    } else {
        return UNKNOWN;
    }
}

static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type,
                          jboolean useBpfStats) {
static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type) {
    ScopedUtfChars iface8(env, iface);
    if (iface8.c_str() == NULL) {
        return UNKNOWN;
@@ -175,7 +87,6 @@ static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type,

    Stats stats = {};

    if (useBpfStats) {
    if (bpfGetIfaceStats(iface8.c_str(), &stats) == 0) {
        return getStatsType(&stats, (StatsType) type);
    } else {
@@ -183,17 +94,9 @@ static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type,
    }
}

    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 = {};

    if (useBpfStats) {
    if (bpfGetUidStats(uid, &stats) == 0) {
        return getStatsType(&stats, (StatsType) type);
    } else {
@@ -201,17 +104,10 @@ static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type, jboolean
    }
}

    if (parseUidStats(uid, &stats) == 0) {
        return getStatsType(&stats, (StatsType) type);
    } else {
        return UNKNOWN;
    }
}

static const JNINativeMethod gMethods[] = {
    {"nativeGetTotalStat", "(IZ)J", (void*) getTotalStat},
    {"nativeGetIfaceStat", "(Ljava/lang/String;IZ)J", (void*) getIfaceStat},
    {"nativeGetUidStat", "(IIZ)J", (void*) getUidStat},
        {"nativeGetTotalStat", "(I)J", (void*)getTotalStat},
        {"nativeGetIfaceStat", "(Ljava/lang/String;I)J", (void*)getIfaceStat},
        {"nativeGetUidStat", "(II)J", (void*)getUidStat},
};

int register_android_server_net_NetworkStatsService(JNIEnv* env) {