You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Yann Ylavic <yl...@gmail.com> on 2017/03/03 17:41:43 UTC

Read scattered to gather send (was [too long]: httpd 2.4.25, mpm_event, ssl: segfaults)

Hi,

few free time lately, yet I tried to implement this and it seems to
work pretty well.

Details below (and 2.4.x patches attached)...

On Thu, Feb 23, 2017 at 4:38 PM, Yann Ylavic <yl...@gmail.com> wrote:
>
> So I'm thinking of another way to achieve the same with the current
> APR_BUCKET_BUFF_SIZE (2 pages) per alloc.
>
> The idea is to have a new apr_allocator_allocv() function which would
> fill an iovec with what's available in the allocator's freelist (i.e.
> spare apr_memnodes) of at least the given min_size bytes (possibly a
> max too but I don't see the need for now) and up to the size of the
> given iovec.
>
> This function could be the base of a new apr_bucket_allocv() (and
> possibly apr_p[c]allocv(), though out of scope here) which in turn
> could be used by file_bucket_read() to get an iovec of available
> buffers.
> This iovec could then be passed to (new still) apr_file_readv() based
> on the readv() syscall, which would allow to read much more data in
> one go.
>
> With this the scheme we'd have iovec from end to end, well, sort of
> since mod_ssl would be break the chain but still produce transient
> buckets on output which anyway will end up in the core_output_filter's
> brigade of aside heap buckets, for apr_socket_sendv() to finally
> writev() them.
>
> We'd also have more recycled heap buckets (hence memnodes in the
> allocator) as the core_output_filter retains buckets, all with
> APR_BUCKET_BUFF_SIZE, up to THRESHOLD_MAX_BUFFER which, if
> configurable and along with MaxMemFree, would be the real limiter of
> recycling.

So here is how I finally got this, the principles remain.
Changes are (obviously) on both httpd and (mainly) APR sides (didn't
propose to APR team yet, wanted some feedbacks here first ;)

