summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qthread.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2021-12-02 11:58:33 -0800
committerThiago Macieira <thiago.macieira@intel.com>2021-12-16 09:24:26 -0800
commit19d231e47d09c509884fb38c537f4fc96d6bf184 (patch)
treec2a349360c3ac74d937235a7553e35b18793957c /src/corelib/thread/qthread.cpp
parentc54fb03446f0b4c6f6492d0bfe459033ea480fe1 (diff)
QThread::idealThreadCount: use the thread affinity set
Instead of returning the total number of logical processors in the system, which we may not be allowed to wholly run on, use the affinity set that the calling thread is allowed to use. Implemented for Linux and FreeBSD only, with up to 4x the number of processors than CPU_SETSIZE has as default (that would be 4096 logical processors on Linux, 1024 on FreeBSD). Implementation for Windows is possible, but the API there is always limited to 64, so I'm unsure if it is correct. Darwin (macOS) does not have this capability. Testing: $ ./tst_qthread idealThreadCount | grep QDEBUG QDEBUG : tst_QThread::idealThreadCount() Ideal thread count: 8 $ taskset 3 ./tst_qthread idealThreadCount | grep QDEBUG QDEBUG : tst_QThread::idealThreadCount() Ideal thread count: 2 [ChangeLog][QtCore][QThread] idealThreadCount() will now return the number of logical processors that the current process (thread) has assigned in its affinity set, instead of the total number of processors in the system. These two numbers can be different if the process is launched by the parent with a different affinity set, with tools like Linux's taskset(1) or schedtool(1). This is currently implemented for Linux and FreeBSD. Change-Id: I2cffe62afda945079b63fffd16bd086f64f5f314 Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/thread/qthread.cpp')
-rw-r--r--src/corelib/thread/qthread.cpp17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp
index a936d189f4..4cfcab2258 100644
--- a/src/corelib/thread/qthread.cpp
+++ b/src/corelib/thread/qthread.cpp
@@ -314,9 +314,20 @@ QThreadPrivate::~QThreadPrivate()
/*!
\fn int QThread::idealThreadCount()
- Returns the ideal number of threads that can be run on the system. This is done querying
- the number of processor cores, both real and logical, in the system. This function returns 1
- if the number of processor cores could not be detected.
+ Returns the ideal number of threads that this process can run in parallel.
+ This is done by querying the number of logical processors available to this
+ process (if supported by this OS) or the total number of logical processors
+ in the system. This function returns 1 if neither value could be
+ determined.
+
+ \note On operating systems that support setting a thread's affinity to a
+ subset of all logical processors, the value returned by this function may
+ change between threads and over time.
+
+ \note On operating systems that support CPU hotplugging and hot-unplugging,
+ the value returned by this function may also change over time (and note
+ that CPUs can be turned on and off by software, without a physical,
+ hardware change).
*/
/*!