Watt the fox?
How do websites waste CPU and Battery and Firefox cycles in Firefox
by H43Z
I recently got a new notebook and noticed that speakers sometimes released a weak but annoying white noise. This issue was found only in a quiet room without music or other audio playing speakers.
After digging, I found the following command
pactl list sinks short
(Check powercfg / Windows Requests?!)
This command lists all available audio sunks for Pulseaudio in Linux with their current status. States can be one of the following: idle, suspended, or run.
I find that speakers remain silent if the audio sink is in suspended condition. However, white noise occurs when the sink is in lazy or running, even if no music or sound is played.
Although I have a setting to automatically suspend audio after 10 seconds inactive:
% grep -R module-suspend-on-idle /etc/pulse/
/etc/pulse/default.pa:load-module module-suspend-on-idle
/etc/pulse/system.pa:load-module module-suspend-on-idle
But sometimes, the sink never wasted and therefore never had been suspended.
Manual for module-suspind-on-idle says:
Since 0.9.11. Disconnects sinks and sources from their backend after a
predetermined amount of idle time. Idle time is accumulated when the sink/source
in question is not connected to any streams.
Advantages: Saves power. ALSA uses considerably more CPU cycles when pulseaudio
has to send empty data to the soundcard during idle. If you don't plan to have
an active stream all the time, set the timer to a low value for best power savings.
I started using the command:
pactl list sink-inputs
Whenever I noticed the white noise, trying to figure out which program uses the audio sink.
It turns out to be Firefox. However, no one of the open tabs should play any sound-not even muted videos.
To get the reason for what happened, I made a small script for my i3 status bar. The script shows a red indicator at a time when the audio sinking went into a running condition.
Culprit looks like a notice sound from my pinned tab to Outlook.office.com. Whenever I received an email, the white noise starts and never stop until the tab is reloaded or restarted firefox.
This issue is not just about the annoying white noise – it also drains my notebook battery.
Here is the CPU / Power Consume without the white noise:
$ htop -F pulseaudio
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ ▽Command
3770 h43z 9 -11 1094M 30924 21580 S 0.0 0.1 10:57.51 /usr/bin/pulseaudio --daemonize=no --log-target=journal
$ powerstat -d0 3
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Watts
20:54:31 0.8 0.1 0.5 98.6 0.0 1 957 900 4.42
20:54:34 0.0 0.1 0.2 99.7 0.0 1 728 591 4.34
20:54:37 0.3 0.1 1.3 98.2 0.1 1 1444 1241 4.32
20:54:40 0.0 0.1 0.1 99.8 0.0 1 695 556 4.46
20:54:43 0.4 0.1 0.4 99.1 0.0 1 1322 935 5.08
20:54:46 0.1 0.1 0.4 99.4 0.0 1 1216 944 4.50
20:54:49 0.0 0.1 0.1 99.8 0.0 1 1032 742 4.45
20:54:52 0.0 0.1 0.2 99.7 0.1 1 952 667 4.44
20:54:55 0.0 0.0 0.3 99.7 0.0 1 873 676 4.31
20:54:58 0.2 0.1 1.1 98.4 0.1 1 1508 1244 4.26
20:55:01 0.1 0.1 0.3 99.5 0.0 1 1191 877 4.42
20:55:04 0.1 0.1 0.3 99.6 0.0 1 994 757 4.44
20:55:07 0.6 0.1 0.3 99.0 0.0 1 1304 947 4.20
20:55:10 0.1 0.1 0.2 99.6 0.0 1 953 696 4.30
20:55:13 0.0 0.2 0.3 99.5 0.0 1 903 701 4.17
And here in the noise:
$ htop -F pulseaudio
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ ▽Command
3770 h43z 9 -11 1094M 30924 21580 S 4.7 0.1 10:56.31 /usr/bin/pulseaudio --daemonize=no --log-target=journal
$ powerstat -d0 3
Time User Nice Sys Idle IO Run Ctxt/s IRQ/s Watts
20:52:42 0.7 0.1 1.3 98.0 0.0 1 3460 1542 5.98
20:52:45 0.7 0.1 1.4 97.8 0.0 2 3723 1788 6.00
20:52:48 0.7 0.1 1.4 97.8 0.0 3 3487 1719 5.99
20:52:51 1.1 0.1 1.4 97.5 0.0 1 3916 1861 6.34
20:52:54 0.6 0.1 1.3 98.0 0.0 1 3514 1692 5.89
20:52:57 0.7 0.1 1.8 97.3 0.1 1 3956 2067 6.57
20:53:00 0.8 0.1 1.1 98.0 0.0 1 3534 1522 5.88
20:53:03 0.6 0.1 1.2 98.0 0.0 1 3437 1626 5.87
20:53:06 0.7 0.0 1.2 98.0 0.0 1 3400 1700 5.94
20:53:09 0.8 0.1 1.3 97.9 0.0 2 3474 1421 5.88
20:53:12 0.7 0.1 1.2 98.0 0.0 1 3317 1441 5.84
20:53:15 1.3 0.1 1.6 97.0 0.0 4 3841 1777 5.83
20:53:18 1.1 0.1 1.6 97.1 0.1 1 4161 1906 5.87
20:53:21 0.7 0.1 1.3 97.9 0.0 1 3422 1505 5.90
20:53:24 1.5 0.1 1.7 96.8 0.0 3 3430 1707 5.91
20:53:27 0.7 0.1 1.3 97.9 0.0 1 3375 1511 6.73
20:53:30 0.7 0.0 1.2 98.0 0.0 1 3572 1563 6.13
It’s not – about 1.5 watt more.
So, in a heavy heart, I decided not to disable email notifications – even if I want to keep them – but the elimination of white noise is my priority.
I thought I had “fixed” the problem. Of course, I blame Microsoft, right?
But the red sound sound of my i3 status status star continues to shine sometimes.
And it turned out other websites also prompted white noise.
For example, when I clicked somewhere x.comThe noise begins. Similarly, whenever I listen to a translation Translate.google.comhave noise.
And have Discord (Web app) Website is open, same bad effect.
So now I’m so shocked. What happened here.
I began to see how you play the HTML / JavaScript audio.
Seems two ways: either on Tag or the Webaudio fire.
As the sight is playing with the dynamic sound, I know that Webaudio API should be used.
And do anything with audio, you need to make a AudioContext
.
const audioCtx = new AudioContext();
And here I realize the problem. Just making this audiocontext makes my speakers play white noise.
You can try it here: https://editor.43z.one/sqare/i
The MDN article is a bit clear about it.
AudioContext.suspend()
Suspends the progression of time in the audio context, temporarily halting
audio hardware access and reducing CPU/battery usage in the process.
AudioContext.resume()
Resumes the progression of time in an audio context that has previously
been suspended.
However most websites never wear audiocontext suspension and create one without immediately for sound play.
Chrome stops the battery / CPU automatic. Firefox no. It keeps playing whitenoise.
I understand that websites to blame here.
But yet, Cmonine Finefox, protected me from this resource steal ?!
Oh and BTW it is also wasting my Bluetoothhone headphone battery if they are connected. Once I have a click on X.com shipping white noise begins.
To solve the whole difference, I make an extension automatically suspended audiocontext while also attempted to keep it if websites want to play sound. (not perfect but employed)
Some related reports of Bugzilla
2025-02-16 00:32:00