[Xiaomi Redmi Note 5 Pro Guides, News, & Discussion] Desmanto:
Wifi deep sleep drain - Troubleshooting and testing

Prologue

I am using Redmi Note 5 whyred global version 4/64 GB. I get it from 9.5.6.0, UBL and root it from the very first day. Update OTA twice via full recovery zip, to 9.5.13.0 and to 9.5.17.0. During all usage of the MIUI ROM usage, I constantly achieve 10+ hours in 1,5 days; usually around 12-14 hours SOT. If I left the wifi on during night, I only lose about 4-5% after awake, which is about 0,6%/hour.

Then ARP hit the deck and I have decided to switch to custom ROM. I picked RR as it is based on LOS and have much more customization. I followed the instruction to flash firmware 8.7.5 before flashing RR 6.1 20180706 + micro gapps 20180705 + Magisk 16.0 again. Since then, i never achieved SOT more than 10 hours anymore. After quick troubleshooting I realize that the idle drain during sleep when wifi is connected is around 2-3%/hour. I wake up in the morning to find my phone has lost 15-20% just overnight doing nothing.

After knowing that it is the wifi, I test the LTE and found out no massive drain, only around 0,6%/hour, which is quite normal (same as in MIUI). I then install BBS and troubleshoot it.

How to know the wifi deep sleep drain?

The easy way to test this is : just setup any wifi router, hotspot from other phone is also OK, no internet needed. Then just leave your phone connected to the wifi, note the percentage and came back again after several hours. The easiest time to spot it at night when we sleep. I have waken up several times to find out 15% drain in 6 hours, or maybe 24% in 8 hours. Approx 3%/hour. While the same setup in LTE, only take away 4-6%.

Why is it bad?

Because at 3%/hour, if we leave the phone doing nothing only connected to wifi for a day, the battery will drop to 52% (consuming 48%) while giving 0 hour SOT. In 2 day, the battery will be dead, no doing anything. While using standard drain, it should be only 0,6%/hour, which is about 15%/day. We can even leave the phone up to almost a week.

The preset condition as the baseline

1. In MIUI 9.5.6.0 - 9.5.17.0 I can connected to wifi/LTE, all get only about 0,6%/hour idle drain.

2. After using RR 20180706 (to 20180724), connected to wifi, dual SIM active, will give idle drain about 2-2,5%/hour (thus I called wifi deep sleep drain).

3. Same RR, switch to LTE, dual SIM active, only 0,6-0,7%/hour

4. No LTE, only dual SIM, about 0,4%/hour

5. Airplane mode (no wireless at all), almost no drain in 5 hours

