Skip to content

About

The threads module of Tarantool 3.9 with call() and eval() inside the process instead of IPROTO over a socket pair

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

9 Commits

Folders and files

Repository files navigation

InstantThreads

The threads module of Tarantool 3.9 and later (application threads) with call() and eval() carried inside the process.

The built-in module sends a call() or an eval() as an IPROTO request over a Unix socket pair: through the kernel into the IPROTO thread, which parses it and passes it through cbus to the target thread; the answer goes the same way back. InstantThreads puts the request as MsgPack into an inbox of the target thread instead: a fiber of that thread takes it, runs it in a fiber of its own and puts the answer into the inbox of the caller. No socket, no IPROTO thread, no system call while an inbox is busy.

The API stays the one of the built-in module, and so do the results and the errors: call(), eval(), export(), info(), the target option ('all', 'any', the number of a thread), require('threads') and require('experimental.threads'). The configuration of the threads, export(), info(), box.info in the threads and the privileges of IPROTO calls to the threads remain with the built-in module.

How it works

  • Inboxes. Every thread (TX is 0, the application threads follow in the order of their groups, as the built-in module numbers them) has one inbox in InstantThreads.Core, a small C library called through FFI: a queue any thread pushes to and only its owner takes from (Vyukov's intrusive MPSC queue), and an eventfd.
  • Buffers. One send is one block for all its targets: the MsgPack body is copied once, every target gets a header of its own in the block, and the block counts the headers not read yet. The last reader gives the block back to the pool of the thread that has sent it: a stack the readers push to and the owner takes whole with one exchange, sorted by the owner by the power of two of the size. A send calls malloc() only when its thread has no block that fits; blocks from 256 bytes to 64 KiB are kept, up to 4 MiB per thread.
  • Waking. The owner waits on its eventfd with the coio of Tarantool only when its inbox is empty: it raises a flag before it looks at the queue the last time, and a sender writes the eventfd only when it finds the flag. A thread sends what it has and wakes each target once, after the fiber that sent has yielded, as tnt_tx_push() and tnt_tx_flush() do.
  • Requests. A call() runs the function the built-in module has exported (box.internal.threads.call()), so both transports see the same functions; an eval() is compiled as the IPROTO one (eval is the name of the chunk). Every request runs in a fiber of a pool of the thread, which keeps its fibers for the next requests as the cbus pool does and ends one idle for a minute.
  • Slots. A call and its results cross as slots: a count, a slot of 16 bytes per value, then the bytes of the strings and of MsgPack fragments, written and read through FFI, which the JIT compiles. A string, a number, a boolean and box.NULL are a tag and the value; any other value is a MsgPack fragment of its own, so MsgPack runs only for the values that need it and keeps its own rules inside them. A simple value goes as a slot only when MsgPack would give it back as it was: a number that is not whole or whole below 1e14 (MsgPack of Tarantool decodes a larger one as an int64 cdata), -0 as 0 (MsgPack writes it as the integer 0); the arrays get the metatable of the arrays MsgPack decodes. Arguments with a hole (table.maxn() not equal to #) and values that carry a tuple send the whole call as MsgPack.
  • Values. They cross as MsgPack, as in IPROTO: nil inside the results becomes box.NULL, an error keeps its type, fields and previous errors (MP_ERROR), an error that is not a box.error becomes a LuajitError. A box.tuple stays a tuple of the same format, nested in tables as well: the format crosses with it, once per message, and the receiving thread makes it once per definition.
  • Start. override/threads.lua makes require('threads') find InstantThreads first. Tarantool loads it in every thread while it builds the Lua state of the thread (read_view.lua requires threads), before the thread knows its place. The transport of TX starts at once; the one of an application thread starts with the first request to it (the caller starts it through the built-in eval()) or when its own code calls. Until the thread groups are configured, call() and eval() are those of the built-in module: they only reach TX then.
  • Fallback. A thread that cannot load InstantThreads.Core keeps the built-in transport, and a request to it goes through the built-in module.

Differences

  • A request runs in a fiber of the pool of InstantThreads, not of the cbus pool of the thread; it is named pool as well, so the log looks the same. In TX its box.session is a background session of admin (box.session.type() is background), where the built-in module runs it in its IPROTO session of admin (binary). Application threads have no box.session.
  • Code of an application thread that took require('threads') before the transport of its thread started keeps the built-in module, which still works. After the start, package.loaded has InstantThreads.

Installation

make install

puts init.lua into $(LUADIR)/InstantThreads, Core.so into $(LIBDIR)/InstantThreads and the override modules into $(LUADIR)/override; LUADIR and LIBDIR give another tree, such as the directory of an application. The rock does the same:

tt rocks make instantthreads-scm-1.rockspec

Building needs a C compiler with C11 atomics and the headers of tarantool-dev (lua.h). Nothing has to change in the application: with the override modules installed, require('threads') is InstantThreads.

Tests

Tests/DockerTest.sh

builds Core.so in a container of the Tarantool image (made once with the compiler and kept as instantthreads-test:<image>) and runs Tests/InstantThreadsTest.lua phase by phase. Every case runs through the built-in module and through InstantThreads, and both must give the same results and the same errors:

  1. plain: no application threads, everything goes to the built-in module;
  2. box: box.cfg({ app_threads = 3 }), the module loaded before box.cfg();
  3. groups: three groups as the config sets them, the threads numbered across the groups;
  4. shutdown: calls from an on_shutdown trigger.

The cases cover the checks of the arguments and their errors, the targets, values of all MsgPack kinds and extensions (decimal, uuid, datetime, interval), tuples with and without a format in arguments and results, Lua errors, custom errors with previous errors, a function that yields, a megabyte both ways, 200 concurrent calls, and calls from an application thread to TX, to its own group and to other groups. SPEED=1 adds the time of a call through both modules; TARANTOOL_IMAGE takes another Tarantool.

Speed

Tarantool 3.9.0, box.cfg({ app_threads = 2 }), a function that returns its argument (a number, { id = 1, name = 'name', tags = { 'a', 'b' } }, or a string of 16 KiB), on a 4-core amd64 host, the mean of two runs of SPEED=1 Tests/DockerTest.sh at 1758df0; the time of a call and the CPU of TX per call:

Calls built-in InstantThreads
one at a time, one thread 32.9 µs, TX 15.1 13.3 µs, TX 8.1
one at a time, both threads 38.8 µs, TX 19.7 17.9 µs, TX 13.5
100 fibers, one thread 5.8 µs, TX 3.9 2.1 µs, TX 2.1
100 fibers, both threads 8.9 µs, TX 6.6 4.0 µs, TX 3.9
a small table, one at a time 34.7 µs, TX 16.1 17.3 µs, TX 10.3
a small table, 100 fibers 8.1 µs, TX 5.0 4.0 µs, TX 3.9
a small table, 100 fibers, both 11.6 µs, TX 8.4 6.7 µs, TX 6.5
16 KiB, one at a time 43.9 µs, TX 19.9 19.8 µs, TX 11.7
16 KiB, 100 fibers, one thread 12.6 µs, TX 12.0 5.5 µs, TX 5.2
16 KiB, 100 fibers, both threads 22.5 µs, TX 22.2 9.8 µs, TX 9.6

What the steps gave, each against the one before it, on the same host:

  • slots for calls of strings, numbers and booleans: 3.3 to 1.9 µs a call with 100 fibers, 16.2 to 12.8 µs one at a time, 5.7 to 4.7 µs with 16 KiB, the CPU of TX 3.2 to 1.9 µs;
  • MsgPack fragments for the other values in slots, instead of MsgPack for the whole call: 4.0 to 3.8 µs with a small table among the arguments, 17.7 to 16.9 µs one at a time; the table still costs a call of msgpack.encode() and msgpack.decode() through the Lua C API each way, which is what is dear, not the bytes;
  • the pool of fibers that run the requests: 5 to 7 %;
  • the pool of blocks: nothing measurable, for small calls and for 16 KiB to both threads (one copy instead of two): the allocator and the copy are not where the time goes;
  • slots written straight into the block of the core instead of a buffer of Lua copied into it: within the spread (16 KiB, 100 fibers: TX 4.9 instead of 5.0 µs; a small call 2.1 instead of 2.0, two calls through FFI instead of one);
  • tables as tagged slots too, walked in Lua by the rules of the serializer, and strings of requests passed by address (the caller waits and keeps them): tried and left out, a call with a small table took 6.2 to 7.3 µs instead of 3.8 and a small call 2.3 instead of 1.9; the walk over a table with next() runs in the interpreter and loses to the C of MsgPack, and the address saved no time measurable with 16 KiB;
  • waking a target right in the send, without the fiber that wakes after the senders have yielded, took one switch of fibers off a single call but cost a write() to the eventfd per call under load; tried and left out.

A call one at a time is mostly the two wake-ups of the threads through their eventfd; under load, the rest is Lua and the switches of fibers.

License

MIT

About

The threads module of Tarantool 3.9 with call() and eval() inside the process instead of IPROTO over a socket pair

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages