From bbda6e1262f8cf6ff62a240dd80a34a043db1ec5 Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Tue, 1 Oct 2019 06:55:50 -0500 Subject: [PATCH] add interface for scheduling priority --- README.md | 12 ++++++++++++ examples/CMakeLists.txt | 3 +++ examples/high_priority.cpp | 12 ++++++++++++ include/perfect/detail/os/linux.hpp | 13 +++++++++++++ include/perfect/priority.hpp | 15 +++++++++++++++ tools/perfect.cpp | 11 ++++++++++- 6 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 examples/high_priority.cpp create mode 100644 include/perfect/priority.hpp diff --git a/README.md b/README.md index 2d060db..d77c922 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,18 @@ perfect::CpuTurboState state; PERFECT(perfect::get_cpu_turbo_state(&state)); ``` +### High Priority + +`perfect` can set high scheduling priority for a process + +See [examples/high_priority.cpp](examples/high_priority.cpp) + +```c++ +#include "perfect/priority.hpp" +``` + +* `Result set_high_priority()`: set the highest possible scheduling priority for the calling process + ### Monitoring `perfect` can monitor and record GPU activity. diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4f77d77..ebd23b6 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -43,6 +43,9 @@ target_link_libraries(cpu-turbo perfect) add_executable(os-perf os_perf.cpp) target_link_libraries(os-perf perfect) +add_executable(high-priority high_priority.cpp) +target_link_libraries(high-priority perfect) + if(CMAKE_CUDA_COMPILER) add_executable(gpu-clocks gpu_clocks.cu) target_link_libraries(gpu-clocks perfect) diff --git a/examples/high_priority.cpp b/examples/high_priority.cpp new file mode 100644 index 0000000..6204cd9 --- /dev/null +++ b/examples/high_priority.cpp @@ -0,0 +1,12 @@ +#include + +#include "perfect/priority.hpp" + +int main(void) { + perfect::init(); + + PERFECT(perfect::set_high_priority()); + + // do things with high process scheduling priority + +} \ No newline at end of file diff --git a/include/perfect/detail/os/linux.hpp b/include/perfect/detail/os/linux.hpp index 2b6f0b4..7b4e3a9 100644 --- a/include/perfect/detail/os/linux.hpp +++ b/include/perfect/detail/os/linux.hpp @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include "perfect/result.hpp" @@ -107,6 +109,17 @@ Result set_personality(const int persona) { } return Result::SUCCESS; } + +// give the calling process the highest priority +Result set_high_priority() { + if (setpriority(PRIO_PROCESS, 0, -20)) { + return from_errno(errno); + } + return Result::SUCCESS; +} + + + } } // namespace perfect \ No newline at end of file diff --git a/include/perfect/priority.hpp b/include/perfect/priority.hpp new file mode 100644 index 0000000..358c4ce --- /dev/null +++ b/include/perfect/priority.hpp @@ -0,0 +1,15 @@ +#pragma once + +#ifdef __linux__ +#include "detail/os/linux.hpp" +#else +#error "unsupported platform" +#endif + +#include "init.hpp" + +namespace perfect { + Result set_high_priority() { + return detail::set_high_priority(); + } +} \ No newline at end of file diff --git a/tools/perfect.cpp b/tools/perfect.cpp index 67841de..ee94b81 100644 --- a/tools/perfect.cpp +++ b/tools/perfect.cpp @@ -26,6 +26,7 @@ #include "perfect/detail/os/linux.hpp" #include "perfect/drop_caches.hpp" #include "perfect/os_perf.hpp" +#include "perfect/priority.hpp" // argv should be null-terminated // outf and errf are file descriptors to where stdout and stderr should be @@ -152,6 +153,7 @@ int main(int argc, char **argv) { nonstd::optional cpuTurbo = false; nonstd::optional maxOsPerf = true; bool dropCaches = true; + bool highPriority = true; std::vector program; std::string stdoutPath; @@ -173,7 +175,8 @@ int main(int argc, char **argv) { .set(aslr, true) .call([&]() { cpuTurbo = nonstd::nullopt; }) .call([&]() { maxOsPerf = nonstd::nullopt; }) - .set(dropCaches, false)); + .set(dropCaches, false) + .set(highPriority, false)); auto modMode = (shieldGroup, option("--no-drop-cache") @@ -183,6 +186,7 @@ int main(int argc, char **argv) { maxOsPerf = false; }), option("--aslr").set(aslr, true).doc("enable ASLR"), + option("--no-priority").set(highPriority, false).doc("don't set high priority"), option("--cpu-turbo").doc("enable CPU turbo").call([&]() { cpuTurbo = true; }), @@ -340,6 +344,11 @@ int main(int argc, char **argv) { } } + if (highPriority) { + std::cerr << "set high priority\n"; + PERFECT(perfect::set_high_priority()); + } + // parent should return for (int runIter = 0; runIter < iters; ++runIter) {