[chrony-dev] [PATCH] MacOS X - Increase priority of chronyd process

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


Run chronyd with a 'nice' value of -15. Also run chronyd as a near real time process to prevent pre-empting of timing calculations and adjustments. This patch has the effect of removing an observed 1usec bias from the mean 'System time' offset.
---
 .../org.tuxfamily.chronyd.plist                    |  2 +
 sys_macosx.c                                       | 45 ++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/contrib/bryan_christianson_1/org.tuxfamily.chronyd.plist b/contrib/bryan_christianson_1/org.tuxfamily.chronyd.plist
index 2bf42aa..58132fa 100644
--- a/contrib/bryan_christianson_1/org.tuxfamily.chronyd.plist
+++ b/contrib/bryan_christianson_1/org.tuxfamily.chronyd.plist
@@ -13,6 +13,8 @@
 		<string>-f</string>
 		<string>/private/etc/chrony.d/chrony.conf</string>
 	</array>
+	<key>Nice</key>
+	<integer>-15</integer>
 	<key>KeepAlive</key>
 	<true/>
 </dict>
diff --git a/sys_macosx.c b/sys_macosx.c
index 33da60d..f76f80c 100644
--- a/sys_macosx.c
+++ b/sys_macosx.c
@@ -42,6 +42,10 @@
 #include <stdio.h>
 #include <signal.h>
 
+#include <mach/mach.h>
+#include <mach/mach_time.h>
+#include <pthread.h>
+
 #include "sys_macosx.h"
 #include "localp.h"
 #include "sched.h"
@@ -85,6 +89,8 @@ static struct timeval Tdrift;
 /* minimum resolution of current_frequency */
 #define FREQUENCY_RES (1.0e-9)
 
+#define NANOS_PER_MSEC (1000000ULL)
+
 /* ================================================== */
 
 static void
@@ -335,6 +341,43 @@ drift_removal_timeout(SCH_ArbitraryArgument not_used)
 }
 
 /* ================================================== */
+/*
+  Give chronyd real time priority so that time critical calculations
+  are not pre-empted by the kernel.
+*/
+
+static int
+set_realtime(void)
+{
+  /* https://developer.apple.com/library/ios/technotes/tn2169/_index.html */
+
+  mach_timebase_info_data_t timebase_info;
+  double clock2abs;
+  thread_time_constraint_policy_data_t policy;
+  int kr;
+
+  mach_timebase_info(&timebase_info);
+  clock2abs = ((double)timebase_info.denom / (double)timebase_info.numer) * NANOS_PER_MSEC;
+
+  policy.period = 0;
+  policy.computation = (uint32_t)(5 * clock2abs); /* 5 ms of work */
+  policy.constraint = (uint32_t)(10 * clock2abs);
+  policy.preemptible = 0;
+
+  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) {
+    LOG(LOGS_WARN, LOGF_SysMacOSX, "Cannot set real-time priority: %d", kr);
+    return -1;
+  }
+  return 0;
+}
+
+/* ================================================== */
 
 void
 SYS_MacOSX_Initialise(void)
@@ -350,6 +393,8 @@ SYS_MacOSX_Initialise(void)
 
   drift_removal_id = SCH_AddTimeoutByDelay(drift_removal_interval, drift_removal_timeout, NULL);
   drift_removal_running = 1;
+
+  set_realtime();
 }
 
 /* ================================================== */
-- 
2.3.2 (Apple Git-55)


-- 
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/