diff options
author | Han-Lin Chen <hanlinchen@chromium.org> | 2024-10-29 08:57:55 +0000 |
---|---|---|
committer | Kieran Bingham <kieran.bingham@ideasonboard.com> | 2024-11-28 17:50:55 +0000 |
commit | 4d9db06d669044c0c461a2aed79c85c7fe32a502 (patch) | |
tree | ef4ef979b3146962a632bc44eef43de43444cc32 /test | |
parent | d711a4c015e74c7a77cfeba17b621909385f4494 (diff) |
libcamera: add method to set thread affinity
Add method to set thread affinity to Thread class.
Signed-off-by: Han-Lin Chen <hanlinchen@chromium.org>
Co-developed-by: Harvey Yang <chenghaoyang@chromium.org>
Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/threads.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/test/threads.cpp b/test/threads.cpp index ceb4fa0f..8d6ee151 100644 --- a/test/threads.cpp +++ b/test/threads.cpp @@ -9,9 +9,11 @@ #include <iostream> #include <memory> #include <pthread.h> +#include <sched.h> #include <thread> #include <time.h> +#include <libcamera/base/object.h> #include <libcamera/base/thread.h> #include "test.h" @@ -66,6 +68,27 @@ private: bool &cancelled_; }; +class CpuSetTester : public Object +{ +public: + CpuSetTester(unsigned int cpuset) + : cpuset_(cpuset) {} + + bool testCpuSet() + { + int ret = sched_getcpu(); + if (static_cast<int>(cpuset_) != ret) { + cout << "Invalid cpuset: " << ret << ", expecting: " << cpuset_ << endl; + return false; + } + + return true; + } + +private: + const unsigned int cpuset_; +}; + class ThreadTest : public Test { protected: @@ -165,6 +188,23 @@ protected: return TestFail; } + const unsigned int numCpus = std::thread::hardware_concurrency(); + for (unsigned int i = 0; i < numCpus; ++i) { + thread = std::make_unique<Thread>(); + const std::array<const unsigned int, 1> cpus{ i }; + thread->setThreadAffinity(cpus); + thread->start(); + + CpuSetTester tester(i); + tester.moveToThread(thread.get()); + + if (!tester.invokeMethod(&CpuSetTester::testCpuSet, ConnectionTypeBlocking)) + return TestFail; + + thread->exit(0); + thread->wait(); + } + return TestPass; } |