2013/2/7 Ville Tuulos <span dir="ltr"><<a href="mailto:tuulos@gmail.com" target="_blank">tuulos@gmail.com</a>></span><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Thu, Feb 7, 2013 at 7:27 AM, Björn-Egil Dahlberg <<a href="mailto:egil@erlang.org">egil@erlang.org</a>> wrote:<br>
> I dug out what I wrote a year ago ..<br>
><br>
> eep-draft:<br>
> <a href="https://github.com/psyeugenic/eep/blob/egil/system_limits/eeps/eep-00xx.md" target="_blank">https://github.com/psyeugenic/eep/blob/egil/system_limits/eeps/eep-00xx.md</a><br>
><br>
> Reference implementation:<br>
> <a href="https://github.com/psyeugenic/otp/commits/egil/limits-system-gc/OTP-9856" target="_blank">https://github.com/psyeugenic/otp/commits/egil/limits-system-gc/OTP-9856</a><br>
> Remember, this is a prototype and a reference implementation.<br>
><br>
> There is a couple of issues not addressed or at least open-ended.<br>
<br>
</div>Looks great! I truly hope this will get accepted rather sooner than<br>
later, at least as an experimental feature.<br></blockquote><div><br></div><div>Well as I said previously, I still have some issues with this approach.</div><div>Envision the goal and then see if this is a good first step.</div>
<div>Meaning: if we want to include other resource limits that is not process oriented how would we then design the interface then? A resource limits interface perhaps?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Does the proposal cover refc binaries as well? Almost all interesting<br>
use cases that I can imagine for limits involve binaries.<br></blockquote><div><br></div><div>My implementation doesn't cover it. It covers process memory blocks, i.e. everything except refc-binaries. (well their headers are covered: procbins)</div>
<div><br></div><div>Binaries are special beasts and needs special care. The easiest way to implement refc binary limits would be to use the virtual binary heap concept already present in the gc.</div><div><br></div><div>By using vheaps we would count all the memory referenced by procbins in the process heap. Now, here it gets interesting. Several procbins may refer to the same refc binary blob and that memory would be counted several times. You would have to take special accounting care if you want refc uniqueness which would make it kind of expensive. You can't mark the binary as counted since other processes might be gc:ed and they might also reference it so you would have to make some other accommodations.</div>
<div><br></div><div>Other than that you can just use the same idea as heap_size limits. The checks are similar and the api could be too: erlang:process_flag(limits, [{bin_vheap_size, Size}]). </div><div> </div><div>// Björn-Egil</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Here's one test case:<br>
<br>
1. Create a web server in Erlang e.g. with Mochiweb, Cowboy, Inets.<br>
2. Create a request handler that expects to receive Zip files, which<br>
it extracts with zip:extract(Request, [memory]).<br>
3. Create a zip bomb [1]: dd if=/dev/zero bs=1M count=8000 | zip req.zip -<br>
4. POST the small req.zip to the web server.<br>
5. See the VM go down in flames.<br>
<br>
Obviously the per-process limits would elegantly solve this problem if<br>
they covered binaries as well. A far less elegant solution would be to<br>
handle the unsafe decompression with an external process and open_port<br>
(inefficient) or implement a sophisticated alternative to the zip<br>
module which handles the limits by itself (inefficient, annoying).<br>
<br>
I understand that limiting the message queue / ets / NIFs can be<br>
trickier. Just covering the basic max-heap with binaries would be a<br>
good starting point.<br>
<br>
Ville<br>
<br>
[1] <a href="http://en.wikipedia.org/wiki/Zip_bomb" target="_blank">http://en.wikipedia.org/wiki/Zip_bomb</a><br>
<div class="HOEnZb"><div class="h5"><br>
> * Should processes be able to set limits on other processes? I think not<br>
> though my draft argues for it. It introduces unnecessary restraints on erts<br>
> and hinders performance. 'save_calls' is such an option.<br>
><br>
> * ets - if your table increases beyond some limit. Who should we punish? The<br>
> inserter? The owner? What would be the rationale? We cannot just punish the<br>
> inserter, the ets table is still there taking a lot of memory and no other<br>
> process could insert into the table. They would be killed as well. Remove<br>
> the owner and hence the table (and potential heir)? What kind of problems<br>
> would arise then? Limits should be tied into a supervision strategy and<br>
> restart the whole thing.<br>
><br>
> * In my draft and reference implementation I use soft limits. Once a process<br>
> reaches its limit it will be marked for termination by an exit signal. The<br>
> trouble here is there is no real guarantee for how long this will take. A<br>
> process can continue appending a binary for a short while and ending the<br>
> beam with OOM still. (If I remember it correctly you have to schedule out to<br>
> terminate a process in SMP thus you need to bump all reduction. But, not all<br>
> things handle return values from the garbage collector, most notably within<br>
> the append_binary instruction). There may be other issues as well.<br>
><br>
> * Message queues. In the current implementation of message queues we have<br>
> two queues. An inner one which is locked by the receiver process while<br>
> executing and an outer one which other processes will use and thus not<br>
> compete for a message queue lock with the executing process. When the inner<br>
> queue is depleted the receiver process will lock the outer queue and move<br>
> the entire thing to the inner one. Rinse and repeat. The only guarantee we<br>
> have to ensure with our implementation is: signal order between two<br>
> processes. So, in the future we might have several queues to improve<br>
> performance. If you introduce monitoring of the total number messages in the<br>
> abstracted queue (all the queues) this will most probable kill any sort of<br>
> scalability. For instance a sender would not be allowed to check the inner<br>
> queue for this reason. Would a "fast" counter check in the inner queue be<br>
> allowed? Perhaps if it is fast enough, but any sort of bookkeeping costs<br>
> performance. If we introduce even more queues for scalability reasons this<br>
> will cost even more.<br>
><br>
> * What about other memory users? Drivers? NIFs?<br>
><br>
> I do believe in increments in development as long it is path to the<br>
> envisioned goal.<br>
> And to reiterate, i'm not convinced that limits on just processes is the way<br>
> to go. I think a complete monitoring system should be envisioned, not just<br>
> for processes.<br>
><br>
> // Björn-Egil<br>
><br>
> On 2013-02-06 23:03, Richard O'Keefe wrote:<br>
><br>
> Just today, I saw Matthew Evans'<br>
><br>
> This pertains to a feature I would like to see<br>
> in Erlang. The ability to set an optional<br>
> "memory limit" when a process and ETS table is<br>
> created (and maybe a global optional per-process<br>
> limit when the VM is started). I've seen a few<br>
> cases where, due to software bugs, a process size<br>
> grows and grows; unfortunately as things stand<br>
> today the result is your entire VM crashing -<br>
> hopefully leaving you with a crash_dump.<br>
><br>
> Having such a limit could cause the process to<br>
> terminate (producing a OOM crash report in<br>
> erlang.log) and the crashing process could be<br>
> handled with supervisor rules. Even better you<br>
> can envisage setting the limits artificially low<br>
> during testing to catch these types of bugs early on.<br>
><br>
> in my mailbox. I have seen too many such e-mail messages.<br>
> Here's a specific proposal. It's time _something_ was done<br>
> about this kind of problem. I don't expect that my EEP is<br>
> the best way to deal with it, but at least there's going to<br>
> be something for people to point to.<br>
><br>
><br>
><br>
> _______________________________________________<br>
> eeps mailing list<br>
> <a href="mailto:eeps@erlang.org">eeps@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/eeps" target="_blank">http://erlang.org/mailman/listinfo/eeps</a><br>
><br>
><br>
><br>
</div></div><div class="HOEnZb"><div class="h5">> _______________________________________________<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>
><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>
</div></div></blockquote></div><br>