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

Commit 946b7e42 authored by Hugo Benichi's avatar Hugo Benichi
Browse files

ConnectivityMetricsEvent: add ifname, netid, transports

This patch adds new fields to ConnectivityMetricsEvent to make it more
symmetric to IpConnectivityEvent in ipconnectivity.proto.

Follow-up patches will start populating these fields for users of
IpConnectivityLog.

Test: unit tests updated, $ runtest frameworks-net passes
Bug: 34901696
Change-Id: I396767cdfcf38cce893c0d6e1f4524f12e3fdc64
parent ec27c4d9
Loading
Loading
Loading
Loading
+20 −4
Original line number Diff line number Diff line
@@ -19,20 +19,32 @@ package android.net;
import android.os.Parcel;
import android.os.Parcelable;

/** {@hide} */
/**
 * Represents a core networking event defined in package android.net.metrics.
 * Logged by IpConnectivityLog and managed by ConnectivityMetrics service.
 * {@hide}
 * */
public final class ConnectivityMetricsEvent implements Parcelable {

    /**  The time when this event was collected, as returned by System.currentTimeMillis(). */
    /** Time when this event was collected, as returned by System.currentTimeMillis(). */
    public long timestamp;

    /** Transports of the network associated with the event, as defined in NetworkCapabilities. */
    public long transports;
    /** Network id of the network associated with the event, or 0 if unspecified. */
    public int netId;
    /** Name of the network interface associated with the event, or null if unspecified. */
    public String ifname;
    /** Opaque event-specific data. */
    public Parcelable data;

    public ConnectivityMetricsEvent() {
    }

    public ConnectivityMetricsEvent(Parcel in) {
    private ConnectivityMetricsEvent(Parcel in) {
        timestamp = in.readLong();
        transports = in.readLong();
        netId = in.readInt();
        ifname = in.readString();
        data = in.readParcelable(null);
    }

@@ -56,11 +68,15 @@ public final class ConnectivityMetricsEvent implements Parcelable {
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(timestamp);
        dest.writeLong(transports);
        dest.writeInt(netId);
        dest.writeString(ifname);
        dest.writeParcelable(data, 0);
    }

    @Override
    public String toString() {
        // TODO: add transports, netId, ifname
        return String.format("ConnectivityMetricsEvent(%tT.%tL): %s", timestamp, timestamp, data);
    }
}
+33 −10
Original line number Diff line number Diff line
@@ -60,23 +60,22 @@ public class IpConnectivityLog {
    }

    /**
     * Log an IpConnectivity event.
     * @param timestamp is the epoch timestamp of the event in ms.
     * @param data is a Parcelable instance representing the event.
     * Log a ConnectivityMetricsEvent.
     * @param ev the event to log. If the event timestamp is 0,
     * the timestamp is set to the current time in milliseconds.
     * @return true if the event was successfully logged.
     */
    public boolean log(long timestamp, Parcelable data) {
    public boolean log(ConnectivityMetricsEvent ev) {
        if (!checkLoggerService()) {
            if (DBG) {
                Log.d(TAG, SERVICE_NAME + " service was not ready");
            }
            return false;
        }

        if (ev.timestamp == 0) {
            ev.timestamp = System.currentTimeMillis();
        }
        try {
            ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
            ev.timestamp = timestamp;
            ev.data = data;
            int left = mService.logEvent(ev);
            return left >= 0;
        } catch (RemoteException e) {
@@ -85,7 +84,31 @@ public class IpConnectivityLog {
        }
    }

    public void log(Parcelable event) {
        log(System.currentTimeMillis(), event);
    /**
     * Log an IpConnectivity event.
     * @param timestamp is the epoch timestamp of the event in ms.
     * If the timestamp is 0, the timestamp is set to the current time in milliseconds.
     * @param data is a Parcelable instance representing the event.
     * @return true if the event was successfully logged.
     */
    public boolean log(long timestamp, Parcelable data) {
        ConnectivityMetricsEvent ev = makeEv(data);
        ev.timestamp = timestamp;
        return log(ev);
    }

    /**
     * Log an IpConnectivity event.
     * @param data is a Parcelable instance representing the event.
     * @return true if the event was successfully logged.
     */
    public boolean log(Parcelable data) {
        return log(makeEv(data));
    }

    private static ConnectivityMetricsEvent makeEv(Parcelable data) {
        ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
        ev.data = data;
        return ev;
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ public class IpConnectivityMetricsTest extends TestCase {
            new Thread() {
                public void run() {
                    for (int j = 0; j < nEvents; j++) {
                        assertTrue(logger.log(i * 100 + j, FAKE_EV));
                        assertTrue(logger.log(1 + i * 100 + j, FAKE_EV));
                    }
                }
            }.start();
@@ -96,7 +96,7 @@ public class IpConnectivityMetricsTest extends TestCase {
        Iterator<ConnectivityMetricsEvent> iter = got.iterator();
        for (int i = 0; i < nCallers; i++) {
            for (int j = 0; j < nEvents; j++) {
                int expectedTimestamp = i * 100 + j;
                int expectedTimestamp = 1 + i * 100 + j;
                assertEventsEqual(expectedEvent(expectedTimestamp), iter.next());
            }
        }
+4 −7
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.net.metrics.ConnectStats;
import android.net.metrics.DnsEvent;
import android.net.metrics.INetdEventListener;
import android.net.metrics.IpConnectivityLog;
import android.os.Parcelable;
import android.os.RemoteException;
import android.system.OsConstants;
import android.test.suitebuilder.annotation.SmallTest;
@@ -165,8 +166,8 @@ public class NetdEventListenerServiceTest extends TestCase {
        // call onLost() asynchronously to logDnsAsync's onDnsEvent() calls.
        mCallbackCaptor.getValue().onLost(new Network(105));

        // do not verify unpredictable batch
        verify(mLog, timeout(500).times(1)).log(any());
        // do not verify batch with unpredictable length
        verify(mLog, timeout(500).times(1)).log(any(Parcelable.class));
    }

    @SmallTest
@@ -279,11 +280,7 @@ public class NetdEventListenerServiceTest extends TestCase {
    }

    void logDnsAsync(int netId, int[] latencies) {
        new Thread() {
            public void run() {
                log(netId, latencies);
            }
        }.start();
        new Thread(() -> log(netId, latencies)).start();
    }

    void verifyLoggedDnsEvents(DnsEvent... expected) {