The project requires traffic statistics for the Android device to be billed, so some research has been done on Android device traffic statistics. There are two ways to find traffic statistics in the mainstream
- Using the system statistics class trafficstats get
- Read through System file parsing
Trafficstats
- Static long Getmobilerxbytes ()//Gets the total number of bytes received via mobile connection, not including WiFi
- Static long Getmobilerxpackets ()//Gets the total number of packets received by the mobile connection
- Static long Getmobiletxbytes ()//mobile The total number of bytes sent
- Static long Getmobiletxpackets ()//mobile The total number of packets sent
- Static long Gettotalrxbytes ()//Gets the total number of bytes accepted, including mobile and WiFi, etc.
- Static long Gettotalrxpackets ()//Total number of accepted packets, including mobile and WiFi, etc.
- Static long Gettotaltxbytes ()//Total number of bytes sent, including mobile and WiFi, etc.
- Static long Gettotaltxpackets ()//total number of packets sent, including mobile and WiFi, etc.
- static long getuidrxbytes (int uid)//Gets the number of accepted bytes for a network uid
- static long getuidtxbytes (int uid)//Gets the number of bytes sent for a network UID
These methods get the traffic data from the last boot to the current traffic usage, this does not matter, we can monitor the phone's boot and shutdown. When the shutdown (or every interval of time) the data are stored, the next time after the boot through the above method to obtain the data on the overlay.
But in these methods, only I found in these methods, the lack of the method I most need, I want to get an app to accept and send the 2g/3g traffic, do not include WiFi, these APIs do not meet this requirement.
So, let's start with the parsing of the system files.
/proc/net/xt_qtaguid/stats format
/proc/net/xt_qtaguid/stats contains the use of each app traffic situation, first to briefly look at the contents of this file
This output is a table structure, and first we need to look at what the column fields in the table represent
- IDX: Serial Number
- Iface: Represents the traffic type (rmnet represents 2g/3g, WLAN represents WiFi traffic, lo indicates local traffic)
- Acct_tag_hex: Thread tagging (used to differentiate between different modules/threads in a single application)
- Uid_tag_int: Use UID to determine whether the traffic data for an application statistic
- Cnt_set: Pre-and post-application markers: 1: Front desk, 0: Backstage
- The number of bytes rx_btyes:receive bytes received
- Rx_packets: Number of Task packages received
- Tx_bytes:transmit total number of bytes sent bytes
- Tx_packets: Total number of packets sent
- Rx_tcp_types: Number of TCP bytes received
- Rx_tcp_packets: Number of TCP packets received
- Rx_udp_bytes: Number of UDP bytes received
- Rx_udp_packets: Number of UDP packets received
- Rx_other_bytes: Number of other types of bytes received
- Rx_other_packets: Number of other types of packets received
- Tx_tcp_bytes: Number of TCP bytes sent
- Tx_tcp_packets: Number of TCP packets sent
- Tx_udp_bytes: Number of UDP bytes sent
- Tx_udp_packets: Number of UDP packets sent
- Tx_other_bytes: Number of other types of bytes sent
- Tx_other_packets: Number of other types of packets sent
You can see that using file parsing to get traffic can be more than the direct use of Acct_tag_hex to enrich the information, including the distinction between WiFi and mobile network, distinguish between the application of different module function flow, front and rear traffic and so on.
Use shell commands to view process traffic
Next Uid_tag_int This field
PID:PROCESSID (Process ID)
Get the PID shell command:
grep com.car.tencent
UID: one UID per application (if an app has two processes, there are two different PID, but two PID corresponding UID is the same)
Get UID based on PID
adb shell cat /proc/1643/status
Get traffic based on UID
adb shell cat /proc/net/xt_qtaguid/stats | grep uid
How to use code to parse/proc/net/xt_qtaguid/stats files
Static Const Char*qtaguid_uid_stats ="/proc/net/xt_qtaguid/stats";structStats {uint64_tRxbytes;uint64_tRxpackets;uint64_tTxbytes;uint64_tTxpackets;uint64_tTcprxpackets;uint64_tTcptxpackets;};Static intParseuidstats (Const uint32_tUidstructStats *stats) {FILE *FP = fopen (Qtaguid_uid_stats,"R");if(fp = = NULL) {return-1; }Charbuffer[384];Chariface[ +];uint32_tIDX, Cur_uid, set;uint64_tTag, rxbytes, Rxpackets, Txbytes, txpackets; while(Fgets (Buffer,sizeof(buffer), fp)! = NULL) {if(SSCANF (Buffer,"%"SCNu32"%31s 0x%"SCNx64"%u%u%"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64"", &idx, Iface, &tag, &cur_uid, &set, &rxbytes, &rxpackets, &am P;txbytes, &txpackets) = =9) {//Bypass local loopback address if(strcmp (Iface,"Lo") && uid = = Cur_uid && Tag = = 0L) {stats->rxbytes + = rxbytes; Stats->rxpackets + = Rxpackets; Stats->txbytes + = Txbytes; Stats->txpackets + = Txpackets; } } }if(fclose (fp)! =0) {return-1; }return 0;}StaticJlong Getuidstat (jnienv *env, Jclass clazz, Jint uid, jint type) {structStats Stats; memset (&stats,0,sizeof(Stats));if(Parseuidstats (uid, &stats) = =0) {returnGetstatstype (&stats, (statstype) type); }Else{returnUNKNOWN; }}
In this code, the point to note is how to ignore the local loopback traffic
if (strcmp(iface, "lo") && uid == cur_uid && tag == 0L) {
Because Iface is a network type, there are only three types, rmnet represents 2g/3g, WLAN represents WiFi traffic, and Lo represents local traffic. So there's a direct use of the string alignment.
Extended
- /PROC/NET/XT_QTAGUID/IFACE_STAT_FMT: The file has been based on the type of the total number of different types of network data, so that the current device total number of accepted bytes and other device-level statistics, directly analyze the file, the fastest to obtain results
- /proc/net/xt_qtaguid/stats: This file is categorized according to UID and is suitable for the analysis of finer intensity (application-level/thread-level)
Appendix:
- Blog Original address: https://chchaooo.github.io/2018/06/13/Android%E6%B5%81%E9%87%8F%E7%BB%9F%E8%AE%A1/
- If there is a problem exchange, please visit: https://www.cnblogs.com/weilf/p/9180373.html
Resources:
- Android Traffic Optimization (i): Modular traffic statistics
- Network traffic for the Android performance test
Android Traffic statistics