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

Commit c39c1d4d authored by Ashish Sharma's avatar Ashish Sharma
Browse files

TrafficStats: Include UDP stats and packet count to API



Change-Id: I70063e6033d3c181bcc5b9e401f9474900e25281
Signed-off-by: default avatarAshish Sharma <ashishsharma@google.com>
parent e8e0ebbf
Loading
Loading
Loading
Loading
+131 −1
Original line number Diff line number Diff line
@@ -113730,6 +113730,71 @@
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidRxPackets"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidTcpRxBytes"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidTcpRxSegments"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidTcpTxBytes"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidTcpTxSegments"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidTxBytes"
 return="long"
 abstract="false"
@@ -113743,6 +113808,71 @@
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidTxPackets"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidUdpRxBytes"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidUdpRxPackets"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidUdpTxBytes"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<method name="getUidUdpTxPackets"
 return="long"
 abstract="false"
 native="true"
 synchronized="false"
 static="true"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="uid" type="int">
</parameter>
</method>
<field name="UNSUPPORTED"
 type="int"
 transient="false"
@@ -263474,7 +263604,7 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="t" type="T">
<parameter name="arg0" type="T">
</parameter>
</method>
</interface>
+135 −0
Original line number Diff line number Diff line
@@ -122,4 +122,139 @@ public class TrafficStats {
     * @return number of bytes
     */
    public static native long getUidRxBytes(int uid);

    /**
     * Get the number of packets (TCP segments + UDP) sent through
     * the network for this UID.
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of packets.
     * If the statistics are not supported by this device,
     * {@link #UNSUPPORTED} will be returned.
     */
    public static native long getUidTxPackets(int uid);

    /**
     * Get the number of packets (TCP segments + UDP) received through
     * the network for this UID.
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of packets
     */
    public static native long getUidRxPackets(int uid);

    /**
     * Get the number of TCP payload bytes sent for this UID.
     * This total does not include protocol and control overheads at
     * the transport and the lower layers of the networking stack.
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of bytes.  If the statistics are not supported by this device,
     * {@link #UNSUPPORTED} will be returned.
     */
    public static native long getUidTcpTxBytes(int uid);

    /**
     * Get the number of TCP payload bytes received for this UID.
     * This total does not include protocol and control overheads at
     * the transport and the lower layers of the networking stack.
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of bytes.  If the statistics are not supported by this device,
     * {@link #UNSUPPORTED} will be returned.
     */
    public static native long getUidTcpRxBytes(int uid);

    /**
     * Get the number of UDP payload bytes sent for this UID.
     * This total does not include protocol and control overheads at
     * the transport and the lower layers of the networking stack.
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of bytes.  If the statistics are not supported by this device,
     * {@link #UNSUPPORTED} will be returned.
     */
    public static native long getUidUdpTxBytes(int uid);

    /**
     * Get the number of UDP payload bytes received for this UID.
     * This total does not include protocol and control overheads at
     * the transport and the lower layers of the networking stack.
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of bytes.  If the statistics are not supported by this device,
     * {@link #UNSUPPORTED} will be returned.
     */
    public static native long getUidUdpRxBytes(int uid);

    /**
     * Get the number of TCP segments sent for this UID.
     * Does not include TCP control packets (SYN/ACKs/FIN/..).
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of TCP segments.  If the statistics are not supported by this device,
     * {@link #UNSUPPORTED} will be returned.
     */
    public static native long getUidTcpTxSegments(int uid);

    /**
     * Get the number of TCP payload bytes received for this UID.
     * Does not include TCP control packets (SYN/ACKs/FIN/..).
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of TCP segments.  If the statistics are not supported by this device,
     * {@link #UNSUPPORTED} will be returned.
     */
    public static native long getUidTcpRxSegments(int uid);


    /**
     * Get the number of UDP packets sent for this UID.
     * Includes DNS requests.
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of packets.  If the statistics are not supported by this device,
     * {@link #UNSUPPORTED} will be returned.
     */
    public static native long getUidUdpTxPackets(int uid);

    /**
     * Get the number of UDP packets received for this UID.
     * Includes DNS responses.
     * The statistics are across all interfaces.
     *
     * {@see android.os.Process#myUid()}.
     *
     * @param uid The UID of the process to examine.
     * @return number of packets.  If the statistics are not supported by this device,
     * {@link #UNSUPPORTED} will be returned.
     */
    public static native long getUidUdpRxPackets(int uid);
}
+151 −6
Original line number Diff line number Diff line
@@ -30,6 +30,17 @@

