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>
This commit is contained in:
Han-Lin Chen 2024-10-29 08:57:55 +00:00 committed by Kieran Bingham
parent d711a4c015
commit 4d9db06d66
3 changed files with 92 additions and 0 deletions

View file

@ -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;
}