zinit icon indicating copy to clipboard operation
zinit copied to clipboard

[bug]: Constant CPU usage

Open mustaqimM opened this issue 1 year ago • 19 comments

What happened?

Hello, I noticed with even just a plain zinit .zshrc, around 0.7% per shell is used; this increases with more plugins.

Steps to reproduce

  1. .zshrc with contents:
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"
source "${ZINIT_HOME}/zinit.zsh"

Relevant output

No response

Screenshots and recordings

Zinit with no plugins: swappy-20220819_070853

Zinit with plugins: swappy-20220819_081746

zsh -df: swappy-20220819_071332

Operating System & Version

OS: linux-gnu | Vendor: unknown | Machine: x86_64 | CPU: x86_64 | Processor: unknown | Hardware: x86_64

Zsh version

zsh 5.9 (x86_64-unknown-linux-gnu)

Terminal emulator

alacritty

If using WSL on Windows, which version of WSL

No response

Additional context

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

mustaqimM avatar Aug 19 '22 05:08 mustaqimM

~And what is the CPU usage without Zinit?~

Didn't see the last picture

vladdoster avatar Aug 19 '22 23:08 vladdoster

I've hunted down this code that I will investigate. Screenshot 2022-08-19 at 18 31 25

It seems to be constantly running in the background

vladdoster avatar Aug 19 '22 23:08 vladdoster

I see the same behavior, CPU usage without zinit for a zsh shell on my end is 0. With zinit is high, the same pattern reported by @mustaqimM. The constant CPU usage stops when an application is run in zsh shell, for instance vim/nvim. On my end the idle CPU usage of vim/nvim is 0. So keeping vim/nvim instance open in a zsh shell has lower overall CPU activity than keeping the shell at prompt waiting for user input.

@vladdoster How did you hunt that down? Any tool/methodology or commenting different parts of code?

poetaman avatar Aug 26 '22 21:08 poetaman

@reportaman Noted. Thanks for the information!

I've finally got some time to dig into this tonight and over Labor day weekend.


To answer your questions:

I started a new Zsh instance with debugging via:

exec zsh --interactive --login --verbose --xtrace

and saw this output over and over when idle.

There is another tool I like for debugging, but will be cumbersome/frustrating to use if you aren't super familiar with using a debugger. That said, observability is useful even if you're just debugging your personal ZSH configuration.

https://zshdb.readthedocs.io/en/latest/

vladdoster avatar Sep 01 '22 00:09 vladdoster

Have a feeling this is related to #315?

vladdoster avatar Nov 01 '22 11:11 vladdoster

This can be lowered down by making scheduler sleep for a longer time like 5 seconds. It currently sleeps for 1 second.

wt., 1 lis 2022, 12:46 użytkownik vladislav doster @.***> napisał:

Have a feeling this is related to #315 https://github.com/zdharma-continuum/zinit/issues/315?

— Reply to this email directly, view it on GitHub https://github.com/zdharma-continuum/zinit/issues/362#issuecomment-1298399159, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOE4CHR3PYJZ6K72GHLRJLWGD7KXANCNFSM567OJ6QA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

psprint avatar Nov 02 '22 06:11 psprint

@psprint whats the reason for running the scheduler so often?

vladdoster avatar Nov 03 '22 00:11 vladdoster

It's for wait ice - it's checked every second to run tasks in proper time slots. I guess that it could be changed to sleep for exactly the needed time or to sleep longer after e.g. fitst 5 seconds.

czw., 3 lis 2022, 01:15 użytkownik vladislav doster < @.***> napisał:

@psprint https://github.com/psprint whats the reason for running the scheduler so often?

— Reply to this email directly, view it on GitHub https://github.com/zdharma-continuum/zinit/issues/362#issuecomment-1301513181, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOE4CHR6JPFHQ3Y5S4W5T3WGL7ZLANCNFSM567OJ6QA . You are receiving this because you were mentioned.Message ID: @.***>

psprint avatar Nov 03 '22 09:11 psprint

@vladdoster: the fix is in #409.

psprint avatar Nov 03 '22 11:11 psprint

Can it be event based, or can a user use zinit in a way to avoid this loop altogether? It might look like low overhead, but that's subjective. At the moment an idle zsh shell with zinit installed executes 2 million+ aarch64 instructions per second on my computer. A power user who has tens of shell sessions on over time would see battery dipping... Or if someone were to port their zshrc to something like raspberry pi or other smaller computers they will have to get rid of zinit.

poetaman avatar Nov 07 '22 22:11 poetaman

Can it be event based, or can a user use zinit in a way to avoid this loop altogether? It might look like low overhead, but that's subjective. At the moment an idle zsh shell with zinit installed executes 2 million+ aarch64 instructions per second on my computer. A power user who has tens of shell sessions on over time would see battery dipping... Or if someone were to port their zshrc to something like raspberry pi or other smaller computers they will have to get rid of zinit.

Yes the fix is in #409, @poetaman . It changes the sleep time to 10 seconds and makes a short circuit exit if no tasks.

psprint avatar Nov 08 '22 07:11 psprint