From 42fc08735a5734f29e443a8f48313f9bf43f857d Mon Sep 17 00:00:00 2001 From: rqg Date: Wed, 9 Jun 2021 22:26:07 +0800 Subject: [PATCH 1/5] support adb-shell style cpp_rpc --- .gitignore | 1 + apps/cpp_rpc/rpc_env.cc | 8 +++++++- apps/cpp_rpc/rpc_server.cc | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 83ef8ea38cd3..587eb44d777c 100644 --- a/.gitignore +++ b/.gitignore @@ -197,6 +197,7 @@ tvm_t.* .pytest_cache .local cmake-build-debug +cmake-build* # Visual Studio .vs diff --git a/apps/cpp_rpc/rpc_env.cc b/apps/cpp_rpc/rpc_env.cc index 5f703e1dc2b0..2369edd06ac4 100644 --- a/apps/cpp_rpc/rpc_env.cc +++ b/apps/cpp_rpc/rpc_env.cc @@ -95,7 +95,13 @@ RPCEnv::RPCEnv(const std::string& wd) { auto cmdline = fopen("/proc/self/cmdline", "r"); fread(cwd, 1, sizeof(cwd), cmdline); fclose(cmdline); - base_ = "/data/data/" + std::string(cwd) + "/cache/rpc"; + std::string android_base_ = "/data/data/" + std::string(cwd) +"/cache"; + struct stat statbuf; + if (stat(android_base_.data(), &statbuf) == -1 || !S_ISDIR(statbuf.st_mode)){ + android_base_ = "."; + } + base_ = android_base_+ "/rpc"; + #elif !defined(_WIN32) char cwd[PATH_MAX]; if (getcwd(cwd, sizeof(cwd))) { diff --git a/apps/cpp_rpc/rpc_server.cc b/apps/cpp_rpc/rpc_server.cc index 52b5da965b4c..e299f102e118 100644 --- a/apps/cpp_rpc/rpc_server.cc +++ b/apps/cpp_rpc/rpc_server.cc @@ -203,7 +203,7 @@ class RPCServer { auto pid = fork(); if (pid == 0) { ServerLoopProc(conn, addr, work_dir_); - exit(0); + _exit(0); } // Wait for the result int status = 0; From 54f88a1450d79a34b1ab70180c888ac8691ece64 Mon Sep 17 00:00:00 2001 From: rqg Date: Thu, 10 Jun 2021 10:02:30 +0800 Subject: [PATCH 2/5] fix review problems, #8223 --- .gitignore | 1 - apps/cpp_rpc/rpc_env.cc | 10 ++++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 587eb44d777c..83ef8ea38cd3 100644 --- a/.gitignore +++ b/.gitignore @@ -197,7 +197,6 @@ tvm_t.* .pytest_cache .local cmake-build-debug -cmake-build* # Visual Studio .vs diff --git a/apps/cpp_rpc/rpc_env.cc b/apps/cpp_rpc/rpc_env.cc index 2369edd06ac4..86f7d9ec1dde 100644 --- a/apps/cpp_rpc/rpc_env.cc +++ b/apps/cpp_rpc/rpc_env.cc @@ -39,6 +39,7 @@ int mkdir(const char* path, int /* ignored */) { return _mkdir(path); } #include #include #include + #include "../../src/support/utils.h" #include "rpc_env.h" @@ -95,12 +96,13 @@ RPCEnv::RPCEnv(const std::string& wd) { auto cmdline = fopen("/proc/self/cmdline", "r"); fread(cwd, 1, sizeof(cwd), cmdline); fclose(cmdline); - std::string android_base_ = "/data/data/" + std::string(cwd) +"/cache"; + std::string android_base_ = "/data/data/" + std::string(cwd) + "/cache"; struct stat statbuf; - if (stat(android_base_.data(), &statbuf) == -1 || !S_ISDIR(statbuf.st_mode)){ - android_base_ = "."; + if (stat(android_base_.data(), &statbuf) == -1 || !S_ISDIR(statbuf.st_mode)) { + android_base_ = "."; } - base_ = android_base_+ "/rpc"; + + base_ = android_base_ + "/rpc"; #elif !defined(_WIN32) char cwd[PATH_MAX]; From a507cdc570cf200f9a1010809df6a607e4069075 Mon Sep 17 00:00:00 2001 From: rqg Date: Thu, 10 Jun 2021 21:07:58 +0800 Subject: [PATCH 3/5] add comment & use /data/local/tmp dir in shell terminal case --- apps/cpp_rpc/rpc_env.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/cpp_rpc/rpc_env.cc b/apps/cpp_rpc/rpc_env.cc index 86f7d9ec1dde..4f0b91039ef3 100644 --- a/apps/cpp_rpc/rpc_env.cc +++ b/apps/cpp_rpc/rpc_env.cc @@ -98,10 +98,12 @@ RPCEnv::RPCEnv(const std::string& wd) { fclose(cmdline); std::string android_base_ = "/data/data/" + std::string(cwd) + "/cache"; struct stat statbuf; + // Check if application data directory exist. If not exist usually mean tvm_rpc run from adb + // shell terminal. if (stat(android_base_.data(), &statbuf) == -1 || !S_ISDIR(statbuf.st_mode)) { - android_base_ = "."; + // Tmp directory always writable for 'shell' user. + android_base_ = "/data/local/tmp"; } - base_ = android_base_ + "/rpc"; #elif !defined(_WIN32) From d2f5442c18dc0d2211bbf85a680e6921e03be9b7 Mon Sep 17 00:00:00 2001 From: rqg Date: Fri, 18 Jun 2021 17:12:33 +0800 Subject: [PATCH 4/5] fix spelling errors --- apps/cpp_rpc/rpc_env.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/cpp_rpc/rpc_env.cc b/apps/cpp_rpc/rpc_env.cc index 4f0b91039ef3..3c0b4e74f303 100644 --- a/apps/cpp_rpc/rpc_env.cc +++ b/apps/cpp_rpc/rpc_env.cc @@ -98,10 +98,10 @@ RPCEnv::RPCEnv(const std::string& wd) { fclose(cmdline); std::string android_base_ = "/data/data/" + std::string(cwd) + "/cache"; struct stat statbuf; - // Check if application data directory exist. If not exist usually mean tvm_rpc run from adb - // shell terminal. + // Check if application data directory exist. If not exist, usually means we run tvm_rpc from + // adb shell terminal shell terminal. if (stat(android_base_.data(), &statbuf) == -1 || !S_ISDIR(statbuf.st_mode)) { - // Tmp directory always writable for 'shell' user. + // Tmp directory is always writable for 'shell' user. android_base_ = "/data/local/tmp"; } base_ = android_base_ + "/rpc"; From bafea225e7763e3387454764ac9c233dd8cdadd5 Mon Sep 17 00:00:00 2001 From: rqg Date: Fri, 18 Jun 2021 17:14:06 +0800 Subject: [PATCH 5/5] fix spelling errors --- apps/cpp_rpc/rpc_env.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/cpp_rpc/rpc_env.cc b/apps/cpp_rpc/rpc_env.cc index 3c0b4e74f303..e897a975de28 100644 --- a/apps/cpp_rpc/rpc_env.cc +++ b/apps/cpp_rpc/rpc_env.cc @@ -99,7 +99,7 @@ RPCEnv::RPCEnv(const std::string& wd) { std::string android_base_ = "/data/data/" + std::string(cwd) + "/cache"; struct stat statbuf; // Check if application data directory exist. If not exist, usually means we run tvm_rpc from - // adb shell terminal shell terminal. + // adb shell terminal. if (stat(android_base_.data(), &statbuf) == -1 || !S_ISDIR(statbuf.st_mode)) { // Tmp directory is always writable for 'shell' user. android_base_ = "/data/local/tmp";