From b1e46c32db520df88e32339a4afad6b593f064c3 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Mon, 13 Jan 2020 00:33:39 +0300 Subject: [PATCH] test: put thread into real time scheduling class on osx for better latencies --- CMakeLists.txt | 1 + configure.ac | 1 + event-config.h.cmake | 3 +++ test/regress_main.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 53d9c3d4..e33d132c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -441,6 +441,7 @@ endif() CHECK_INCLUDE_FILE(sys/uio.h EVENT__HAVE_SYS_UIO_H) CHECK_INCLUDE_FILES("sys/types.h;ifaddrs.h" EVENT__HAVE_IFADDRS_H) CHECK_INCLUDE_FILE(mach/mach_time.h EVENT__HAVE_MACH_MACH_TIME_H) +CHECK_INCLUDE_FILE(mach/mach.h EVENT__HAVE_MACH_MACH_H) CHECK_INCLUDE_FILE(netinet/tcp.h EVENT__HAVE_NETINET_TCP_H) CHECK_INCLUDE_FILE(sys/wait.h EVENT__HAVE_SYS_WAIT_H) CHECK_INCLUDE_FILE(sys/resource.h EVENT__HAVE_SYS_RESOURCE_H) diff --git a/configure.ac b/configure.ac index 7fa285dc..31a6614d 100644 --- a/configure.ac +++ b/configure.ac @@ -222,6 +222,7 @@ AC_CHECK_HEADERS([ \ fcntl.h \ ifaddrs.h \ mach/mach_time.h \ + mach/mach.h \ netdb.h \ netinet/in.h \ netinet/in6.h \ diff --git a/event-config.h.cmake b/event-config.h.cmake index 216e7f5c..bbcdbab6 100644 --- a/event-config.h.cmake +++ b/event-config.h.cmake @@ -178,6 +178,9 @@ /* Define to 1 if you have the header file. */ #cmakedefine EVENT__HAVE_MACH_MACH_TIME_H 1 +/* Define to 1 if you have the header file. */ +#cmakedefine EVENT__HAVE_MACH_MACH_H 1 + /* Define to 1 if you have the header file. */ #cmakedefine EVENT__HAVE_MEMORY_H 1 diff --git a/test/regress_main.c b/test/regress_main.c index 1b6ede7e..1bf3efc2 100644 --- a/test/regress_main.c +++ b/test/regress_main.c @@ -186,6 +186,45 @@ ignore_log_cb(int s, const char *msg) { } +#if defined(__APPLE__) + +#ifdef EVENT__HAVE_MACH_MACH_H +#include +#endif +#ifdef EVENT__HAVE_MACH_MACH_TIME_H +#include +#endif +#include + +/** + * Put into the real time scheduling class for better timers latency. + * https://developer.apple.com/library/archive/technotes/tn2169/_index.html#//apple_ref/doc/uid/DTS40013172-CH1-TNTAG6000 + */ +void move_pthread_to_realtime_scheduling_class(pthread_t pthread) +{ + mach_timebase_info_data_t timebase_info; + mach_timebase_info(&timebase_info); + + const uint64_t NANOS_PER_MSEC = 1000000ULL; + double clock2abs = ((double)timebase_info.denom / (double)timebase_info.numer) * NANOS_PER_MSEC; + + thread_time_constraint_policy_data_t policy; + policy.period = 0; + policy.computation = (uint32_t)(5 * clock2abs); // 5 ms of work + policy.constraint = (uint32_t)(10 * clock2abs); + policy.preemptible = FALSE; + + int kr = thread_policy_set(pthread_mach_thread_np(pthread_self()), + THREAD_TIME_CONSTRAINT_POLICY, + (thread_policy_t)&policy, + THREAD_TIME_CONSTRAINT_POLICY_COUNT); + if (kr != KERN_SUCCESS) { + mach_error("thread_policy_set:", kr); + exit(1); + } +} +#endif + void * basic_test_setup(const struct testcase_t *testcase) { @@ -199,6 +238,10 @@ basic_test_setup(const struct testcase_t *testcase) evthread_flags |= EVTHREAD_PTHREAD_PRIO_INHERIT; #endif +#if defined(__APPLE__) + move_pthread_to_realtime_scheduling_class(pthread_self()); +#endif + #ifndef _WIN32 if (testcase->flags & TT_ENABLE_IOCP_FLAG) return (void*)TT_SKIP; -- 2.40.0