blob: c517a68e2b90d80668b80c4a4da61cbe48531161 [file] [log] [blame]
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001/*
2 * Copyright (C) 2011 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 com.android.internal.net;
18
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070019import static android.net.NetworkStats.SET_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070020import static android.net.NetworkStats.TAG_NONE;
21import static android.net.NetworkStats.UID_ALL;
22import static com.android.server.NetworkManagementSocketTagger.kernelToTag;
23
24import android.net.NetworkStats;
Jeff Sharkey453dafa2012-02-27 17:42:34 -080025import android.os.StrictMode;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070026import android.os.SystemClock;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070027
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080028import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey163e6442011-10-31 16:37:52 -070029import com.android.internal.util.ProcFileReader;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070030
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070031import java.io.File;
Jeff Sharkey163e6442011-10-31 16:37:52 -070032import java.io.FileInputStream;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070033import java.io.IOException;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070034
35import libcore.io.IoUtils;
36
37/**
38 * Creates {@link NetworkStats} instances by parsing various {@code /proc/}
39 * files as needed.
40 */
41public class NetworkStatsFactory {
42 private static final String TAG = "NetworkStatsFactory";
43
44 // TODO: consider moving parsing to native code
45
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070046 /** Path to {@code /proc/net/xt_qtaguid/iface_stat_all}. */
47 private final File mStatsXtIfaceAll;
Jeff Sharkeye8914c32012-05-01 16:26:09 -070048 /** Path to {@code /proc/net/xt_qtaguid/iface_stat_fmt}. */
49 private final File mStatsXtIfaceFmt;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070050 /** Path to {@code /proc/net/xt_qtaguid/stats}. */
51 private final File mStatsXtUid;
52
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070053 public NetworkStatsFactory() {
54 this(new File("/proc/"));
55 }
56
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080057 @VisibleForTesting
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070058 public NetworkStatsFactory(File procRoot) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070059 mStatsXtIfaceAll = new File(procRoot, "net/xt_qtaguid/iface_stat_all");
Jeff Sharkeye8914c32012-05-01 16:26:09 -070060 mStatsXtIfaceFmt = new File(procRoot, "net/xt_qtaguid/iface_stat_fmt");
61 mStatsXtUid = new File(procRoot, "net/xt_qtaguid/stats");
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070062 }
63
64 /**
Jeff Sharkeye8914c32012-05-01 16:26:09 -070065 * Parse and return interface-level summary {@link NetworkStats} measured
66 * using {@code /proc/net/dev} style hooks, which may include non IP layer
67 * traffic. Values monotonically increase since device boot, and may include
68 * details about inactive interfaces.
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070069 *
70 * @throws IllegalStateException when problem parsing stats.
71 */
Jeff Sharkeye8914c32012-05-01 16:26:09 -070072 public NetworkStats readNetworkStatsSummaryDev() throws IllegalStateException {
Jeff Sharkey453dafa2012-02-27 17:42:34 -080073 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
74
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070075 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
76 final NetworkStats.Entry entry = new NetworkStats.Entry();
77
Jeff Sharkey1d29a302012-02-27 18:08:01 -080078 ProcFileReader reader = null;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070079 try {
Jeff Sharkey1d29a302012-02-27 18:08:01 -080080 reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceAll));
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070081
Jeff Sharkey1d29a302012-02-27 18:08:01 -080082 while (reader.hasMoreData()) {
83 entry.iface = reader.nextString();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070084 entry.uid = UID_ALL;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070085 entry.set = SET_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070086 entry.tag = TAG_NONE;
87
Jeff Sharkey1d29a302012-02-27 18:08:01 -080088 final boolean active = reader.nextInt() != 0;
89
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070090 // always include snapshot values
Jeff Sharkey1d29a302012-02-27 18:08:01 -080091 entry.rxBytes = reader.nextLong();
92 entry.rxPackets = reader.nextLong();
93 entry.txBytes = reader.nextLong();
94 entry.txPackets = reader.nextLong();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070095
96 // fold in active numbers, but only when active
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070097 if (active) {
Jeff Sharkey1d29a302012-02-27 18:08:01 -080098 entry.rxBytes += reader.nextLong();
99 entry.rxPackets += reader.nextLong();
100 entry.txBytes += reader.nextLong();
101 entry.txPackets += reader.nextLong();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700102 }
103
104 stats.addValues(entry);
Jeff Sharkey1d29a302012-02-27 18:08:01 -0800105 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700106 }
107 } catch (NullPointerException e) {
108 throw new IllegalStateException("problem parsing stats: " + e);
109 } catch (NumberFormatException e) {
110 throw new IllegalStateException("problem parsing stats: " + e);
111 } catch (IOException e) {
112 throw new IllegalStateException("problem parsing stats: " + e);
113 } finally {
114 IoUtils.closeQuietly(reader);
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800115 StrictMode.setThreadPolicy(savedPolicy);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700116 }
117 return stats;
118 }
119
120 /**
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700121 * Parse and return interface-level summary {@link NetworkStats}. Designed
122 * to return only IP layer traffic. Values monotonically increase since
123 * device boot, and may include details about inactive interfaces.
124 *
125 * @throws IllegalStateException when problem parsing stats.
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700126 */
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700127 public NetworkStats readNetworkStatsSummaryXt() throws IllegalStateException {
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800128 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
129
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700130 // return null when kernel doesn't support
131 if (!mStatsXtIfaceFmt.exists()) return null;
132
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700133 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
134 final NetworkStats.Entry entry = new NetworkStats.Entry();
135
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700136 ProcFileReader reader = null;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700137 try {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700138 // open and consume header line
139 reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceFmt));
140 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700141
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700142 while (reader.hasMoreData()) {
143 entry.iface = reader.nextString();
144 entry.uid = UID_ALL;
145 entry.set = SET_ALL;
146 entry.tag = TAG_NONE;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700147
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700148 entry.rxBytes = reader.nextLong();
149 entry.rxPackets = reader.nextLong();
150 entry.txBytes = reader.nextLong();
151 entry.txPackets = reader.nextLong();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700152
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700153 stats.addValues(entry);
154 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700155 }
156 } catch (NullPointerException e) {
157 throw new IllegalStateException("problem parsing stats: " + e);
158 } catch (NumberFormatException e) {
159 throw new IllegalStateException("problem parsing stats: " + e);
160 } catch (IOException e) {
161 throw new IllegalStateException("problem parsing stats: " + e);
162 } finally {
163 IoUtils.closeQuietly(reader);
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800164 StrictMode.setThreadPolicy(savedPolicy);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700165 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700166 return stats;
167 }
168
169 public NetworkStats readNetworkStatsDetail() {
170 return readNetworkStatsDetail(UID_ALL);
171 }
172
173 /**
174 * Parse and return {@link NetworkStats} with UID-level details. Values
175 * monotonically increase since device boot.
176 *
177 * @throws IllegalStateException when problem parsing stats.
178 */
179 public NetworkStats readNetworkStatsDetail(int limitUid) throws IllegalStateException {
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800180 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
181
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700182 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 24);
183 final NetworkStats.Entry entry = new NetworkStats.Entry();
184
Jeff Sharkey163e6442011-10-31 16:37:52 -0700185 int idx = 1;
186 int lastIdx = 1;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700187
Jeff Sharkey163e6442011-10-31 16:37:52 -0700188 ProcFileReader reader = null;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700189 try {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700190 // open and consume header line
191 reader = new ProcFileReader(new FileInputStream(mStatsXtUid));
192 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700193
Jeff Sharkey163e6442011-10-31 16:37:52 -0700194 while (reader.hasMoreData()) {
195 idx = reader.nextInt();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700196 if (idx != lastIdx + 1) {
197 throw new IllegalStateException(
198 "inconsistent idx=" + idx + " after lastIdx=" + lastIdx);
199 }
200 lastIdx = idx;
201
Jeff Sharkey163e6442011-10-31 16:37:52 -0700202 entry.iface = reader.nextString();
203 entry.tag = kernelToTag(reader.nextString());
204 entry.uid = reader.nextInt();
205 entry.set = reader.nextInt();
206 entry.rxBytes = reader.nextLong();
207 entry.rxPackets = reader.nextLong();
208 entry.txBytes = reader.nextLong();
209 entry.txPackets = reader.nextLong();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700210
211 if (limitUid == UID_ALL || limitUid == entry.uid) {
212 stats.addValues(entry);
213 }
Jeff Sharkey163e6442011-10-31 16:37:52 -0700214
215 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700216 }
217 } catch (NullPointerException e) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700218 throw new IllegalStateException("problem parsing idx " + idx, e);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700219 } catch (NumberFormatException e) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700220 throw new IllegalStateException("problem parsing idx " + idx, e);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700221 } catch (IOException e) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700222 throw new IllegalStateException("problem parsing idx " + idx, e);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700223 } finally {
224 IoUtils.closeQuietly(reader);
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800225 StrictMode.setThreadPolicy(savedPolicy);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700226 }
Jeff Sharkey163e6442011-10-31 16:37:52 -0700227
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700228 return stats;
229 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700230}