Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Tabs use "excessive" CPU when application is backgrounded #28452

Closed
Cheap-Skate opened this issue Jan 8, 2023 · 5 comments
Closed

Tabs use "excessive" CPU when application is backgrounded #28452

Cheap-Skate opened this issue Jan 8, 2023 · 5 comments
Labels
🐞 bug Crashes, Something isn't working, .. needs:triage Issue needs triage

Comments

@Cheap-Skate
Copy link

Cheap-Skate commented Jan 8, 2023

Steps to reproduce

STR: Open Firefox Beta and/or Nightly. Open 3-4 tabs. Leave for a few hours. Firefox gets killed by ActivityManager due to "excessive CPU". Hav

I'd like to re-open #23140, I see this issue quite a lot on Nightly and on Beta, several times per day, usually when the phone is locked and in my pocket. I have tried:- logging out of Firefox sync; making the app "unrestricted" in battery monitor; comparing Beta with Nightly. Nothing really makes much difference. Sometimes a tab process gets killed and I lose half my open tabs, sometimes the main process gets killed and Firefox cold launches when I switch to it.

I had Beta and Nightly opened simultaneously on my phone, each with 3-4 tabs open. They were both killed at different times this morning, typically when the phone was at a low level of activity as seen in the logs below

Expected behaviour

Firefox should not get killed due to excessive CPU

Actual behaviour

Several times per day Firefox parent or one tab process gets killed, resulting in loss of half my open tabs or complete closing of the app and re-launching

Device name

Galaxy M32

Android version

Android 13

Firefox release type

Firefox Beta

Firefox version

109.0b5

Device logs

adb logcat | grep “excessive"

01-08 08:07:54.764  1425  1476 I ActivityManager: Killing 16364:org.mozilla.firefox_beta/u0a295 (adj 920): excessive cpu 9340 during 300151 dur=1123666 limit=2
...
01-08 10:17:11.017  1425  1476 I ActivityManager: Killing 9589:org.mozilla.fenix:tab20/u0a275 (adj 900): excessive cpu 6940 during 300143 dur=928426 limit=2

Investigating the Beta kill in more detail, there is nothing exciting in the log beforehand, the phone was at a very low level of activity:-

adb logcat | grep "08:07:5”
 
01-08 08:07:50.117  1425  1425 D SGM:GameManager: identifyGamePackage. org.mozilla.fenix, mCurrentUserId: 0, callerUserId: 0, callingMethodInfo: com.android.server.ssrm.fgapps.GameAppUtils.isGame(GameAppUtils.java:84)
01-08 08:07:50.117  1425  1425 D SGM:PkgDataHelper: getGamePkgData(). org.mozilla.fenix
01-08 08:07:50.119  1425  1425 D SGM:GameManager: identifyGamePackage. org.mozilla.fenix, mCurrentUserId: 0, callerUserId: 0, callingMethodInfo: com.android.server.ssrm.fgapps.GameAppUtils.isGame(GameAppUtils.java:84)
01-08 08:07:50.119  1425  1425 D SGM:PkgDataHelper: getGamePkgData(). org.mozilla.fenix
01-08 08:07:51.156  3780  4631 I SDHMS:D : SIOP:: AP:175(283,100) BAT:288(288,0) USB:0(0,0) CHG:276(276,0) PA:283(283,0) BLK:0(0,0) SUBBAT:0(0,0) LRP:301(301,0) LRP2:286(286,0) LRF:301(301) LRB:300(300) LRF2:286(286) LRB2:284(284) AP2:286(286) CHG2:283(283) VAP:275(275) 
01-08 08:07:53.020  1425  1502 D PowerManagerService: UserActivityStateListenerState: 0
01-08 08:07:54.148 12030 12030 I wpa_supplicant: Heartbeat 6576
01-08 08:07:54.764  1425  1476 I ActivityManager: Killing 16364:org.mozilla.firefox_beta/u0a295 (adj 920): excessive cpu 9340 during 300151 dur=1123666 limit=2

Additional information

No response

┆Issue is synchronized with this Jira Task

@Cheap-Skate Cheap-Skate added needs:triage Issue needs triage 🐞 bug Crashes, Something isn't working, .. labels Jan 8, 2023
@Cheap-Skate
Copy link
Author

Cheap-Skate commented Jan 10, 2023

