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

Commit 43be1748 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Add Socket tagging for granular data accounting.

Introduces public API to apply "tags" to track data traffic originating
from the current thread.  (Under the hood, the tags are maintained and
applied in BlockGuard.)  Also adds tag/untag methods for developers who
maintain their own Socket pools.

Change-Id: Ic2dd3155559a93a7b613c7853748d4c44fb3a39e
parent 8c2f85d9
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -10951,6 +10951,7 @@ package android.net {
  public class TrafficStats {
    ctor public TrafficStats();
    method public static void clearThreadStatsTag();
    method public static long getMobileRxBytes();
    method public static long getMobileRxPackets();
    method public static long getMobileTxBytes();
@@ -10971,6 +10972,9 @@ package android.net {
    method public static long getUidUdpRxPackets(int);
    method public static long getUidUdpTxBytes(int);
    method public static long getUidUdpTxPackets(int);
    method public static void setThreadStatsTag(java.lang.String);
    method public static void tagSocket(java.net.Socket) throws java.net.SocketException;
    method public static void untagSocket(java.net.Socket) throws java.net.SocketException;
    field public static final int UNSUPPORTED = -1; // 0xffffffff
  }
+60 −4
Original line number Diff line number Diff line
@@ -16,11 +16,10 @@

package android.net;

import android.util.Log;
import dalvik.system.BlockGuard;

import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;

/**
 * Class that provides network traffic statistics.  These statistics include
@@ -36,6 +35,63 @@ public class TrafficStats {
     */
    public final static int UNSUPPORTED = -1;

    /**
     * Set active tag to use when accounting {@link Socket} traffic originating
     * from the current thread. Only one active tag per thread is supported.
     * <p>
     * Changes only take effect during subsequent calls to
     * {@link #tagSocket(Socket)}.
     */
    public static void setThreadStatsTag(String tag) {
        BlockGuard.setThreadSocketStatsTag(tag);
    }

    public static void clearThreadStatsTag() {
        BlockGuard.setThreadSocketStatsTag(null);
    }

    /**
     * Set specific UID to use when accounting {@link Socket} traffic
     * originating from the current thread. Designed for use when performing an
     * operation on behalf of another application.
     * <p>
     * Changes only take effect during subsequent calls to
     * {@link #tagSocket(Socket)}.
     * <p>
     * To take effect, caller must hold
     * {@link android.Manifest.permission#UPDATE_DEVICE_STATS} permission.
     *
     * {@hide}
     */
    public static void setThreadStatsUid(int uid) {
        BlockGuard.setThreadSocketStatsUid(uid);
    }

    /** {@hide} */
    public static void clearThreadStatsUid() {
        BlockGuard.setThreadSocketStatsUid(-1);
    }

    /**
     * Tag the given {@link Socket} with any statistics parameters active for
     * the current thread. Subsequent calls always replace any existing
     * parameters. When finished, call {@link #untagSocket(Socket)} to remove
     * statistics parameters.
     *
     * @see #setThreadStatsTag(String)
     * @see #setThreadStatsUid(int)
     */
    public static void tagSocket(Socket socket) throws SocketException {
        BlockGuard.tagSocketFd(socket.getFileDescriptor$());
    }

    /**
     * Remove any statistics parameters from the given {@link Socket}.
     */
    public static void untagSocket(Socket socket) throws SocketException {
        BlockGuard.untagSocketFd(socket.getFileDescriptor$());
    }

    /**
     * Get the total number of packets transmitted through the mobile interface.
     *