]> granicus.if.org Git - libevent/commitdiff
test: put thread into real time scheduling class on osx for better latencies
authorAzat Khuzhin <azat@libevent.org>
Sun, 12 Jan 2020 21:33:39 +0000 (00:33 +0300)
committerAzat Khuzhin <azat@libevent.org>
Mon, 13 Jan 2020 20:36:19 +0000 (23:36 +0300)
CMakeLists.txt
configure.ac
event-config.h.cmake
test/regress_main.c

index 53d9c3d41baaff00358ee388be1ffd77514e6666..e33d132cb33b3513680c962880345e4d7f19351f 100644 (file)
@@ -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)
index 7fa285dcc609e92ebf12571df53ab9511f9a67e1..31a6614da435b42856860729a977fce39b70cbc9 100644 (file)
@@ -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 \
index 216e7f5cadc2273226e13f6f67e9097f7f3607a7..bbcdbab62f32088d45b0c0682635ea8b44800bbf 100644 (file)
 /* Define to 1 if you have the <mach/mach_time.h> header file. */
 #cmakedefine EVENT__HAVE_MACH_MACH_TIME_H 1
 
+/* Define to 1 if you have the <mach/mach.h> header file. */
+#cmakedefine EVENT__HAVE_MACH_MACH_H 1
+
 /* Define to 1 if you have the <memory.h> header file. */
 #cmakedefine EVENT__HAVE_MEMORY_H 1
 
index 1b6ede7ec367f2a8b55ed1b5e7c363133e12aac8..1bf3efc2231746c3511e15835701f522322f5a84 100644 (file)
@@ -186,6 +186,45 @@ ignore_log_cb(int s, const char *msg)
 {
 }
 
+#if defined(__APPLE__)
+
+#ifdef EVENT__HAVE_MACH_MACH_H
+#include <mach/mach.h>
+#endif
+#ifdef EVENT__HAVE_MACH_MACH_TIME_H
+#include <mach/mach_time.h>
+#endif
+#include <pthread.h>
+
+/**
+ * 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;