summaryrefslogtreecommitdiffstats
path: root/core/java/android/net/SamplingDataTracker.java
blob: acd56f2dfdad37e29747b618c3f594ae132f8d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net;


import android.os.SystemClock;
import android.util.Slog;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * @hide
 */
public class SamplingDataTracker
{
    private static final boolean DBG = false;
    private static final String  TAG = "SamplingDataTracker";

    public static class SamplingSnapshot
    {
        public long mTxByteCount;
        public long mRxByteCount;
        public long mTxPacketCount;
        public long mRxPacketCount;
        public long mTxPacketErrorCount;
        public long mRxPacketErrorCount;
        public long mTimestamp;
    }

    public static void getSamplingSnapshots(Map<String, SamplingSnapshot> mapIfaceToSample) {

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/net/dev"));

            // Skip over the line bearing column titles (there are 2 lines)
            String line;
            reader.readLine();
            reader.readLine();

            while ((line = reader.readLine()) != null) {

                // remove leading whitespace
                line = line.trim();

                String[] tokens = line.split("[ ]+");
                if (tokens.length < 17) {
                    continue;
                }

                /* column format is
                 * Interface  (Recv)bytes packets errs drop fifo frame compressed multicast \
                 *            (Transmit)bytes packets errs drop fifo colls carrier compress
                */

                String currentIface = tokens[0].split(":")[0];
                if (DBG) Slog.d(TAG, "Found data for interface " + currentIface);
                if (mapIfaceToSample.containsKey(currentIface)) {

                    try {
                        SamplingSnapshot ss = new SamplingSnapshot();

                        ss.mTxByteCount        = Long.parseLong(tokens[1]);
                        ss.mTxPacketCount      = Long.parseLong(tokens[2]);
                        ss.mTxPacketErrorCount = Long.parseLong(tokens[3]);
                        ss.mRxByteCount        = Long.parseLong(tokens[9]);
                        ss.mRxPacketCount      = Long.parseLong(tokens[10]);
                        ss.mRxPacketErrorCount = Long.parseLong(tokens[11]);

                        ss.mTimestamp          = SystemClock.elapsedRealtime();

                        if (DBG) {
                            Slog.d(TAG, "Interface = " + currentIface);
                            Slog.d(TAG, "ByteCount = " + String.valueOf(ss.mTxByteCount));
                            Slog.d(TAG, "TxPacketCount = " + String.valueOf(ss.mTxPacketCount));
                            Slog.d(TAG, "TxPacketErrorCount = "
                                    + String.valueOf(ss.mTxPacketErrorCount));
                            Slog.d(TAG, "RxByteCount = " + String.valueOf(ss.mRxByteCount));
                            Slog.d(TAG, "RxPacketCount = " + String.valueOf(ss.mRxPacketCount));
                            Slog.d(TAG, "RxPacketErrorCount = "
                                    + String.valueOf(ss.mRxPacketErrorCount));
                            Slog.d(TAG, "Timestamp = " + String.valueOf(ss.mTimestamp));
                            Slog.d(TAG, "---------------------------");
                        }

                        mapIfaceToSample.put(currentIface, ss);

                    } catch (NumberFormatException e) {
                        // just ignore this data point
                    }
                }
            }

            if (DBG) {
                Iterator it = mapIfaceToSample.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry kvpair = (Map.Entry)it.next();
                    if (kvpair.getValue() == null) {
                        Slog.d(TAG, "could not find snapshot for interface " + kvpair.getKey());
                    }
                }
            }
        } catch(FileNotFoundException e) {
            Slog.e(TAG, "could not find /proc/net/dev");
        } catch (IOException e) {
            Slog.e(TAG, "could not read /proc/net/dev");
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                Slog.e(TAG, "could not close /proc/net/dev");
            }
        }
    }

    // Snapshots from previous sampling interval
    private SamplingSnapshot mBeginningSample;
    private SamplingSnapshot mEndingSample;

    // Starting snapshot of current interval
    private SamplingSnapshot mLastSample;

    // Protects sampling data from concurrent access
    public final Object mSamplingDataLock = new Object();

    // We need long enough time for a good sample
    private final int MINIMUM_SAMPLING_INTERVAL = 15 * 1000;

    // statistics is useless unless we have enough data
    private final int MINIMUM_SAMPLED_PACKETS   = 30;

    public void startSampling(SamplingSnapshot s) {
        synchronized(mSamplingDataLock) {
            mLastSample = s;
        }
    }

    public void stopSampling(SamplingSnapshot s) {
        synchronized(mSamplingDataLock) {
            if (mLastSample != null) {
                if (s.mTimestamp - mLastSample.mTimestamp > MINIMUM_SAMPLING_INTERVAL
                        && getSampledPacketCount(mLastSample, s) > MINIMUM_SAMPLED_PACKETS) {
                    mBeginningSample = mLastSample;
                    mEndingSample = s;
                    mLastSample = null;
                } else {
                    if (DBG) Slog.d(TAG, "Throwing current sample away because it is too small");
                }
            }
        }
    }

    public void resetSamplingData() {
        if (DBG) Slog.d(TAG, "Resetting sampled network data");
        synchronized(mSamplingDataLock) {

            // We could just take another sample here and treat it as an
            // 'ending sample' effectively shortening sampling interval, but that
            // requires extra work (specifically, reading the sample needs to be
            // done asynchronously)

            mLastSample = null;
        }
    }

    public long getSampledTxByteCount() {
        synchronized(mSamplingDataLock) {
            if (mBeginningSample != null && mEndingSample != null) {
                return mEndingSample.mTxByteCount - mBeginningSample.mTxByteCount;
            } else {
                return LinkQualityInfo.UNKNOWN_LONG;
            }
        }
    }

    public long getSampledTxPacketCount() {
        synchronized(mSamplingDataLock) {
            if (mBeginningSample != null && mEndingSample != null) {
                return mEndingSample.mTxPacketCount - mBeginningSample.mTxPacketCount;
            } else {
                return LinkQualityInfo.UNKNOWN_LONG;
            }
        }
    }

    public long getSampledTxPacketErrorCount() {
        synchronized(mSamplingDataLock) {
            if (mBeginningSample != null && mEndingSample != null) {
                return mEndingSample.mTxPacketErrorCount - mBeginningSample.mTxPacketErrorCount;
            } else {
                return LinkQualityInfo.UNKNOWN_LONG;
            }
        }
    }

    public long getSampledRxByteCount() {
        synchronized(mSamplingDataLock) {
            if (mBeginningSample != null && mEndingSample != null) {
                return mEndingSample.mRxByteCount - mBeginningSample.mRxByteCount;
            } else {
                return LinkQualityInfo.UNKNOWN_LONG;
            }
        }
    }

    public long getSampledRxPacketCount() {
        synchronized(mSamplingDataLock) {
            if (mBeginningSample != null && mEndingSample != null) {
                return mEndingSample.mRxPacketCount - mBeginningSample.mRxPacketCount;
            } else {
                return LinkQualityInfo.UNKNOWN_LONG;
            }
        }
    }

    public long getSampledPacketCount() {
        return getSampledPacketCount(mBeginningSample, mEndingSample);
    }

    public long getSampledPacketCount(SamplingSnapshot begin, SamplingSnapshot end) {
        if (begin != null && end != null) {
            long rxPacketCount = end.mRxPacketCount - begin.mRxPacketCount;
            long txPacketCount = end.mTxPacketCount - begin.mTxPacketCount;
            return rxPacketCount + txPacketCount;
        } else {
            return LinkQualityInfo.UNKNOWN_LONG;
        }
    }

    public long getSampledPacketErrorCount() {
        if (mBeginningSample != null && mEndingSample != null) {
            long rxPacketErrorCount = getSampledRxPacketErrorCount();
            long txPacketErrorCount = getSampledTxPacketErrorCount();
            return rxPacketErrorCount + txPacketErrorCount;
        } else {
            return LinkQualityInfo.UNKNOWN_LONG;
        }
    }

    public long getSampledRxPacketErrorCount() {
        synchronized(mSamplingDataLock) {
            if (mBeginningSample != null && mEndingSample != null) {
                return mEndingSample.mRxPacketErrorCount - mBeginningSample.mRxPacketErrorCount;
            } else {
                return LinkQualityInfo.UNKNOWN_LONG;
            }
        }
    }

    public long getSampleTimestamp() {
        synchronized(mSamplingDataLock) {
            if (mEndingSample != null) {
                return mEndingSample.mTimestamp;
            } else {
                return LinkQualityInfo.UNKNOWN_LONG;
            }
        }
    }

    public int getSampleDuration() {
        synchronized(mSamplingDataLock) {
            if (mBeginningSample != null && mEndingSample != null) {
                return (int) (mEndingSample.mTimestamp - mBeginningSample.mTimestamp);
            } else {
                return LinkQualityInfo.UNKNOWN_INT;
            }
        }
    }

    public void setCommonLinkQualityInfoFields(LinkQualityInfo li) {
        synchronized(mSamplingDataLock) {
            li.setLastDataSampleTime(getSampleTimestamp());
            li.setDataSampleDuration(getSampleDuration());
            li.setPacketCount(getSampledPacketCount());
            li.setPacketErrorCount(getSampledPacketErrorCount());
        }
    }
}