Tyrrrz / LightBulb

Reduces eye strain by adjusting screen gamma based on the current time
MIT License
2.23k stars 141 forks source link

Identify and reduce background CPU usage #273

Closed Tyrrrz closed 1 month ago

Tyrrrz commented 5 months ago

Version

v2.4.7 / v2.5*

Platform

Windows 11

Steps to reproduce

Start LightBulb and hide it to the tray.

Details

Observe passive CPU usage of 0.1-1%, depending on the environment.

Need to identify the largest contributors to CPU usage and come up with a strategy to reduce it. Open for external contributions.

Related to #248, but split out into a separate issue to focus only on the CPU aspect because the relatively high RAM usage (50-70mb) is a byproduct of running on the .NET runtime and can't easily be resolved.

Checklist

seapat commented 5 months ago

Maybe, implementing a separate process could solve this?

(Windows-Auto-Night-Mode)[https://github.com/AutoDarkMode/Windows-Auto-Night-Mode/tree/master] seems to do it that way. AutoDarkModeSvc.exe runs constantly, when opening the app from the tray icon, AutoDarkModeApp.exe spawns as a separate process.

I have no experience with Windows app development, so I am unsure if that really would help (maybe with the ram consumption in #248 ?). Obviously, rewriting the app takes some considerable effort, which might not be worth it.

Tyrrrz commented 5 months ago

Maybe, implementing a separate process could solve this?

(Windows-Auto-Night-Mode)[https://github.com/AutoDarkMode/Windows-Auto-Night-Mode/tree/master] seems to do it that way. AutoDarkModeSvc.exe runs constantly, when opening the app from the tray icon, AutoDarkModeApp.exe spawns as a separate process.

I have no experience with Windows app development, so I am unsure if that really would help (maybe with the ram consumption in #248 ?). Obviously, rewriting the app takes some considerable effort, which might not be worth it.

It could help, but first need to figure out what's even contributing to the CPU usage in the first place, to understand which areas need optimization.

LilithSilver commented 4 months ago

From some quick profiling, it looks like the vast majority of the background CPU usage comes from Avalonia's main render loop, with no traceable path to any code LightBulb seems to be calling.

image

LightBulb's internal modules only have 2% of the total CPU usage compared to Avalonia's >80%.

In terms of memory usage, I'm seeing about 150-200mb on my machine, the vast majority of which is Avalonia components:

image

I agree with @seapat, I think the solution here is to split off the backend into a separate process and only run the frontend when requested. I think that'll solve 99% of the memory and CPU issues.

EdoaLive commented 1 month ago

Hello, I don't know if it's related to this or it's another issue but I noticed an higher CPU usage that's not directly traceable to Lightbulb (in the details tab of task manager) but shows up as "kernel times" usage, mostly on a single core. When Lightbulb is closed the usage goes away and when it's opened again it comes back. (see attached picture) The same thing also happen with a slight gpu usage. This is enough to keep my laptop's fan spinning in the summer. I'm not much experienced here, but my guess is that it's likely due to frequent GPU driver calls.

image

I hope this helps to improve this great tool.

EDIT: forgot the version is 2.6.0, the OS is W11 and the GPU driver is AMD

Tyrrrz commented 1 month ago

@EdoaLive do you have gamma polling enabled by any chance? Was this measured during an active transition (when the temperature/brightness is in the process of change)?

EdoaLive commented 1 month ago

do you have gamma polling?

Nope. Also tried enabling this or disabling gamma smoothing, nothing changes.

Was this measured during an active transition?

Nope, at idle.

I also re-checked: actually GPU usage goes up (to 6% from 1-2% baseline) only when the GUI is open, while it goes down when reduced to systray. The CPU usage remains the same (as previous screenshot) until I close the app with right click->exit from the systray icon.

pbaykalov commented 1 month ago

@EdoaLive I don't know how you are measuring the GPU usage but two things are very important: 1) % GPU usage does not acoount for frequency change. When GPU is at low frequency usage stats are inflated. 2) % GPU usage you see in Task Manager is very likely calculated as max(per engine GPU usage), and there are multiple engines reported to task manager.

Tyrrrz commented 1 month ago

It seems that detaching the UI rendering loop from the gamma manipulation loop is the way forward. I have created a separate issue to focus specifically on that goal: #328. Closing this one, as it has served its investigative purpose (thanks everyone).