On the httpd side, it's mainly the parametrization of some hardcoded
values in the core output filter, namely:
+AP_INIT_TAKE1("OutputFlushThreshold", set_output_flush_threshold,
NULL, RSRC_CONF,
+  "Size (in bytes) above which pending data are flushed to the network"),
+AP_INIT_TAKE1("OutputFlushMemThreshold",
set_output_flush_mem_threshold, NULL, RSRC_CONF,
+  "Size (in bytes) above which pending memory data are flushed to the
network"),
+AP_INIT_TAKE1("OutputFlushMaxPipelined",
set_output_flush_max_pipelined, NULL, RSRC_CONF,
+  "Number of pipelined/pending responses above which they are flushed
to the network"),

These were respectively THRESHOLD_MIN_WRITE, THRESHOLD_MAX_BUFFER and
MAX_REQUESTS_IN_PIPELINE.
The iovec used previously (on stack) is removed, in favor of a
per-connection (ctx) dynamic/growable one which allows to get rid of
an artificial/harcoded flush limit (MAX_IOVEC_TO_WRITE).

Hence the flush is now solely governed by either the overall pending
bytes (OutputFlushThreshold), the in-memory pending bytes
(OutputFlushMemThreshold, which catches the MAX_IOVEC_TO_WRITE case),
or the number of pipelined requests (OutputFlushMaxPipelined).

Also, to reach these thresholds, there is a way to enable scattered
reads (i.e. readv) on files (à la EnableMMAP/EnableSendfile), with a
tunable buffer size:
+AP_INIT_TAKE1("EnableReadfileScatter", set_enable_readfile_scatter,
NULL, OR_FILEINFO,
+  "Controls whether buffer scattering may be used to read files
('off', 'on', 'fixed')"),
+AP_INIT_TAKE1("ReadfileBufferSize", set_readfile_buf_size, NULL, OR_FILEINFO,
+  "Size (in bytes) of the memory buffers used to read files"),

These are set in the core_handler only (for now), and it's where APR
comes into play :)

Currently I have good results (with gdb/LOG_TRACE, no stress test yet ;)

For "http:" (main server) with:

    EnableMMAP off
    EnableSendfile off

    EnableScatterReadfile on
    #FileReadBufferSize 8192 <= default

    FlushThreshold    1048576
    FlushMemThreshold 65536
    FlushMaxPipelined 5

And for "https:" (vhost) with overridden:

    EnableScatterReadfile fixed

    FileReadBufferSize 16384

Not heavily tested though, but the traces look good :p


So on the APR side, apr_file_readv() obviously (implemented only with
readv() for now, i.e. APR_ENOTIMPL on Windows and OS/2 which seem to
be the ones that don't share the unix fileio code), is used by
apr_bucket_read()::file_bucket_read() to fill in its buffers and
morph/chain heap buckets.

The buffers come from an apr_bucket_alloc_t, thus the new
apr_bucket_bulk_alloc(), apr_bucket_bulk_free() and
apr_allocator_bulk_alloc() functions where finally all the "automagic"
is.

With apr_allocator_bulk_alloc(), one can request several apr_memnode_t
of a fixed (optional) or minimal given size, and in the worst case get
a single one (allocaœted), or in the best case as much free ones as
available (within a maximal size, also given).
Since this is old and sensible code, I tried to be the least invasive
possible, though efficient too :)

Regarding apr_bucket_bulk_alloc() (and its apr_bucket_bulk_free()
counterpart), it's mainly wrappers around apr_allocator_bulk_alloc()
to produce an iovec of memory blocks from a list of apr_memnode_t
(resp. free them in a one go).


That is (sorry if it was too long or too short), some comments in the
code, and comments / questions / tests / bug{reports,fixes} welcome ;)

Regards,
Yann.

Re: Read scattered to gather send (was [too long]: httpd 2.4.25, mpm_event, ssl: segfaults)

Posted by Jim Jagielski <ji...@jaguNET.com>.
This really belongs in dev@apr... Adding it now.

If we are *really* drastically changing this core aspect of
pool allocation, we should reconsider my thoughts related to
more granular mutex related to anytime we alloc from a
pool to allow for, if required/desired, full thread safety
not just when creating a subpool or destroying.

> On Mar 3, 2017, at 2:38 PM, Yann Ylavic <yl...@gmail.com> wrote:
> 
> First fix :)
> 
> On Fri, Mar 3, 2017 at 6:41 PM, Yann Ylavic <yl...@gmail.com> wrote:
>> 
>> With apr_allocator_bulk_alloc(), one can request several apr_memnode_t
>> of a fixed (optional) or minimal given size, and in the worst case get
>> a single one (allocaœted), or in the best case as much free ones as
>> available (within a maximal size, also given).
> 
> The non-fixed version was buggy (couldn't reuse lower indexes), so
> "apr_allocators-bulk-v2.patch" attached.
> Will post some numbers (w.r.t. buffers' resuse and writev sizes) soon.
> <apr_allocators-bulk-v2.patch>


Re: Read scattered to gather send (was [too long]: httpd 2.4.25, mpm_event, ssl: segfaults)

Posted by Jim Jagielski <ji...@jaguNET.com>.
This really belongs in dev@apr... Adding it now.

If we are *really* drastically changing this core aspect of
pool allocation, we should reconsider my thoughts related to
more granular mutex related to anytime we alloc from a
pool to allow for, if required/desired, full thread safety
not just when creating a subpool or destroying.

> On Mar 3, 2017, at 2:38 PM, Yann Ylavic <yl...@gmail.com> wrote:
> 
> First fix :)
> 
> On Fri, Mar 3, 2017 at 6:41 PM, Yann Ylavic <yl...@gmail.com> wrote:
>> 
>> With apr_allocator_bulk_alloc(), one can request several apr_memnode_t
>> of a fixed (optional) or minimal given size, and in the worst case get
>> a single one (allocaœted), or in the best case as much free ones as
>> available (within a maximal size, also given).
> 
> The non-fixed version was buggy (couldn't reuse lower indexes), so
> "apr_allocators-bulk-v2.patch" attached.
> Will post some numbers (w.r.t. buffers' resuse and writev sizes) soon.
> <apr_allocators-bulk-v2.patch>


Re: Read scattered to gather send

Posted by Jacob Champion <ch...@gmail.com>.
On 03/03/2017 11:38 AM, Yann Ylavic wrote:
> First fix :)
>
> On Fri, Mar 3, 2017 at 6:41 PM, Yann Ylavic <yl...@gmail.com> wrote:
>>
>> With apr_allocator_bulk_alloc(), one can request several apr_memnode_t
>> of a fixed (optional) or minimal given size, and in the worst case get
>> a single one (allocated), or in the best case as much free ones as
>> available (within a maximal size, also given).
>
> The non-fixed version was buggy (couldn't reuse lower indexes), so
> "apr_allocators-bulk-v2.patch" attached.
> Will post some numbers (w.r.t. buffers' resuse and writev sizes) soon.

Neat! I will try to give this a shot some time next week (and I still 
haven't followed up on your mod_bucketeer fix in another thread, sorry...).

--Jacob

Re: Read scattered to gather send (was [too long]: httpd 2.4.25, mpm_event, ssl: segfaults)

Posted by Mike Rumph <mi...@oracle.com>.
Hello Yann,

Just one question at this time:
It appears that your changes to httpd will depend on new functions in 
APR-util which would require a new release of APR-util (and APR as well?).
What is your plan for handling this dependency?

Thanks,

Mike Rumph

On 3/3/2017 11:38 AM, Yann Ylavic wrote:
> First fix :)
>
> On Fri, Mar 3, 2017 at 6:41 PM, Yann Ylavic <yl...@gmail.com> wrote:
>> With apr_allocator_bulk_alloc(), one can request several apr_memnode_t
>> of a fixed (optional) or minimal given size, and in the worst case get
>> a single one (allocated), or in the best case as much free ones as
>> available (within a maximal size, also given).
> The non-fixed version was buggy (couldn't reuse lower indexes), so
> "apr_allocators-bulk-v2.patch" attached.
> Will post some numbers (w.r.t. buffers' resuse and writev sizes) soon.


Re: Read scattered to gather send (was [too long]: httpd 2.4.25, mpm_event, ssl: segfaults)

Posted by Yann Ylavic <yl...@gmail.com>.
First fix :)

On Fri, Mar 3, 2017 at 6:41 PM, Yann Ylavic <yl...@gmail.com> wrote:
>
> With apr_allocator_bulk_alloc(), one can request several apr_memnode_t
> of a fixed (optional) or minimal given size, and in the worst case get
> a single one (allocaœted), or in the best case as much free ones as
> available (within a maximal size, also given).

The non-fixed version was buggy (couldn't reuse lower indexes), so
"apr_allocators-bulk-v2.patch" attached.
Will post some numbers (w.r.t. buffers' resuse and writev sizes) soon.

Re: Read scattered to gather send (was [too long]: httpd 2.4.25, mpm_event, ssl: segfaults)

Posted by Yann Ylavic <yl...@gmail.com>.
On Fri, Mar 3, 2017 at 6:41 PM, Yann Ylavic <yl...@gmail.com> wrote:
>
> Currently I have good results (with gdb/LOG_TRACE, no stress test yet ;)
>
> For "http:" (main server) with:
>
>     EnableMMAP off
>     EnableSendfile off
>
>     EnableScatterReadfile on
>     #FileReadBufferSize 8192 <= default
>
>     FlushThreshold    1048576

Sorry, wrong copy/paste value, I tested that but 1MB looks not really
reasonable for a common case.
So:

     FlushThreshold    131072

should be enough for a (already powerful) test ;)

>     FlushMemThreshold 65536
>     FlushMaxPipelined 5

>
> And for "https:" (vhost) with overridden:
>
>     EnableScatterReadfile fixed
>
>     FileReadBufferSize 16384

The other values are fine I think.