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 package com.android.framework.tests; 17 18 import com.android.tradefed.device.DeviceNotAvailableException; 19 import com.android.tradefed.device.ITestDevice; 20 import com.android.tradefed.log.LogUtil.CLog; 21 import com.android.tradefed.util.StreamUtil; 22 23 import java.io.File; 24 import java.io.FileInputStream; 25 import java.io.IOException; 26 import java.util.HashMap; 27 import java.util.Map; 28 29 public class BandwidthUtils { 30 31 private BandwidthStats mDevStats; 32 private Map<Integer, BandwidthStats> mUidStats = new HashMap<Integer, BandwidthStats>(); 33 private String mIface; 34 ITestDevice mTestDevice = null; 35 36 BandwidthUtils(ITestDevice device, String iface) throws DeviceNotAvailableException { 37 mTestDevice = device; 38 mIface = iface; 39 parseUidStats(); 40 parseNetDevStats(); 41 } 42 43 public BandwidthStats getStatsForUid(int uid) { 44 return mUidStats.get(uid); 45 } 46 47 public BandwidthStats getDevStats() { 48 return mDevStats; 49 } 50 51 public BandwidthStats getSumOfUidStats() { 52 long rb = 0, rp = 0, tb = 0, tp = 0; 53 for (BandwidthStats stats : mUidStats.values()) { 54 rb += stats.getRxBytes(); 55 rp += stats.getRxPackets(); 56 tb += stats.getTxBytes(); 57 tp += stats.getTxPackets(); 58 } 59 return new BandwidthStats(rb, rp, tb, tp); 60 } 61 62 /** 63 * Parses the uid stats from /proc/net/xt_qtaguid/stats 64 * @throws DeviceNotAvailableException 65 */ 66 private void parseUidStats() throws DeviceNotAvailableException { 67 File statsFile = mTestDevice.pullFile("/proc/net/xt_qtaguid/stats"); 68 FileInputStream fStream = null; 69 try { 70 fStream = new FileInputStream(statsFile); 71 String tmp = StreamUtil.getStringFromStream(fStream); 72 String[] lines = tmp.split("\n"); 73 for (String line : lines) { 74 if (line.contains("idx")) { 75 continue; 76 } 77 String[] parts = line.trim().split(" "); 78 String iface = parts[1]; 79 if (!mIface.equals(iface)) { // skip if its not iface we need 80 continue; 81 } 82 String foreground = parts[4]; 83 if (!"0".equals(foreground)) { // test uses background data, skip foreground 84 continue; 85 } 86 String tag = parts[2]; 87 int uid = Integer.parseInt(parts[3]); 88 int rb = Integer.parseInt(parts[5]); 89 int rp = Integer.parseInt(parts[6]); 90 int tb = Integer.parseInt(parts[7]); 91 int tp = Integer.parseInt(parts[8]); 92 if ("0x0".equals(tag)) { 93 CLog.v("qtag iface %s pid %d rb %d rp %d tb %d tp %d", 94 iface, uid, rb, rp, tb, tp); 95 mUidStats.put(uid, new BandwidthStats(rb, rp, tb, tp)); 96 } 97 } 98 } catch (IOException e) { 99 CLog.d("Failed to read file %s: %s", statsFile.toString(), e.getMessage()); 100 } finally { 101 StreamUtil.close(fStream); 102 } 103 } 104 105 /** 106 * Add stats, if the iface is currently active or it is an unknown iface found in /proc/net/dev. 107 * @throws DeviceNotAvailableException 108 */ 109 private void parseNetDevStats() throws DeviceNotAvailableException { 110 File file = mTestDevice.pullFile("/proc/net/dev"); 111 FileInputStream fStream = null; 112 try { 113 fStream = new FileInputStream(file); 114 String tmp = StreamUtil.getStringFromStream(fStream); 115 String[] lines = tmp.split("\n"); 116 for (String line : lines) { 117 if (line.contains("Receive") || line.contains("multicast")) { 118 continue; 119 } 120 String[] parts = line.trim().split("[ :]+"); 121 String iface = parts[0].replace(":", "").trim(); 122 if (mIface.equals(iface)) { 123 int rb = Integer.parseInt(parts[1]); 124 int rp = Integer.parseInt(parts[2]); 125 int tb = Integer.parseInt(parts[9]); 126 int tp = Integer.parseInt(parts[10]); 127 CLog.v("net iface %s rb %d rp %d tb %d tp %d", iface, rb, rp, tb, tp); 128 mDevStats = new BandwidthStats(rb, rp, tb, tp); 129 break; 130 } 131 } 132 } catch (IOException e) { 133 CLog.d("Failed to read file %s: %s", file.toString(), e.getMessage()); 134 } finally { 135 StreamUtil.close(fStream); 136 } 137 } 138 } 139