xanderfrangos / twinkle-tray

Easily manage the brightness of your monitors in Windows from the system tray
https://twinkletray.com
MIT License
4.86k stars 160 forks source link

Frequent huge CPU consumption #493

Open phenetas opened 1 year ago

phenetas commented 1 year ago

Hello,

TLDR: see my "EDIT 3" below, I have managed to reproduce the heavy CPU usage, everytime I switch from 1 virtual desktop to another. Twinkle Tray goes crazy with 3/4 of my CPU usage for 1 minute.

I am on Windows 11, and uses Twinkle Tray v1.14.7.

Frequently I notice the app consumes a huge part of my CPU which is a Ryzen 5 5600H on a Lenovo Legion 5 laptop.

Please see the screenshot below showing TT usage. Sometimes TT uses up to 80/90% of the CPU during 20 to 30 seconds, and then releases it. The RAM usage can go as well up to a few hundreds MB ~ 400 or 500 is not impossible. It is reproducible at each Windows boot. And from time to time it happens as well when the laptop runs normaly.

I uses 3 monitors (2 external + 1 from the laptop)

The problem is that my fans go crazy when this happen, and it is loud for a few seconds. Is this known? And is there a way to prevent that?

Thanks

image image

post+20minutes : EDIT : just now 20 min after my initial post : see last screenshot image image

post+4hours : EDIT 2 : if it helps I am a heavy user of 4 virtual desktops. With 3 monitors. If it helps.

post+5hours : EDIT 3 : OK, now I see that is it reproducible. Everytime I change from 1 virtual desktop to another using Windows + Tab and mouse click on the one virtual desktop I want : BOOM instant Twinkle Tray heavy CPU usage.

xanderfrangos commented 1 year ago

Hi @phenetas,

That's very weird. I'm not able to replicate it on any of my devices.

If you're not using the Microsoft Store version, you can hopefully find out what's going on by doing this.

  1. Close Twinkle Tray.
  2. Open Command Prompt
  3. Run "%LocalAppData%\Programs\twinkle-tray\Twinkle Tray.exe" -console
  4. Reproduce the issue and check what is logged by the console.

Then copy/paste it here and I can see about resolving the issue. I recently added extra logging to the console, so please update to v1.14.8 if possible.

Thanks!

phenetas commented 1 year ago

Thanks for your fast answer. I was using the Store version, but I replaced it with the .exe one for the test. Here is the log. While writing this, TT uses around 50% of my CPU and 670Mo of Ram.

C:\Users\phene> Starting Twinkle Tray... Unloaded module: uuid/v4 Function: getThemeRegistry Monitor.js starting. If you see this more than once, something bad happened. Running wmi-bridge test... Analytics: sending with UUID c9dd3fb4-cc83-4506-b5f5-aafd888f2c7e

-------------- Refresh Monitors -------------- Doing full refresh. WMI-BRIDGE TEST: READY WMI-BRIDGE TEST: OK getMonitorsWMIC() Total: 55ms getMonitorsWin32() Total: 1ms Event: handleMonitorChange Pausing mouse events... Event: handleBackgroundUpdate Checking for updates... Unloaded module: node-fetch Found version: v1.14.8 Event: handleBackgroundUpdate getFeaturesDDC() Total: 6283ms getBrightnessWMIC() Total: 85ms getAllMonitors() total: 6427ms Monitors found: 5&64287e1&0&UID256,5&935f3ea&0&UID4352,5&935f3ea&0&UID4354

Setting brightness for known displays false false 1 Creating panel...

-------------- Refresh Monitors -------------- Doing full refresh. Analytics: sending with UUID c9dd3fb4-cc83-4506-b5f5-aafd888f2c7e Function: getThemeRegistry Function: getThemeRegistry Panel ready!


getMonitorsWMIC() Total: 260ms
getMonitorsWin32() Total: 5ms
Analytics: sending with UUID c9dd3fb4-cc83-4506-b5f5-aafd888f2c7e
getFeaturesDDC() Total: 5789ms
getBrightnessWMIC() Total: 116ms
getAllMonitors() total: 6177ms
Monitors found: 5&64287e1&0&UID256,5&935f3ea&0&UID4352,5&935f3ea&0&UID4354
===--- NO CHANGE ---===
----------------------------------------------

-------------- Refresh Monitors --------------
Doing full refresh.
getMonitorsWMIC() Total: 95ms
getMonitorsWin32() Total: 1ms
getFeaturesDDC() Total: 5799ms
getBrightnessWMIC() Total: 288ms
getAllMonitors() total: 6188ms
Monitors found: 5&64287e1&0&UID256,5&935f3ea&0&UID4352,5&935f3ea&0&UID4354
===--- NO CHANGE ---===
----------------------------------------------

-------------- Refresh Monitors --------------
Resuming mouse events...
getBrightnessWMIC() Total: 116ms
===--- NO CHANGE ---===
----------------------------------------------
Event: theme updated
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry

-------------- Refresh Monitors --------------
getBrightnessWMIC() Total: 374ms
===--- NO CHANGE ---===
----------------------------------------------
Function: getThemeRegistry
Analytics: sending with UUID c9dd3fb4-cc83-4506-b5f5-aafd888f2c7e
~~~~~ MAIN WINDOW RESTORED ~~~~~~
Pausing mouse events...
~~~~~ MAIN WINDOW MINIMIZED ~~~~~~
Saved known displays!
Event: theme updated
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Ev
xanderfrangos commented 1 year ago

According to that, something on your PC is triggering a bunch of theme changes in Windows. Do you have any 3rd party software that changes your theme (dark/light mode) or accent color?

phenetas commented 1 year ago

Nope, nothing that I am aware of. I tend to keep my PC clean with the least possible tweaks and softs installed. I think twice before installing anything.

I use Windows dark mode (native). And my wallpaper is a static image.

xanderfrangos commented 1 year ago

Perhaps there's some Lenovo software on the laptop that's triggering the theme changes?

phenetas commented 1 year ago

I have this from Lenovo Vantage running in the background. How do I stop it from running ? for a few minutes so I can test.

image

phenetas commented 1 year ago

Ok so, I've manually killed every LenovoVantage process I could see. Closed all my opened Chrome windows, and closed everything possible except for 1 Firefox window + task manager + cmd

Redo the same test, and this time TT did not use any CPU.

Here is a log dump:

C:\Users\phene> Starting Twinkle Tray... Unloaded module: uuid/v4 Function: getThemeRegistry Monitor.js starting. If you see this more than once, something bad happened. Running wmi-bridge test... Analytics: sending with UUID c9dd3fb4-cc83-4506-b5f5-aafd888f2c7e

-------------- Refresh Monitors -------------- Doing full refresh. WMI-BRIDGE TEST: READY WMI-BRIDGE TEST: OK getMonitorsWMIC() Total: 62ms getMonitorsWin32() Total: 1ms Event: handleMonitorChange Pausing mouse events... Event: handleBackgroundUpdate Checking for updates... Unloaded module: node-fetch Found version: v1.14.8 Event: handleBackgroundUpdate Couldn't refresh monitors Monitor thread timed out.

Setting brightness for known displays false false 1 Could not update brightness

TypeError: Cannot convert undefined or null to object at Function.values () at updateBrightness (C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:1354:24) at applyProfile (C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:631:11) at setKnownBrightness (C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:605:3) at C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:2183:39

Could not update brightness

TypeError: Cannot convert undefined or null to object at Function.values () at updateBrightness (C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:1354:24) at applyProfile (C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:631:11) at setKnownBrightness (C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:605:3) at C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:2183:39

Could not update brightness

TypeError: Cannot convert undefined or null to object at Function.values () at updateBrightness (C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:1354:24) at applyProfile (C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:631:11) at setKnownBrightness (C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:605:3) at C:\Users\phene\AppData\Local\Programs\twinkle-tray\resources\app.asar\src\electron.js:2183:39

Creating panel...

-------------- Refresh Monitors -------------- Doing full refresh. Analytics: sending with UUID c9dd3fb4-cc83-4506-b5f5-aafd888f2c7e Function: getThemeRegistry Function: getThemeRegistry Panel ready! Thread busy. Cancelling refresh. Couldn't refresh monitors No monitors recieved!


Unloaded module: sharp
Unloaded module: sharp
getFeaturesDDC Timed out.
getFeaturesDDC() failed! {}
getBrightnessWMIC() Total: 318ms
getBrightnessWMI() skipped due to previous failure.
getAllMonitors() total: 15884ms
Monitors found: 5&64287e1&0&UID256,5&935f3ea&0&UID4354,5&935f3ea&0&UID4352
Unloaded module: sharp
Unloaded module: sharp

-------------- Refresh Monitors --------------
Doing full refresh.
getMonitorsWMIC() Total: 56ms
getMonitorsWMI() skipped due to previous failure.
getMonitorsWin32() Total: 0ms
getFeaturesDDC() Total: 5841ms
getBrightnessWMIC() Total: 67ms
getBrightnessWMI() skipped due to previous failure.
getAllMonitors() total: 5967ms
Monitors found: 5&64287e1&0&UID256,5&935f3ea&0&UID4354,5&935f3ea&0&UID4352
----------------------------------------------
Event: handleBackgroundUpdate
xanderfrangos commented 1 year ago

Yeah, looks like whatever you closed fixed it. I guess restart your PC and try tinkering with the Lenovo Vantage software. Whatever is causing the theme update spam hopefully can be turned off without disabling/uninstalling the LenovoVantage service entirely.

