Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ RejitWorkOffloader::RejitWorkOffloader(ICorProfilerInfo7* pInfo) :
m_offloader_queue_thread(std::make_unique<std::thread>([this]
{
Threads::SetNativeThreadName(WStr("DD_rejit"));
Threads::RaiseThreadPriorityAboveNormal();
EnqueueThreadLoop(this);
}))
{
Expand Down
45 changes: 45 additions & 0 deletions tracer/src/Datadog.Tracer.Native/threadUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@

#include "threadUtils.h"
#include "logger.h"

#ifdef __linux__
#include <sys/prctl.h>
#include <pthread.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/syscall.h>
#include <unistd.h>
#elif __APPLE__
#include <pthread.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <errno.h>
#elif _WIN32
#include <windows.h>
#endif

#ifdef _WIN32
Expand Down Expand Up @@ -73,3 +85,36 @@ bool Threads::SetNativeThreadName(const WCHAR* description)
return true;
#endif
}

void Threads::RaiseThreadPriorityAboveNormal()
{
#ifdef _WIN32
if (!::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL))
{
trace::Logger::Warn("SetThreadPriority(ABOVE_NORMAL) failed – GLE=%d", ::GetLastError());
}

#elif defined(__APPLE__)
// One level above default (“Utility”) but below UI-critical.
if (pthread_set_qos_class_self_np(QOS_CLASS_USER_INITIATED, 0) != 0)
{
trace::Logger::Warn("pthread_set_qos_class_self_np(USER_INITIATED) failed – errno=%d", errno);

// Fallback: modest nice() boost for the whole process.
if (setpriority(PRIO_PROCESS, 0, -5) != 0)
{
trace::Logger::Warn("setpriority(-5) fallback failed – errno=%d", errno);
}
}

#elif defined(__linux__)
// true per-thread tweak: target the kernel thread ID (TID)
const pid_t tid = static_cast<pid_t>(::syscall(SYS_gettid));
constexpr int kDelta = -5; // “above normal” without RT

if (::setpriority(PRIO_PROCESS, tid, kDelta) != 0)
{
trace::Logger::Warn("setpriority(TID=%d, %d) failed – errno=%d", tid, kDelta, errno);
}
#endif
}
1 change: 1 addition & 0 deletions tracer/src/Datadog.Tracer.Native/threadUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Threads
{
public:
static bool SetNativeThreadName(const WCHAR* description);
static void RaiseThreadPriorityAboveNormal();

private:
#ifdef _WIN32
Expand Down
Loading