• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Please help me to optimize code in C++.

mm11751

Lurker
Mar 7, 2022
3
0
Now question is when I using large capacity usb flash disk ota(16G,100,000 files) it will reboot.
I check source code finding the reason it was Check function, so I add some code below.Now the flash disk don't reboot .
but how can I opimize code to kill thread and fix large more disk (ex.256G flash disk)?
My os is Android automotive10,ram and rom is 4+32G, cpu is mx8q

the diff file is

diff --git a/system/vold/fs/Vfat.cpp b/system/vold/fs/Vfat.cpp
old mode 100644
new mode 100755
index 4f1e982eb3..d2bac6fc7b
--- a/system/vold/fs/Vfat.cpp
+++ b/system/vold/fs/Vfat.cpp
@@ -41,7 +41,30 @@
#include "Utils.h"
#include "Vfat.h"
#include "VoldUtil.h"
-
+#include "../../core/libutils/include/utils/Thread.h" //wp add
+#include <inttypes.h>
+#include <pwd.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <sys/cdefs.h>
+#include <sys/epoll.h>
+#include <sys/eventfd.h>
+#include <sys/resource.h>
+#include <sys/socket.h>
+#include <sys/sysinfo.h>
+#include <sys/time.h>
+#include <time.h>
+#include <unistd.h>
+#include <cutils/properties.h>
+#include <cutils/sched_policy.h>
+#include <cutils/sockets.h>
+#include <../../core/lmkd/include/lmkd.h>
+#include <log/log.h>
+#include <log/log_event_list.h>
+#include <log/log_time.h>
+#include <../../core/lmkd/libpsi/include/psi/psi.h>
+#include <system/thread_defs.h>
using android::base::StringPrintf;

namespace android {
@@ -55,10 +78,32 @@ bool IsSupported() {
return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
IsFilesystemSupported("vfat");
}
+//wp add
+class CheckThread:public Thread {
+public:
+ CheckThread(std::vector<std::string> cmd, int fd):
+ mCmd(cmd),mFd(fd) {
+ }
+ virtual ~CheckThread() {
+ }
+
+ virtual bool threadLoop() {
+ //int rc = ForkExecvp(mCmd, sFsckUntrustedContext);//fork进程,check卡等待结果
+ int rc = ForkExecvp(mCmd, nullptr, sFsckUntrustedContext);
+ write(mFd, &rc, sizeof(int));//把结果写入管道
+ LOG(WARNING) << "xwg:rc = " << rc;
+ return false;//返回false,不循环了
+ }
+
+private:
+ std::vector<std::string> mCmd;
+ int mFd;
+}; //wp end
+

status_t Check(const std::string& source) {
int pass = 1;
- int rc = 0;
+ int rc = 3;
do {
std::vector<std::string> cmd;
cmd.push_back(kFsckPath);
@@ -66,9 +111,41 @@ status_t Check(const std::string& source) {
cmd.push_back("-f");
cmd.push_back("-y");
cmd.push_back(source);
-
+ //wp add
+ int pipefd[2];
+ if (pipe(pipefd) < 0) { //管道
+ LOG(INFO) << "xwg 0303 pipe failed";
+ return -1;
+ }
+ CheckThread* thread = new CheckThread(cmd, pipefd[1]);
+ thread->run("");//开启线程,执行fork进程,等待结果,同时把管道一端给它
+ int mEpollFd = epoll_create(2);
+ struct epoll_event eventItem;
+ memset(& eventItem, 0, sizeof(epoll_event));
+ // zero out unused members of data field union
+ eventItem.events = EPOLLIN;
+ eventItem.data.fd = pipefd[0];
+ epoll_ctl(mEpollFd, EPOLL_CTL_ADD, pipefd[0], & eventItem);
+ struct epoll_event eventItems[2];
+ int eventCount = epoll_wait(mEpollFd, eventItems, 2, 10000);//使用epoll机制,设置timeout为5min
+ LOG(WARNING) << "xwg:eventConut = "<<eventCount;
+ for (int i = 0; i < eventCount; i++) {
+ LOG(WARNING) << "xwg:int i = "<<i;
+ int fd = eventItems.data.fd;//有管道数据,说明check好了,返回结果就在管道中
+ LOG(WARNING) << "xwg:fd = "<<fd;
+ uint32_t epollEvents = eventItems.events;
+ if (fd == pipefd[0]) {
+ if (epollEvents & EPOLLIN) {
+ read(fd, &rc, sizeof(int));
+ }
+ }
+ }//end for,直接timeout了,说明10秒还没有check完
+ close(mEpollFd);
+ close(pipefd[0]);//关闭fd
+ close(pipefd[1]);
+ //wp end
// Fat devices are currently always untrusted
- rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
+ rc = 0;

if (rc < 0) {
LOG(ERROR) << "Filesystem check failed due to logwrap error";
@@ -86,6 +163,12 @@ status_t Check(const std::string& source) {
errno = ENODATA;
return -1;

+ case 3://3说明check超时了,wp add
+ //SLOGE("xwg 0303 Filesystem check outime");
+ LOG(INFO) << "xwg 0303 Filesystem check outime";
+ errno = ENODATA;
+ return -1;
+
case 4:
if (pass++ <= 3) {
LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")";
 
now I had solved this problem only using one line:
Code:
--- a/system/vold/fs/Vfat.cpp
+++ b/system/vold/fs/Vfat.cpp
@@ -68,7 +68,9 @@ status_t Check(const std::string& source) {
         cmd.push_back(source);

         // Fat devices are currently always untrusted
-        rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
+        //rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
+
+               ForkExecvpAsync(cmd);
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones