diff options
author | Alessio Balsini <balsini@google.com> | 2024-03-06 08:54:21 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2024-03-06 08:54:21 +0000 |
commit | f49a1949a48863babbba66081096fdc39d298502 (patch) | |
tree | 66f401df161b184ca978742c1d0f98c2e2561916 | |
parent | b59284705a0b581c1fc976e57fa4a4ff76a490f9 (diff) | |
parent | 040233a7d45d2f60f361042b01f7f8f1b4c57550 (diff) | |
download | dittosuite-f49a1949a48863babbba66081096fdc39d298502.tar.gz |
Merge changes Ibb906796,I2e980637,I05ca8ffb into main
* changes:
Update protobuf header include path
Move gettid and sched_setattr to Syscall interface
Move SchedAttr__ to syscall
-rw-r--r-- | BUILD.bazel | 5 | ||||
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | dittotrace.cpp | 4 | ||||
-rw-r--r-- | include/ditto/instruction_factory.h | 4 | ||||
-rw-r--r-- | include/ditto/multithreading_utils.h | 28 | ||||
-rw-r--r-- | include/ditto/parser.h | 4 | ||||
-rw-r--r-- | include/ditto/result.h | 4 | ||||
-rw-r--r-- | include/ditto/syscall.h | 21 | ||||
-rw-r--r-- | include/ditto/tracer.h | 4 | ||||
-rw-r--r-- | src/instruction_factory.cpp | 4 | ||||
-rw-r--r-- | src/multithreading_utils.cpp | 21 | ||||
-rw-r--r-- | src/syscall.cpp | 67 | ||||
-rw-r--r-- | test/include/mock_syscall.h | 6 |
13 files changed, 111 insertions, 62 deletions
diff --git a/BUILD.bazel b/BUILD.bazel index 016091a..cc96d1d 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -41,7 +41,10 @@ cc_library( hdrs = glob([ "include/ditto/*.h", ]), - includes = ["include"], + includes = [ + "include", + "schema", + ], deps = [ ":dittosuite_cc_proto", ], diff --git a/CMakeLists.txt b/CMakeLists.txt index a1196d9..12ed4ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ add_subdirectory(schema) include_directories(include) include_directories(${CMAKE_CURRENT_BINARY_DIR}) +include_directories(${CMAKE_BINARY_DIR}/schema) file(GLOB libditto_src src/*.cpp diff --git a/dittotrace.cpp b/dittotrace.cpp index 47a1bc2..39281f5 100644 --- a/dittotrace.cpp +++ b/dittotrace.cpp @@ -18,11 +18,7 @@ #include <string> #include <vector> -#ifdef __ANDROID__ #include <benchmark.pb.h> -#else -#include "schema/benchmark.pb.h" -#endif #include <google/protobuf/io/zero_copy_stream_impl.h> #include <google/protobuf/text_format.h> diff --git a/include/ditto/instruction_factory.h b/include/ditto/instruction_factory.h index 33fa75a..213eaa3 100644 --- a/include/ditto/instruction_factory.h +++ b/include/ditto/instruction_factory.h @@ -21,11 +21,7 @@ #include <ditto/instruction_set.h> #include <ditto/read_write_file.h> -#ifdef __ANDROID__ #include <benchmark.pb.h> -#else -#include "schema/benchmark.pb.h" -#endif namespace dittosuite { diff --git a/include/ditto/multithreading_utils.h b/include/ditto/multithreading_utils.h index b1ca472..0efce35 100644 --- a/include/ditto/multithreading_utils.h +++ b/include/ditto/multithreading_utils.h @@ -14,13 +14,10 @@ #pragma once -#ifdef __ANDROID__ #include <benchmark.pb.h> -#else -#include "schema/benchmark.pb.h" -#endif #include <ditto/logger.h> +#include <ditto/syscall.h> #include <sys/syscall.h> #include <unistd.h> @@ -37,28 +34,14 @@ enum SchedPolicy { SchedDeadline = 6, }; -struct SchedAttr__ { - uint32_t size; /* Size of this structure */ - uint32_t sched_policy; /* Policy (SCHED_*) */ - uint64_t sched_flags; /* Flags */ - - int32_t sched_nice; /* Nice value (SCHED_OTHER, - SCHED_BATCH) */ - uint32_t sched_priority; /* Static priority (SCHED_FIFO, - SCHED_RR) */ - /* Remaining fields are for SCHED_DEADLINE */ - uint64_t sched_runtime; - uint64_t sched_deadline; - uint64_t sched_period; -}; - -std::string to_string(const SchedAttr__& attr); - class SchedAttr { + SyscallInterface& syscall_; bool initialized_ = false; SchedAttr__ sched_attr_; public: + SchedAttr(SyscallInterface& syscall) : syscall_(syscall) {} + void Set() const; bool IsSet() const; @@ -66,10 +49,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/parser.h b/include/ditto/parser.h index 8c7a767..5d7c9b3 100644 --- a/include/ditto/parser.h +++ b/include/ditto/parser.h @@ -17,11 +17,7 @@ #include <string> #include <ditto/instruction_set.h> -#ifdef __ANDROID__ #include <benchmark.pb.h> -#else -#include "schema/benchmark.pb.h" -#endif namespace dittosuite { diff --git a/include/ditto/result.h b/include/ditto/result.h index 7f6f3d9..fbacab7 100644 --- a/include/ditto/result.h +++ b/include/ditto/result.h @@ -14,11 +14,7 @@ #pragma once -#ifdef __ANDROID__ #include <result.pb.h> -#else -#include "schema/result.pb.h" -#endif #include <time.h> diff --git a/include/ditto/syscall.h b/include/ditto/syscall.h index c7e3af8..6f90483 100644 --- a/include/ditto/syscall.h +++ b/include/ditto/syscall.h @@ -25,6 +25,23 @@ namespace dittosuite { +struct SchedAttr__ { + uint32_t size; /* Size of this structure */ + uint32_t sched_policy; /* Policy (SCHED_*) */ + uint64_t sched_flags; /* Flags */ + + int32_t sched_nice; /* Nice value (SCHED_OTHER, + SCHED_BATCH) */ + uint32_t sched_priority; /* Static priority (SCHED_FIFO, + SCHED_RR) */ + /* Remaining fields are for SCHED_DEADLINE */ + uint64_t sched_runtime; + uint64_t sched_deadline; + uint64_t sched_period; +}; + +std::string to_string(const SchedAttr__& attr); + class SyscallInterface { public: virtual ~SyscallInterface() {} @@ -37,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; @@ -62,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/include/ditto/tracer.h b/include/ditto/tracer.h index 0832950..c46b619 100644 --- a/include/ditto/tracer.h +++ b/include/ditto/tracer.h @@ -17,11 +17,7 @@ #include <memory> #include <fstream> -#ifdef __ANDROID__ #include <benchmark.pb.h> -#else -#include "schema/benchmark.pb.h" -#endif namespace dittosuite { 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 9b9716f..e1d6c67 100644 --- a/src/multithreading_utils.cpp +++ b/src/multithreading_utils.cpp @@ -19,21 +19,6 @@ namespace dittosuite { -std::string to_string(const SchedAttr__& attr) { - std::string ret; - - ret += "size: " + std::to_string(attr.size); - ret += ", policy: " + std::to_string(attr.sched_policy); - ret += ", flags: " + std::to_string(attr.sched_flags); - ret += ", nice: " + std::to_string(attr.sched_nice); - ret += ", priority: " + std::to_string(attr.sched_priority); - ret += ", runtime: " + std::to_string(attr.sched_runtime); - ret += ", deadline: " + std::to_string(attr.sched_deadline); - ret += ", period: " + std::to_string(attr.sched_period); - - return ret; -} - bool SchedAttr::IsSet() const { return initialized_; } void SchedAttr::Set() const { @@ -42,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"); } @@ -105,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 83e6cee..288e59b 100644 --- a/src/syscall.cpp +++ b/src/syscall.cpp @@ -12,6 +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 { @@ -53,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); } @@ -73,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(); } @@ -85,4 +143,13 @@ int64_t Syscall::Write(int fd, char* buf, int64_t count, int64_t offset) { return pwrite64(fd, buf, count, offset); } +std::string to_string(const SchedAttr__& attr) { + std::stringstream ss; + ss << "size: " << attr.size << ", policy: " << attr.sched_policy + << ", flags: " << attr.sched_flags << ", nice: " << attr.sched_nice + << ", priority: " << attr.sched_priority << ", runtime: " << attr.sched_runtime + << ", deadline: " << attr.sched_deadline << ", period: " << attr.sched_period; + return ss.str(); +} + } // namespace dittosuite 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)); |