buggregator / trap

Buggregator local debug server for PHP projects as composer package
https://buggregator.dev
BSD 3-Clause "New" or "Revised" License
115 stars 9 forks source link

High cpu usage #139

Open paveldvorak5 opened 3 weeks ago

paveldvorak5 commented 3 weeks ago

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 commented 3 weeks ago

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

wesamls commented 5 days ago

Same for me, using macOS 12.3.

paveldvorak5 commented 5 days ago

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

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

roxblnfk commented 5 days ago

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

Socket accepts: https://github.com/buggregator/trap/blob/17f299e2e93c3492c75537f2c6f6be565737903e/src/Socket/Server.php#L85

And socket select https://github.com/buggregator/trap/blob/17f299e2e93c3492c75537f2c6f6be565737903e/src/Socket/Client.php#L80

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).

https://github.com/buggregator/trap/blob/17f299e2e93c3492c75537f2c6f6be565737903e/src/Application.php#L127

paveldvorak5 commented 4 days ago

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

roxblnfk commented 4 days ago

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 commented 1 day ago

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

roxblnfk commented 1 day ago

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

paveldvorak5 commented 6 hours ago

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 commented 5 hours ago

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