Yeah 30% is really not that much. There is one optimization you can do however. If you have an after block, this means erlang will be setting up a new timer for every packet. I would use erlang:send_after and have a constant timer that only gets executed every X seconds.<br>
<div><br></div><div>When it comes to serving network traffic erlang will surely work very well. I would recommend you take a look at the cowboy server and build on that (can be used as a http server or just plain TCP).</div>
<div><br></div><div><br></div><div>Sergej</div><div><br><div class="gmail_quote">On Fri, Oct 12, 2012 at 9:55 AM, Gleb Peregud <span dir="ltr"><<a href="mailto:gleber.p@gmail.com" target="_blank">gleber.p@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Fri, Oct 12, 2012 at 9:48 AM, Anita Wong <<a href="mailto:anita.wong@talkboxapp.com">anita.wong@talkboxapp.com</a>> wrote:<br>
> Hi All,<br>
><br>
> Sorry that I'm an Erlang newbie and may make stupid question. But please<br>
> help me to solve the issue.<br>
><br>
> I have written an Erlang server to replace the one I'm using with Node.js,<br>
> which ate all my memory and I'm praying that Erlang could be a way out.<br>
> The server works properly under unit test and internal testing, but face a<br>
> high CPU usage in stress test.<br>
><br>
> After trimming down, I found that the CPU burst was due to the TCP receive<br>
> from clients.<br>
><br>
> receiveClientPacket(Sock) -><br>
> inet:setopts(Sock, [{active, once}, {buffer, ?CLIENTHEARTBEATSIZE}]),<br>
> receive<br>
> {tcp, Sock, Data} -><br>
> {ok, Data};<br>
> {tcp_closed, Sock} -><br>
> {error, closed}<br>
> after ?CLIENTRECCEIVETIMEOUT -><br>
> {error, timeout}<br>
> end.<br>
><br>
><br>
> I tried making the process sleep for 10 hours at the beginning of the<br>
> function (to prevent it from calling receive), the CPU didn't burst at all.<br>
> Therefore I conclude that the burst of CPU is due to TCP receive. (Please<br>
> correct me if I made any mistake)<br>
><br>
> Here are information about my stress test:<br>
><br>
> start the Erlang server with:<br>
><br>
> erl +zdbbl 2097151 -K true +A 128 +P 5000000<br>
><br>
> connect 5000 clients to the Erlang server<br>
> each connected client sends a 2 byte data to the server every 1 min<br>
> after all the connections is done, (i.e. only the 2 byte data per min are<br>
> performing), the CPU burst to ~30%sy (from "top")<br>
><br>
><br>
> I'm using an Amazon Linux AMI (large instance, 64-bit) for the Erlang<br>
> server. Is the burst due to the linux? As I have no idea how the system will<br>
> use up the CPU. Or is it my poor code's problem? (I believe so...)<br>
><br>
> In real situation, our servers don't only receive ping pong, but also<br>
> messages, which is a lot more loading... This is only the first step...<br>
><br>
> Millions of thanks to anyone who can save me.<br>
><br>
> Anita~*<br>
><br>
> ~~~~~~~~~~~~~~~~~~~~~~~<br>
><br>
> Information about large instance (for reference):<br>
> 7.5 GB memory<br>
> 4 EC2 Compute Units (2 virtual cores with 2 EC2 Compute Units each)<br>
> 850 GB instance storage<br>
> 64-bit platform<br>
> I/O Performance: High<br>
<br>
</div></div>Hello Anita<br>
<br>
Please try rewriting the code to use {active, false} and gen_tcp:recv<br>
instead and see if it helps. Also high CPU usage might be due to lock<br>
contention of the VM - try running a VM with lock-counters enabled (<br>
<a href="http://www.erlang.org/doc/apps/tools/lcnt_chapter.html" target="_blank">http://www.erlang.org/doc/apps/tools/lcnt_chapter.html</a> ) to see if<br>
there's some lock which is contended.<br>
<br>
But the most important question - is 30% CPU burst really a problem?<br>
30% is usually not a big CPU usage, unless you need some sort of real<br>
time guarantees. Also Erlang VM uses spin locks and sometimes it uses<br>
more CPU than similar code in other language, but spin locks are used<br>
to reduce latency.<br>
<br>
Best regards,<br>
Gleb Peregud<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br></div>