Kodehawa commented 1 year ago

I have this everytime I switch desktops. Just switching desktops back and forth is enough for it to use 50% of my CPU in a 5700X on 1.15.0.

image image

Kodehawa commented 1 year ago

Still an issue on 1.15.1.


Starting Twinkle Tray...
Unloaded module: uuid/v4
Function: getThemeRegistry
Monitor.js starting. If you see this more than once, something bad happened.
Running wmi-bridge test...
Analytics: sending with UUID d70cfbe7-d1b5-4a36-809b-9030a43a7982

-------------- Refresh Monitors --------------
Doing full refresh.
WMI-BRIDGE TEST: READY
WMI-BRIDGE TEST: OK
getMonitorsWMIC() Total: 43ms
getMonitorsWin32() Total: 1ms
Event: handleMonitorChange
Pausing mouse events...
getFeaturesDDC() Total: 4010ms
getBrightnessWMIC() Total: 43ms
getAllMonitors() total: 4098ms
Monitors found: 5&2536744b&0&UID512,5&2536744b&0&UID520
----------------------------------------------
Setting brightness for known displays false false 1
Creating panel...

-------------- Refresh Monitors --------------
Doing full refresh.
Analytics: sending with UUID d70cfbe7-d1b5-4a36-809b-9030a43a7982
Function: getThemeRegistry
Function: getThemeRegistry
Panel ready!
getMonitorsWMIC() Total: 44ms
getMonitorsWin32() Total: 1ms
getForegroundWindow: 6948580
~~~~~ MAIN WINDOW MINIMIZED ~~~~~~
trySetForegroundWindow: 6948580
trySetForegroundWindow: 6948580
trySetForegroundWindow: 6948580
trySetForegroundWindow: 6948580
getForegroundWindow: 6948580
trySetForegroundWindow: 6948580
trySetForegroundWindow: 6948580
trySetForegroundWindow: 6948580
trySetForegroundWindow: 6948580
Unloaded module: sharp
Event: handleBackgroundUpdate
Checking for updates...
Unloaded module: node-fetch
Found version: v1.15.1
Event: handleBackgroundUpdate
getFeaturesDDC() Total: 3898ms
getBrightnessWMIC() Total: 42ms
getAllMonitors() total: 3987ms
Monitors found: 5&2536744b&0&UID512,5&2536744b&0&UID520
===--- NO CHANGE ---===
----------------------------------------------

-------------- Refresh Monitors --------------
Doing full refresh.
getMonitorsWMIC() Total: 44ms
getMonitorsWin32() Total: 1ms
getFeaturesDDC() Total: 3437ms
getBrightnessWMIC() Total: 44ms
getAllMonitors() total: 3527ms
Monitors found: 5&2536744b&0&UID512,5&2536744b&0&UID520
===--- NO CHANGE ---===
----------------------------------------------
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Unloaded module: sharp
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Unloaded module: sharp
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: handleAccentChange
Function: getThemeRegistry
Event: theme updated
Function: getThemeRegistry
Unloaded module: sharp```
Kodehawa commented 1 year ago

This happens with so much as switching desktops. No external application needed for that to be absolutely spammed (and my CPU jumping to 100%) :P

Switching desktops isn't the only thing that seems to trigger it, changing wallpaper or similar stuff also causes it (which would cause it if you have a wallpaper change every few)

Why does it need to handle theme changes? And... why does it use 100% CPU while doing so? I'd be inclined to think it's better to put some sort of internal ratelimit to this (just switching desktop spams endless "handleAccentChange" and "getThemeRegistry" in <1s). This being the only application that does as much as change its CPU usage when switching desktops 🤔

Anyway, I fully appreciate this application :D, just trying to make issue poof

xanderfrangos commented 1 year ago

@Kodehawa Theme changes are tracked for:

  1. Detecting accent color
  2. Detecting light/dark theme
  3. Detecting and processing the current wallpaper for the fake "Mica" effect

I've made some improvements for v1.15.2 that should significantly reduce CPU usage when the theme changes. I'm getting 2-3% usage in Twinkle Tray when switching wallpapers now (13700k), so I'm guessing you'll see like 3-5% on your CPU.

Kodehawa commented 1 year ago

I just realized this is an electron app and not a native WinUI 3 application. Geez that's a pretty good imitation of the WinUI interface haha.

I'll give it a shot. Thanks you :)

Kodehawa commented 1 year ago

Haven't pushed it to the MS Store? I'd have preferred to update it there. Maybe MS is just delaying sending it :P

xanderfrangos commented 1 year ago

@Kodehawa Microsoft can take a while. I pushed it at the same time as GitHub:

Screenshot_20221211-141029

Kodehawa commented 1 year ago

image

Yep, way better now :) 3~4% CPU usage when switching wallpapers.

Thanks for the fix