MTProxy icon indicating copy to clipboard operation
MTProxy copied to clipboard

high CPU load due epoll_work() doesn't actually sleep

Open themiron opened this issue 8 years ago • 26 comments

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.

themiron avatar Jun 07 '18 13:06 themiron

+1 10% xeon cpu load with 0 users connected. Debian. Official docker container.

denis-isaev avatar Jun 13 '18 22:06 denis-isaev

I switched from socks5 to this proxy 2.5 days ago and here is how it looks performance-wise for 7 users: image

Can it be fixed?

Timsonrobl avatar Jun 18 '18 06:06 Timsonrobl

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 avatar Jun 18 '18 17:06 stek29

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

themiron avatar Jun 18 '18 17:06 themiron

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

Timsonrobl avatar Jun 19 '18 06:06 Timsonrobl

i regisrer the same bug. and i anlyse source code https://github.com/TelegramMessenger/MTProxy/issues/174

iqdoctor avatar Jul 17 '18 16:07 iqdoctor

@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 avatar Jul 18 '18 16:07 shadowlmd

@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

stek29 avatar Jul 19 '18 07:07 stek29

we shoud clone repo and fix this bug, if ok - make pull request

iqdoctor avatar Jul 19 '18 13:07 iqdoctor

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.

iqdoctor avatar Jul 19 '18 13:07 iqdoctor

fix is easy.

iqdoctor avatar Jul 19 '18 13:07 iqdoctor

@stek29, I'm sorry, thought you were one of the developers. :)

shadowlmd avatar Jul 19 '18 13:07 shadowlmd

Hi. How can i patch manualy to fix this problem? Do you have patch?

eastygh avatar Jul 24 '18 13:07 eastygh

@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

shadowlmd avatar Jul 24 '18 14:07 shadowlmd

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.

DanteMarshal avatar Sep 22 '18 03:09 DanteMarshal

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.

DanteMarshal avatar Sep 23 '18 14:09 DanteMarshal

Faced with the same issue and want to share some thoughts about the issue:

  1. The timeout of 1 ms has been chosen as the best option for heavy-loaded deployments (when CPU does work instead of idling).
  2. The low timeout is necessary for immediate handling of signals (like SIGTERM) and doing some housekeeping like collecting stats.
  3. It's just a typo and the original intention was -1, not 1, 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.

azhuchkov avatar Dec 29 '18 10:12 azhuchkov

After I replaced MTProxy with mtg CPU usage immediately dropped:

image

This is a 4 CPU Linode droplet.

IlyaSemenov avatar Mar 26 '19 10:03 IlyaSemenov

@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 avatar Mar 26 '19 20:03 stek29

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

dvershinin avatar May 28 '19 20:05 dvershinin