Repository navigation
net: the connection engine in pycvc and arisrvx (ARIADNE_NET_ENGINE N2) - #1434
Merged
Merged
Conversation
pycvc wraps inc/cvc/net/net_engine.h with SWIG under its C++ names (net_engine, engine_options, listener_options, framing, inbound_limits, conn_info, net_event, net_batch, net_counters, net_stats), so Python has the engine's whole API: listen, port, stop_listening, send, send_many, broadcast, close, join, leave, set_framing, resume, want_writable, wait, poll, set_notify, stats and stop. pycvc.ariadne.net is the same names. Additions only where Python needs them: bytes-like (or str) arguments, framing.delimited(b"|"), milliseconds as ints, keyword constructors for the option structs, OSError from listen, net_counters.closed as a dict, and wait/poll/stop/destruction without the GIL. Class-typed members read as copies and vectors as lists: SWIG's getters point into the parent and dangled once a temporary went (stats().threads read freed memory). Frames: net_batch.events gives each event's frames as memoryviews into the batch's buffers. Each memoryview holds those buffers, so a kept frame stays valid instead of dangling. The exporter type is made at module init and kept in the module, so the writable-data scan stays at 0. The asyncio side: listen() returns a net_listener (the id, an int) with on_accept (which may refuse), on_frames (one call a batch a listener, [(conn, [memoryview, ...]), ...]), on_close, on_writable and on_error. A waker thread waits in wait() without the GIL and hands each batch to the loop, then waits for the loop to take it, so input that arrives while the loop is busy comes as one larger batch. arisrvx: host.net is the relay's engine, made on the program's first use (_arihost.net(), a "cvc.net.net_engine" capsule) with --net-threads I/O threads (2 by default); it outlives the program. Under plain python, Host(net_threads=2) starts an engine of its own and stops it at shutdown, after its closes have reached on_close. Tests: test_pycvc_net.py (pycvc_net), arisrvx_net. bench_pycvc_net.py is the Python fan-out check against N1's numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part N2 of the connection engine (docs/roadmap/ARIADNE_NET_ENGINE.md §2.7, §3, §4): N1's
cvc::net::net_engine(#1392) in pycvc, andhost.netin arisrvx.What Python gets
Joe asked for "the full C++ api in python", not a wrapper over it. So
bindings/pycvc/pycvc_net.iSWIG-wrapsinc/cvc/net/net_engine.hitself, under the C++ names:net_engine,engine_options,listener_options,framing,inbound_limits,conn_info,net_event,net_batch,net_counters,net_stats(listener_stats,thread_stats), and the enums as SWIG spells them (close_reason_slow_reader,net_event.type_frames,framing.mode_delimiter);listen,port,stop_listening,send,send_many,broadcast,close,join,leave,set_framing,resume,want_writable,wait,poll,set_notify,statsandstop.pycvc.ariadne.netholds the same objects, andhost.netis anet_engine.Python-side additions, only where the language needs them:
framing.delimited(b"|")works as well as an int;listen()raises OSError with the engine's reason instead of returning 0;net_counters.closedis a dict keyed by reason name;wait,poll,stopand destruction release the GIL;e.stats().threadsread freed memory. There is a regression test for this.Frames and the loop
net_batch.eventsgives each event's frames as memoryviews into the batch's buffers, with no copy. Each memoryview holds a reference to those buffers, so a frame kept after the callback stays valid instead of dangling. The exporter type is created once, at module init, and kept in the module dict.listen()returns anet_listener: the listener id, an int, with decorator methods:on_accept(conn, info), which may return a reason to refuse the connection (sync or async);on_frames(batch), with batch as[(conn, [memoryview, ...]), ...]: one call per batch per listener, each connection once;on_close(conn, reason[, message]),on_writableandon_error.wait()without the GIL and hands each batch to the loop withcall_soon_threadsafe. It takes the next batch only after the loop has taken this one, so input that arrives while the loop is busy comes as one larger batch. A batch is delivered as accepts, then frames, then writables, then closes, then errors. That keeps each connection's events in order.HostHandles::netmakes the relay's engine the first time the program useshost.net, through a"cvc.net.net_engine"capsule from_arihost.net(). It has--net-threads NI/O threads (2 by default) and outlives the program.Host(net_threads=2)starts its own engine and stops it at shutdown, after its closes have reachedon_close.Measured (prettyhatemachine, Xeon E5-2650 v2)
The setup is the same as N1's: 6000 loopback receivers, a 113-byte line, 2 I/O threads, 21 rounds. Other sessions' builds were running (load about 10 on 32 cores), so N1's C++ benchmark and the Python one were run back to back, twice:
net_engine_testbench_pycvc_net.pybroadcast()callwrite().io_threads()costs 0.8 µs,send2.8 µs andbroadcastto an empty group 5.3 µs, with the engine's queueing included. The remaining gap on a single broadcast comes from the benchmark's Python reader threads waking at that moment.sock.send()to the same 6000 sockets costs 33.5–34.1 µs of the program's CPU per receiver, 0.20 s per message. sublevels' soak measured about 35 µs with asyncio's transports. Through the engine, the program makes one call of 30–35 µs.on_frameson an asyncio loop within 116–122 ms, in 2on_framescalls, at 1.3–1.4 µs of loop CPU per frame.The same numbers are in the doc's §4 under (N2, measured). A real NIC and sublevels' own load generator are for the sublevels soak (cy-pca/sublevels#8), which can now rerun on the engine.
Tests
pycvc_net(test_pycvc_net.py): the C++ API over loopback, covering the option structs,wait/pollbatches and memoryview lifetime, every command,set_framingwithpause_after, close reasons, stats andset_notify. It also covers the asyncio waker (refusals, oneon_framesper listener, callbacks only on the loop's thread) andHost.netunder plain python.arisrvx_net: an arisrvx--pythonprogram with--net-threads 3greets a connection, receives its frames, broadcasts to it and sees it close.pycvc_net,arisrvx_net,arisrvx_program/serve/check, and the existingpycvc_ariadne*tests pass. The writable-data scan on pycvc and arisrvx finds 0 symbols, and clang-format 18 is clean.Not in this PR: TLS (N3).