[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/3] buffer: remove ioctl syscall in common paths
From: |
Eric Wong |
Subject: |
[PATCH 2/3] buffer: remove ioctl syscall in common paths |
Date: |
Sun, 6 Oct 2013 11:34:52 +0000 |
We do not need this for single sink situations (the common case)
at all. We also do not need to check IO#nread for splice, either;
we can just do non-blocking I/O. The only common path where we
might still need it is the non-splice case with multiple sinks.
---
lib/dtas/buffer.rb | 7 ++-----
lib/dtas/buffer/read_write.rb | 13 +++++++++----
lib/dtas/buffer/splice.rb | 21 +++++++++++++--------
test/test_buffer.rb | 20 --------------------
4 files changed, 24 insertions(+), 37 deletions(-)
diff --git a/lib/dtas/buffer.rb b/lib/dtas/buffer.rb
index c9f096c..c0ba6a6 100644
--- a/lib/dtas/buffer.rb
+++ b/lib/dtas/buffer.rb
@@ -47,16 +47,13 @@ class DTAS::Buffer # :nodoc:
# - some type of StandardError
# - nil
def broadcast(targets)
- bytes = inflight
- return :wait_readable if 0 == bytes # spurious wakeup
-
case targets.size
when 0
:ignore # this will pause decoders
when 1
- broadcast_one(targets, bytes)
+ broadcast_one(targets)
else # infinity
- broadcast_inf(targets, bytes)
+ broadcast_inf(targets)
end
end
diff --git a/lib/dtas/buffer/read_write.rb b/lib/dtas/buffer/read_write.rb
index 0044400..11d1a95 100644
--- a/lib/dtas/buffer/read_write.rb
+++ b/lib/dtas/buffer/read_write.rb
@@ -17,25 +17,30 @@ module DTAS::Buffer::ReadWrite # :nodoc:
def discard(bytes)
buf = _rbuf
begin
- @to_io.read(bytes, buf) or break # EOF
+ @to_io.readpartial(bytes, buf)
bytes -= buf.bytesize
+ rescue EOFError
+ return
end until bytes == 0
end
# always block when we have a single target
- def broadcast_one(targets, bytes)
+ def broadcast_one(targets)
buf = _rbuf
- @to_io.read(bytes, buf)
+ @to_io.readpartial(MAX_AT_ONCE, buf)
n = targets[0].write(buf) # IO#write has write-in-full behavior
@bytes_xfer += n
:wait_readable
+ rescue EOFError
+ nil
rescue Errno::EPIPE, IOError => e
__dst_error(targets[0], e)
targets.clear
nil # do not return error here, we already spewed an error message
end
- def broadcast_inf(targets, bytes)
+ def broadcast_inf(targets)
+ bytes = inflight
nr_nb = targets.count { |sink| sink.nonblock? }
if nr_nb == 0 || nr_nb == targets.size
# if all targets are full, don't start until they're all writable
diff --git a/lib/dtas/buffer/splice.rb b/lib/dtas/buffer/splice.rb
index 18dfd82..90c2e47 100644
--- a/lib/dtas/buffer/splice.rb
+++ b/lib/dtas/buffer/splice.rb
@@ -8,6 +8,7 @@ require_relative '../pipe'
module DTAS::Buffer::Splice # :nodoc:
MAX_AT_ONCE = 4096 # page size in Linux
+ MAX_AT_ONCE_1 = 65536
MAX_SIZE = File.read("/proc/sys/fs/pipe-max-size").to_i
DEVNULL = File.open("/dev/null", "r+")
F_MOVE = IO::Splice::F_MOVE
@@ -28,9 +29,9 @@ module DTAS::Buffer::Splice # :nodoc:
IO.splice(@to_io, nil, DEVNULL, nil, bytes)
end
- def broadcast_one(targets, bytes)
+ def broadcast_one(targets)
# single output is always non-blocking
- s = IO.trysplice(@to_io, nil, targets[0], nil, bytes, F_MOVE)
+ s = IO.trysplice(@to_io, nil, targets[0], nil, MAX_AT_ONCE_1, F_MOVE)
if Symbol === s
targets # our one and only target blocked on write
else
@@ -48,11 +49,14 @@ module DTAS::Buffer::Splice # :nodoc:
most_teed = 0
targets.delete_if do |dst|
begin
- t = dst.nonblock? ?
+ t = (dst.nonblock? || most_teed == 0) ?
IO.trytee(@to_io, dst, chunk_size) :
IO.tee(@to_io, dst, chunk_size, WAITALL)
if Integer === t
- most_teed = t if t > most_teed
+ if t > most_teed
+ chunk_size = t if most_teed == 0
+ most_teed = t
+ end
else
blocked << dst
end
@@ -65,7 +69,7 @@ module DTAS::Buffer::Splice # :nodoc:
most_teed
end
- def broadcast_inf(targets, bytes)
+ def broadcast_inf(targets)
if targets.none? { |sink| sink.nonblock? }
# if all targets are blocking, don't start until they're all writable
r = IO.select(nil, targets, nil, 0) or return targets
@@ -80,9 +84,10 @@ module DTAS::Buffer::Splice # :nodoc:
end
# don't pin too much on one target
- bytes = bytes > MAX_AT_ONCE ? MAX_AT_ONCE : bytes
-
+ bytes = MAX_AT_ONCE
last = targets.pop # we splice to the last one, tee to the rest
+
+ # this may return zero if all targets were non-blocking
most_teed = __broadcast_tee(blocked, targets, bytes)
# don't splice more than the largest amount we successfully teed
@@ -90,7 +95,7 @@ module DTAS::Buffer::Splice # :nodoc:
begin
targets << last
- if last.nonblock?
+ if last.nonblock? || most_teed == 0
s = IO.trysplice(@to_io, nil, last, nil, bytes, F_MOVE)
if Symbol === s
blocked << last
diff --git a/test/test_buffer.rb b/test/test_buffer.rb
index d74cdce..13f4352 100644
--- a/test/test_buffer.rb
+++ b/test/test_buffer.rb
@@ -51,13 +51,9 @@ class TestBuffer < Testcase
def test_broadcast_1
buf = new_buffer
r, w = IO.pipe
- assert_equal :wait_readable, buf.broadcast([w])
- assert_equal 0, buf.bytes_xfer
buf.wr.write "HIHI"
assert_equal :wait_readable, buf.broadcast([w])
assert_equal 4, buf.bytes_xfer
- assert_equal :wait_readable, buf.broadcast([w])
- assert_equal 4, buf.bytes_xfer
tmp = [w]
r.close
buf.wr.write "HIHI"
@@ -90,20 +86,6 @@ class TestBuffer < Testcase
a[1].nonblock = false
b[0].read(b[0].nread)
b[1].write(max)
- t = Thread.new do
- sleep 0.005
- [ a[0].read(max.size).size, b[0].read(max.size).size ]
- end
- assert_equal 5, buf.__broadcast_tee(blocked, [a[1], b[1]], 5)
- assert_equal [a[1]], blocked
- assert_equal [ max.size, max.size ], t.value
- b[0].close
- tmp = [a[1], b[1]]
-
- newerr = tmperr { assert_equal 5, buf.__broadcast_tee(blocked, tmp, 5) }
- assert_equal [a[1]], blocked
- assert_match(%r{dropping}, newerr.string)
- assert_equal [a[1]], tmp
end
def test_broadcast
@@ -115,8 +97,6 @@ class TestBuffer < Testcase
assert_equal 5, buf.bytes_xfer
assert_equal "HELLO", a[0].read(5)
assert_equal "HELLO", b[0].read(5)
- assert_equal :wait_readable, buf.broadcast([a[1], b[1]])
- assert_equal 5, buf.bytes_xfer
return unless b[1].respond_to?(:pipe_size)
--
1.8.4