1. Are you ready for the Galaxy S20? Here is everything we know so far!

Please help me to optimize code in C++.

Discussion in 'Android Development' started by mm11751, Mar 7, 2022.

  1. mm11751

    mm11751 Lurker
    Thread Starter

    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 << ")";
     



    1. Download the Forums for Android™ app!


      Download

       
  2. mm11751

    mm11751 Lurker
    Thread Starter

    now I had solved this problem only using one line:
    Code (Text):
    1. --- a/system/vold/fs/Vfat.cpp
    2. +++ b/system/vold/fs/Vfat.cpp
    3. @@ -68,7 +68,9 @@ status_t Check(const std::string& source) {
    4.          cmd.push_back(source);
    5.  
    6.          // Fat devices are currently always untrusted
    7. -        rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
    8. +        //rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
    9. +
    10. +               ForkExecvpAsync(cmd);
     
Loading...
Similar Threads - Please help optimize
  1. Balenoy
    Replies:
    1
    Views:
    352
  2. richardben23
    Replies:
    6
    Views:
    472
  3. dennispacito
    Replies:
    6
    Views:
    579
  4. Peggy Bookout
    Replies:
    2
    Views:
    1,100
  5. hstroph
    Replies:
    10
    Views:
    1,200
  6. EonHarper
    Replies:
    5
    Views:
    806
  7. rdslayer
    Replies:
    1
    Views:
    1,299
  8. Samwisegamjee
    Replies:
    4
    Views:
    3,597
  9. Laszlo Lebrun
    Replies:
    3
    Views:
    648
  10. spre248
    Replies:
    8
    Views:
    5,511

Share This Page

Loading...