trap icon indicating copy to clipboard operation
trap copied to clipboard

High cpu usage

Open paveldvorak5 opened this issue 1 year ago • 14 comments

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)

paveldvorak5 avatar Sep 02 '24 15:09 paveldvorak5

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

roxblnfk avatar Sep 02 '24 15:09 roxblnfk

Same for me, using macOS 12.3.

wesamls avatar Sep 19 '24 13:09 wesamls

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

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

paveldvorak5 avatar Sep 19 '24 16:09 paveldvorak5

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

roxblnfk avatar Sep 19 '24 20:09 roxblnfk

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

paveldvorak5 avatar Sep 19 '24 22:09 paveldvorak5

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.

roxblnfk avatar Sep 20 '24 11:09 roxblnfk

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

paveldvorak5 avatar Sep 23 '24 17:09 paveldvorak5

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

roxblnfk avatar Sep 23 '24 17:09 roxblnfk

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.

paveldvorak5 avatar Sep 24 '24 16:09 paveldvorak5

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

roxblnfk avatar Sep 24 '24 17:09 roxblnfk

@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?

wesamls avatar Sep 25 '24 07:09 wesamls

@wesamls see #141

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

roxblnfk avatar Sep 25 '24 11:09 roxblnfk

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

wesamls avatar Sep 25 '24 13:09 wesamls

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.

paveldvorak5 avatar Sep 25 '24 20:09 paveldvorak5