Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt application core affinity
Forum Updated to NodeBB v4.3 + New Features

Qt application core affinity

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 2 Posters 605 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    romain.donze
    wrote on 14 Sept 2023, 10:25 last edited by romain.donze
    #1

    I’m using Boot2Qt on an apalis iMX8QP board. As the iMX8QP contains 4 Cortex A53 cores as well as a Cortex A73 core, I’d like to force the main thread of my Qt application to be executed only on the A73 core in order to gain in performance. So, at the start of my main(), I execute this function:

    cpu_set_t mask;
    CPU_ZERO(&mask);
    CPU_SET(coreNo, &mask);
    if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
        qCritical()<<"Error while executing sched_setaffinity to set affinity";
        assert(false);
    }
    

    The application is then executed on Core A73, but then all the different threads, whether created manually with QThread or automatically with QtConcurrent for example, are also executed on Core A73 and are not distributed between the other A53 cores. i understand that i could reuse sched_setaffinity at the start of each execution of other threads, but that’s not the point. so is there any way of forcing the main thread to run on a particular core without neglecting the use of the other cores by the subsequently created threads?

    J 1 Reply Last reply 15 Sept 2023, 03:15
    0
    • R romain.donze
      14 Sept 2023, 10:25

      I’m using Boot2Qt on an apalis iMX8QP board. As the iMX8QP contains 4 Cortex A53 cores as well as a Cortex A73 core, I’d like to force the main thread of my Qt application to be executed only on the A73 core in order to gain in performance. So, at the start of my main(), I execute this function:

      cpu_set_t mask;
      CPU_ZERO(&mask);
      CPU_SET(coreNo, &mask);
      if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
          qCritical()<<"Error while executing sched_setaffinity to set affinity";
          assert(false);
      }
      

      The application is then executed on Core A73, but then all the different threads, whether created manually with QThread or automatically with QtConcurrent for example, are also executed on Core A73 and are not distributed between the other A53 cores. i understand that i could reuse sched_setaffinity at the start of each execution of other threads, but that’s not the point. so is there any way of forcing the main thread to run on a particular core without neglecting the use of the other cores by the subsequently created threads?

      J Online
      J Online
      jeremy_k
      wrote on 15 Sept 2023, 03:15 last edited by
      #2

      sched_setaffinity(2) says:

      A child created via fork(2) inherits its parent's CPU affinity mask.

      pthread_create(3) has a similar statement.

      The choices appear to be explicitly setting the CPU affinity of each thread created, or to spawn addition threads from a thread that has previously had its affinity mask set.

      clone(2), with lots of flags for other things, makes no mention of the topic.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      R 1 Reply Last reply 15 Sept 2023, 06:14
      0
      • J jeremy_k
        15 Sept 2023, 03:15

        sched_setaffinity(2) says:

        A child created via fork(2) inherits its parent's CPU affinity mask.

        pthread_create(3) has a similar statement.

        The choices appear to be explicitly setting the CPU affinity of each thread created, or to spawn addition threads from a thread that has previously had its affinity mask set.

        clone(2), with lots of flags for other things, makes no mention of the topic.

        R Offline
        R Offline
        romain.donze
        wrote on 15 Sept 2023, 06:14 last edited by romain.donze
        #3

        Hi @jeremy_k thank you for your answer. Correct me if I am wrong but how can I manage affinity of the threads in QThreadPool::globalInstance()? as the affinity seems to be reset after a short time of innactivity. So is there a way to monitor all the QThreads in QThreadPool so maybe I can connect to QThread::started to call a slot that set the affinity once and not every time I call QtConcurrent::run() ?

        For now I am doing something like that at the beginning of my code:

            for(int i=0; i<QThreadPool::globalInstance()->maxThreadCount(); i++) {
                QThreadPool::globalInstance()->start([]() {
                    QObject::connect(QThread::currentThread(), &QThread::started, []() {
                        resetCoreAffinity();
                    });
                });
            }
        
        J 1 Reply Last reply 15 Sept 2023, 18:43
        0
        • R romain.donze
          15 Sept 2023, 06:14

          Hi @jeremy_k thank you for your answer. Correct me if I am wrong but how can I manage affinity of the threads in QThreadPool::globalInstance()? as the affinity seems to be reset after a short time of innactivity. So is there a way to monitor all the QThreads in QThreadPool so maybe I can connect to QThread::started to call a slot that set the affinity once and not every time I call QtConcurrent::run() ?

          For now I am doing something like that at the beginning of my code:

              for(int i=0; i<QThreadPool::globalInstance()->maxThreadCount(); i++) {
                  QThreadPool::globalInstance()->start([]() {
                      QObject::connect(QThread::currentThread(), &QThread::started, []() {
                          resetCoreAffinity();
                      });
                  });
              }
          
          J Online
          J Online
          jeremy_k
          wrote on 15 Sept 2023, 18:43 last edited by
          #4

          @romain-donze said in Qt application core affinity:

          Hi @jeremy_k thank you for your answer. Correct me if I am wrong but how can I manage affinity of the threads in QThreadPool::globalInstance()?

          That may make it tough. Thread pool threads appear to be started from the thread that caused it to be created, ie the thread in which QThreadPool::tryStart() was invoked when a new thread is required. If the application only queues new jobs from a thread with the desired affinity, that should work.

          as the affinity seems to be reset after a short time of innactivity.

          Threads don't reset their affinity. They terminate if QThreadPool::expiryTimeout elapses between jobs. Setting it to a negative value prevents timeout termination.

          For now I am doing something like that at the beginning of my code:

              for(int i=0; i<QThreadPool::globalInstance()->maxThreadCount(); i++) {
                  QThreadPool::globalInstance()->start([]() {
                      QObject::connect(QThread::currentThread(), &QThread::started, []() {
          [...]
          

          I don't think this will work.

          • QThreadPoolThread defines its own override for QThread::run(), which doesn't appear to emit QThread::started()
          • Even if QThread::started is emitted, the thread is already running by the time connect is called.
          • There is no guarantee that each job will be assigned to a different thread.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0

          1/4

          14 Sept 2023, 10:25

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved