High cpu usage
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)
Hi. Looks like just sockets checking. Is it Linux?
Same for me, using macOS 12.3.
Hi. Looks like just sockets checking. Is it Linux?
yes, it it What socket are we talking about? Maybe it's too often?
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
Yes, that's it. I "solved" the issue but changing the interval. Can setting a low interval break things?
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.
I changed it to 5000. I use it for local dev so the delay does not matter. Maybe put the value to config?
Could you check 1000 and 500? I think 1ms is comfortable value for developer and CPU :)
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.
I will release a patch with #140. It includes personal throttling for polling each socket. I would appreciate any feedback here about it.
@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 see #141
It would be great if you could find the optimal values for yourself and share them here.
Thanks for adding the environment variables.
I've used TRAP_MAIN_LOOP_INTERVAL=1000 and cpu went down to ~ 4.
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.