19

I have a feeling this is possible, I'm just not quite sure where the information is held.

I want to get the up/down statistics for specific applications, but I want to do it using ADB and not wireshark or netty.

I know I can see the vmData using

adb shell
cd proc
cd pid#
cat status 

and I know I can see the netstats using:

ADB Shell dumpsys netstats details full

which gives me these results:

Dev stats:

  Pending bytes: 1410076

  Complete history:

  ident=[[type=MOBILE, subType=COMBINED, subscriberId=310260...]] uid=-1 set=ALL tag=0x0
NetworkStatsHistory: bucketDuration=3600000
  bucketStart=1349211600000 activeTime=3600000 rxBytes=19656154 rxPackets=16897 txBytes=615620 txPackets=8084 operations=0
  bucketStart=1349215200000 activeTime=3600000 rxBytes=28854708 rxPackets=23363 txBytes=1037409 txPackets=12206 operations=0
  bucketStart=1349218800000 activeTime=3600000 rxBytes=1839274 rxPackets=1565 txBytes=89791 txPackets=914 operations=0
  bucketStart=1349222400000 activeTime=3600000 rxBytes=17421 rxPackets=88 txBytes=18376 txPackets=95 operations=0
  bucketStart=1349226000000 activeTime=3600000 rxBytes=506966 rxPackets=788 txBytes=96491 txPackets=859 operations=0

Unfortunately this looks like a combined netstat that does not differentiate between applications.

So my question, is there a way to see network traffic by unique PID#'s or application names, by simply using the command prompt?


EDIT


Alright I made some good strides

With this code

 adb shell cat proc/1638(thePID)/net/dev > C:\netstats.txt 

I can get this information:

Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
lo:        3564      28    0    0    0     0          0         0     3564      28    0    0    0     0       0          0
dummy0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet0: 117062940  191775  0    0    0     0          0         0 19344640  177574    0    0    0     0       0          0
rmnet1: 2925492    5450    0    0    0     0          0         0  1448544    5664    0    0    0     0       0          0
rmnet2:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet3:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet4:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet5:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet6:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
rmnet7:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
  sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
  vip0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0

Unfortunately after double checking these numbers with programs like "Network Usage" from the android market place, I discovered that these numbers are the total up and down across the entire device.

So it still leaves me with, how/where the heck are programs like "Network Usage" and "Spare Parts" getting their information from?

Nefariis
  • 3,451
  • 10
  • 34
  • 52
  • Wow, huge amounts of useful info here. Thanks for posting this. – Edward Falk Mar 05 '14 at 18:10
  • Thank you, if there is anything else you need to know or have any questions, let me know. Chances are I've scripted something for it or can point you to the right direction. – Nefariis Mar 06 '14 at 06:43
  • @Nefarii I am doing a very similar thing, but without a super user privileges, is there any way to achieve the same without su – SwiftParser Mar 09 '15 at 03:26
  • @Swiftparser - This shouldnt need SU. Im actually still running this command on brand new devices every week without SU privledges. – Nefariis Mar 30 '15 at 22:34

2 Answers2

21

Well I figured out where "spare parts" and "Net Usage" get their information from.

