aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlessio Balsini <balsini@google.com>2023-10-13 21:59:45 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2023-10-13 21:59:45 +0000
commit5a1c2286d35053cd055a4f983331d8f9437aebcd (patch)
tree115e16db698c4cccd089156fae7bc24f7aeaf0a5
parentf6a34da4385c98cb1f5c131da6cc24cc6e8846db (diff)
parent7ce3bdd245b95d1e2b6863c7af40ee3b0ca0bd0e (diff)
downloaddittosuite-5a1c2286d35053cd055a4f983331d8f9437aebcd.tar.gz
Introduce sched affinity property am: 9192aeb5ae am: 7b6d808f55 am: 615a6bfe80 am: 7ce3bdd245
Original change: https://android-review.googlesource.com/c/platform/test/dittosuite/+/2786127 Change-Id: Iffd5eac863187d3c28957c65b8b449ed249bac05 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--example/sched_affinities.ditto30
-rw-r--r--include/ditto/multithreading_utils.h17
-rw-r--r--schema/benchmark.proto1
-rw-r--r--src/instruction.cpp3
-rw-r--r--src/instruction_factory.cpp7
-rw-r--r--src/multiprocessing.cpp3
-rw-r--r--src/multithreading_utils.cpp42
7 files changed, 100 insertions, 3 deletions
diff --git a/example/sched_affinities.ditto b/example/sched_affinities.ditto
new file mode 100644
index 0000000..3416399
--- /dev/null
+++ b/example/sched_affinities.ditto
@@ -0,0 +1,30 @@
+main: {
+ multithreading: {
+ fork: false;
+ threads: [
+ {
+ name: "CPU 0",
+ sched_affinity: 1,
+ instruction: {
+ cpu_work: {
+ utilization: 0.2
+ }
+ repeat: 100,
+ period_us: 60000
+ },
+ },
+ {
+ name: "CPU 3"
+ sched_affinity: 4,
+ instruction: {
+ cpu_work: {
+ utilization: 0.5
+ }
+ repeat: 100,
+ period_us: 50000
+ },
+ }
+ ]
+ }
+}
+global: {}
diff --git a/include/ditto/multithreading_utils.h b/include/ditto/multithreading_utils.h
index b46272a..f775e1b 100644
--- a/include/ditto/multithreading_utils.h
+++ b/include/ditto/multithreading_utils.h
@@ -65,13 +65,26 @@ class SchedAttr {
SchedAttr& operator=(const dittosuiteproto::SchedAttr& pb);
};
+class SchedAffinity {
+ bool initialized_ = false;
+ uint64_t mask_;
+
+ public:
+ void Set() const;
+ bool IsSet() const;
+
+ SchedAffinity& operator=(const uint64_t mask);
+};
+
struct MultithreadingParams {
const std::string name_;
SchedAttr sched_attr_;
+ SchedAffinity sched_affinity_;
- MultithreadingParams(const std::string& name, const SchedAttr& sched_attr)
- : name_(name), sched_attr_(sched_attr) {}
+ MultithreadingParams(const std::string& name, const SchedAttr& sched_attr,
+ const SchedAffinity &sched_affinity)
+ : name_(name), sched_attr_(sched_attr), sched_affinity_(sched_affinity) {}
};
} // namespace dittosuite
diff --git a/schema/benchmark.proto b/schema/benchmark.proto
index 5c5d182..ef0052e 100644
--- a/schema/benchmark.proto
+++ b/schema/benchmark.proto
@@ -114,6 +114,7 @@ message Thread {
optional int32 spawn = 2 [default = 1];
optional string name = 3;
optional SchedAttr sched_attr = 4;
+ optional int64 sched_affinity = 5 [default = -1];
}
message SchedAttr {
diff --git a/src/instruction.cpp b/src/instruction.cpp
index 8195303..9e1260f 100644
--- a/src/instruction.cpp
+++ b/src/instruction.cpp
@@ -53,6 +53,9 @@ void Instruction::RunSynchronized(pthread_barrier_t* barrier, const Multithreadi
if (params.sched_attr_.IsSet()) {
params.sched_attr_.Set();
}
+ if (params.sched_affinity_.IsSet()) {
+ params.sched_affinity_.Set();
+ }
pthread_barrier_wait(barrier);
Instruction::Run();
diff --git a/src/instruction_factory.cpp b/src/instruction_factory.cpp
index 5833fb2..6d9cead 100644
--- a/src/instruction_factory.cpp
+++ b/src/instruction_factory.cpp
@@ -224,7 +224,12 @@ std::unique_ptr<Instruction> InstructionFactory::CreateFromProtoInstruction(
sched_attr = thread.sched_attr();
}
- thread_params.push_back(MultithreadingParams(thread_name, sched_attr));
+ SchedAffinity sched_affinity = {};
+ if (thread.has_sched_affinity()) {
+ sched_affinity = thread.sched_affinity();
+ }
+
+ thread_params.push_back(MultithreadingParams(thread_name, sched_attr, sched_affinity));
}
}
diff --git a/src/multiprocessing.cpp b/src/multiprocessing.cpp
index 91f95be..9b9cf7f 100644
--- a/src/multiprocessing.cpp
+++ b/src/multiprocessing.cpp
@@ -104,6 +104,9 @@ void Multiprocessing::SetUpSingle() {
if (thread_params_[instruction_id_].sched_attr_.IsSet()) {
thread_params_[instruction_id_].sched_attr_.Set();
}
+ if (thread_params_[instruction_id_].sched_affinity_.IsSet()) {
+ thread_params_[instruction_id_].sched_affinity_.Set();
+ }
LOGD("Process initializing instruction: " + std::to_string(instruction_id_) +
" pid: " + std::to_string(getpid()));
diff --git a/src/multithreading_utils.cpp b/src/multithreading_utils.cpp
index 493c009..4d2e488 100644
--- a/src/multithreading_utils.cpp
+++ b/src/multithreading_utils.cpp
@@ -14,6 +14,8 @@
#include <ditto/multithreading_utils.h>
+#include <sched.h>
+
namespace dittosuite {
std::string to_string(const SchedAttr__& attr) {
@@ -96,4 +98,44 @@ SchedAttr& SchedAttr::operator=(const dittosuiteproto::SchedAttr& pb) {
return *this;
}
+void SchedAffinity::Set() const {
+ if (!initialized_) {
+ LOGF("Setting uninitialized affinity attributes");
+ }
+
+ LOGD("Setting affinity mask [" + std::to_string(mask_) +
+ "] to thread: " + std::to_string(gettid()));
+
+ cpu_set_t mask;
+ CPU_ZERO(&mask);
+ uint64_t tmp_bitset = mask_;
+ for (unsigned int i=0; i<sizeof(mask_) * 8; ++i) {
+ if (tmp_bitset & 1) {
+ LOGD("Enabling on CPU: " + std::to_string(i));
+ CPU_SET(i, &mask);
+ }
+ tmp_bitset >>= 1;
+ }
+
+ int ret = sched_setaffinity(0 /* self */, sizeof(mask), &mask);
+ if (ret) {
+ PLOGF("Failed setting scheduling affinity");
+ }
+}
+
+bool SchedAffinity::IsSet() const {
+ return initialized_;
+}
+
+SchedAffinity& SchedAffinity::operator=(const uint64_t mask) {
+ if (mask == 0) {
+ LOGF("Empty CPU affinity mask");
+ }
+
+ mask_ = mask;
+ initialized_ = true;
+
+ return *this;
+}
+
} // namespace dittosuite