Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring data usage in Android: difference between total stats and per UID stats

Tags:

android

uid

I'm having some troubles with a data traffic monitor in Android. I have to get bytes transmitted per UID and bytes received per UID and also the total sum of all UIDs (divided into transmitted and received too). The problem is that the sum of all UID's individual values is different than the values I get from calling TrafficStats.getTotalRxBytes() and TrafficStats.getTotalTxBytes() (these are the total transmitted and received values for all UIDs). Generally, total values are bigger than the sums of all UIDs (for example, 105MB vs 95MB for received values).

I compare the value retrieved from TrafficStats.getTotalRxBytes() with the sum of TrafficStats.getUidRxBytes(uid) called for each UID. The same thing is for transmitted values.

Here is the snippet I use to get the list of UIDs:

File dir = new File("/proc/uid_stat/");
String[] children = dir.list();
List<Integer> uids = new ArrayList<>();
if (children != null) {
    for (int i = 0; i < children.length; i++) {
        int uid = Integer.parseInt(children[i]);
        if ((uid >= 0 && uid <= 2000) || (uid >= 10000)) {
            uids.add(uid);
        }
    }
}

I also tried without the condition ((uid >= 0 && uid <= 2000) || (uid >= 10000)), checking only if uid is >= 0, but in my case it's the same.

Some things I've noticed: if I send a 6MB video through whatsapp, next time I perform reading, TX value for whatsapp doesn't increase that quantity. It only increases a few bytes (like 4500 for example). I look for another application that has been increased approximately 6MB, like for example MEDIA, but there isn't any. I don't know if whatsapp compresses the video or what.

I have read about some bugs like duplicated values in UID's files (/proc/uid_stat/[uid]/) in Android 2.3 or older versions but I'm testing the app in a real device with 4.1.2.

I have followed pretty much the steps described in this blog.

I want to know if that difference is normal or if there is something wrong with my code. I read that the correct values are the totals, not the sums of UIDs.

like image 879
Martín Marengo Avatar asked Apr 11 '26 15:04

Martín Marengo


1 Answers

When You're using TrafficStats.getUidRxBytes or TrafficStats.getUidTxBytes it counts only the pay load sent by the UID but not the size of headers, So there would be small difference between sum of all the UIDs and getTotalRxBytes() or getTotalTxBytes. The difference would seem larger for streaming services , Voip or some large file upload.

like image 52
Sree Vishnu Avatar answered Apr 19 '26 11:04

Sree Vishnu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!