Android 流量监控

最近在测试中经常要去查看一个进程的流量。所以了解了一下这方面的知识!在此总结一下

 

Android流量监控主要是有两种方法:

 

 

一.抓包

这个方法获取的流量更加精准,但是难度应该大点。本人没有了解过,所以在此略过。

 

二.读取linux本地文件

Android是基于linux的一个操作系统。

在Android中,你用Root Explorer去查看系统文件的话,与流量监控相关的会有这么几个文件

 

 

/proc/net/dev这个文件中具体记录的暂时不是非常清楚,可能是整个系统的一个流量情况。

 

/proc/uid_stat/%d" 和"/proc/uid_stat/%d" %d为进程的UID。这个文件里只有两项数据tcp_rcv和tcp_snd。

看命名大家应该就能看出代表什么,一个代表总的接受字节数,一个代表总的发送字节数。

 

这两个文件为非标准linux内核文件,由android内核层/kernel/net/Socket.c 的__sock_sendmsg函数负责写入, 用户层套接字通信在内核层最终会调用此函数 (包括本地套接字和网络套接字)。

 

而Android在2.3之前是没有封装响应的流量监控API的。在2.3之后呢,把数据流量监控封装到了Android.net.TrafficStats类中。其原理就是读取上文提到的那几处文件。其中有的方法也是读取的别的文件。

 

其主要的方法

static long getMobileRxBytes()//获取通过Mobile连接收到的字节总数,但不包含WiFi
static long getMobileRxPackets()//获取Mobile连接收到的数据包总数
static long getMobileTxBytes()//Mobile发送的总字节数
static long getMobileTxPackets()//Mobile发送的总数据包数
static long getTotalRxBytes()//获取总的接受字节数,包含Mobile和WiFi等
static long getTotalRxPackets()//总的接受数据包数,包含Mobile和WiFi等
static long getTotalTxBytes()//总的发送字节数,包含Mobile和WiFi等
static long getTotalTxPackets()//发送的总数据包数,包含Mobile和WiFi等
static long getUidRxBytes(int uid)//获取某个网络UID的接受字节数
static long getUidTxBytes(int uid) //获取某个网络UID的发送字节数

返回类型均为long型,如果返回等于-1代表 UNSUPPORTED 当前设备不支持统计,可能是因为系统版本低。 

getUidRxBytes(int uid)此方法就是根据uid去查找系统中响应的文件,并读取响应的值。

这其中可能遇到的问题是:没有wifi的情况下,各进程获得的getUidRxBytes之和与getMobileRxBytes所返回的值不相等。原因在于getUidRxBytes使读取上文提到的文件。而getMobileRxBytes读取的是sys/class/net/rmnet0/statistics/rx_bytes 和sys/class/net/ppp0/statistics/rx_bytes 。这俩文件。而且在getUidRxBytes返回的值中包含了本地通信的流量,比如本地进程间的socket通信。所以这两个值加起来有所出入!

UID可以通过下面方法取

PackageManager packageManager = getPackageManager();
List<ApplicationInfo> infoList = packageManager.getInstalledApplications(0);
for(ApplicationInfo info : infoList){
Log.i(TAG, "info pkgname = " + info.packageName);
Log.i(TAG, "info uid = " + info.uid);
}

如果是播放器之类的流量统计,则可能需要通过meidaserver的UID ,因为app层和meidaserver不是在同一个进程当中。真正做数据接收的是mediaserver。而不是app层的mediaplayer。mediaplayer是通过binder调用mediaserver。所以系统不会把流量放在app层,会把流量放在meidaserver中。mediaserver的默认UID是1013

定义的文件在system/core/include/private/android_filesystem_config.h,这个文件定义来系统进程的UID。APP层是无法访问到的

#define _ANDROID_FILESYSTEM_CONFIG_H_
24
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28
29 /* This is the master Users and Groups config for the platform.
30 ** DO NOT EVER RENUMBER.
31 */
32
33 #define AID_ROOT 0 /* traditional unix root user */
34
35 #define AID_SYSTEM 1000 /* system server */
36
37 #define AID_RADIO 1001 /* telephony subsystem, RIL */
38 #define AID_BLUETOOTH 1002 /* bluetooth subsystem */
39 #define AID_GRAPHICS 1003 /* graphics devices */
40 #define AID_INPUT 1004 /* input devices */
41 #define AID_AUDIO 1005 /* audio devices */
42 #define AID_CAMERA 1006 /* camera devices */
43 #define AID_LOG 1007 /* log devices */
44 #define AID_COMPASS 1008 /* compass device */
45 #define AID_MOUNT 1009 /* mountd socket */
46 #define AID_WIFI 1010 /* wifi subsystem */
47 #define AID_ADB 1011 /* android debug bridge (adbd) */
48 #define AID_INSTALL 1012 /* group for installing packages */
49 #define AID_MEDIA 1013 /* mediaserver process */
50 #define AID_DHCP 1014 /* dhcp client */
51 #define AID_SDCARD_RW 1015 /* external storage write access */
52 #define AID_VPN 1016 /* vpn system */
53 #define AID_KEYSTORE 1017 /* keystore subsystem */
54 #define AID_USB 1018 /* USB devices */
55 #define AID_DRM 1019 /* DRM server */
56 #define AID_MDNSR 1020 /* MulticastDNSResponder (service discovery) */
57 #define AID_GPS 1021 /* GPS daemon */
58 #define AID_UNUSED1 1022 /* deprecated, DO NOT USE */
59 #define AID_MEDIA_RW 1023 /* internal media storage write access */
60 #define AID_MTP 1024 /* MTP USB driver access */
61 #define AID_UNUSED2 1025 /* deprecated, DO NOT USE */
62 #define AID_DRMRPC 1026 /* group for drm rpc */
63 #define AID_NFC 1027 /* nfc subsystem */
64 #define AID_SDCARD_R 1028 /* external storage read access */

 

posted @ 2013-06-28 19:06  xumin_minzi  阅读(635)  评论(0)    收藏  举报