(Edited to delete comment about Focus (
More info

  • It affects both Nightly and Beta
  • It only happens when they are in the background
  • It's not caused by Sync or Add-ons, it still occurs in Beta if I disconnect from Sync and uninstall all Add-ons
  • Sometimes the main process gets killed, sometimes one or both tab processes

I think ActivityMonitor sees Fenix in the background using more than 2% of the CPU time during a given time window, and it kills it... I think the times are in milliseconds so the window is quite large, minutes

https://cs.android.com/android/platform/superproject/+/android-11.0.0_r48:frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java;l=17669

In the first example below, at 08:47:03 fenix tab24 uses 9100 ms of CPU in an interval of 300362 ms and (9100*100/300362) = 3.02%, against a target limit of 2%, and it gets killed. Etc

Examples

Both tab processes in Nightly being killed simultaneously:-

01-10 08:47:03.244  1647  1695 I ActivityManager: Killing 6373:org.mozilla.fenix:tab24/u0a275 (adj 930): excessive cpu 9100 during 300362 dur=1022229 limit=2
01-10 08:47:03.246  1647  1695 I ActivityManager: Killing 13526:org.mozilla.fenix:tab11/u0a275 (adj 930): excessive cpu 7010 during 300362 dur=983509 limit=2

Beta's main process being killed even though Beta is "stock", it has no Sync and no Add-ons installed:-

01-10 16:52:49.344  1647  1695 I ActivityManager: Killing 9494:org.mozilla.firefox_beta/u0a295 (adj 700): excessive cpu 8690 during 300148 dur=1263787 limit=2

@Cheap-Skate Cheap-Skate changed the title [Bug]: Firefox gets killed due to excessive CPU, affects both Nightly and Beta, new filing of #23140 (stale) [Bug]: Tabs loaded but not viewed use CPU indefinitely even when Fenix app is put into background. Can cause ActivityManager to kill Fenix and presumably wastes battery. Now with STR :) :) !!!! Jan 14, 2023
@Cheap-Skate
Copy link
Author

(edited title to reflect new understanding)

With Fenix, if a tab is loaded but not viewed that tab uses CPU indefinitely until the tab is viewed. Even if Fenix is put into the background, that tab still uses some CPU.

The amount of CPU seems to depend on number of loaded-but-not-viewed pages; whether that page is in a new or existing tab; and the exact page content. But it can be substantial >10% CPU use per tab.

You don’t really need detailed STR but I have given them below. Just open several tabs in the background by long pressing a link and selecting "open link in new tab". Do not view the new tabs. Watch about:processes or use adb shell top (better). A background tab opened but not viewed causes Fenix to use CPU indefinitely even when the Fenix app is in the background. Once the tab has been viewed, the process CPU drops to near idle.

Example high background CPU pages:- www.telegraph.co.uk; edition.cnn.com. Not all are as bad as this, I will post more if I find them.

The constant use of CPU resources in the background can cause ActivityManager to kill Fenix process(es). I don’t fully understand the criteria ActivityManager uses, but I have fairly reliable STR for killing a tab process, see below.

I think Focus suffers more from this problem than Fenix but I am not clear about the details. As soon as Focus has any tab loaded, OneUI 5 shows a “1 active in background” warning in the task switcher which I don’t see for any other app. Tapping the warning says “Check background activity. These apps and services are active while running in the background: Firefox Focus"

Presumably this also wastes battery.

Steps to reproduce

Here I use eight background tabs to exaggerate the effect but I think even two or three is enough to cause problems. It could be vendor/CPU/Android version dependent (I have Galaxy M32, Android 13, OneUI 5). In the comments above it seems ActivityManager will sometimes kill on 2% CPU use, it is not hard to make Fenix’s background tabs use more than this when in the background.

  1. close all tabs
  2. open about:processes in a first tab
  3. open a new tab and go to www.telegraph.co.uk
  4. accept the cookie banner
  5. long press the olde-worlde “The Telegraph” logo and select “open in a new tab”. This loads a new www.telegraph.co.uk in the background. Do not view the newly opened tab.
  6. repeat step 3 seven more times making eight unviewed background pages; one viewed page; plus about:processes
  7. switch back to about:processes. use the tab tray, don't swipe through tabs, they need to stay unviewed
  8. watch tab process CPU remain at fairly high levels indefinitely. INCORRECT BEHAVIOR
  9. check adb shell top and see the same thing
  10. drop Fenix into background, adb shell top shows it still using CPU. INCORRECT BEHAVIOR
  11. bring Fenix to the foreground, background tabs are still using CPU
  12. view all the background tabs
  13. CPU in about:processes and adb shell top drops to near zero. CORRECT BEHAVIOR
  14. put Fenix in the background, it still uses near zero CPU. CORRECT BEHAVIOR

To trick ActivityManager into killing a Fenix process:-

(this is fairly repeatable but doesn't always work)

repeat steps 1 through 10. Fenix has eight unviewed background tabs, and the Fenix app is in the background. Leave phone untouched for 30 minutes. ActivityManager will usually kill a process for using excessive CPU, you can see it in about:processes, or adb shell top, or adb logcat | grep “excessive"; or by switching between the 8 tabs, half of them will reload.

I think Focus is affected even worse but I have not looked in detail.

Fenix in foreground with 8x unviewed background tabs:-

https://share.firefox.dev/3kgBQEp

Fenix in background with same 8 unviewed background tabs (waited 30 seconds after backgrounding):-

https://share.firefox.dev/3kguBfB

@Cheap-Skate
Copy link
Author

High CPU load sites:- walgreens.com; bestbuy.com; ibm.com/design; xbox.com; google.com

Turning off Javascript in about:config helps somewhat. I think script-heavy sites that are loaded but not viewed can keep "doing stuff" indefinitely until they are viewed, even if Fenix is in the background.

@csadilek
Copy link
Contributor

Thank you for this investigation, @Cheap-Skate. I will move this ticket to Bugzilla where we track all work related tab kills / reloads.

Thanks also for attaching the profiles!

@csadilek csadilek changed the title [Bug]: Tabs loaded but not viewed use CPU indefinitely even when Fenix app is put into background. Can cause ActivityManager to kill Fenix and presumably wastes battery. Now with STR :) :) !!!! Tabs use "excessive" CPU when application is backgrounded Jan 16, 2023
@csadilek
Copy link
Contributor

Moved to bugzilla: https://bugzilla.mozilla.org/show_bug.cgi?id=1810550

Change performed by the Move to Bugzilla add-on.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🐞 bug Crashes, Something isn't working, .. needs:triage Issue needs triage
Projects
None yet
Development

No branches or pull requests

2 participants