2023-03-10 16:48:53 -06:00
|
|
|
//===-- Shared memory RPC client / server interface -------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2023-03-20 09:29:43 -05:00
|
|
|
//
|
|
|
|
// This file implements a remote procedure call mechanism to communicate between
|
|
|
|
// heterogeneous devices that can share an address space atomically. We provide
|
|
|
|
// a client and a server to facilitate the remote call. The client makes request
|
|
|
|
// to the server using a shared communication channel. We use separate atomic
|
|
|
|
// signals to indicate which side, the client or the server is in ownership of
|
|
|
|
// the buffer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2023-03-10 16:48:53 -06:00
|
|
|
|
|
|
|
#ifndef LLVM_LIBC_SRC_SUPPORT_RPC_RPC_H
|
|
|
|
#define LLVM_LIBC_SRC_SUPPORT_RPC_RPC_H
|
|
|
|
|
2023-03-30 09:50:56 -05:00
|
|
|
#include "rpc_util.h"
|
2023-03-10 16:48:53 -06:00
|
|
|
#include "src/__support/CPP/atomic.h"
|
2023-04-13 21:27:51 -05:00
|
|
|
#include "src/__support/CPP/optional.h"
|
2023-04-18 09:44:27 -05:00
|
|
|
#include "src/__support/GPU/utils.h"
|
2023-04-13 21:27:51 -05:00
|
|
|
#include "src/string/memory_utils/memcpy_implementations.h"
|
2023-03-10 16:48:53 -06:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
namespace __llvm_libc {
|
|
|
|
namespace rpc {
|
|
|
|
|
2023-04-13 21:27:51 -05:00
|
|
|
/// A list of opcodes that we use to invoke certain actions on the server.
|
|
|
|
enum Opcode : uint16_t {
|
2023-03-10 16:48:53 -06:00
|
|
|
NOOP = 0,
|
|
|
|
PRINT_TO_STDERR = 1,
|
|
|
|
EXIT = 2,
|
2023-04-14 09:24:00 -05:00
|
|
|
TEST_INCREMENT = 3,
|
2023-03-10 16:48:53 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// A fixed size channel used to communicate between the RPC client and server.
|
2023-04-13 21:27:51 -05:00
|
|
|
struct alignas(64) Buffer {
|
|
|
|
uint8_t data[62];
|
|
|
|
uint16_t opcode;
|
2023-03-10 16:48:53 -06:00
|
|
|
};
|
2023-04-13 21:27:51 -05:00
|
|
|
static_assert(sizeof(Buffer) == 64, "Buffer size mismatch");
|
2023-03-10 16:48:53 -06:00
|
|
|
|
|
|
|
/// A common process used to synchronize communication between a client and a
|
|
|
|
/// server. The process contains an inbox and an outbox used for signaling
|
2023-04-13 21:27:51 -05:00
|
|
|
/// ownership of the shared buffer between both sides.
|
|
|
|
///
|
2023-05-04 00:21:18 +01:00
|
|
|
/// No process writes to its inbox. Each toggles the bit in the outbox to pass
|
|
|
|
/// ownership to the other process.
|
|
|
|
/// When inbox == outbox, the current state machine owns the buffer.
|
|
|
|
/// Initially the client is able to open any port as it will load 0 from both.
|
|
|
|
/// The server inbox read is inverted, so it loads inbox==1, outbox==0 until
|
|
|
|
/// the client has written to its outbox.
|
|
|
|
///
|
2023-04-13 21:27:51 -05:00
|
|
|
/// This process is designed to support mostly arbitrary combinations of 'send'
|
|
|
|
/// and 'recv' operations on the shared buffer as long as these operations are
|
|
|
|
/// mirrored by the other process. These operations exchange ownership of the
|
|
|
|
/// fixed-size buffer between the users of the protocol. The assumptions when
|
|
|
|
/// using this process are as follows:
|
|
|
|
/// - The client will always start with a 'send' operation
|
|
|
|
/// - The server will always start with a 'recv' operation
|
|
|
|
/// - For every 'send' / 'recv' call on one side of the process there is a
|
|
|
|
/// mirrored 'recv' / 'send' call.
|
|
|
|
///
|
2023-05-04 00:21:18 +01:00
|
|
|
template <bool InvertInbox> struct Process {
|
2023-03-29 15:24:58 -05:00
|
|
|
LIBC_INLINE Process() = default;
|
|
|
|
LIBC_INLINE Process(const Process &) = default;
|
|
|
|
LIBC_INLINE Process &operator=(const Process &) = default;
|
|
|
|
LIBC_INLINE ~Process() = default;
|
|
|
|
|
2023-04-13 21:27:51 -05:00
|
|
|
cpp::Atomic<uint32_t> *lock;
|
2023-03-10 16:48:53 -06:00
|
|
|
cpp::Atomic<uint32_t> *inbox;
|
|
|
|
cpp::Atomic<uint32_t> *outbox;
|
|
|
|
Buffer *buffer;
|
|
|
|
|
|
|
|
/// Initialize the communication channels.
|
2023-04-13 21:27:51 -05:00
|
|
|
LIBC_INLINE void reset(void *lock, void *inbox, void *outbox, void *buffer) {
|
2023-03-10 16:48:53 -06:00
|
|
|
*this = {
|
2023-04-13 21:27:51 -05:00
|
|
|
reinterpret_cast<cpp::Atomic<uint32_t> *>(lock),
|
2023-03-10 16:48:53 -06:00
|
|
|
reinterpret_cast<cpp::Atomic<uint32_t> *>(inbox),
|
|
|
|
reinterpret_cast<cpp::Atomic<uint32_t> *>(outbox),
|
|
|
|
reinterpret_cast<Buffer *>(buffer),
|
|
|
|
};
|
|
|
|
}
|
2023-04-13 21:27:51 -05:00
|
|
|
|
2023-05-04 00:21:18 +01:00
|
|
|
/// Inverting the bits loaded from the inbox in exactly one of the pair of
|
|
|
|
/// processes means that each can use the same state transitions.
|
|
|
|
/// Whichever process has InvertInbox==false is the initial owner.
|
|
|
|
/// Inbox equal Outbox => current process owns the buffer
|
|
|
|
/// Inbox difer Outbox => current process does not own the buffer
|
|
|
|
/// At startup, memory is zero initialised and raw loads of either mailbox
|
|
|
|
/// would return zero. Thus both would succeed in opening a port and data
|
|
|
|
/// races result. If either inbox or outbox is inverted for one process, that
|
|
|
|
/// process interprets memory as Inbox!=Outbox and thus waits for the other.
|
|
|
|
/// It is simpler to invert reads from the inbox than writes to the outbox.
|
|
|
|
LIBC_INLINE uint32_t load_inbox(uint64_t index) {
|
|
|
|
uint32_t i = inbox[index].load(cpp::MemoryOrder::RELAXED);
|
|
|
|
return InvertInbox ? !i : i;
|
|
|
|
}
|
|
|
|
|
2023-05-04 13:58:24 +01:00
|
|
|
/// Determines if this process needs to wait for ownership of the buffer.
|
2023-05-04 13:09:34 +01:00
|
|
|
LIBC_INLINE static bool buffer_unavailable(uint32_t in, uint32_t out) {
|
|
|
|
return in != out;
|
2023-04-13 21:27:51 -05:00
|
|
|
}
|
2023-05-04 13:58:24 +01:00
|
|
|
|
|
|
|
/// Attempt to claim the lock at index. Return true on lock taken.
|
|
|
|
/// The lock is held when the zeroth bit of the uint32_t at lock[index]
|
|
|
|
/// is set, and available when that bit is clear. Bits [1, 32) are zero.
|
|
|
|
/// Or with one is a no-op when the lock is already held.
|
|
|
|
LIBC_INLINE bool try_lock(uint64_t index) {
|
|
|
|
return lock[index].fetch_or(1, cpp::MemoryOrder::RELAXED) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock the lock at index.
|
|
|
|
LIBC_INLINE void unlock(uint64_t index) {
|
|
|
|
lock[index].store(0, cpp::MemoryOrder::RELAXED);
|
|
|
|
}
|
2023-04-13 21:27:51 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// The port provides the interface to communicate between the multiple
|
|
|
|
/// processes. A port is conceptually an index into the memory provided by the
|
|
|
|
/// underlying process that is guarded by a lock bit.
|
2023-05-04 00:21:18 +01:00
|
|
|
template <bool T> struct Port {
|
2023-04-13 21:27:51 -05:00
|
|
|
// TODO: This should be move-only.
|
2023-05-04 00:21:18 +01:00
|
|
|
LIBC_INLINE Port(Process<T> &process, uint64_t index, uint32_t out)
|
2023-04-13 21:27:51 -05:00
|
|
|
: process(process), index(index), out(out) {}
|
|
|
|
LIBC_INLINE Port(const Port &) = default;
|
|
|
|
LIBC_INLINE Port &operator=(const Port &) = delete;
|
|
|
|
LIBC_INLINE ~Port() = default;
|
|
|
|
|
|
|
|
template <typename U> LIBC_INLINE void recv(U use);
|
|
|
|
template <typename F> LIBC_INLINE void send(F fill);
|
|
|
|
template <typename F, typename U>
|
|
|
|
LIBC_INLINE void send_and_recv(F fill, U use);
|
|
|
|
template <typename W> LIBC_INLINE void recv_and_send(W work);
|
|
|
|
LIBC_INLINE void send_n(const void *src, uint64_t size);
|
|
|
|
template <typename A> LIBC_INLINE void recv_n(A alloc);
|
|
|
|
|
|
|
|
LIBC_INLINE uint16_t get_opcode() const {
|
|
|
|
return process.buffer[index].opcode;
|
|
|
|
}
|
|
|
|
|
2023-05-04 13:58:24 +01:00
|
|
|
LIBC_INLINE void close() { process.unlock(index); }
|
2023-04-13 21:27:51 -05:00
|
|
|
|
|
|
|
private:
|
2023-05-04 00:21:18 +01:00
|
|
|
Process<T> &process;
|
2023-04-13 21:27:51 -05:00
|
|
|
uint64_t index;
|
|
|
|
uint32_t out;
|
2023-03-10 16:48:53 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// The RPC client used to make requests to the server.
|
2023-05-04 00:21:18 +01:00
|
|
|
struct Client : public Process<false> {
|
2023-03-29 15:24:58 -05:00
|
|
|
LIBC_INLINE Client() = default;
|
|
|
|
LIBC_INLINE Client(const Client &) = default;
|
|
|
|
LIBC_INLINE Client &operator=(const Client &) = default;
|
|
|
|
LIBC_INLINE ~Client() = default;
|
|
|
|
|
2023-05-04 00:21:18 +01:00
|
|
|
using Port = rpc::Port<false>;
|
2023-04-13 21:27:51 -05:00
|
|
|
LIBC_INLINE cpp::optional<Port> try_open(uint16_t opcode);
|
|
|
|
LIBC_INLINE Port open(uint16_t opcode);
|
2023-03-10 16:48:53 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// The RPC server used to respond to the client.
|
2023-05-04 00:21:18 +01:00
|
|
|
struct Server : public Process<true> {
|
2023-03-29 15:24:58 -05:00
|
|
|
LIBC_INLINE Server() = default;
|
|
|
|
LIBC_INLINE Server(const Server &) = default;
|
|
|
|
LIBC_INLINE Server &operator=(const Server &) = default;
|
|
|
|
LIBC_INLINE ~Server() = default;
|
|
|
|
|
2023-05-04 00:21:18 +01:00
|
|
|
using Port = rpc::Port<true>;
|
2023-04-13 21:27:51 -05:00
|
|
|
LIBC_INLINE cpp::optional<Port> try_open();
|
|
|
|
LIBC_INLINE Port open();
|
2023-03-10 16:48:53 -06:00
|
|
|
};
|
|
|
|
|
2023-04-13 21:27:51 -05:00
|
|
|
/// Applies \p fill to the shared buffer and initiates a send operation.
|
2023-05-04 00:21:18 +01:00
|
|
|
template <bool T> template <typename F> LIBC_INLINE void Port<T>::send(F fill) {
|
|
|
|
uint32_t in = process.load_inbox(index);
|
2023-04-13 21:27:51 -05:00
|
|
|
|
|
|
|
// We need to wait until we own the buffer before sending.
|
2023-05-04 13:09:34 +01:00
|
|
|
while (Process<T>::buffer_unavailable(in, out)) {
|
2023-04-13 21:27:51 -05:00
|
|
|
sleep_briefly();
|
2023-05-04 00:21:18 +01:00
|
|
|
in = process.load_inbox(index);
|
2023-03-10 16:48:53 -06:00
|
|
|
}
|
2023-04-13 21:27:51 -05:00
|
|
|
|
|
|
|
// Apply the \p fill function to initialize the buffer and release the memory.
|
|
|
|
fill(&process.buffer[index]);
|
2023-05-04 00:21:18 +01:00
|
|
|
out = !out;
|
2023-04-13 21:27:51 -05:00
|
|
|
atomic_thread_fence(cpp::MemoryOrder::RELEASE);
|
|
|
|
process.outbox[index].store(out, cpp::MemoryOrder::RELAXED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies \p use to the shared buffer and acknowledges the send.
|
2023-05-04 00:21:18 +01:00
|
|
|
template <bool T> template <typename U> LIBC_INLINE void Port<T>::recv(U use) {
|
|
|
|
uint32_t in = process.load_inbox(index);
|
2023-04-13 21:27:51 -05:00
|
|
|
|
|
|
|
// We need to wait until we own the buffer before receiving.
|
2023-05-04 13:09:34 +01:00
|
|
|
while (Process<T>::buffer_unavailable(in, out)) {
|
2023-04-13 21:27:51 -05:00
|
|
|
sleep_briefly();
|
2023-05-04 00:21:18 +01:00
|
|
|
in = process.load_inbox(index);
|
2023-03-10 16:48:53 -06:00
|
|
|
}
|
2023-04-13 21:27:51 -05:00
|
|
|
atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
|
|
|
|
|
|
|
|
// Apply the \p use function to read the memory out of the buffer.
|
|
|
|
use(&process.buffer[index]);
|
2023-05-04 00:21:18 +01:00
|
|
|
out = !out;
|
2023-04-13 21:27:51 -05:00
|
|
|
process.outbox[index].store(out, cpp::MemoryOrder::RELAXED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combines a send and receive into a single function.
|
2023-05-04 00:21:18 +01:00
|
|
|
template <bool T>
|
2023-04-13 21:27:51 -05:00
|
|
|
template <typename F, typename U>
|
2023-05-04 00:21:18 +01:00
|
|
|
LIBC_INLINE void Port<T>::send_and_recv(F fill, U use) {
|
2023-04-13 21:27:51 -05:00
|
|
|
send(fill);
|
|
|
|
recv(use);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combines a receive and send operation into a single function. The \p work
|
|
|
|
/// function modifies the buffer in-place and the send is only used to initiate
|
|
|
|
/// the copy back.
|
2023-05-04 00:21:18 +01:00
|
|
|
template <bool T>
|
|
|
|
template <typename W>
|
|
|
|
LIBC_INLINE void Port<T>::recv_and_send(W work) {
|
2023-04-13 21:27:51 -05:00
|
|
|
recv(work);
|
|
|
|
send([](Buffer *) { /* no-op */ });
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends an arbitrarily sized data buffer \p src across the shared channel in
|
|
|
|
/// multiples of the packet length.
|
2023-05-04 00:21:18 +01:00
|
|
|
template <bool T>
|
|
|
|
LIBC_INLINE void Port<T>::send_n(const void *src, uint64_t size) {
|
2023-04-13 21:27:51 -05:00
|
|
|
// TODO: We could send the first bytes in this call and potentially save an
|
|
|
|
// extra send operation.
|
|
|
|
send([=](Buffer *buffer) { buffer->data[0] = size; });
|
|
|
|
const uint8_t *ptr = reinterpret_cast<const uint8_t *>(src);
|
|
|
|
for (uint64_t idx = 0; idx < size; idx += sizeof(Buffer::data)) {
|
|
|
|
send([=](Buffer *buffer) {
|
|
|
|
const uint64_t len =
|
|
|
|
size - idx > sizeof(Buffer::data) ? sizeof(Buffer::data) : size - idx;
|
|
|
|
inline_memcpy(buffer->data, ptr + idx, len);
|
|
|
|
});
|
2023-03-10 16:48:53 -06:00
|
|
|
}
|
2023-04-13 21:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Receives an arbitrarily sized data buffer across the shared channel in
|
|
|
|
/// multiples of the packet length. The \p alloc function is called with the
|
|
|
|
/// size of the data so that we can initialize the size of the \p dst buffer.
|
2023-05-04 00:21:18 +01:00
|
|
|
template <bool T>
|
|
|
|
template <typename A>
|
|
|
|
LIBC_INLINE void Port<T>::recv_n(A alloc) {
|
2023-04-13 21:27:51 -05:00
|
|
|
uint64_t size = 0;
|
|
|
|
recv([&](Buffer *buffer) { size = buffer->data[0]; });
|
|
|
|
uint8_t *dst = reinterpret_cast<uint8_t *>(alloc(size));
|
|
|
|
for (uint64_t idx = 0; idx < size; idx += sizeof(Buffer::data)) {
|
|
|
|
recv([=](Buffer *buffer) {
|
|
|
|
uint64_t len =
|
|
|
|
size - idx > sizeof(Buffer::data) ? sizeof(Buffer::data) : size - idx;
|
|
|
|
inline_memcpy(dst + idx, buffer->data, len);
|
|
|
|
});
|
2023-03-10 16:48:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 21:27:51 -05:00
|
|
|
/// Attempts to open a port to use as the client. The client can only open a
|
|
|
|
/// port if we find an index that is in a valid sending state. That is, there
|
|
|
|
/// are send operations pending that haven't been serviced on this port. Each
|
|
|
|
/// port instance uses an associated \p opcode to tell the server what to do.
|
2023-05-04 00:21:18 +01:00
|
|
|
LIBC_INLINE cpp::optional<Client::Port> Client::try_open(uint16_t opcode) {
|
|
|
|
constexpr uint64_t index = 0;
|
2023-04-13 21:27:51 -05:00
|
|
|
// Attempt to acquire the lock on this index.
|
2023-05-04 13:58:24 +01:00
|
|
|
if (!try_lock(index))
|
2023-04-13 21:27:51 -05:00
|
|
|
return cpp::nullopt;
|
|
|
|
|
2023-05-03 23:23:45 +01:00
|
|
|
// The mailbox state must be read with the lock held.
|
|
|
|
atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
|
|
|
|
|
2023-05-04 00:21:18 +01:00
|
|
|
uint32_t in = load_inbox(index);
|
2023-05-04 10:52:44 +01:00
|
|
|
uint32_t out = outbox[index].load(cpp::MemoryOrder::RELAXED);
|
2023-04-13 21:27:51 -05:00
|
|
|
|
|
|
|
// Once we acquire the index we need to check if we are in a valid sending
|
|
|
|
// state.
|
2023-05-04 13:09:34 +01:00
|
|
|
|
|
|
|
if (buffer_unavailable(in, out)) {
|
2023-05-04 13:58:24 +01:00
|
|
|
unlock(index);
|
2023-04-13 21:27:51 -05:00
|
|
|
return cpp::nullopt;
|
2023-03-10 16:48:53 -06:00
|
|
|
}
|
2023-04-13 21:27:51 -05:00
|
|
|
|
|
|
|
buffer->opcode = opcode;
|
2023-05-04 00:21:18 +01:00
|
|
|
return Port(*this, index, out);
|
2023-04-13 21:27:51 -05:00
|
|
|
}
|
|
|
|
|
2023-05-04 00:21:18 +01:00
|
|
|
LIBC_INLINE Client::Port Client::open(uint16_t opcode) {
|
2023-04-13 21:27:51 -05:00
|
|
|
for (;;) {
|
2023-05-04 00:21:18 +01:00
|
|
|
if (cpp::optional<Client::Port> p = try_open(opcode))
|
2023-04-13 21:27:51 -05:00
|
|
|
return p.value();
|
|
|
|
sleep_briefly();
|
2023-03-10 16:48:53 -06:00
|
|
|
}
|
2023-04-13 21:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to open a port to use as the server. The server can only open a
|
|
|
|
/// port if it has a pending receive operation
|
2023-05-04 00:21:18 +01:00
|
|
|
LIBC_INLINE cpp::optional<Server::Port> Server::try_open() {
|
|
|
|
constexpr uint64_t index = 0;
|
|
|
|
uint32_t in = load_inbox(index);
|
2023-05-04 10:52:44 +01:00
|
|
|
uint32_t out = outbox[index].load(cpp::MemoryOrder::RELAXED);
|
2023-04-13 21:27:51 -05:00
|
|
|
|
|
|
|
// The server is passive, if there is no work pending don't bother
|
|
|
|
// opening a port.
|
2023-05-04 13:09:34 +01:00
|
|
|
if (buffer_unavailable(in, out))
|
2023-04-13 21:27:51 -05:00
|
|
|
return cpp::nullopt;
|
|
|
|
|
|
|
|
// Attempt to acquire the lock on this index.
|
2023-05-04 13:58:24 +01:00
|
|
|
if (!try_lock(index))
|
2023-04-13 21:27:51 -05:00
|
|
|
return cpp::nullopt;
|
|
|
|
|
2023-05-03 23:23:45 +01:00
|
|
|
// The mailbox state must be read with the lock held.
|
|
|
|
atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
|
|
|
|
|
2023-05-04 00:21:18 +01:00
|
|
|
in = load_inbox(index);
|
2023-05-04 10:52:44 +01:00
|
|
|
out = outbox[index].load(cpp::MemoryOrder::RELAXED);
|
2023-04-13 21:27:51 -05:00
|
|
|
|
2023-05-04 13:09:34 +01:00
|
|
|
if (buffer_unavailable(in, out)) {
|
2023-05-04 13:58:24 +01:00
|
|
|
unlock(index);
|
2023-04-13 21:27:51 -05:00
|
|
|
return cpp::nullopt;
|
2023-03-10 16:48:53 -06:00
|
|
|
}
|
|
|
|
|
2023-05-04 00:21:18 +01:00
|
|
|
return Port(*this, index, out);
|
2023-04-13 21:27:51 -05:00
|
|
|
}
|
|
|
|
|
2023-05-04 00:21:18 +01:00
|
|
|
LIBC_INLINE Server::Port Server::open() {
|
2023-04-13 21:27:51 -05:00
|
|
|
for (;;) {
|
2023-05-04 00:21:18 +01:00
|
|
|
if (cpp::optional<Server::Port> p = try_open())
|
2023-04-13 21:27:51 -05:00
|
|
|
return p.value();
|
|
|
|
sleep_briefly();
|
|
|
|
}
|
2023-03-10 16:48:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rpc
|
|
|
|
} // namespace __llvm_libc
|
|
|
|
|
|
|
|
#endif
|