Skip to content

Use send()/recv() in IoSocketHandleImpl::writev()/readv() when there is only one IO buffer to work with. - #25695

Merged
yanavlasov merged 31 commits into
envoyproxy:mainfrom
weisisea:iov-opt
Mar 16, 2023
Merged

yanavlasov merged 31 commits into
envoyproxy:mainfrom
weisisea:iov-opt

Conversation

@weisisea

@weisisea weisisea commented Feb 21, 2023 •

Copy link
Copy Markdown
Contributor

Commit Message: Use send()/recv() in IoSocketHandleImpl::writev()/readv() when there is only one IO buffer to work with.
Additional Description: Performance testing on Redis filter shows that this change gives 7% performance improvement.
Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional API Considerations:]

only one IO buffer to work with.

Internal testing on Redis filter shows that this change gives 7%
performance improvement.

Signed-off-by: Wei Si <[email protected]>
@weisisea

Copy link
Copy Markdown
Contributor Author

/assign @jmarantz

@weisisea

Copy link
Copy Markdown
Contributor Author

/assign @yanavlasov

ssize_t rc;
if (num_iov == 1) {
// Avoid paying the VFS overhead when there is only one IO buffer to work with
rc = ::write(fd, iov[0].iov_base, iov[0].iov_len);

@soulxu soulxu Feb 22, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OsSysCallsImpl is just simple wrap of syscall, we shouldn't hack it here. It seems like the only place write one slice with writev is SSL socket transport socket.

auto result = bio_io_handle(b)->writev(&slice, 1);

if this is the right place, we should change the io handle bio to use IoHandle::write.

Maybe we need some benchmark on generic TLS, which probably needs maintainers input.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is the right place, we should change the io handle bio to use IoHandle::write.

And that needs an different read/write interface IoHandle for accept plain buffer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the input!

Actually after talking with my colleague, it looks like that the perf improvement doesn't come from replacing writev() with write() (the scatter-gather doesn't introduce much overhead), but from replacing writev() with send(), which will reduce some kernel VFS handling overhead.

I'm thinking whether to move the logic to:

Api::IoCallUint64Result IoSocketHandleImpl::writev(const Buffer::RawSlice* slices,

@weisisea

weisisea commented Feb 22, 2023 •

Copy link
Copy Markdown
Contributor Author

After talking with my colleague, it looks like that the perf improvement doesn't come from replacing writev()/read() with write()/read() (the scatter-gather doesn't introduce much overhead), but from replacing writev()/readv() with send()/recv(), which will reduce some kernel VFS handling overhead.

Planning to change write()/read() back to send()/recv()

@jmarantz

Copy link
Copy Markdown
Contributor

@weisisea sounds like this is still a WiP?

I'll flip into draft mode until this is sorted out. Thanks.

@jmarantz
jmarantz marked this pull request as draft February 22, 2023 14:09
@weisisea

Copy link
Copy Markdown
Contributor Author

Sounds good, thanks Joshua!

@weisisea weisisea changed the title Use write()/read() in OsSysCallsImpl::writev()/readv() when there is only one IO buffer to work with. Use send()/recv() in OsSysCallsImpl::writev()/readv() when there is only one IO buffer to work with. Feb 22, 2023
@weisisea weisisea changed the title Use send()/recv() in OsSysCallsImpl::writev()/readv() when there is only one IO buffer to work with. Use send()/recv() in IoSocketHandleImpl::writev()/readv() when there is only one IO buffer to work with. Feb 28, 2023
if (num_slices_to_read == 1) {
// Avoid paying the VFS overhead when there is only one IO buffer to work with
return sysCallResultToIoCallResult(
Api::OsSysCallsSingleton::get().recv(fd_, iov[0].iov_base, iov[0].iov_len, 0));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emm...if this give good performance, I'm little to prefer to replace writev/readv with send/recv at here

auto result = bio_io_handle(b)->writev(&slice, 1);

But yea, that means we need to figure out what to do for mac and windows. If we just want to focus on linux platform, then this is probably the right place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that we can change

auto result = bio_io_handle(b)->writev(&slice, 1);
to use send() since it explicitly specify the number of slices to be 1.

I wonder whether it makes sense to generalize the optimization (not only TLS but other filters like Redis) - this PR tries to look at the number of slices in run time and calls send()/recv() if the number of slices is 1.

@weisisea

weisisea commented Mar 2, 2023

Copy link
Copy Markdown
Contributor Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #25695 (comment) was created by @weisisea.

see: more, trace.

ravenblackx
ravenblackx previously approved these changes Mar 6, 2023

@ravenblackx ravenblackx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay to me, one optional cleanup.

Comment thread test/common/buffer/owned_impl_test.cc Outdated
Comment on lines 1353 to 1357
#ifdef WIN32
ASSERT_EQ(os_sys_calls.socketpair(AF_INET, SOCK_STREAM, 0, pipe_fds).return_value_, 0);
ASSERT_EQ(os_sys_calls.socketpair(AF_INET, SOCK_STREAM, 0, fds).return_value_, 0);
#else
ASSERT_EQ(pipe(pipe_fds), 0);
ASSERT_EQ(os_sys_calls.socketpair(AF_UNIX, SOCK_STREAM, 0, fds).return_value_, 0);
#endif

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth having something like, in win32/os_sys_calls_impl.h, #define ENVOY_DEFAULT_PIPE_TYPE AF_INET, and in posix/os_sys_calls_impl.h, #define ENVOY_DEFAULT_PIPE_TYPE AF_UNIX (or with an ifdef/else in the base os_sys_calls.h), then in the various places like this where os_sys_calls.socketpair is called with ifdefs around it, it could be called without the ifdefs, using this shared value instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, will change!

@weisisea

weisisea commented Mar 7, 2023

Copy link
Copy Markdown
Contributor Author

Checking how to fix //test/extensions/filters/listener/http_inspector:http_inspector_test on Windows:

ERROR: C:/source/test/extensions/filters/listener/http_inspector/BUILD:15:24: Testing //test/extensions/filters/listener/http_inspector:http_inspector_test failed: (Exit 34): Remote Execution Failure:
Invalid Argument: stdout limit exceeded
  Unrecognized error detail: type_url: "type.googleapis.com/google.devtools.remotebuildbot.CommandStatus"
value: "\b\001"

https://dev.azure.com/cncf/envoy/_build/results?buildId=129848&view=logs&j=307ebadc-bea7-5cb1-4513-6195e03b9250&t=001b70b1-8584-5b9e-88d7-53fe6d97a0fc

@weisisea
weisisea dismissed stale reviews from ravenblackx and yanavlasov via 70821ad March 9, 2023 07:56
@weisisea
weisisea marked this pull request as draft March 9, 2023 07:57
Signed-off-by: Wei Si <[email protected]>
@weisisea

weisisea commented Mar 9, 2023

Copy link
Copy Markdown
Contributor Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Check envoy-presubmit isn't fully completed, but will still attempt retrying.
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #25695 (comment) was created by @weisisea.

see: more, trace.

ravenblackx
ravenblackx previously approved these changes Mar 9, 2023
@weisisea

Copy link
Copy Markdown
Contributor Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #25695 (comment) was created by @weisisea.

see: more, trace.

ravenblackx
ravenblackx previously approved these changes Mar 13, 2023
@weisisea

Copy link
Copy Markdown
Contributor Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #25695 (comment) was created by @weisisea.

see: more, trace.

@weisisea
weisisea marked this pull request as ready for review March 14, 2023 16:34
@yanavlasov
yanavlasov merged commit 9c1cc0b into envoyproxy:main Mar 16, 2023
@weisisea

Copy link
Copy Markdown
Contributor Author

@yanavlasov Thanks a lot for merging, Yan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants