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