[hal] Set HAL Notifier thread as RT by default (#3482)

This PR gives the Notifier HAL thread RT priority 40 in RobotBase after
HAL initialization and before the user code is run. This drastically
improves scheduling jitter for TimedRobot's AddPeriodic() functions (in
3512's experience).

It's too risky to set user code as RT because badly behaved code
will lock up the Rio (potentially requiring safe mode to recover).

This needs the user program to be setuid admin to succeed.
This commit is contained in:
Tyler Veness
2021-08-14 11:42:35 -07:00
committed by GitHub
parent 192d251ee8
commit 5d9ae3cdb4
8 changed files with 182 additions and 14 deletions

View File

@@ -6,9 +6,42 @@
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
#include <cerrno>
#include <cstdlib>
#include <system_error>
#include <fmt/format.h>
#include "hal/Errors.h"
namespace {
class UidSetter {
public:
explicit UidSetter(uid_t uid) {
m_uid = geteuid();
if (uid == 0 && setuid(uid) == -1) {
throw std::system_error(errno, std::generic_category(),
fmt::format("setuid({}) failed", uid));
} else if (uid != 0 && seteuid(uid) == -1) {
throw std::system_error(errno, std::generic_category(),
fmt::format("seteuid({}) failed", uid));
}
}
~UidSetter() noexcept(false) {
if (seteuid(m_uid) == -1) {
throw std::system_error(errno, std::generic_category(),
fmt::format("seteuid({}) failed", m_uid));
}
}
private:
uid_t m_uid;
};
} // namespace
namespace hal::init {
void InitializeThreads() {}
} // namespace hal::init
@@ -70,13 +103,21 @@ HAL_Bool HAL_SetThreadPriority(NativeThreadHandle handle, HAL_Bool realTime,
// Only need to set 0 priority for non RT thread
sch.sched_priority = 0;
}
if (pthread_setschedparam(*reinterpret_cast<const pthread_t*>(handle),
scheduler, &sch)) {
*status = HAL_THREAD_PRIORITY_ERROR;
try {
UidSetter uidSetter{0};
if (pthread_setschedparam(*reinterpret_cast<const pthread_t*>(handle),
scheduler, &sch)) {
*status = HAL_THREAD_PRIORITY_ERROR;
return false;
} else {
*status = 0;
return true;
}
} catch (const std::system_error& e) {
*status = HAL_SETUID_ERROR;
return false;
} else {
*status = 0;
return true;
}
}

View File

@@ -26,7 +26,7 @@ Java_edu_wpi_first_hal_ThreadsJNI_getCurrentThreadPriority
HAL_Bool isRT = false;
auto ret = HAL_GetCurrentThreadPriority(&isRT, &status);
CheckStatus(env, status);
return (jint)ret;
return static_cast<jint>(ret);
}
/*
@@ -42,7 +42,7 @@ Java_edu_wpi_first_hal_ThreadsJNI_getCurrentThreadIsRealTime
HAL_Bool isRT = false;
HAL_GetCurrentThreadPriority(&isRT, &status);
CheckStatus(env, status);
return (jboolean)isRT;
return static_cast<jboolean>(isRT);
}
/*
@@ -56,9 +56,9 @@ Java_edu_wpi_first_hal_ThreadsJNI_setCurrentThreadPriority
{
int32_t status = 0;
auto ret = HAL_SetCurrentThreadPriority(
(HAL_Bool)realTime, static_cast<int32_t>(priority), &status);
CheckStatus(env, status);
return (jboolean)ret;
static_cast<HAL_Bool>(realTime), static_cast<int32_t>(priority), &status);
CheckStatus(env, status, false);
return static_cast<jboolean>(ret);
}
} // extern "C"

View File

@@ -135,6 +135,10 @@
#define HAL_USE_LAST_ERROR_MESSAGE \
"HAL: Use HAL_GetLastError(status) to get last error"
#define HAL_SETUID_ERROR -1157
#define HAL_SETUID_ERROR_MESSAGE \
"HAL: Setting the effective user ID has failed";
#define HAL_CAN_BUFFER_OVERRUN -35007
#define HAL_CAN_BUFFER_OVERRUN_MESSAGE \
"HAL: CAN Output Buffer Full. Ensure a device is attached"