blob: 8b222f02a3ca4cfb678ec4eb4b5eaf18a3507ba4 [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 Sharkey163e6442011-10-31 16:37:52 -070028import com.android.internal.util.ProcFileReader;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070029
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070030import java.io.File;
Jeff Sharkey163e6442011-10-31 16:37:52 -070031import java.io.FileInputStream;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070032import java.io.IOException;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070033
34import libcore.io.IoUtils;
35
36/**
37 * Creates {@link NetworkStats} instances by parsing various {@code /proc/}
38 * files as needed.
39 */
40public class NetworkStatsFactory {
41 private static final String TAG = "NetworkStatsFactory";
42
43 // TODO: consider moving parsing to native code
44
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070045 /** Path to {@code /proc/net/xt_qtaguid/iface_stat_all}. */
46 private final File mStatsXtIfaceAll;
Jeff Sharkeye8914c32012-05-01 16:26:09 -070047 /** Path to {@code /proc/net/xt_qtaguid/iface_stat_fmt}. */
48 private final File mStatsXtIfaceFmt;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070049 /** Path to {@code /proc/net/xt_qtaguid/stats}. */
50 private final File mStatsXtUid;
51
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070052 public NetworkStatsFactory() {
53 this(new File("/proc/"));
54 }
55
56 // @VisibleForTesting
57 public NetworkStatsFactory(File procRoot) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070058 mStatsXtIfaceAll = new File(procRoot, "net/xt_qtaguid/iface_stat_all");
Jeff Sharkeye8914c32012-05-01 16:26:09 -070059 mStatsXtIfaceFmt = new File(procRoot, "net/xt_qtaguid/iface_stat_fmt");
60 mStatsXtUid = new File(procRoot, "net/xt_qtaguid/stats");
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070061 }
62
63 /**
Jeff Sharkeye8914c32012-05-01 16:26:09 -070064 * Parse and return interface-level summary {@link NetworkStats} measured
65 * using {@code /proc/net/dev} style hooks, which may include non IP layer
66 * traffic. Values monotonically increase since device boot, and may include
67 * details about inactive interfaces.
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070068 *
69 * @throws IllegalStateException when problem parsing stats.
70 */
Jeff Sharkeye8914c32012-05-01 16:26:09 -070071 public NetworkStats readNetworkStatsSummaryDev() throws IllegalStateException {
Jeff Sharkey453dafa2012-02-27 17:42:34 -080072 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
73
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070074 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
75 final NetworkStats.Entry entry = new NetworkStats.Entry();
76
Jeff Sharkey1d29a302012-02-27 18:08:01 -080077 ProcFileReader reader = null;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070078 try {
Jeff Sharkey1d29a302012-02-27 18:08:01 -080079 reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceAll));
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070080
Jeff Sharkey1d29a302012-02-27 18:08:01 -080081 while (reader.hasMoreData()) {
82 entry.iface = reader.nextString();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070083 entry.uid = UID_ALL;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070084 entry.set = SET_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070085 entry.tag = TAG_NONE;
86
Jeff Sharkey1d29a302012-02-27 18:08:01 -080087 final boolean active = reader.nextInt() != 0;
88
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070089 // always include snapshot values
Jeff Sharkey1d29a302012-02-27 18:08:01 -080090 entry.rxBytes = reader.nextLong();
91 entry.rxPackets = reader.nextLong();
92 entry.txBytes = reader.nextLong();
93 entry.txPackets = reader.nextLong();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070094
95 // fold in active numbers, but only when active
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070096 if (active) {
Jeff Sharkey1d29a302012-02-27 18:08:01 -080097 entry.rxBytes += reader.nextLong();
98 entry.rxPackets += reader.nextLong();
99 entry.txBytes += reader.nextLong();
100 entry.txPackets += reader.nextLong();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700101 }
102
103 stats.addValues(entry);
Jeff Sharkey1d29a302012-02-27 18:08:01 -0800104 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700105 }
106 } catch (NullPointerException e) {
107 throw new IllegalStateException("problem parsing stats: " + e);
108 } catch (NumberFormatException e) {
109 throw new IllegalStateException("problem parsing stats: " + e);
110 } catch (IOException e) {
111 throw new IllegalStateException("problem parsing stats: " + e);
112 } finally {
113 IoUtils.closeQuietly(reader);
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800114 StrictMode.setThreadPolicy(savedPolicy);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700115 }
116 return stats;
117 }
118
119 /**
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700120 * Parse and return interface-level summary {@link NetworkStats}. Designed
121 * to return only IP layer traffic. Values monotonically increase since
122 * device boot, and may include details about inactive interfaces.
123 *
124 * @throws IllegalStateException when problem parsing stats.
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700125 */
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700126 public NetworkStats readNetworkStatsSummaryXt() throws IllegalStateException {
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800127 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
128
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700129 // return null when kernel doesn't support
130 if (!mStatsXtIfaceFmt.exists()) return null;
131
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700132 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6);
133 final NetworkStats.Entry entry = new NetworkStats.Entry();
134
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700135 ProcFileReader reader = null;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700136 try {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700137 // open and consume header line
138 reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceFmt));
139 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700140
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700141 while (reader.hasMoreData()) {
142 entry.iface = reader.nextString();
143 entry.uid = UID_ALL;
144 entry.set = SET_ALL;
145 entry.tag = TAG_NONE;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700146
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700147 entry.rxBytes = reader.nextLong();
148 entry.rxPackets = reader.nextLong();
149 entry.txBytes = reader.nextLong();
150 entry.txPackets = reader.nextLong();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700151
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700152 stats.addValues(entry);
153 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700154 }
155 } catch (NullPointerException e) {
156 throw new IllegalStateException("problem parsing stats: " + e);
157 } catch (NumberFormatException e) {
158 throw new IllegalStateException("problem parsing stats: " + e);
159 } catch (IOException e) {
160 throw new IllegalStateException("problem parsing stats: " + e);
161 } finally {
162 IoUtils.closeQuietly(reader);
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800163 StrictMode.setThreadPolicy(savedPolicy);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700164 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700165 return stats;
166 }
167
168 public NetworkStats readNetworkStatsDetail() {
169 return readNetworkStatsDetail(UID_ALL);
170 }
171
172 /**
173 * Parse and return {@link NetworkStats} with UID-level details. Values
174 * monotonically increase since device boot.
175 *
176 * @throws IllegalStateException when problem parsing stats.
177 */
178 public NetworkStats readNetworkStatsDetail(int limitUid) throws IllegalStateException {
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800179 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
180
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700181 final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 24);
182 final NetworkStats.Entry entry = new NetworkStats.Entry();
183
Jeff Sharkey163e6442011-10-31 16:37:52 -0700184 int idx = 1;
185 int lastIdx = 1;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700186
Jeff Sharkey163e6442011-10-31 16:37:52 -0700187 ProcFileReader reader = null;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700188 try {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700189 // open and consume header line
190 reader = new ProcFileReader(new FileInputStream(mStatsXtUid));
191 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700192
Jeff Sharkey163e6442011-10-31 16:37:52 -0700193 while (reader.hasMoreData()) {
194 idx = reader.nextInt();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700195 if (idx != lastIdx + 1) {
196 throw new IllegalStateException(
197 "inconsistent idx=" + idx + " after lastIdx=" + lastIdx);
198 }
199 lastIdx = idx;
200
Jeff Sharkey163e6442011-10-31 16:37:52 -0700201 entry.iface = reader.nextString();
202 entry.tag = kernelToTag(reader.nextString());
203 entry.uid = reader.nextInt();
204 entry.set = reader.nextInt();
205 entry.rxBytes = reader.nextLong();
206 entry.rxPackets = reader.nextLong();
207 entry.txBytes = reader.nextLong();
208 entry.txPackets = reader.nextLong();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700209
210 if (limitUid == UID_ALL || limitUid == entry.uid) {
211 stats.addValues(entry);
212 }
Jeff Sharkey163e6442011-10-31 16:37:52 -0700213
214 reader.finishLine();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700215 }
216 } catch (NullPointerException e) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700217 throw new IllegalStateException("problem parsing idx " + idx, e);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700218 } catch (NumberFormatException e) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700219 throw new IllegalStateException("problem parsing idx " + idx, e);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700220 } catch (IOException e) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700221 throw new IllegalStateException("problem parsing idx " + idx, e);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700222 } finally {
223 IoUtils.closeQuietly(reader);
Jeff Sharkey453dafa2012-02-27 17:42:34 -0800224 StrictMode.setThreadPolicy(savedPolicy);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700225 }
Jeff Sharkey163e6442011-10-31 16:37:52 -0700226
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700227 return stats;
228 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700229}