Skip to content

Commit

Permalink
Address global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrdadh committed Dec 7, 2021
1 parent f87f51d commit 31fda2b
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions src/runtime/hexagon/rpc/hexagon/rpc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ namespace hexagon {
* \brief Hexagon IO Handler used in HexagonRPCServer(MinRPCServer).
*
* \param read_buffer The pointer to read buffer.
* \param read_buffer_size_bytes The read buffer size in bytes.
*/
class HexagonIOHandler {
public:
explicit HexagonIOHandler(uint8_t* read_buffer)
: read_buffer_{read_buffer}, read_buffer_size_bytes_{0} {}
explicit HexagonIOHandler(uint8_t* read_buffer, size_t read_buffer_size_bytes)
: read_buffer_{read_buffer}, read_buffer_size_bytes_{read_buffer_size_bytes}, read_buffer_index_{0} {}

void MessageStart(size_t message_size_bytes) {}

Expand All @@ -75,18 +76,18 @@ class HexagonIOHandler {

ssize_t PosixRead(uint8_t* buf, size_t read_len_bytes) {
HEXAGON_PRINT(ALWAYS, "HexagonIOHandler PosixRead called, %d, %d", read_len_bytes,
read_buffer_size_bytes_);
read_buffer_index_);

uint32_t bytes_to_read = 0;
if ((read_buffer_size_bytes_ - read_len_bytes) < 0) {
bytes_to_read = read_buffer_size_bytes_;
if ((read_buffer_index_ - read_len_bytes) < 0) {
bytes_to_read = read_buffer_index_;
} else {
bytes_to_read = read_len_bytes;
}

std::memcpy(buf, read_buffer_, bytes_to_read);
read_buffer_ += bytes_to_read;
read_buffer_size_bytes_ -= bytes_to_read;
read_buffer_index_ -= bytes_to_read;
if (bytes_to_read != read_len_bytes) {
HEXAGON_PRINT(ERROR, "Error bytes_to_read (%d) < read_len_bytes (%d).", bytes_to_read,
read_len_bytes);
Expand All @@ -98,21 +99,27 @@ class HexagonIOHandler {
* \brief Set read buffer in IOHandler to data pointer.
* \param data The data pointer.
* \param data_size_bytes The size of data in bytes.
*
* \return The status
*/
void SetReadBuffer(const uint8_t* data, size_t data_size_bytes) {
AEEResult SetReadBuffer(const uint8_t* data, size_t data_size_bytes) {
HEXAGON_PRINT(ALWAYS,
"HexagonIOHandler SetReadBuffer called: %d, prev read_buffer_size_bytes_: ",
data_size_bytes, read_buffer_size_bytes_);
"HexagonIOHandler SetReadBuffer called: %d, prev read_buffer_index_: ",
data_size_bytes, read_buffer_index_);
if (data_size_bytes > read_buffer_size_bytes_) {
return AEE_EFAILED;
}
read_buffer_ = data;
read_buffer_size_bytes_ = data_size_bytes;
read_buffer_index_ = data_size_bytes;
return AEE_SUCCESS;
}

/*!
* \brief Get pointer to the buffer that a packet has been written to.
* \param buf The data pointer.
* \param read_size_bytes The size of read in bytes.
*
* \returns The size of data that is read in bytes.
* \return The size of data that is read in bytes.
*/
int64_t GetWriteBuffer(uint8_t* buf, size_t read_size_bytes) {
HEXAGON_PRINT(ALWAYS, "HexagonIOHandler GetWriteBuffer called, read_len_bytes: %d",
Expand All @@ -126,24 +133,27 @@ class HexagonIOHandler {

private:
const uint8_t* read_buffer_;
uint32_t read_buffer_size_bytes_;
uint32_t read_buffer_index_;
size_t read_buffer_size_bytes_;

std::stringbuf write_buffer_;
};

class HexagonRPCServer {
public:
explicit HexagonRPCServer(uint8_t* receive_buffer) : io_{receive_buffer}, rpc_server_{&io_} {};
explicit HexagonRPCServer(uint8_t* receive_buffer, size_t receive_buffer_size_bytes) : io_{receive_buffer, receive_buffer_size_bytes}, rpc_server_{&io_} {};

/*!
* \brief Wrtie to IOHandler.
* \param data The data pointer
* \param data_size_bytes The data size in bytes.
*
* \returns The size of data written to IOHandler.
* \return The size of data written to IOHandler.
*/
int64_t Write(const uint8_t* data, size_t data_size_bytes) {
io_.SetReadBuffer(data, data_size_bytes);
if (io_.SetReadBuffer(data, data_size_bytes) != AEE_SUCCESS) {
return -1;
}
rpc_server_.ProcessOnePacket();
return (int64_t)data_size_bytes;
}
Expand All @@ -153,7 +163,7 @@ class HexagonRPCServer {
* \param buf The buffer pointer
* \param read_size_bytes Read request size in bytes.
*
* \returns The size of data that is read in bytes.
* \return The size of data that is read in bytes.
*/
int64_t Read(uint8_t* buf, size_t read_size_bytes) {
return io_.GetWriteBuffer(buf, read_size_bytes);
Expand All @@ -168,13 +178,11 @@ class HexagonRPCServer {
} // namespace runtime
} // namespace tvm

static tvm::runtime::hexagon::HexagonRPCServer* g_hexagon_rpc_server = nullptr;

static AEEResult hexagon_rpc_server_init() {
uint8_t* receive_buffer = new uint8_t[TVM_HEXAGON_RPC_BUFF_SIZE_BYTES];
tvm::runtime::hexagon::HexagonRPCServer* rpc_server =
new tvm::runtime::hexagon::HexagonRPCServer(receive_buffer);
g_hexagon_rpc_server = rpc_server;
namespace {
tvm::runtime::hexagon::HexagonRPCServer* get_hexagon_rpc_server() {
static tvm::runtime::hexagon::HexagonRPCServer g_hexagon_rpc_server(new uint8_t[TVM_HEXAGON_RPC_BUFF_SIZE_BYTES], TVM_HEXAGON_RPC_BUFF_SIZE_BYTES);
return &g_hexagon_rpc_server;
}
}

const tvm::runtime::PackedFunc get_runtime_func(const std::string& name) {
Expand All @@ -196,7 +204,7 @@ int __QAIC_HEADER(hexagon_rpc_open)(const char* uri, remote_handle64* handle) {
return AEE_ENOMEMORY;
}
reset_device_api();
hexagon_rpc_server_init();
get_hexagon_rpc_server();
return AEE_SUCCESS;
}

Expand All @@ -214,16 +222,11 @@ int __QAIC_HEADER(hexagon_rpc_close)(remote_handle64 handle) {
* \param data The data sent to host.
* \param dataLen The size of the data.
*
* \returns The status.
* \return The status.
*/
AEEResult __QAIC_HEADER(hexagon_rpc_send)(remote_handle64 _handle, const unsigned char* data,
int dataLen) {
if (g_hexagon_rpc_server == nullptr) {
HEXAGON_PRINT(ERROR, "RPC Server is not initialized.");
return AEE_EFAILED;
}

int64_t written_size = g_hexagon_rpc_server->Write(reinterpret_cast<const uint8_t*>(data),
int64_t written_size = get_hexagon_rpc_server()->Write(reinterpret_cast<const uint8_t*>(data),
static_cast<size_t>(dataLen));
if (written_size != dataLen) {
HEXAGON_PRINT(ERROR, "RPC Server Write failed, written_size (%d) != dataLen (%d)", written_size,
Expand All @@ -240,12 +243,12 @@ AEEResult __QAIC_HEADER(hexagon_rpc_send)(remote_handle64 _handle, const unsigne
* \param dataLen The size of the data that is requested to read in bytes.
* \param buf_written_size The size of the data that is actually read in bytes.
*
* \returns The status.
* \return The status.
*/
AEEResult __QAIC_HEADER(hexagon_rpc_receive)(remote_handle64 _handle, unsigned char* buf,
int bufLen, int64_t* buf_written_size) {
int64_t read_size =
g_hexagon_rpc_server->Read(reinterpret_cast<uint8_t*>(buf), static_cast<size_t>(bufLen));
get_hexagon_rpc_server()->Read(reinterpret_cast<uint8_t*>(buf), static_cast<size_t>(bufLen));
*buf_written_size = read_size;
if (read_size == bufLen) {
return AEE_SUCCESS;
Expand Down

0 comments on commit 31fda2b

Please sign in to comment.