uringmachine 0.3 → 0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +2 -1
  3. data/CHANGELOG.md +23 -0
  4. data/README.md +128 -0
  5. data/TODO.md +14 -0
  6. data/examples/bm_snooze.rb +89 -0
  7. data/examples/bm_write.rb +56 -0
  8. data/examples/dns_client.rb +12 -0
  9. data/examples/echo_server.rb +18 -40
  10. data/examples/http_server.rb +42 -43
  11. data/examples/inout.rb +19 -0
  12. data/examples/nc.rb +36 -0
  13. data/examples/server_client.rb +64 -0
  14. data/examples/snooze.rb +44 -0
  15. data/examples/write_dev_null.rb +16 -0
  16. data/ext/um/extconf.rb +24 -23
  17. data/ext/um/um.c +524 -278
  18. data/ext/um/um.h +146 -44
  19. data/ext/um/um_buffer.c +49 -0
  20. data/ext/um/um_class.c +217 -106
  21. data/ext/um/um_const.c +213 -0
  22. data/ext/um/um_ext.c +4 -0
  23. data/ext/um/um_mutex_class.c +47 -0
  24. data/ext/um/um_op.c +86 -114
  25. data/ext/um/um_queue_class.c +58 -0
  26. data/ext/um/um_sync.c +273 -0
  27. data/ext/um/um_utils.c +49 -4
  28. data/lib/uringmachine/dns_resolver.rb +84 -0
  29. data/lib/uringmachine/version.rb +1 -1
  30. data/lib/uringmachine.rb +28 -0
  31. data/supressions/ruby.supp +71 -0
  32. data/test/helper.rb +8 -0
  33. data/test/test_um.rb +685 -46
  34. data/vendor/liburing/.github/workflows/build.yml +29 -1
  35. data/vendor/liburing/.gitignore +6 -0
  36. data/vendor/liburing/CHANGELOG +16 -0
  37. data/vendor/liburing/CONTRIBUTING.md +165 -0
  38. data/vendor/liburing/configure +64 -0
  39. data/vendor/liburing/examples/Makefile +9 -1
  40. data/vendor/liburing/examples/kdigest.c +405 -0
  41. data/vendor/liburing/examples/proxy.c +75 -8
  42. data/vendor/liburing/examples/reg-wait.c +159 -0
  43. data/vendor/liburing/liburing.pc.in +1 -1
  44. data/vendor/liburing/liburing.spec +1 -1
  45. data/vendor/liburing/src/Makefile +16 -2
  46. data/vendor/liburing/src/include/liburing/io_uring.h +77 -0
  47. data/vendor/liburing/src/include/liburing/sanitize.h +39 -0
  48. data/vendor/liburing/src/include/liburing.h +59 -6
  49. data/vendor/liburing/src/int_flags.h +10 -3
  50. data/vendor/liburing/src/liburing-ffi.map +16 -0
  51. data/vendor/liburing/src/liburing.map +10 -0
  52. data/vendor/liburing/src/queue.c +28 -16
  53. data/vendor/liburing/src/register.c +106 -1
  54. data/vendor/liburing/src/sanitize.c +176 -0
  55. data/vendor/liburing/src/setup.c +47 -19
  56. data/vendor/liburing/src/setup.h +6 -0
  57. data/vendor/liburing/test/35fa71a030ca.c +7 -0
  58. data/vendor/liburing/test/500f9fbadef8.c +2 -0
  59. data/vendor/liburing/test/7ad0e4b2f83c.c +0 -25
  60. data/vendor/liburing/test/917257daa0fe.c +7 -0
  61. data/vendor/liburing/test/Makefile +38 -4
  62. data/vendor/liburing/test/a0908ae19763.c +7 -0
  63. data/vendor/liburing/test/a4c0b3decb33.c +7 -0
  64. data/vendor/liburing/test/accept.c +14 -4
  65. data/vendor/liburing/test/b19062a56726.c +7 -0
  66. data/vendor/liburing/test/bind-listen.c +2 -2
  67. data/vendor/liburing/test/buf-ring-nommap.c +10 -3
  68. data/vendor/liburing/test/buf-ring.c +2 -0
  69. data/vendor/liburing/test/cmd-discard.c +427 -0
  70. data/vendor/liburing/test/coredump.c +7 -0
  71. data/vendor/liburing/test/cq-overflow.c +13 -1
  72. data/vendor/liburing/test/d4ae271dfaae.c +11 -3
  73. data/vendor/liburing/test/defer-taskrun.c +2 -2
  74. data/vendor/liburing/test/defer-tw-timeout.c +4 -1
  75. data/vendor/liburing/test/defer.c +2 -2
  76. data/vendor/liburing/test/double-poll-crash.c +1 -1
  77. data/vendor/liburing/test/eeed8b54e0df.c +2 -0
  78. data/vendor/liburing/test/eventfd.c +0 -1
  79. data/vendor/liburing/test/exit-no-cleanup.c +11 -0
  80. data/vendor/liburing/test/fadvise.c +9 -26
  81. data/vendor/liburing/test/fdinfo.c +9 -1
  82. data/vendor/liburing/test/fifo-nonblock-read.c +69 -0
  83. data/vendor/liburing/test/file-exit-unreg.c +48 -0
  84. data/vendor/liburing/test/file-register.c +14 -2
  85. data/vendor/liburing/test/file-update.c +1 -1
  86. data/vendor/liburing/test/file-verify.c +27 -16
  87. data/vendor/liburing/test/files-exit-hang-timeout.c +1 -2
  88. data/vendor/liburing/test/fixed-buf-iter.c +3 -1
  89. data/vendor/liburing/test/fixed-hugepage.c +12 -1
  90. data/vendor/liburing/test/fsnotify.c +1 -0
  91. data/vendor/liburing/test/futex.c +16 -4
  92. data/vendor/liburing/test/helpers.c +47 -0
  93. data/vendor/liburing/test/helpers.h +6 -0
  94. data/vendor/liburing/test/init-mem.c +5 -3
  95. data/vendor/liburing/test/io-cancel.c +0 -24
  96. data/vendor/liburing/test/io_uring_passthrough.c +4 -0
  97. data/vendor/liburing/test/io_uring_register.c +38 -8
  98. data/vendor/liburing/test/iopoll-leak.c +4 -0
  99. data/vendor/liburing/test/iopoll-overflow.c +1 -1
  100. data/vendor/liburing/test/iopoll.c +3 -3
  101. data/vendor/liburing/test/kallsyms.c +203 -0
  102. data/vendor/liburing/test/link-timeout.c +159 -0
  103. data/vendor/liburing/test/linked-defer-close.c +224 -0
  104. data/vendor/liburing/test/madvise.c +12 -25
  105. data/vendor/liburing/test/min-timeout-wait.c +0 -25
  106. data/vendor/liburing/test/min-timeout.c +0 -25
  107. data/vendor/liburing/test/mkdir.c +6 -0
  108. data/vendor/liburing/test/msg-ring.c +8 -2
  109. data/vendor/liburing/test/napi-test.c +16 -3
  110. data/vendor/liburing/test/no-mmap-inval.c +3 -1
  111. data/vendor/liburing/test/nop.c +44 -0
  112. data/vendor/liburing/test/ooo-file-unreg.c +1 -1
  113. data/vendor/liburing/test/open-close.c +40 -0
  114. data/vendor/liburing/test/openat2.c +37 -14
  115. data/vendor/liburing/test/poll-many.c +13 -7
  116. data/vendor/liburing/test/poll-mshot-update.c +17 -10
  117. data/vendor/liburing/test/poll-v-poll.c +6 -3
  118. data/vendor/liburing/test/pollfree.c +148 -0
  119. data/vendor/liburing/test/read-mshot-empty.c +158 -153
  120. data/vendor/liburing/test/read-mshot-stdin.c +121 -0
  121. data/vendor/liburing/test/read-mshot.c +282 -27
  122. data/vendor/liburing/test/read-write.c +78 -13
  123. data/vendor/liburing/test/recv-msgall-stream.c +3 -0
  124. data/vendor/liburing/test/recv-msgall.c +5 -0
  125. data/vendor/liburing/test/recvsend_bundle-inc.c +680 -0
  126. data/vendor/liburing/test/recvsend_bundle.c +94 -31
  127. data/vendor/liburing/test/reg-fd-only.c +15 -5
  128. data/vendor/liburing/test/reg-wait.c +251 -0
  129. data/vendor/liburing/test/regbuf-clone.c +645 -0
  130. data/vendor/liburing/test/regbuf-merge.c +7 -0
  131. data/vendor/liburing/test/register-restrictions.c +86 -85
  132. data/vendor/liburing/test/rename.c +59 -1
  133. data/vendor/liburing/test/resize-rings.c +643 -0
  134. data/vendor/liburing/test/ringbuf-read.c +5 -0
  135. data/vendor/liburing/test/ringbuf-status.c +5 -1
  136. data/vendor/liburing/test/rsrc_tags.c +1 -1
  137. data/vendor/liburing/test/runtests.sh +16 -1
  138. data/vendor/liburing/test/send-zerocopy.c +59 -0
  139. data/vendor/liburing/test/short-read.c +1 -0
  140. data/vendor/liburing/test/socket.c +43 -0
  141. data/vendor/liburing/test/splice.c +3 -1
  142. data/vendor/liburing/test/sq-poll-dup.c +1 -1
  143. data/vendor/liburing/test/sq-poll-share.c +2 -0
  144. data/vendor/liburing/test/sqpoll-disable-exit.c +8 -0
  145. data/vendor/liburing/test/sqpoll-exit-hang.c +1 -25
  146. data/vendor/liburing/test/sqpoll-sleep.c +40 -33
  147. data/vendor/liburing/test/sqwait.c +136 -0
  148. data/vendor/liburing/test/statx.c +89 -0
  149. data/vendor/liburing/test/stdout.c +2 -0
  150. data/vendor/liburing/test/submit-and-wait.c +1 -25
  151. data/vendor/liburing/test/submit-reuse.c +4 -26
  152. data/vendor/liburing/test/symlink.c +12 -1
  153. data/vendor/liburing/test/sync-cancel.c +56 -22
  154. data/vendor/liburing/test/thread-exit.c +5 -0
  155. data/vendor/liburing/test/timeout-new.c +1 -26
  156. data/vendor/liburing/test/timeout.c +25 -34
  157. data/vendor/liburing/test/unlink.c +94 -1
  158. data/vendor/liburing/test/uring_cmd_ublk.c +1252 -0
  159. data/vendor/liburing/test/waitid.c +62 -8
  160. data/vendor/liburing/test/wq-aff.c +35 -0
  161. data/vendor/liburing/test/xfail_prep_link_timeout_out_of_scope.c +46 -0
  162. data/vendor/liburing/test/xfail_register_buffers_out_of_scope.c +51 -0
  163. metadata +37 -6
  164. data/examples/event_loop.rb +0 -69
  165. data/examples/fibers.rb +0 -105
  166. data/examples/http_server_multishot.rb +0 -57
  167. data/examples/http_server_simpler.rb +0 -34
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'resolv'
4
+
5
+ class UringMachine
6
+ class DNSResolver
7
+ def initialize(machine)
8
+ @machine = machine
9
+ @requests = UM::Queue.new
10
+ @nameservers = get_nameservers
11
+ @fiber = @machine.spin { handle_requests_loop }
12
+ @last_id = 0
13
+ @cache = {}
14
+ end
15
+
16
+ def resolve(hostname, type)
17
+ @machine.push(@requests, [hostname, type, Fiber.current])
18
+ @machine.yield
19
+ end
20
+
21
+ def handle_requests_loop
22
+ while true
23
+ hostname, type, fiber = @machine.shift(@requests)
24
+ res = do_resolve(hostname, type)
25
+ @machine.schedule(fiber, res)
26
+ end
27
+ end
28
+
29
+ def get_nameservers
30
+ nameservers = []
31
+ IO.readlines('/etc/resolv.conf').each do |line|
32
+ if line =~ /^nameserver (.+)$/
33
+ nameservers << $1.split(/\s+/).first
34
+ end
35
+ end
36
+ nameservers
37
+ end
38
+
39
+ def socket_fd
40
+ @socket_fd ||= prepare_socket
41
+ end
42
+
43
+ def prepare_socket
44
+ fd = @machine.socket(UM::AF_INET, UM::SOCK_DGRAM, 0, 0)
45
+ @machine.bind(fd, '0.0.0.0', 0)
46
+ @machine.connect(fd, @nameservers.sample, 53)
47
+ fd
48
+ end
49
+
50
+ def do_resolve(hostname, type, try_count = 0)
51
+ fd = socket_fd
52
+ req = prepare_request_packet(hostname, type)
53
+ msg = req.encode
54
+ @machine.send(fd, msg, msg.bytesize, 0)
55
+
56
+ buf = +''
57
+ @machine.recv(fd, buf, 16384, 0)
58
+
59
+ msg = Resolv::DNS::Message.decode buf
60
+ addrs = []
61
+ msg.each_answer do |name, ttl, data|
62
+ p [name, ttl, data]
63
+ if data.kind_of?(Resolv::DNS::Resource::IN::A) ||
64
+ data.kind_of?(Resolv::DNS::Resource::IN::AAAA)
65
+ addrs << data.address.to_s
66
+ end
67
+ end
68
+ addrs
69
+ end
70
+
71
+ def prepare_request_packet(hostname, type)
72
+ msg = Resolv::DNS::Message.new
73
+ msg.id = (@last_id += 1)
74
+ msg.rd = 1
75
+ msg.add_question hostname, msg_type(type)
76
+ msg
77
+ end
78
+
79
+ def msg_type(type)
80
+ # TODO: add support for other types
81
+ Resolv::DNS::Resource::IN::A
82
+ end
83
+ end
84
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class UringMachine
4
- VERSION = '0.3'
4
+ VERSION = '0.5'
5
5
  end
