Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High cpu usage #139

Open
paveldvorak5 opened this issue Sep 2, 2024 · 14 comments · Fixed by #140
Open

High cpu usage #139

paveldvorak5 opened this issue Sep 2, 2024 · 14 comments · Fixed by #140
Labels
bug Something isn't working

Comments

@paveldvorak5
Copy link

Hello, trap --ui use a lot of cpu even if it's doing nothing.
Here's the cause, it's checking something every 50ns but I don't know which resource is not available.
Running with -vvv does not help.

strace log

clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=50000}, NULL) = 0
accept(4, 0x7ffea2aef0a0, [128])        = -1 EAGAIN (Resource temporarily unavailable)
accept(5, 0x7ffea2aef0a0, [128])        = -1 EAGAIN (Resource temporarily unavailable)
pselect6(9, [8], [8], [8], {tv_sec=0, tv_nsec=0}, NULL) = 1 (out [8], left {tv_sec=0, tv_nsec=0})
accept(6, 0x7ffea2aef0a0, [128])        = -1 EAGAIN (Resource temporarily unavailable)
accept(7, 0x7ffea2aef0a0, [128])        = -1 EAGAIN (Resource temporarily unavailable)
@roxblnfk
Copy link
Member

roxblnfk commented Sep 2, 2024

Hi.
Looks like just sockets checking.
Is it Linux?

@roxblnfk roxblnfk added the bug Something isn't working label Sep 2, 2024
@wesamls
Copy link

wesamls commented Sep 19, 2024

Same for me, using macOS 12.3.

@paveldvorak5
Copy link
Author

Hi. Looks like just sockets checking. Is it Linux?

yes, it it
What socket are we talking about? Maybe it's too often?

@roxblnfk
Copy link
Member

roxblnfk commented Sep 19, 2024

What socket are we talking about? Maybe it's too often?

Socket accepts:

while (!$this->cancelled and false !== ($socket = \socket_accept($this->socket))) {

And socket select

if (\socket_select($read, $write, $except, 0, 0) === false) {

It happens every tick.

I don't experience such issues on Windows, probably because the time for usleep() on Windows cannot be less than 10ms. Linux ticks at a smaller interval (50 microseconds).

\usleep($sleep);

@paveldvorak5
Copy link
Author

Yes, that's it. I "solved" the issue but changing the interval. Can setting a low interval break things?

@roxblnfk
Copy link
Member

The higher this value, the slower the content will be delivered to the browser, and it directly affects TTFB.
What value are you using? I think it can be used for socket polling, while delivering content at the minimum interval.

@paveldvorak5
Copy link
Author

I changed it to 5000. I use it for local dev so the delay does not matter. Maybe put the value to config?

@roxblnfk
Copy link
Member

roxblnfk commented Sep 23, 2024

Could you check 1000 and 500? I think 1ms is comfortable value for developer and CPU :)

@paveldvorak5
Copy link
Author

As expected, the usage went up from 1% to 4%.
When changed to 10000, the app seems to work fine, only it takes 30 s to start the port 8000.

@roxblnfk
Copy link
Member

I will release a patch with #140. It includes personal throttling for polling each socket. I would appreciate any feedback here about it.

@roxblnfk roxblnfk reopened this Sep 24, 2024
@wesamls
Copy link

wesamls commented Sep 25, 2024

@roxblnfk Thanks for the update.
After installing the latest version, the cpu usage still high but lower than last time (was ~ 23, now ~ 18).
Not sure how to change the "wait period"? any command parameter to pass or config file?

@roxblnfk
Copy link
Member

roxblnfk commented Sep 25, 2024

@wesamls see #141

It would be great if you could find the optimal values for yourself and share them here.

@wesamls
Copy link

wesamls commented Sep 25, 2024

Thanks for adding the environment variables.
I've used TRAP_MAIN_LOOP_INTERVAL=1000 and cpu went down to ~ 4.

@paveldvorak5
Copy link
Author

Can you check the optimal values?
I tried on local machine TRAP_TCP_POLLING_INTERVAL=10000 TRAP_MAIN_LOOP_INTERVAL=10000 vendor/bin/trap --ui to get to 0.5% CPU and it still works fine. So I think the default values are too high.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants