high CPU load due epoll_work() doesn't actually sleep
epoll_work(timeout) calls epoll_fetch_events(1), this means epoll can sleep for 1ms only ~= no sleep at all. due that epoll_work() loop becomes busyloop and eats CPU without real need.
--multithread can't work due it's marked as optional argument in engine_add_engine_parse_options(). if fix that, multithread mode can be enabled, and epoll_work(timeout) is called with epoll_wait_timeout.
but, still, epoll_timeout harcoded as 1ms in mtproto_front_functions so gives same 1ms wait only.
is it possible to use more meaningful timeout for allowing proxy to sleep? there's check for timeout not more 1s, is it really required? under low load, even 30-60sec sleeping seems ok after some testing.
+1 10% xeon cpu load with 0 users connected. Debian. Official docker container.
I switched from socks5 to this proxy 2.5 days ago and here is how it looks performance-wise for 7 users:

Can it be fixed?
It's not just because of epoll not waiting -- socks5 has no encryption and it just forwards packets, while mtproxy has AES decryption/encryption
@stek29 aes enc/dec is not the case for lack of idling with no/few users active. please refer consumed cpu time for nginx (tls enabled, public, run for one week) and mtproto-proxy (2 users, run for 22h 24m) from one and the same host: mtproto-proxy was active for 21h 15m from 22h 24m total since the restart, including 8+ hours of time with no active user at all, nginx load is more than 700 times (!) less, counting day vs week diff.
$ ps axf
PID TTY STAT TIME COMMAND
9513 ? Ss 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
9514 ? S 0:11 \_ nginx: worker process
23185 ? Ssl 21:15 /usr/local/bin/mtproto-proxy ...
23192 ? Sl 21:18 \_ /usr/local/bin/mtproto-proxy ...
@stek29 nope, I just switched to python implementation of MTproto proxy (https://github.com/alexbers/mtprotoproxy) and CPU usage dropped back to 1-3% where it belongs. Same encryption, same users, same traffic. This is just a significant performance problem with this implementation.
i regisrer the same bug. and i anlyse source code https://github.com/TelegramMessenger/MTProxy/issues/174
@stek29, can you please update us on the status of this issue? Having single process eat up 2-3% CPU without a single client is not a good thing, it should really be fixed ASAP. Currently I have to patch it locally but that is not something I would like to do on every source update. Creating own fork and maintaining it is not a good option either.
@shadowlmd I'm not working on it -- I don't understand MTProxy code enough to avoid mistakes fixing it, and I don't have time to study the code deeply.
As of official Telegram -- I don't know what their plans are. I'm not affiliated with official Telegram in any way, I'm just helping to keep the repo at least somewhat maintained.
It's not a simple fix like ones I've done before -- see https://github.com/alexdoesh/mtproxy/pull/3#issuecomment-397624454
we shoud clone repo and fix this bug, if ok - make pull request
Just start new MTProto and strace it. You can see infinite cycle ot this: epoll_wait(6, [], 524288, 1) = 0 nanosleep({tv_sec=0, tv_nsec=0}, NULL) = 0 You can see 1ms timeout in epoll_wait and zero timeout in nanosleep.
So 6-10% CPU of vps out per each MTProto instnace. If you have 5 on one VPS (as proxy vendor) - it is a problem. I look into souce code and find this:
mtproto/mtproto-proxy.c:2300 .epoll_timeout = 1,
engine/engine.c:67 #define DEFAULT_EPOLL_WAIT_TIMEOUT 37
engine/engine.c:580 ( engine_startup() ) E->epoll_wait_timeout = DEFAULT_EPOLL_WAIT_TIMEOUT;
but in default_main i see this: engine/engine.c:594 engine_set_epoll_wait_timeout (E->epoll_timeout); //uses 1 ms
Please strace it, fix and reduce CPU usage.
fix is easy.
@stek29, I'm sorry, thought you were one of the developers. :)
Hi. How can i patch manualy to fix this problem? Do you have patch?
@eastygh, you can find the patch after the second reply in this issue. Here's a direct link: https://github.com/gerasiov/mtproxy/commit/a18c80be1ea7fd8c1321640276e8cd4eaebb3d01
I used to get 2 - 3 % CPU usage on my VPS, And after seeing this issue I changed the following lines :
mtproto/mtproto-proxy.c:2300
.epoll_timeout = 1, to .epoll_timeout = 500,
And
engine/engine.c:67
#define DEFAULT_EPOLL_WAIT_TIMEOUT 37 to #define DEFAULT_EPOLL_WAIT_TIMEOUT 500
Then recompiled and restarted, But nothing changed. Still using 3% CPU
UPDATE :
I straced it on the new build, I'm getting these two lines :
epoll_wait(6, [], 524288, 1) = 0
nanosleep({0, 0}, NULL) = 0
So it seems the problem is not there, Not in those lines.
Fixed it :grin:
I also sent a pull request to this repo, So you can use this one until the pull request is accepted : https://github.com/Marshal313/MTProxy
Only one commit is added, Also, you can change the DEFAULT_EPOLL_WAIT_TIMEOUT in engine.c line 67 and set it to your preferred timeout value (From 1 to 1000) based on your server.
Faced with the same issue and want to share some thoughts about the issue:
- The timeout of 1 ms has been chosen as the best option for heavy-loaded deployments (when CPU does work instead of idling).
- The low timeout is necessary for immediate handling of signals (like SIGTERM) and doing some housekeeping like collecting stats.
- It's just a typo and the original intention was
-1, not1, which means no timeout. :)
Anyway, these statements are just my unproven guesses. It is disappointing that developers keep away from solving problems and even collaboration.
@IlyaSemenov I've ended up applying epoll_wait patch -- that made cpu usage drop too. Third party implementations are much, much slower than mtproxy -- I've tried like 3 of most popular ones and all of them are way worse than mtproxy.
@stek29 well why then not really apply that patch or merge in this similar pull request? The current master tree does not include either of those.