data/lib/uringmachine.rb CHANGED
@@ -1,5 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative './um_ext'
4
+ require_relative 'uringmachine/dns_resolver'
4
5
 
5
6
  UM = UringMachine
7
+
8
+ class UringMachine
9
+ @@fiber_map = {}
10
+
11
+ def spin(value = nil, &block)
12
+ f = Fiber.new do |resume_value|
13
+ block.(resume_value)
14
+ rescue Exception => e
15
+ STDERR.puts "Unhandled fiber exception: #{e.inspect}"
16
+ STDERR.puts e.backtrace.join("\n")
17
+ exit
18
+ ensure
19
+ @@fiber_map.delete(f)
20
+ # yield control
21
+ self.yield
22
+ p :bad_bad_bad
23
+ end
24
+ schedule(f, value)
25
+ @@fiber_map[f] = true
26
+ f
27
+ end
28
+
29
+ def resolve(hostname, type = :A)
30
+ @resolver ||= DNSResolver.new(self)
31
+ @resolver.resolve(hostname, type)
32
+ end
33
+ end
@@ -0,0 +1,71 @@
1
+ {
2
+ On platforms where memcpy is safe for overlapped memory, the compiler will sometimes replace memmove with memcpy. Valgrind may report a false positive.
3
+ Memcheck:Overlap
4
+ fun:__memcpy_chk
5
+ fun:memmove
6
+ ...
7
+ }
8
+ {
9
+ Requiring a file will add it to the loaded features, which may be reported as a leak.
10
+ Memcheck:Leak
11
+ ...
12
+ fun:require_internal
13
+ ...
14
+ }
15
+ {
16
+ recursive_list_access creates a hash called `list` that is stored on the threadptr_recursive_hash. This is reported as a memory leak.
17
+ Memcheck:Leak
18
+ ...
19
+ fun:rb_ident_hash_new
20
+ fun:recursive_list_access
21
+ fun:exec_recursive
22
+ ...
23
+ }
24
+ {
25
+ "Invalid read of size 8" when marking the stack of fibers
26
+ Memcheck:Addr8
27
+ fun:each_location*
28
+ ...
29
+ }
30
+ {
31
+ Rust probes for statx(buf), will be fixed in Valgrind >= 3.1.6.0
32
+ Memcheck:Param
33
+ statx(buf)
34
+ ...
35
+ fun:*try_statx*
36
+ ...
37
+ }
38
+ {
39
+ Rust probes for statx(file_name), will be fixed in Valgrind >= 3.1.6.0
40
+ Memcheck:Param
41
+ statx(file_name)
42
+ ...
43
+ fun:*try_statx*
44
+ ...
45
+ }
46
+ {
47
+ strscan_do_scan in strscan.c will sometimes replace the ptr of the regex, which can be reported as a memory leak if the regex is stored in an iseq. https://github.com/ruby/ruby/pull/8136
48
+ Memcheck:Leak
49
+ ...
50
+ fun:rb_reg_prepare_re
51
+ fun:strscan_do_scan
52
+ ...
53
+ }
54
+ {
55
+ The callcache table (RCLASS_CC_TBL) is lazily created, so it is allocated when the first method that gets cached. If this happens in a native extension, it may be reported as a memory leak.
56
+ Memcheck:Leak
57
+ ...
58
+ fun:rb_id_table_create
59
+ ...
60
+ fun:rb_callable_method_entry
61
+ ...
62
+ }
63
+ {
64
+ The date library lazily initializes Regexps using static local variables through the function `regcomp`. The Regexp will end up being reported as a memory leak.
65
+ Memcheck:Leak
66
+ ...
67
+ fun:rb_enc_reg_new
68
+ ...
69
+ fun:date__parse
70
+ ...
71
+ }
data/test/helper.rb CHANGED
@@ -60,4 +60,12 @@ class UMBaseTest < Minitest::Test
60
60
  def teardown
61
61
  # @machine&.cleanup
62
62
  end
63
+
64
+ def assign_port
65
+ @@port_assign_mutex ||= Mutex.new
66
+ @@port_assign_mutex.synchronize do
67
+ @@port ||= 1024 + rand(60000)
68
+ @@port += 1
69
+ end
70
+ end
63
71
  end