namespace android {

enum Tx_Rx {
    TX,
    RX
};

enum Tcp_Udp {
    TCP,
    UDP,
    TCP_AND_UDP
};

// Returns an ASCII decimal number read from the specified file, -1 on error.
static jlong readNumber(char const* filename) {
#ifdef HAVE_ANDROID_OS
@@ -140,16 +151,136 @@ static jlong getTotalRxBytes(JNIEnv* env, jobject clazz) {

// Per-UID stats require reading from a constructed filename.

static jlong getUidBytes(JNIEnv* env, jobject clazz, jint uid,
                         enum Tx_Rx tx_or_rx, enum Tcp_Udp tcp_or_udp) {
    char tcp_filename[80], udp_filename[80];
    jlong tcp_bytes = -1, udp_bytes = -1, total_bytes = -1;

    switch (tx_or_rx) {
        case TX:
            sprintf(tcp_filename, "/proc/uid_stat/%d/tcp_snd", uid);
            sprintf(udp_filename, "/proc/uid_stat/%d/udp_snd", uid);
            break;
        case RX:
            sprintf(tcp_filename, "/proc/uid_stat/%d/tcp_rcv", uid);
            sprintf(udp_filename, "/proc/uid_stat/%d/udp_rcv", uid);
            break;
        default:
            return -1;
    }

    switch (tcp_or_udp) {
        case TCP:
            tcp_bytes = readNumber(tcp_filename);
            total_bytes = (tcp_bytes >= 0) ? tcp_bytes : -1;
            break;
        case UDP:
            udp_bytes = readNumber(udp_filename);
            total_bytes = (udp_bytes >= 0) ? udp_bytes : -1;
            break;
        case TCP_AND_UDP:
            tcp_bytes = readNumber(tcp_filename);
            total_bytes += (tcp_bytes >= 0 ? tcp_bytes : 0);

            udp_bytes = readNumber(udp_filename);
            total_bytes += (udp_bytes >= 0 ? udp_bytes : 0);
            break;
        default:
            return -1;
    }

    return total_bytes;
}

static jlong getUidPkts(JNIEnv* env, jobject clazz, jint uid,
                         enum Tx_Rx tx_or_rx, enum Tcp_Udp tcp_or_udp) {
    char tcp_filename[80], udp_filename[80];
    jlong tcp_pkts = -1, udp_pkts = -1, total_pkts = -1;

    switch (tx_or_rx) {
        case TX:
            sprintf(tcp_filename, "/proc/uid_stat/%d/tcp_snd_pkt", uid);
            sprintf(udp_filename, "/proc/uid_stat/%d/udp_snd_pkt", uid);
            break;
        case RX:
            sprintf(tcp_filename, "/proc/uid_stat/%d/tcp_rcv_pkt", uid);
            sprintf(udp_filename, "/proc/uid_stat/%d/udp_rcv_pkt", uid);
            break;
        default:
            return -1;
    }

    switch (tcp_or_udp) {
        case TCP:
            tcp_pkts = readNumber(tcp_filename);
            total_pkts = (tcp_pkts >= 0) ? tcp_pkts : -1;
            break;
        case UDP:
            udp_pkts = readNumber(udp_filename);
            total_pkts = (udp_pkts >= 0) ? udp_pkts : -1;
            break;
        case TCP_AND_UDP:
            tcp_pkts = readNumber(tcp_filename);
            total_pkts += (tcp_pkts >= 0 ? tcp_pkts : 0);

            udp_pkts = readNumber(udp_filename);
            total_pkts += (udp_pkts >= 0 ? udp_pkts : 0);
            break;
        default:
            return -1;
    }

    return total_pkts;
}

static jlong getUidRxBytes(JNIEnv* env, jobject clazz, jint uid) {
    char filename[80];
    sprintf(filename, "/proc/uid_stat/%d/tcp_rcv", uid);
    return readNumber(filename);
    return getUidBytes(env, clazz, uid, RX, TCP_AND_UDP);
}

static jlong getUidTxBytes(JNIEnv* env, jobject clazz, jint uid) {
    char filename[80];
    sprintf(filename, "/proc/uid_stat/%d/tcp_snd", uid);
    return readNumber(filename);
    return getUidBytes(env, clazz, uid, TX, TCP_AND_UDP);
}

/* TCP Segments + UDP Packets */
static jlong getUidTxPackets(JNIEnv* env, jobject clazz, jint uid) {
    return getUidPkts(env, clazz, uid, TX, TCP_AND_UDP);
}

/* TCP Segments + UDP Packets */
static jlong getUidRxPackets(JNIEnv* env, jobject clazz, jint uid) {
    return getUidPkts(env, clazz, uid, RX, TCP_AND_UDP);
}

static jlong getUidTcpTxBytes(JNIEnv* env, jobject clazz, jint uid) {
    return getUidBytes(env, clazz, uid, TX, TCP);
}

static jlong getUidTcpRxBytes(JNIEnv* env, jobject clazz, jint uid) {
    return getUidBytes(env, clazz, uid, RX, TCP);
}

static jlong getUidUdpTxBytes(JNIEnv* env, jobject clazz, jint uid) {
    return getUidBytes(env, clazz, uid, TX, UDP);
}

static jlong getUidUdpRxBytes(JNIEnv* env, jobject clazz, jint uid) {
    return getUidBytes(env, clazz, uid, RX, UDP);
}

static jlong getUidTcpTxSegments(JNIEnv* env, jobject clazz, jint uid) {
    return getUidPkts(env, clazz, uid, TX, TCP);
}

static jlong getUidTcpRxSegments(JNIEnv* env, jobject clazz, jint uid) {
    return getUidPkts(env, clazz, uid, RX, TCP);
}

static jlong getUidUdpTxPackets(JNIEnv* env, jobject clazz, jint uid) {
    return getUidPkts(env, clazz, uid, TX, UDP);
}

static jlong getUidUdpRxPackets(JNIEnv* env, jobject clazz, jint uid) {
    return getUidPkts(env, clazz, uid, RX, UDP);
}

static JNINativeMethod gMethods[] = {
@@ -161,8 +292,22 @@ static JNINativeMethod gMethods[] = {
    {"getTotalRxPackets", "()J", (void*) getTotalRxPackets},
    {"getTotalTxBytes", "()J", (void*) getTotalTxBytes},
    {"getTotalRxBytes", "()J", (void*) getTotalRxBytes},

    /* Per-UID Stats */
    {"getUidTxBytes", "(I)J", (void*) getUidTxBytes},
    {"getUidRxBytes", "(I)J", (void*) getUidRxBytes},
    {"getUidTxPackets", "(I)J", (void*) getUidTxPackets},
    {"getUidRxPackets", "(I)J", (void*) getUidRxPackets},

    {"getUidTcpTxBytes", "(I)J", (void*) getUidTcpTxBytes},
    {"getUidTcpRxBytes", "(I)J", (void*) getUidTcpRxBytes},
    {"getUidUdpTxBytes", "(I)J", (void*) getUidUdpTxBytes},
    {"getUidUdpRxBytes", "(I)J", (void*) getUidUdpRxBytes},

    {"getUidTcpTxSegments", "(I)J", (void*) getUidTcpTxSegments},
    {"getUidTcpRxSegments", "(I)J", (void*) getUidTcpRxSegments},
    {"getUidUdpTxPackets", "(I)J", (void*) getUidUdpTxPackets},
    {"getUidUdpRxPackets", "(I)J", (void*) getUidUdpRxPackets},
};

int register_android_net_TrafficStats(JNIEnv* env) {