mkatanbaf commented on code in PR #10967:
URL: https://github.com/apache/tvm/pull/10967#discussion_r850737018


##########
src/runtime/minrpc/minrpc_logger.h:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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_LOGGER_H_
+#define TVM_RUNTIME_MINRPC_MINRPC_LOGGER_H_
+
+#include <tvm/runtime/c_runtime_api.h>
+
+#include <functional>
+#include <sstream>
+#include <string>
+#include <unordered_map>
+
+#include "minrpc_intrfc.h"
+#include "rpc_reference.h"
+
+namespace tvm {
+namespace runtime {
+
+#define PRINT_BYTES false
+
+/*!
+ * \brief Generates a user readeable log on the console
+ */
+class Logger {
+ public:
+  Logger() {}
+
+  void LogTVMValue(int tcode, TVMValue value) {
+    switch (tcode) {
+      case kDLInt:
+        this->LogVal<int64_t>("(int64)", value.v_int64);
+        break;
+      case kDLUInt:
+        this->LogVal<uint64_t>("(uint64)", value.v_int64);
+        break;
+      case kDLFloat: {
+        this->LogVal<float>("(float)", value.v_float64);
+        break;
+      }
+      case kTVMDataType: {
+        this->LogDLData("DLDataType(code,bits,lane)", &value.v_type);
+        break;
+      }
+      case kDLDevice: {
+        this->LogDLDevice("DLDevice(type,id)", &value.v_device);
+        break;
+      }
+      case kTVMPackedFuncHandle: {
+        this->LogVal<void*>("(PackedFuncHandle)", value.v_handle);
+        break;
+      }
+      case kTVMModuleHandle: {
+        this->LogVal<void*>("(ModuleHandle)", value.v_handle);
+        break;
+      }
+      case kTVMOpaqueHandle: {
+        this->LogVal<void*>("(OpaqueHandle)", value.v_handle);
+        break;
+      }
+      case kTVMDLTensorHandle: {
+        this->LogVal<void*>("(TensorHandle)", value.v_handle);
+        break;
+      }
+      case kTVMNDArrayHandle: {
+        this->LogVal<void*>("kTVMNDArrayHandle", value.v_handle);
+        break;
+      }
+      case kTVMNullptr:
+        this->LogString("Nullptr");
+        break;
+      case kTVMStr: {
+        this->LogString("\"");
+        this->LogString(value.v_str);
+        this->LogString("\"");
+        break;
+      }
+      case kTVMBytes: {
+        TVMByteArray* bytes = static_cast<TVMByteArray*>(value.v_handle);
+        int len = bytes->size;
+        this->LogVal<int64_t>("(Bytes) [size]: ", len);
+        if (PRINT_BYTES) {
+          this->LogString(", [Values]:");
+          this->LogString(" { ");
+          if (len > 0) {
+            this->LogVal<uint64_t>("", (uint8_t)bytes->data[0]);
+          }
+          for (int j = 1; j < len; j++) this->LogVal<uint64_t>(" - ", 
(uint8_t)bytes->data[j]);
+          this->LogString(" } ");
+        }
+        break;
+      }
+      default: {
+        this->LogString("ERROR-kUnknownTypeCode)");
+        break;
+      }
+    }
+    this->LogString("; ");
+  }
+
+  void LogString(const char* s) { os_ << s; }
+
+  void LogStr(std::string s) { os_ << s; }
+
+  void LogHandleName(std::string name) {
+    if (name.length() > 0) {
+      os_ << " <" << name.c_str() << ">";
+    }
+  }
+
+  template <typename T>
+  void LogVal(const char* s, T val) {
+    os_ << s << val;
+  }
+
+  void LogDLDevice(const char* s, DLDevice* dev) {
+    os_ << s << "(" << dev->device_type << "," << dev->device_id << ")";
+  }
+
+  void LogDLData(const char* s, DLDataType* data) {
+    os_ << s << "(" << (uint16_t)data->code << "," << (uint16_t)data->bits << 
"," << data->lanes
+        << ")";
+  }
+
+  std::stringstream LogTime();
+
+  void OutputLog();
+
+ private:
+  std::stringstream os_;
+};
+
+/*!
+ * \brief A wrapper for a MinRPCReturns object, that also logs the responses.
+ *
+ * \tparam ReturnInterface* underlying MinRPCReturns that generates the 
responses.
+ */
+class MinRPCReturnsWithLog : public ReturnInterface {
+ public:
+  /*!
+   * \brief Constructor.
+   * \param io The IO handler.
+   */
+  explicit MinRPCReturnsWithLog(ReturnInterface* next);
+  ~MinRPCReturnsWithLog();
+  void ReturnVoid();
+  void ReturnHandle(void* handle);
+  void ReturnException(const char* msg);
+  void ReturnPackedSeq(const TVMValue* arg_values, const int* type_codes, int 
num_args);
+  void ReturnCopyAck(uint64_t* num_bytes, uint8_t** data_ptr);
+  void ReturnLastTVMError();
+  void ThrowError(RPCServerStatus code, RPCCode info);
+  void processValues(const TVMValue** values, const int** tcodes, int* 
num_args);
+  void resetCurrHandleName(RPCCode code);
+  void updateCurrHandleName(const char* name);
+  void getHandleName(void* handle);
+  void releaseHandleName(void* handle);
+  Logger* getLogger();
+
+ private:
+  void registerHandleName(void* handle);
+  ReturnInterface* next_;
+  std::string CurrHandleName;
+  std::unordered_map<void*, std::string> array_tracker;
+  RPCCode code;
+  Logger logger_;
+  Logger* logger = &logger_;

Review Comment:
   thanks, fixed it!



##########
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;

Review Comment:
   thanks, fixed it.



##########
src/runtime/minrpc/minrpc_logger.h:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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_LOGGER_H_
+#define TVM_RUNTIME_MINRPC_MINRPC_LOGGER_H_
+
+#include <tvm/runtime/c_runtime_api.h>
+
+#include <functional>
+#include <sstream>
+#include <string>
+#include <unordered_map>
+
+#include "minrpc_intrfc.h"
+#include "rpc_reference.h"
+
+namespace tvm {
+namespace runtime {
+
+#define PRINT_BYTES false
+
+/*!
+ * \brief Generates a user readeable log on the console
+ */
+class Logger {
+ public:
+  Logger() {}
+
+  void LogTVMValue(int tcode, TVMValue value) {

Review Comment:
   I moved the entire code to the .h file and deleted the .cc file, I believe 
this is inline with the code base convention.



-- 
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]

Reply via email to