… | |
… | |
76 | than using fork+exec in big processes. |
76 | than using fork+exec in big processes. |
77 | |
77 | |
78 | Special care has been taken to make this module useful from other modules, |
78 | Special care has been taken to make this module useful from other modules, |
79 | while still supporting specialised environments such as L<App::Staticperl> |
79 | while still supporting specialised environments such as L<App::Staticperl> |
80 | or L<PAR::Packer>. |
80 | or L<PAR::Packer>. |
|
|
81 | |
|
|
82 | =head1 WHAT THIS MODULE IS NOT |
|
|
83 | |
|
|
84 | This module only creates processes and lets you pass file handles and |
|
|
85 | strings to it, and run perl code. It does not implement any kind of RPC - |
|
|
86 | there is no back channel from the process back to you, and there is no RPC |
|
|
87 | or message passing going on. |
|
|
88 | |
|
|
89 | If you need some form of RPC, you can either implement it yourself |
|
|
90 | in whatever way you like, use some message-passing module such |
|
|
91 | as L<AnyEvent::MP>, some pipe such as L<AnyEvent::ZeroMQ>, use |
|
|
92 | L<AnyEvent::Handle> on both sides to send e.g. JSON or Storable messages, |
|
|
93 | and so on. |
81 | |
94 | |
82 | =head1 PROBLEM STATEMENT |
95 | =head1 PROBLEM STATEMENT |
83 | |
96 | |
84 | There are two ways to implement parallel processing on UNIX like operating |
97 | There are two ways to implement parallel processing on UNIX like operating |
85 | systems - fork and process, and fork+exec and process. They have different |
98 | systems - fork and process, and fork+exec and process. They have different |
… | |
… | |
272 | |
285 | |
273 | #TODO: maybe append the packet to any existing string command already in the queue |
286 | #TODO: maybe append the packet to any existing string command already in the queue |
274 | |
287 | |
275 | # ideally, we would want to use "a (w/a)*" as format string, but perl versions |
288 | # ideally, we would want to use "a (w/a)*" as format string, but perl versions |
276 | # from at least 5.8.9 to 5.16.3 are all buggy and can't unpack it. |
289 | # from at least 5.8.9 to 5.16.3 are all buggy and can't unpack it. |
277 | push @{ $self->[2] }, pack "N/a*", pack "(w/a*)*", @_; |
290 | push @{ $self->[2] }, pack "L/a*", pack "(w/a*)*", @_; |
278 | |
291 | |
279 | $self->[3] ||= AE::io $self->[1], 1, sub { |
292 | $self->[3] ||= AE::io $self->[1], 1, sub { |
280 | # send the next "thing" in the queue - either a reference to an fh, |
293 | # send the next "thing" in the queue - either a reference to an fh, |
281 | # or a plain string. |
294 | # or a plain string. |
282 | |
295 | |
… | |
… | |
329 | if ($pid eq 0) { |
342 | if ($pid eq 0) { |
330 | require AnyEvent::Fork::Serve; |
343 | require AnyEvent::Fork::Serve; |
331 | $AnyEvent::Fork::Serve::OWNER = $parent; |
344 | $AnyEvent::Fork::Serve::OWNER = $parent; |
332 | close $fh; |
345 | close $fh; |
333 | $0 = "$_[1] of $parent"; |
346 | $0 = "$_[1] of $parent"; |
|
|
347 | $SIG{CHLD} = 'IGNORE'; |
334 | AnyEvent::Fork::Serve::serve ($slave); |
348 | AnyEvent::Fork::Serve::serve ($slave); |
335 | exit 0; |
349 | exit 0; |
336 | } elsif (!$pid) { |
350 | } elsif (!$pid) { |
337 | die "AnyEvent::Fork::Early/Template: unable to fork template process: $!"; |
351 | die "AnyEvent::Fork::Early/Template: unable to fork template process: $!"; |
338 | } |
352 | } |
… | |
… | |
603 | $self->_cmd (r => $func); |
617 | $self->_cmd (r => $func); |
604 | } |
618 | } |
605 | |
619 | |
606 | =back |
620 | =back |
607 | |
621 | |
|
|
622 | =head1 PERFORMANCE |
|
|
623 | |
|
|
624 | Now for some unscientific benchmark numbers (all done on an amd64 |
|
|
625 | GNU/Linux box). These are intended to give you an idea of the relative |
|
|
626 | performance you can expect. |
|
|
627 | |
|
|
628 | Ok, so, I ran a simple benchmark that creates a socketpair, forks, calls |
|
|
629 | exit in the child and waits for the socket to close in the parent. I did |
|
|
630 | load AnyEvent, EV and AnyEvent::Fork, for a total process size of 6312kB. |
|
|
631 | |
|
|
632 | 2079 new processes per second, using socketpair + fork manually |
|
|
633 | |
|
|
634 | Then I did the same thing, but instead of calling fork, I called |
|
|
635 | AnyEvent::Fork->new->run ("CORE::exit") and then again waited for the |
|
|
636 | socket form the child to close on exit. This does the same thing as manual |
|
|
637 | socketpair + fork, except that what is forked is the template process |
|
|
638 | (2440kB), and the socket needs to be passed to the server at the other end |
|
|
639 | of the socket first. |
|
|
640 | |
|
|
641 | 2307 new processes per second, using AnyEvent::Fork->new |
|
|
642 | |
|
|
643 | And finally, using C<new_exec> instead C<new>, using vforks+execs to exec |
|
|
644 | a new perl interpreter and compile the small server each time, I get: |
|
|
645 | |
|
|
646 | 479 vfork+execs per second, using AnyEvent::Fork->new_exec |
|
|
647 | |
|
|
648 | So how can C<< AnyEvent->new >> be faster than a standard fork, een though |
|
|
649 | it uses the same operations, but adds a lot of overhead? |
|
|
650 | |
|
|
651 | The difference is simply the process size: forking the 6MB process takes |
|
|
652 | so much longer than forking the 2.5MB template process that the overhead |
|
|
653 | introduced is canceled out. |
|
|
654 | |
|
|
655 | If the benchmark process grows, the normal fork becomes even slower: |
|
|
656 | |
|
|
657 | 1340 new processes, manual fork in a 20MB process |
|
|
658 | 731 new processes, manual fork in a 200MB process |
|
|
659 | 235 new processes, manual fork in a 2000MB process |
|
|
660 | |
|
|
661 | What that means (to me) is that I can use this module without havign a |
|
|
662 | very bad conscience because of the extra overhead requried to strat new |
|
|
663 | processes. |
|
|
664 | |
608 | =head1 TYPICAL PROBLEMS |
665 | =head1 TYPICAL PROBLEMS |
609 | |
666 | |
610 | This section lists typical problems that remain. I hope by recognising |
667 | This section lists typical problems that remain. I hope by recognising |
611 | them, most can be avoided. |
668 | them, most can be avoided. |
612 | |
669 | |