Skip to content

Commit

Permalink
[libc][NFC] Cleanup the RPC server implementation prior to installing
Browse files Browse the repository at this point in the history
This does some simple cleanup prior to landing the patch to install
these.

Differential Revision: https://reviews.llvm.org/D153439
  • Loading branch information
jhuber6 committed Jun 21, 2023
1 parent 037952f commit 4272d09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
20 changes: 17 additions & 3 deletions libc/utils/gpu/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "src/__support/RPC/rpc.h"
#include <atomic>
#include <cstdio>
#include <cstring>
#include <memory>
#include <mutex>
#include <unordered_map>
Expand Down Expand Up @@ -91,7 +92,8 @@ struct Server {
: (port->get_opcode() == RPC_WRITE_TO_STDERR ? stderr
: files[id]);
int ret = fwrite(strs[id], sizes[id], 1, file);
reinterpret_cast<int *>(buffer->data)[0] = ret >= 0 ? sizes[id] : ret;
ret = ret >= 0 ? sizes[id] : ret;
std::memcpy(buffer->data, &ret, sizeof(int));
});
for (uint64_t i = 0; i < rpc::MAX_LANE_SIZE; ++i) {
if (strs[i])
Expand All @@ -101,7 +103,9 @@ struct Server {
}
case RPC_EXIT: {
port->recv([](rpc::Buffer *buffer) {
exit(reinterpret_cast<uint32_t *>(buffer->data)[0]);
int status = 0;
std::memcpy(&status, buffer->data, sizeof(int));
exit(status);
});
break;
}
Expand Down Expand Up @@ -143,7 +147,7 @@ struct Server {
break;
}
case RPC_NOOP: {
port->recv([](rpc::Buffer *buffer) {});
port->recv([](rpc::Buffer *) {});
break;
}
default: {
Expand Down Expand Up @@ -215,6 +219,8 @@ rpc_status_t rpc_shutdown(void) {
rpc_status_t rpc_server_init(uint32_t device_id, uint64_t num_ports,
uint32_t lane_size, rpc_alloc_ty alloc,
void *data) {
if (!state)
return RPC_STATUS_NOT_INITIALIZED;
if (device_id >= state->num_devices)
return RPC_STATUS_OUT_OF_RANGE;

Expand Down Expand Up @@ -250,6 +256,8 @@ rpc_status_t rpc_server_init(uint32_t device_id, uint64_t num_ports,

rpc_status_t rpc_server_shutdown(uint32_t device_id, rpc_free_ty dealloc,
void *data) {
if (!state)
return RPC_STATUS_NOT_INITIALIZED;
if (device_id >= state->num_devices)
return RPC_STATUS_OUT_OF_RANGE;
if (!state->devices[device_id])
Expand All @@ -263,6 +271,8 @@ rpc_status_t rpc_server_shutdown(uint32_t device_id, rpc_free_ty dealloc,
}

rpc_status_t rpc_handle_server(uint32_t device_id) {
if (!state)
return RPC_STATUS_NOT_INITIALIZED;
if (device_id >= state->num_devices)
return RPC_STATUS_OUT_OF_RANGE;
if (!state->devices[device_id])
Expand All @@ -280,6 +290,8 @@ rpc_status_t rpc_handle_server(uint32_t device_id) {
rpc_status_t rpc_register_callback(uint32_t device_id, rpc_opcode_t opcode,
rpc_opcode_callback_ty callback,
void *data) {
if (!state)
return RPC_STATUS_NOT_INITIALIZED;
if (device_id >= state->num_devices)
return RPC_STATUS_OUT_OF_RANGE;
if (!state->devices[device_id])
Expand All @@ -291,6 +303,8 @@ rpc_status_t rpc_register_callback(uint32_t device_id, rpc_opcode_t opcode,
}

void *rpc_get_buffer(uint32_t device_id) {
if (!state)
return nullptr;
if (device_id >= state->num_devices)
return nullptr;
if (!state->devices[device_id])
Expand Down
1 change: 1 addition & 0 deletions libc/utils/gpu/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef enum {
RPC_STATUS_OUT_OF_RANGE = 0x1001,
RPC_STATUS_UNHANDLED_OPCODE = 0x1002,
RPC_STATUS_INVALID_LANE_SIZE = 0x1003,
RPC_STATUS_NOT_INITIALIZED = 0x1004,
} rpc_status_t;

/// A struct containing an opaque handle to an RPC port. This is what allows the
Expand Down

0 comments on commit 4272d09

Please sign in to comment.