adb shell cat proc/uid_stat/(uid#)/tcp_rcv
adb shell cat proc/uid_stat/(uid#)/tcp_snd

The Problem I see with how they are doing it though is that this only accounts for TCP usage and does not account for and UDP usage.

The only way to figure out the total tx_bytes and rx_bytes is through this command.

adb shell cat /proc/net/xt_qtaguid/stats

or if you would like to convert it to a text file and view it easier.

adb shell cat /proc/net/xt_qtaguid/stats > C:\Netstats.txt

This gives you something that looks like this:

------ QTAGUID STATS INFO (su root cat /proc/net/xt_qtaguid/stats) ------

idx iface acct_tag_hex uid_tag_int cnt_set rx_bytes rx_packets tx_bytes tx_packets     rx_tcp_bytes rx_tcp_packets rx_udp_bytes rx_udp_packets rx_other_bytes   rx_other_packets tx_tcp_bytes tx_tcp_packets tx_udp_bytes tx_udp_packets tx_other_bytes tx_other_packets
2 rmnet0 0x0 0 0 18393 326 8506 166 10889 267 7504 59 0 0 4180 101 3397 54 929 11
3 rmnet0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 rmnet0 0x0 1000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 rmnet0 0x0 1000 1 7181 14 1834 19 7023 12 158 2 0 0 1616 16 218 3 0 0
6 rmnet0 0x0 10001 0 5723 19 3162 26 5723 19 0 0 0 0 3162 26 0 0 0 0
7 rmnet0 0x0 10001 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8 rmnet0 0x0 10007 0 1895740 1570 44556 898 1895740 1570 0 0 0 0 44556 898 0 0 0 0
9 rmnet0 0x0 10007 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10 rmnet0 0x0 10019 0 5319 12 2546 14 5319 12 0 0 0 0 2546 14 0 0 0 0
11 rmnet0 0x0 10019 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12 rmnet0 0x0 10026 0 6866 19 2846 24 6866 19 0 0 0 0 2846 24 0 0 0 0
13 rmnet0 0x0 10026 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

The fourth tab over (1000, 10001, etc) is the UID number. The easiest way to find out what application belongs to what UID number is:

adb shell dumpsys package > C:\apps.txt

Go down to the "Package:" section, and then its the first line down after the process name labeled "userid=".

Now to read the above chart, the main two numbers that you want to know are the 6th number in (the rx_bytes) and the 8th number in (the tx_bytes). Those two numbers should be an accurate portrayal of all the bytes in and out, for any particular application.

Enjoy.

Edward Falk
  • 9,991
  • 11
  • 77
  • 112
Nefariis
  • 3,451
  • 10
  • 34
  • 52
  • 1
    it not working on 2.3 it seems that /proc/net/xt_qtaguid/stats not available for gingerbread. – Ajeet47 Mar 13 '14 at 06:37
  • When I first started to develop this I believe I was doing it with both gingerbread and ice cream sandwich... though i think it had to have been at least 2.3.6 by then .... Also, what are you still doing on 2.3? Can you go to the folder manually? "adb shell" > "cd /proc/net/xt_qtaguid ? if you can do that then type in "ls" and see if "stats" exist if it exists then just type in "cat stats" – Nefariis Mar 14 '14 at 03:53
  • /proc/net/xt_qtaguid/ is not exist in 2.3 – Ajeet47 Mar 14 '14 at 06:34
  • Also, you will want to ignore lines with an account_tag_hex that isn't 0x0. The file will break out the usage per thread for each userid. If you count those lines that aren't 0x0, then you will be double counting the data used. To test what I'm saying, start a download from your application using the download manager functionality. It will use a separate thread and you'll see that download accounted for in a separate line in your file with the appropriate userid. – TALE Nov 14 '14 at 15:42
  • adb shell cat /proc/net/xt_qtaguid/stats does this command gives network stats at a any given moment or gives the total network usage by the app since it is installed ? my usecase is to have network stat for a given instance / date-time – Harshawardhan Jan 02 '15 at 14:03
  • @Harshawardhan it should give you the total data since the time the device was restarted - on restart it goes back to "0". How I do a given time period in my scripts is to take the total at time "N" and then take the total again when I want the script to stop - and then just minus the two. – Nefariis Jan 14 '15 at 22:50
  • Easiest way to find UID is actually looking in `/proc/#pid/status`. – André Chalella Jun 09 '15 at 09:59
1

Adding a snippet to Nefarii's comment, the easiest way to find out the UID for a particular application, e.g., com.example.myapp, is:

adb shell dumpsys package com.example.myapp | grep userId=

Joe Bowbeer
  • 3,574
  • 3
  • 36
  • 47
  • Ive never been able to get the grep thing to work correctly, even with a fresh install of busybox. – Nefariis Oct 10 '13 at 22:38
  • 1
    grep doesn't work under Android; you run it on the host. I guess if you're trying to develop from Windows, you're going to have some issues. – Edward Falk Mar 06 '14 at 16:12