alanmacd commented on code in PR #10967: URL: https://github.com/apache/tvm/pull/10967#discussion_r849989729
########## src/runtime/minrpc/minrpc_server_logging.h: ########## @@ -0,0 +1,141 @@ +/* + * 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. + */ + +#ifndef TVM_RUNTIME_MINRPC_MINRPC_SERVER_LOGGING_H_ +#define TVM_RUNTIME_MINRPC_MINRPC_SERVER_LOGGING_H_ + +#include "minrpc_logger.h" +#include "minrpc_server.h" + +namespace tvm { +namespace runtime { + +/*! + * \brief A minimum RPC server that logs the received commands. + * + * \tparam TIOHandler IO provider to provide io handling. + */ +template <typename TIOHandler> +class MinRPCServerWithLog { + public: + explicit MinRPCServerWithLog(TIOHandler* io) + : ret(io), retWLog(&ret), exec(io, &retWLog), execWLog(&exec), next_(io, &execWLog) {} + + bool ProcessOnePacket() { return next_.ProcessOnePacket(); } + + private: + MinRPCReturns<TIOHandler> ret; + MinRPCExecute<TIOHandler> exec; + MinRPCReturnsWithLog retWLog; + MinRPCExecuteWithLog execWLog; + MinRPCServer<TIOHandler> next_; +}; + +/*! + * \brief A minimum RPC server that only logs the outgoing commands and received responses. + * (Does not process the packets or respond to them.) + * + * \tparam TIOHandler IO provider to provide io handling. + */ +template <typename TIOHandler> +class MinRPCSniffer { + public: + explicit MinRPCSniffer(TIOHandler* io) + : io_(io), ret(io), retWLog(&ret), exec(&retWLog), execWLog(&exec), next_(io, &execWLog) {} + + bool ProcessOnePacket() { return next_.ProcessOnePacket(); } + + void ProcessOneResponse() { + RPCCode code; + uint64_t packet_len = 0; + + this->Read(&packet_len); + if (packet_len == 0) OutputLog(); + this->Read(&code); + switch (code) { + case RPCCode::kReturn: { + HandleReturn(); + break; + } + case RPCCode::kException: { + retWLog.ReturnException(""); + break; + } + default: { + OutputLog(); + break; + } + } + } + + private: + void HandleReturn() { + int32_t num_args; + int32_t tcode; + + this->Read(&num_args); + if (num_args == 1) { + this->Read(&tcode); + if (tcode == kTVMNullptr) { + retWLog.ReturnVoid(); + return; + } + if (tcode == kTVMOpaqueHandle) { + uint64_t handle; + this->Read(&handle); + retWLog.ReturnHandle(reinterpret_cast<void*>(handle)); + return; + } + } + OutputLog(); + } + + void OutputLog() { retWLog.getLogger()->OutputLog(); } + + template <typename T> + void Read(T* data) { + static_assert(std::is_pod<T>::value, "need to be trival"); + ReadRawBytes(data, sizeof(T)); + } + + void ReadRawBytes(void* data, size_t size) { + uint8_t* buf = reinterpret_cast<uint8_t*>(data); + size_t ndone = 0; + while (ndone < size) { + ssize_t ret = io_->PosixRead(buf, size - ndone); + if (ret <= 0) { + retWLog.getLogger()->LogString("-> No Response Received."); + break; + } + ndone += ret; + buf += ret; + } + } + + TIOHandler* io_; Review Comment: add trailing underscores -- 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]
