alanmacd commented on code in PR #10967: URL: https://github.com/apache/tvm/pull/10967#discussion_r849981746
########## src/runtime/rpc/rpc_channel_logger.h: ########## @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file rpc_channel_logger.h + * \brief A wrapper for RPCChannel with a NanoRPCListener for logging the commands. + */ +#ifndef TVM_RUNTIME_RPC_RPC_CHANNEL_LOGGER_H_ +#define TVM_RUNTIME_RPC_RPC_CHANNEL_LOGGER_H_ + +#include <memory> +#include <utility> + +#include "../minrpc/minrpc_server_logging.h" +#include "rpc_channel.h" + +#define RX_BUFFER_SIZE 65536 + +namespace tvm { +namespace runtime { + +class Buffer { + public: + Buffer(uint8_t* data, size_t data_size_bytes) + : data_{data}, capacity_{data_size_bytes}, num_valid_bytes_{0}, read_cursor_{0} {} + + size_t Write(const uint8_t* data, size_t data_size_bytes) { + size_t num_bytes_available = capacity_ - num_valid_bytes_; + size_t num_bytes_to_copy = data_size_bytes; + if (num_bytes_available < num_bytes_to_copy) { + num_bytes_to_copy = num_bytes_available; + } + + memcpy(&data_[num_valid_bytes_], data, num_bytes_to_copy); + num_valid_bytes_ += num_bytes_to_copy; + return num_bytes_to_copy; + } + + size_t Read(uint8_t* data, size_t data_size_bytes) { + size_t num_bytes_to_copy = data_size_bytes; + size_t num_bytes_available = num_valid_bytes_ - read_cursor_; + if (num_bytes_available < num_bytes_to_copy) { + num_bytes_to_copy = num_bytes_available; + } + + memcpy(data, &data_[read_cursor_], num_bytes_to_copy); + read_cursor_ += num_bytes_to_copy; + return num_bytes_to_copy; + } + + void Clear() { + num_valid_bytes_ = 0; + read_cursor_ = 0; + } + + size_t ReadAvailable() const { return num_valid_bytes_ - read_cursor_; } + + size_t Size() const { return num_valid_bytes_; } + + private: + /*! \brief pointer to data buffer. */ + uint8_t* data_; + + /*! \brief The total number of bytes available in data_. Always a power of 2. */ + size_t capacity_; + + /*! \brief index into data_ of the next potentially-available byte in the buffer. + * The byte is available when tail_ != data_ + capacity_. Review Comment: update comment "tail_" -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
