Cont'd work to improve macOS rt priority

This commit is contained in:
Robin Gareus 2021-05-02 20:24:10 +02:00
parent e82b2b48fb
commit 438b1e5eab
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
4 changed files with 54 additions and 19 deletions

View file

@ -339,40 +339,75 @@ pbd_set_thread_priority (pthread_t thread, const int policy, int priority)
}
bool
pbd_mach_set_realtime_policy (pthread_t thread_id, double period_ns)
pbd_mach_set_realtime_policy (pthread_t thread_id, double period_ns, bool main)
{
#ifdef __APPLE__
/* https://opensource.apple.com/source/xnu/xnu-4570.61.1/osfmk/mach/thread_policy.h.auto.html
* https://opensource.apple.com/source/xnu/xnu-4570.61.1/:sposfmk/kern/sched.h.auto.html
*/
kern_return_t res;
/* Ask for fixed priority */
thread_extended_policy_data_t tep;
tep.timeshare = false;
res = thread_policy_set (pthread_mach_thread_np (thread_id),
THREAD_EXTENDED_POLICY,
(thread_policy_t)&tep,
THREAD_EXTENDED_POLICY_COUNT);
#ifndef NDEBUG
printf ("Mach Thread(%p) set timeshare: %d OK: %d\n", thread_id, tep.timeshare, res == KERN_SUCCESS);
#endif
/* relative value of the computation compared to the other threads in the task. */
thread_precedence_policy_data_t tpp;
tpp.importance = main ? 63 : 62; // MAXPRI_USER = 63
res = thread_policy_set (pthread_mach_thread_np (thread_id),
THREAD_PRECEDENCE_POLICY,
(thread_policy_t)&tpp,
THREAD_PRECEDENCE_POLICY_COUNT);
#ifndef NDEBUG
printf ("Mach Thread(%p) set precedence: %d OK: %d\n", thread_id, tpp.importance, res == KERN_SUCCESS);
#endif
/* Realtime constraints */
double ticks_per_ns = 1.;
mach_timebase_info_data_t timebase;
if (KERN_SUCCESS == mach_timebase_info (&timebase)) {
ticks_per_ns = timebase.denom / timebase.numer;
}
thread_time_constraint_policy_data_t policy;
thread_time_constraint_policy_data_t tcp;
#ifndef NDEBUG
mach_msg_type_number_t msgt = 4;
boolean_t dflt = false;
kern_return_t rv = thread_policy_get (pthread_mach_thread_np (thread_id),
THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&policy, &msgt, &dflt);
THREAD_TIME_CONSTRAINT_POLICY,
(thread_policy_t)&tcp,
&msgt, &dflt);
printf ("Mach Thread(%p) get: period=%d comp=%d constraint=%d preemt=%d OK: %d\n", thread_id, policy.period, policy.computation, policy.constraint, policy.preemptible, rv == KERN_SUCCESS);
printf ("Mach Thread(%p) get: period=%d comp=%d constraint=%d preemt=%d OK: %d\n", thread_id, tcp.period, tcp.computation, tcp.constraint, tcp.preemptible, rv == KERN_SUCCESS);
#endif
mach_timebase_info_data_t timebase_info;
mach_timebase_info (&timebase_info);
const double period_clk = period_ns * (double)timebase_info.denom / (double)timebase_info.numer;
policy.period = ticks_per_ns * period_clk;
policy.computation = ticks_per_ns * period_clk * .9;
policy.constraint = ticks_per_ns * period_clk * .95;
policy.preemptible = true;
kern_return_t res = thread_policy_set (pthread_mach_thread_np (thread_id),
THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&policy,
THREAD_TIME_CONSTRAINT_POLICY_COUNT);
tcp.period = ticks_per_ns * period_clk;
tcp.computation = ticks_per_ns * period_clk * .9;
tcp.constraint = ticks_per_ns * period_clk * .95;
tcp.preemptible = true;
res = thread_policy_set (pthread_mach_thread_np (thread_id),
THREAD_TIME_CONSTRAINT_POLICY,
(thread_policy_t)&tcp,
THREAD_TIME_CONSTRAINT_POLICY_COUNT);
#ifndef NDEBUG
printf ("Mach Thread(%p) set: period=%d comp=%d constraint=%d preemt=%d OK: %d\n", thread_id, policy.period, policy.computation, policy.constraint, policy.preemptible, res == KERN_SUCCESS);
printf ("Mach Thread(%p) set: period=%d comp=%d constraint=%d preemt=%d OK: %d\n", thread_id, tcp.period, tcp.computation, tcp.constraint, tcp.preemptible, res == KERN_SUCCESS);
#endif
return res != KERN_SUCCESS;
#endif
return false; // OK