Search code examples
androidtraffic-measurement

In Android, how to measure the data traffic per interface per application


I want to implement an extention to android.net.TrafficStats to measure the data traffic per APN or per APN per application. the methods prototype looks like:

getInterfaceTxPackets(String interface)
getInterfaceRxPackets(String interface)
getInterfaceTxBytes(String interface)
getInterfaceRxBytes(String interface)
getUidInterfaceTxPackets(int uid,String interface)
getUidInterfaceRxPackets(int uid,String interface)
getUidInterfaceTxBytes(int uid,String interface)
getUidInterfaceRxBytes(int uid,String interface)

I read the android.net.TrafficStats.java, it call the native methods in android_net_TrafficStats.cpp, and the native methods read the traffic statistics from some files:

  • the total traffic statistic files are in folder /sys/class/net/, in this folder there are some sub folders like: eth0, ip6tnl0, rmnet0, rmnet1, rmnet2, rmnet3., the detailed statistic files are saved in these folders.

  • the per application traffic statistic files are in /proc/uid_stat/[uid]/

My questions:

  1. Do the folder rmnet0, rmnet1, rmnet2, rmnet3... match each APN? If do, what's the match rule? if don't, how can I get each APN traffic statistic?

  2. In folder /proc/uid_stat/[uid]/, there are only two files: tcp_rcv and tcp_snd, how can I get each applicaton on each APN traffic stats?


Solution

  • Please switch over to Android 4.0, where for the data usage monitoring this has been beefed up. These APIs now sit on top of a much more complicated service that collects per-interface per-uid traffic information.

    Note that there is no meaning associated with each of the interface names, so you can't just assume that a particular name you find on one device means the same thing on another.