aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlessio Balsini <balsini@google.com>2024-03-04 14:42:17 +0000
committerAlessio Balsini <balsini@google.com>2024-03-05 10:19:55 +0000
commit147963359aea13e21fa45e2425cf8a31f1f130bd (patch)
treee8018c8705c0a608d4e1efd9c839d797e8c28ce2
parent7b4e122027eac90ba76da2eb6a4703a3f76813c4 (diff)
downloaddittosuite-147963359aea13e21fa45e2425cf8a31f1f130bd.tar.gz
Move gettid and sched_setattr to Syscall interface
Moving function calls that are strictly depending on the platform into an external redirection that complies to the SyscallInterface interface is a design pattern that should have been always followed. Not complying to this design pattern can cause issues, for example when building the tool with Kleaf, whose build environment does not support some of the latest syscalls. Fix by moving these function calls into the Syscall interface and implementing them in a more baremetal fashion. Bug: 322744630 Test: build, run on Android Change-Id: I2e9806373868fadd54f9d500346aaab424e34539 Signed-off-by: Alessio Balsini <balsini@google.com>
-rw-r--r--include/ditto/multithreading_utils.h6
-rw-r--r--include/ditto/syscall.h4
-rw-r--r--src/instruction_factory.cpp4
-rw-r--r--src/multithreading_utils.cpp6
-rw-r--r--src/syscall.cpp56
-rw-r--r--test/include/mock_syscall.h6
6 files changed, 77 insertions, 5 deletions
diff --git a/include/ditto/multithreading_utils.h b/include/ditto/multithreading_utils.h
index b8295be..9c17a6d 100644
--- a/include/ditto/multithreading_utils.h
+++ b/include/ditto/multithreading_utils.h
@@ -39,10 +39,13 @@ enum SchedPolicy {
};
class SchedAttr {
+ SyscallInterface& syscall_;
bool initialized_ = false;
SchedAttr__ sched_attr_;
public:
+ SchedAttr(SyscallInterface& syscall) : syscall_(syscall) {}
+
void Set() const;
bool IsSet() const;
@@ -50,10 +53,13 @@ class SchedAttr {
};
class SchedAffinity {
+ SyscallInterface& syscall_;
bool initialized_ = false;
uint64_t mask_;
public:
+ SchedAffinity(SyscallInterface& syscall) : syscall_(syscall) {}
+
void Set() const;
bool IsSet() const;
diff --git a/include/ditto/syscall.h b/include/ditto/syscall.h
index e51cc95..6f90483 100644
--- a/include/ditto/syscall.h
+++ b/include/ditto/syscall.h
@@ -54,9 +54,11 @@ class SyscallInterface {
virtual int FTruncate(int fd, int64_t length) = 0;
virtual int FStat(int filedes, struct stat64* buf) = 0;
virtual int FSync(int fd) = 0;
+ virtual pid_t GetTid() = 0;
virtual int Open(const std::string& path_name, int flags, int mode) = 0;
virtual DIR* OpenDir(const std::string& name) = 0;
virtual int64_t Read(int fd, char* buf, int64_t count, int64_t offset) = 0;
+ virtual int SchedSetattr(pid_t pid, const SchedAttr__& attr, unsigned int flags) = 0;
virtual struct dirent* ReadDir(DIR* dirp) = 0;
virtual int64_t ReadLink(const std::string& path_name, char* buf, int64_t bufsiz) = 0;
virtual void Sync() = 0;
@@ -79,9 +81,11 @@ class Syscall : public SyscallInterface {
int FTruncate(int fd, int64_t length) override;
int FStat(int filedes, struct stat64* buf) override;
int FSync(int fd) override;
+ pid_t GetTid() override;
int Open(const std::string& path_name, int flags, int mode) override;
DIR* OpenDir(const std::string& name) override;
int64_t Read(int fd, char* buf, int64_t count, int64_t offset) override;
+ int SchedSetattr(pid_t pid, const SchedAttr__& attr, unsigned int flags) override;
struct dirent* ReadDir(DIR* dirp) override;
int64_t ReadLink(const std::string& path_name, char* buf, int64_t bufsiz) override;
void Sync() override;
diff --git a/src/instruction_factory.cpp b/src/instruction_factory.cpp
index 69c48d0..1ac4433 100644
--- a/src/instruction_factory.cpp
+++ b/src/instruction_factory.cpp
@@ -220,12 +220,12 @@ std::unique_ptr<Instruction> InstructionFactory::CreateFromProtoInstruction(
thread_name = std::to_string(i);
}
- SchedAttr sched_attr = {};
+ SchedAttr sched_attr(Syscall::GetSyscall());
if (thread.has_sched_attr()) {
sched_attr = thread.sched_attr();
}
- SchedAffinity sched_affinity = {};
+ SchedAffinity sched_affinity(Syscall::GetSyscall());
if (thread.has_sched_affinity()) {
sched_affinity = thread.sched_affinity();
}
diff --git a/src/multithreading_utils.cpp b/src/multithreading_utils.cpp
index b3355d6..e1d6c67 100644
--- a/src/multithreading_utils.cpp
+++ b/src/multithreading_utils.cpp
@@ -27,9 +27,9 @@ void SchedAttr::Set() const {
}
LOGD("Setting scheduling policy [" + std::to_string(sched_attr_.sched_policy) +
- "] to thread: " + std::to_string(gettid()));
+ "] to thread: " + std::to_string(syscall_.GetTid()));
- int ret = syscall(SYS_sched_setattr, 0 /* self */, &sched_attr_, 0 /* still not implemented */);
+ int ret = syscall_.SchedSetattr(0 /* self */, sched_attr_, 0 /* still not implemented */);
if (ret) {
PLOGF("Failed setting scheduling attributes \n" + to_string(sched_attr_) + "\n");
}
@@ -90,7 +90,7 @@ void SchedAffinity::Set() const {
}
LOGD("Setting affinity mask [" + std::to_string(mask_) +
- "] to thread: " + std::to_string(gettid()));
+ "] to thread: " + std::to_string(syscall_.GetTid()));
cpu_set_t mask;
CPU_ZERO(&mask);
diff --git a/src/syscall.cpp b/src/syscall.cpp
index 13d81e0..288e59b 100644
--- a/src/syscall.cpp
+++ b/src/syscall.cpp
@@ -12,8 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+#include <sys/syscall.h>
+
#include <sstream>
+#include <ditto/logger.h>
#include <ditto/syscall.h>
namespace dittosuite {
@@ -55,6 +58,14 @@ int Syscall::FSync(int fd) {
return fsync(fd);
}
+pid_t Syscall::GetTid() {
+ long ret = syscall(SYS_gettid);
+ if (ret == -1) {
+ PLOGF("Error calling syscall(SYS_gettid)");
+ }
+ return ret;
+}
+
int Syscall::Open(const std::string& path_name, int flags, int mode) {
return open(path_name.c_str(), flags, mode);
}
@@ -75,6 +86,51 @@ int64_t Syscall::ReadLink(const std::string& path_name, char* buf, int64_t bufsi
return readlink(path_name.c_str(), buf, bufsiz);
}
+#ifndef __NR_sched_setattr
+
+/* Define all the __NR_sched_setattr syscall numbers for every architecture */
+
+#ifdef __x86_64__
+#define __NR_sched_setattr 314
+#endif
+
+#ifdef __i386__
+#define __NR_sched_setattr 351
+#endif
+
+#ifdef __arm__
+#define __NR_sched_setattr 380
+#endif
+
+/* If none of the architecture above have been matched, then use the
+ * asm-generic/unistd.h definition 274, which also matches the aarch64
+ * definition of __NR_sched_setattr. */
+#ifndef __NR_sched_setattr
+#define __NR_sched_setattr 274
+#endif
+
+#else /* __NR_sched_setattr */
+
+/* Make sure the __NR_sched_setattr syscall numbers are consistent with the
+Linux implementation */
+
+#if ((defined(__x86_64__) && __NR_sched_setattr != 314) || \
+ (defined(__i386__) && __NR_sched_setattr != 351) || \
+ (defined(__arm__) && __NR_sched_setattr != 380)) && \
+ __NR_sched_setattr != 274 /* aarch64 and asm-generic/unistd.h */
+#error "Wrong definition of __NR_sched_setattr"
+#endif
+
+#endif /* __NR_sched_setattr */
+
+int Syscall::SchedSetattr(pid_t pid, const SchedAttr__& attr, unsigned int flags) {
+ long ret = syscall(__NR_sched_setattr, pid, &attr, flags);
+ if (ret == -1) {
+ PLOGF("Error calling syscall(__NR_sched_setattr)");
+ }
+ return ret;
+}
+
void Syscall::Sync() {
return sync();
}
diff --git a/test/include/mock_syscall.h b/test/include/mock_syscall.h
index c81a02c..4019197 100644
--- a/test/include/mock_syscall.h
+++ b/test/include/mock_syscall.h
@@ -39,12 +39,15 @@ class MockSyscall : public dittosuite::SyscallInterface {
return 0;
}));
ON_CALL(*this, FSync(::testing::_)).WillByDefault(::testing::Return(0));
+ ON_CALL(*this, GetTid()).WillByDefault(::testing::Return(0));
ON_CALL(*this, Open(::testing::_, ::testing::_, ::testing::_))
.WillByDefault(::testing::Return(kDefaultFileDescriptor));
ON_CALL(*this, Read(::testing::_, ::testing::_, ::testing::_, ::testing::_))
.WillByDefault(::testing::ReturnArg<2>());
ON_CALL(*this, ReadLink(::testing::_, ::testing::_, ::testing::_))
.WillByDefault(::testing::ReturnArg<2>());
+ ON_CALL(*this, SchedSetattr(::testing::_, ::testing::_, ::testing::_))
+ .WillByDefault(::testing::Return(0));
ON_CALL(*this, Unlink(::testing::_)).WillByDefault(::testing::Return(0));
ON_CALL(*this, Write(::testing::_, ::testing::_, ::testing::_, ::testing::_))
.WillByDefault(::testing::ReturnArg<2>());
@@ -58,9 +61,12 @@ class MockSyscall : public dittosuite::SyscallInterface {
MOCK_METHOD(int, FTruncate, (int fd, int64_t length), (override));
MOCK_METHOD(int, FStat, (int filedes, struct stat64* buf), (override));
MOCK_METHOD(int, FSync, (int fd), (override));
+ MOCK_METHOD(pid_t, GetTid, (), (override));
MOCK_METHOD(int, Open, (const std::string& path_name, int flags, int mode), (override));
MOCK_METHOD(DIR*, OpenDir, (const std::string& name), (override));
MOCK_METHOD(int64_t, Read, (int fd, char* buf, int64_t count, int64_t offset), (override));
+ MOCK_METHOD(int, SchedSetattr,
+ (pid_t pid, const dittosuite::SchedAttr__& attr, unsigned int flags), (override));
MOCK_METHOD(struct dirent*, ReadDir, (DIR * dirp), (override));
MOCK_METHOD(int64_t, ReadLink, (const std::string& path_name, char* buf, int64_t bufsiz),
(override));