6. Wifi on, but forget the password (so it won't connected), almost no drain in 1 hour

7. The sensor, either Google play services is there all the time and doesn't give any difference whether in wifi, LTE, or airplane. So sensor wakeup is not the culprit.

8. All wifi signal level are strong (most of the time within 1-2 metres from router). Most of the wifi AP/router are from Tp-link. And before we say it is the wifi AP, i don't experience the drain using the same wifi when in MIUI.

9. You can the comparison of the same battery life and SOT, Wifi vs LTE on the same preset condition ROM RR 6.1 20180723; but the result is very different.

List of troubleshooting done

I am going to post a very thorough but not exhausting list of the testing I have made (and failed to solve the problem)

01. Turn on wifi and connected, phone lose approx 2%/hour (and later approx 3%/hour)

02. Turn off wifi and switch to LTE, phone lose approx 0,6-0,7%/hour, which is normal drain

03. Check with BBS, google play services always wake up the sensor. Sometimes it is the Lineage OS system or phone. But I quickly rule out this as the culprit. As I can see the same wake up when using LTE, but no massive drain. So even same wake up, only wifi connected will give massive drain

04. Airplane mode, phone almost not lose any percent in 5 hours!!! Even though the BBS is reporting the Play services is waking up the phone for 3+ hours. So sensor wake up is no the culprit. (confirmed)

05. Try different wifi, I have two wifi router at home. I also have connected to 2 different wifi router at office. All give the same drain. But when in MIUI, I am connected to the same wifi, but no such drain

06. Set the wifi as metered connection, same drain. Unmetered, same drain

07. Found the possible culprit about wifi logging by cnss_diag. So I tried to disable the logging by editing the /data/misc/wifi/cnss_diag.conf using the tutorial how to disable the wifi logging. The log is not written anymore, but the drain still there. Result : Same

08. Try another solution by Aqq123 to edit the /vendor/etc/init/hw/init.qcom.rc. Commented out those lines. Reboot and confirmed the cnss_diag is not running anymore from ps -A. Result : Still same

09. Try in unmetered wifi 2,4 GHz, same. Try in metered wifi ac 5,0 or n 2,4 Ghz, same.

10. Try to connect to another separate wifi router 2,4 Ghz with no internet connection and only my RN5 as the sole wifi client (so no possible interference or broadcast from other devices), still the same.

11. Disable various RR configuration, such as double tap to sleep/wake, brightness slider, etc; same drain

12. backup the kernel, flash derp kernel 1.8, same. Restore back to stock kernel. I tried again derp kernel 2.2, same.

13. There is gapps update, so I reflash from micro to nano; still same. Currently updated to nano gapps 20180818, same

14. Disable wifi always scanning in the location setting, same. Disable the bluetooth scanning too, same.

15. Disable location feature (completely off), still the same. Deny all apps for the location permission, and worse, drain become 4%/hour

16. Reset wifi, mobile, bluetooth and reconnect again, still same

17. Change the static IP to DHCP, still the same.

18. Turn off Automagic services, since I have some remote server flow enabled when connected to wifi, same drain

19. Boot to safe mode, with only gapps and system app enabled, same drain

20. Disable Wi-Fi calling (derived from VoLTE), same. I enabled it and choose wifi. Then choose mobile. Then turn it off too. All still same drain.

21. I though it might the modification I made to the VoLTE. I undo the modification, set the VoLTE back to nothing, so the VoLTE completely non active (can't call anymore); same drain

22. Try something else not related, disable the network traffic indicator and seconds in the time. Same, no effect.

23. In the logcat, there are same crashes from LOS thermal engine every few seconds. But the same crash happen on LTE, still not drain. Only at wifi connected, so must not be the crash.

24. Set the wifi sleep policy to never. Seems to solve it, but fake solution, only placebo effect. It came back to 3%/hour drain again.

25. at the system setting, i find any value related to wifi, such as wifi_networks_available_notification_on, wifi_wakeup_available, wifi_wakeup_enable and etc; all set to 0, same drain (I have restarted after setting the value)

26. I compare the battery life of the LTE vs wifi in bugreport, throw them at battery historian. I can't find any difference in relation to the drain. (maybe it is too complex for me to read it)

27. I compare the ps -A, using automagic to capture the command output when connecting to wifi; switch to LTE and capture again. Compared both result and show the line difference. All processes are the same, all exist, only in different PID

28. Using the similar flow, I compare the tcp connection. same result, only different PID and port.

29. Uninstalled several app those probably related to wifi, and still the same

30. I think it maybe caused by the firmware. I am using 8.7.5 firmware. So I flashed back to 9.5.17.0, same dari. Flash to non arb 8.8.16, same drain. So I flashed back to 9.5.17.0 firmware.

31. Tried AOSiP ROM 20180808 build. At first hour after flashed the ROM, haven't enter the google account and haven't restore apps; only stock apps and nano gapps. Connect to the wifi and setup several configuration. leave and no drain in 1 hour. But after I login my google account and restore 98 apps; leave it again for another hour, now suddenly the 3%/hour appear. So I can safely said it was cause by some apps in 98 apps or the google account itself.

32. Because it is caused by the apps/account; I tried to disable/freeze some apps to test. I have froze Google search, Google Text to Speech, Playstore, whatsapp, Mi Fit and several others; still same drain.

33. Tried to disable wifi and data access for Google Play services, still same drain

34. Switch to battery extreme power saver mode, still same drain (seriously?)

What is not the possible culprit anymore?

1. The wifi router. Since they are all the same wifi I connected when using MIUI ROM. This rule out them

2. Kernel and firmware, have changed them and give no effect

3. ROM, I changed to AOSiP, same drain. Sidamos77 also have tested several ROM and the drain persist.

4. Location, wifi scanning, sleep policy etc; no effect

5. Sensor wake up or any wakeup in BBS or GSAM, as the same wake up appear in airplane mode but no drain

6. Not the user app, as safe mode rule them out

7. not the wifi logging (cnss_diag), as disabling it also same drain

8. wifi configuration, have changed it still the same drain

9. VoLTE, wifi calling, not related

10. Logcat show nothing to indicate the drain; same crash in wifi and LTE

11. Running services and TCP connection also almost the same

12. Not the system app/gapps such as Google search, Google text to speech, Playstore

Keypoint of possible culprit

1. vendor partition of all AOSP-ish ROM. But why not everyone experienced the drain

2. the gapps, again, why not everyone

3. hardware problem. This might explained it, but why all got the same problem only after switch to custom ROM

4. user apps, very difficult to trace.

5. Google account. Arrgh, this remove out most of the android usability.

Solution I haven't try

1. Factory reset and restore app backup. But I have tested this in AOSiP, so I won't do it again

2. Remove the google account and try. I am afraid to mess up something, so I need preparation and check before trying this

3. Install pico gapps. But I wonder if it will do anything. I need the text to speech anyway.

4. Switch back to MIUI and try the drain again, to prove it doesn't have drain in MIUI. Too hassle, I fell in love with RR and can't stand MIUI again.

Workaround

I have thought a workaround by using Automagic to turn off the wifi after certain amount the screen goes off. I will set the wifi to be on for 1 minute for every 15 minute. But I occassionally download something using wifi. This will break most of my activities, so it is not acceptable. Beside i also transfer data and remote scrcpy to my phone. Screen off will kill the remote server and makes me need to turn it on again manually (why I use Automagic then, if I still need to do it manually?)

Epilogue

Now, my phone is set at TWRP 3.2.2.0, Magisk 16.0, ROM RR 6.1 20180723, nano gapps 20180818, kernel Derp 2.2, firmware 9.5.17.0 + various user apps such as WA, Chrome, Gmail, Maps, MiXplorer, Automagic, GMD gesture, Gcam Arnova v8.2, etc. And I still experience this 3%/hour wifi deep sleep drain. It has been a month and I have spent quite a lot of hours and energy troubleshooting this.