Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RUTNIME] Support C++ RPC #4281

Merged
merged 13 commits into from
Nov 10, 2019
4 changes: 2 additions & 2 deletions apps/cpp_rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ Command line usage
--tracker - The RPC tracker address in host:port format e.g. 10.1.1.2:9190 Default=""
--key - The key used to identify the device type in tracker. Default=""
--custom-addr - Custom IP Address to Report to RPC Tracker. Default=""
--silent - Whether to run in silent mode. Default=True
--silent - Whether to run in silent mode. Default=False
--proxy - Whether to run in proxy mode. Default=False
Example
./tvm_rpc server --host=0.0.0.0 --port=9000 --port-end=9090 --tracker=127.0.0.1:9190 --key=rasp
```

## Note
Currently support is only there for Linux / Android environment.
Currently support is only there for Linux / Android environment and proxy mode doesn't be supported currently.
8 changes: 6 additions & 2 deletions apps/cpp_rpc/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static const string kUSAGE = \
"--tracker - The RPC tracker address in host:port format e.g. 10.1.1.2:9190 Default=\"\"\n" \
"--key - The key used to identify the device type in tracker. Default=\"\"\n" \
"--custom-addr - Custom IP Address to Report to RPC Tracker. Default=\"\"\n" \
"--silent - Whether to run in silent mode. Default=True\n" \
"--silent - Whether to run in silent mode. Default=False\n" \
"--proxy - Whether to run in proxy mode. Default=False\n" \
"\n" \
" Example\n" \
Expand All @@ -64,7 +64,7 @@ static const string kUSAGE = \
* \arg tracker The address of RPC tracker in host:port format e.g. 10.77.1.234:9190 Default=""
* \arg key The key used to identify the device type in tracker. Default=""
* \arg custom_addr Custom IP Address to Report to RPC Tracker. Default=""
* \arg silent Whether run in silent mode. Default=True
* \arg silent Whether run in silent mode. Default=False
* \arg is_proxy Whether to run in proxy mode. Default=False
*/
struct RpcServerArgs {
Expand Down Expand Up @@ -169,6 +169,10 @@ void ParseCmdArgs(int argc, char * argv[], struct RpcServerArgs &args) {
// Only errors and fatal is logged
dmlc::InitLogging("--minloglevel=2");
}
string proxy = GetCmdOption(argc, argv, "--proxy", true);
if (!proxy.empty()) {
args.is_proxy = true;
}

string host = GetCmdOption(argc, argv, "--host=");
if (!host.empty()) {
Expand Down
15 changes: 10 additions & 5 deletions apps/cpp_rpc/rpc_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/

/*!
* \file rpc_env.cc
* \brief Server environment of the RPC.
Expand Down Expand Up @@ -164,8 +163,11 @@ void LinuxShared(const std::string output,
cmd += " " + *f;
}
cmd += " " + options;
std::string executed_result = common::Execute(cmd);
CHECK(executed_result.length() == 0) << executed_result;
std::string err_msg;
auto executed_status = common::Execute(cmd, err_msg);
if (executed_status) {
LOG(ERROR) << err_msg;
FrozenGene marked this conversation as resolved.
Show resolved Hide resolved
}
}

/*!
Expand Down Expand Up @@ -207,8 +209,11 @@ Module Load(std::string *fileIn, const std::string fmt) {
std::string tmp_dir = "./rpc/tmp/";
mkdir(&tmp_dir[0], 0777);
std::string cmd = "tar -C " + tmp_dir + " -zxf " + file;
std::string executed_result = common::Execute(cmd);
CHECK(executed_result.length() == 0) << executed_result;
std::string err_msg;
int executed_status = common::Execute(cmd, err_msg);
if (executed_status) {
LOG(ERROR) << err_msg;
}
CreateShared(file_name, ListDir(tmp_dir));
CleanDir(tmp_dir);
rmdir(&tmp_dir[0]);
Expand Down
2 changes: 1 addition & 1 deletion apps/cpp_rpc/rpc_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct RPCEnv {

private:
/*!
* \base_ Holds the environment path.
* \brief Holds the environment path.
*/
std::string base_;
FrozenGene marked this conversation as resolved.
Show resolved Hide resolved
}; // RPCEnv
Expand Down
8 changes: 6 additions & 2 deletions apps/cpp_rpc/rpc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
namespace tvm {
namespace runtime {

/*!
* \brief wait the child process end.
* \param status status value
*/
#if defined(__linux__) || defined(__ANDROID__)
static pid_t waitPidEintr(int *status) {
pid_t pid = 0;
Expand Down Expand Up @@ -176,9 +180,9 @@ class RPCServer {
// Logging.
if (finished_first == timer_pid) {
LOG(INFO) << "Child pid=" << worker_pid << " killed (timeout = " << timeout
<< "), Process status =" << status_second;
<< "), Process status = " << status_second;
} else if (finished_first == worker_pid) {
LOG(INFO) << "Child pid=" << timer_pid << " killed, Process status =" << status_second;
LOG(INFO) << "Child pid=" << timer_pid << " killed, Process status = " << status_second;
}
} else {
auto pid = fork();
Expand Down
4 changes: 2 additions & 2 deletions src/common/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class Socket {
}
/*!
* \brief try bind the socket to host, from start_port to end_port
* \param host host_address to bind the socket
* \param host host address to bind the socket
* \param start_port starting port number to try
* \param end_port ending port number to try
* \return the port successfully bind to, return -1 if failed to bind any port
Expand All @@ -264,7 +264,7 @@ class Socket {
sizeof(sockaddr_in))) == 0) {
return port;
} else {
LOG(WARNING) << "Bind failed to " << host << ":" << port;
LOG(WARNING) << "Bind failed to " << host << ":" << port;
}
#if defined(_WIN32)
if (WSAGetLastError() != WSAEADDRINUSE) {
Expand Down
60 changes: 52 additions & 8 deletions src/common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#define TVM_COMMON_UTIL_H_

#include <stdio.h>
#ifndef _WIN32
#include <sys/wait.h>
#include <sys/types.h>
#endif
#include <vector>
#include <string>
#include <sstream>
Expand All @@ -41,7 +45,7 @@ namespace common {
* \param type "r" is for reading or "w" for writing.
* \return normal standard stream
*/
inline FILE * TVMPOpen(const char* command, const char* type) {
inline FILE* TVMPOpen(const char* command, const char* type) {
#if defined(_WIN32)
return _popen(command, type);
#else
Expand All @@ -61,6 +65,41 @@ inline int TVMPClose(FILE* stream) {
return pclose(stream);
#endif
}

/*
* gnulib sys_wait.h.in says on Windows
FrozenGene marked this conversation as resolved.
Show resolved Hide resolved
* When an unhandled fatal signal terminates a process, the exit code is 3.
* # define WIFSIGNALED(x) ((x) == 3)
* # define WIFEXITED(x) ((x) != 3)
* # define WIFSTOPPED(x) 0
*/
/*!
* \brief TVMWifexited wrapper of WIFEXITED between windows / linux
* \param status The status field that was filled in by the wait or waitpid function
* \return the exit code of the child process
*/
inline int TVMWifexited(int status) {
#if defined(_WIN32)
return (status != 3);
#else
return WIFEXITED(status);
#endif
}

/*!
* \brief TVMWexitstatus wrapper of WEXITSTATUS between windows / linux
* \param status The status field that was filled in by the wait or waitpid function.
* \return the child process exited normally or not
*/
inline int TVMWexitstatus(int status) {
#if defined(_WIN32)
return status;
#else
return WEXITSTATUS(status);
#endif
}


/*!
* \brief IsNumber check whether string is a number.
* \param str input string
Expand Down Expand Up @@ -93,7 +132,7 @@ inline std::vector<std::string> Split(const std::string& str, char delim) {
* \param end The end substring
* \return bool The result.
*/
inline bool EndsWith(std::string const & value, std::string const & end) {
inline bool EndsWith(std::string const& value, std::string const& end) {
if (end.size() <= value.size()) {
return std::equal(end.rbegin(), end.rend(), value.rbegin());
}
Expand All @@ -103,17 +142,22 @@ inline bool EndsWith(std::string const & value, std::string const & end) {
/*!
* \brief Execute the command
* \param cmd The command we want to execute
* \return executed output message
* \param err_msg The error message if we have
* \return executed output status
*/
inline std::string Execute(std::string cmd) {
inline int Execute(std::string cmd, std::string& err_msg) {
std::array<char, 128> buffer;
std::string result;
cmd += " 2>&1";
std::unique_ptr<FILE, decltype(&TVMPClose)> pipe(TVMPOpen(cmd.c_str(), "r"), TVMPClose);
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
FILE* fd = TVMPOpen(cmd.c_str(), "r");
while (fgets(buffer.data(), buffer.size(), fd) != nullptr) {
err_msg += buffer.data();
}
int status = TVMPClose(fd);
if (TVMWifexited(status)) {
return TVMWexitstatus(status);
}
return result;
return 255;
}

} // namespace common
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/rpc/rpc_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const int kRPCTrackerMagic = 0x2f271;
// sucess response
const int kRPCSuccess = kRPCMagic + 0;
// duplicate key in proxy
const int kRPCDupicate = kRPCMagic + 1;
const int kRPCDuplicate = kRPCMagic + 1;
// cannot found matched key in server
const int kRPCMismatch = kRPCMagic + 2;

Expand Down