DnsEvent.java revision 627b42494d82eca4fd51abfc0a5d7f330862b881
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net.metrics;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * {@hide}
24 */
25final public class DnsEvent extends IpConnectivityEvent implements Parcelable {
26    public final int netId;
27
28    // The event type is currently only 1 or 2, so we store it as a byte.
29    public final byte[] eventTypes;
30    // Current getaddrinfo codes go from 1 to EAI_MAX = 15. gethostbyname returns errno, but there
31    // are fewer than 255 errno values. So we store the result code in a byte as well.
32    public final byte[] returnCodes;
33    // The latency is an integer because a) short arrays aren't parcelable and b) a short can only
34    // store a maximum latency of 32757 or 65535 ms, which is too short for pathologically slow
35    // queries.
36    public final int[] latenciesMs;
37
38    private DnsEvent(int netId, byte[] eventTypes, byte[] returnCodes, int[] latenciesMs) {
39        this.netId = netId;
40        this.eventTypes = eventTypes;
41        this.returnCodes = returnCodes;
42        this.latenciesMs = latenciesMs;
43    }
44
45    private DnsEvent(Parcel in) {
46        this.netId = in.readInt();
47        this.eventTypes = in.createByteArray();
48        this.returnCodes = in.createByteArray();
49        this.latenciesMs = in.createIntArray();
50    }
51
52    @Override
53    public void writeToParcel(Parcel out, int flags) {
54        out.writeInt(netId);
55        out.writeByteArray(eventTypes);
56        out.writeByteArray(returnCodes);
57        out.writeIntArray(latenciesMs);
58    }
59
60    public int describeContents() {
61        return 0;
62    }
63
64    public static final Parcelable.Creator<DnsEvent> CREATOR = new Parcelable.Creator<DnsEvent>() {
65        @Override
66        public DnsEvent createFromParcel(Parcel in) {
67            return new DnsEvent(in);
68        }
69
70        @Override
71        public DnsEvent[] newArray(int size) {
72            return new DnsEvent[size];
73        }
74    };
75
76    public static void logEvent(
77            int netId, byte[] eventTypes, byte[] returnCodes, int[] latenciesMs) {
78        logEvent(IPCE_DNS_LOOKUPS, new DnsEvent(netId, eventTypes, returnCodes, latenciesMs));
79    }
80}
81