Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.net; |
| 18 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 19 | import static android.net.NetworkStats.SET_ALL; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 20 | import static android.net.NetworkStats.TAG_NONE; |
| 21 | import static android.net.NetworkStats.UID_ALL; |
| 22 | import static com.android.server.NetworkManagementSocketTagger.kernelToTag; |
| 23 | |
| 24 | import android.net.NetworkStats; |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 25 | import android.os.StrictMode; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 26 | import android.os.SystemClock; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 27 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 28 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 29 | import com.android.internal.util.ProcFileReader; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 30 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 31 | import java.io.File; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 32 | import java.io.FileInputStream; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 33 | import java.io.IOException; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 34 | |
| 35 | import libcore.io.IoUtils; |
| 36 | |
| 37 | /** |
| 38 | * Creates {@link NetworkStats} instances by parsing various {@code /proc/} |
| 39 | * files as needed. |
| 40 | */ |
| 41 | public class NetworkStatsFactory { |
| 42 | private static final String TAG = "NetworkStatsFactory"; |
| 43 | |
| 44 | // TODO: consider moving parsing to native code |
| 45 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 46 | /** Path to {@code /proc/net/xt_qtaguid/iface_stat_all}. */ |
| 47 | private final File mStatsXtIfaceAll; |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 48 | /** Path to {@code /proc/net/xt_qtaguid/iface_stat_fmt}. */ |
| 49 | private final File mStatsXtIfaceFmt; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 50 | /** Path to {@code /proc/net/xt_qtaguid/stats}. */ |
| 51 | private final File mStatsXtUid; |
| 52 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 53 | public NetworkStatsFactory() { |
| 54 | this(new File("/proc/")); |
| 55 | } |
| 56 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 57 | @VisibleForTesting |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 58 | public NetworkStatsFactory(File procRoot) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 59 | mStatsXtIfaceAll = new File(procRoot, "net/xt_qtaguid/iface_stat_all"); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 60 | mStatsXtIfaceFmt = new File(procRoot, "net/xt_qtaguid/iface_stat_fmt"); |
| 61 | mStatsXtUid = new File(procRoot, "net/xt_qtaguid/stats"); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 65 | * 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 Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 69 | * |
| 70 | * @throws IllegalStateException when problem parsing stats. |
| 71 | */ |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 72 | public NetworkStats readNetworkStatsSummaryDev() throws IllegalStateException { |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 73 | final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); |
| 74 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 75 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6); |
| 76 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 77 | |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 78 | ProcFileReader reader = null; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 79 | try { |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 80 | reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceAll)); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 81 | |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 82 | while (reader.hasMoreData()) { |
| 83 | entry.iface = reader.nextString(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 84 | entry.uid = UID_ALL; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 85 | entry.set = SET_ALL; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 86 | entry.tag = TAG_NONE; |
| 87 | |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 88 | final boolean active = reader.nextInt() != 0; |
| 89 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 90 | // always include snapshot values |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 91 | entry.rxBytes = reader.nextLong(); |
| 92 | entry.rxPackets = reader.nextLong(); |
| 93 | entry.txBytes = reader.nextLong(); |
| 94 | entry.txPackets = reader.nextLong(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 95 | |
| 96 | // fold in active numbers, but only when active |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 97 | if (active) { |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 98 | entry.rxBytes += reader.nextLong(); |
| 99 | entry.rxPackets += reader.nextLong(); |
| 100 | entry.txBytes += reader.nextLong(); |
| 101 | entry.txPackets += reader.nextLong(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | stats.addValues(entry); |
Jeff Sharkey | 1d29a30 | 2012-02-27 18:08:01 -0800 | [diff] [blame] | 105 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 106 | } |
| 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 Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 115 | StrictMode.setThreadPolicy(savedPolicy); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 116 | } |
| 117 | return stats; |
| 118 | } |
| 119 | |
| 120 | /** |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 121 | * 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 Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 126 | */ |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 127 | public NetworkStats readNetworkStatsSummaryXt() throws IllegalStateException { |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 128 | final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); |
| 129 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 130 | // return null when kernel doesn't support |
| 131 | if (!mStatsXtIfaceFmt.exists()) return null; |
| 132 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 133 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 6); |
| 134 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 135 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 136 | ProcFileReader reader = null; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 137 | try { |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 138 | // open and consume header line |
| 139 | reader = new ProcFileReader(new FileInputStream(mStatsXtIfaceFmt)); |
| 140 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 141 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 142 | while (reader.hasMoreData()) { |
| 143 | entry.iface = reader.nextString(); |
| 144 | entry.uid = UID_ALL; |
| 145 | entry.set = SET_ALL; |
| 146 | entry.tag = TAG_NONE; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 147 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 148 | entry.rxBytes = reader.nextLong(); |
| 149 | entry.rxPackets = reader.nextLong(); |
| 150 | entry.txBytes = reader.nextLong(); |
| 151 | entry.txPackets = reader.nextLong(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 152 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 153 | stats.addValues(entry); |
| 154 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 155 | } |
| 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 Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 164 | StrictMode.setThreadPolicy(savedPolicy); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 165 | } |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 166 | 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 Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 180 | final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); |
| 181 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 182 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 24); |
| 183 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 184 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 185 | int idx = 1; |
| 186 | int lastIdx = 1; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 187 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 188 | ProcFileReader reader = null; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 189 | try { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 190 | // open and consume header line |
| 191 | reader = new ProcFileReader(new FileInputStream(mStatsXtUid)); |
| 192 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 193 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 194 | while (reader.hasMoreData()) { |
| 195 | idx = reader.nextInt(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 196 | if (idx != lastIdx + 1) { |
| 197 | throw new IllegalStateException( |
| 198 | "inconsistent idx=" + idx + " after lastIdx=" + lastIdx); |
| 199 | } |
| 200 | lastIdx = idx; |
| 201 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 202 | 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 Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 210 | |
| 211 | if (limitUid == UID_ALL || limitUid == entry.uid) { |
| 212 | stats.addValues(entry); |
| 213 | } |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 214 | |
| 215 | reader.finishLine(); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 216 | } |
| 217 | } catch (NullPointerException e) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 218 | throw new IllegalStateException("problem parsing idx " + idx, e); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 219 | } catch (NumberFormatException e) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 220 | throw new IllegalStateException("problem parsing idx " + idx, e); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 221 | } catch (IOException e) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 222 | throw new IllegalStateException("problem parsing idx " + idx, e); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 223 | } finally { |
| 224 | IoUtils.closeQuietly(reader); |
Jeff Sharkey | 453dafa | 2012-02-27 17:42:34 -0800 | [diff] [blame] | 225 | StrictMode.setThreadPolicy(savedPolicy); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 226 | } |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 227 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 228 | return stats; |
| 229 | } |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 230 | } |