Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Linux timers for sleeps up to 1ms #6697

Merged
merged 2 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#include <sys/resource.h>
#include <time.h>
#endif
#ifdef __linux__
#include <sys/timerfd.h>
#endif

#include "sync.h"
#include "Log.h"
Expand Down Expand Up @@ -1719,6 +1722,18 @@ void thread_base::initialize(bool(*wait_cb)(const void*))
#elif !defined(_WIN32)
pthread_setname_np(pthread_self(), m_name.get().substr(0, 15).c_str());
#endif

#ifdef __linux__
m_timer = timerfd_create(CLOCK_MONOTONIC, 0);
if (m_timer != -1)
{
LOG_SUCCESS(GENERAL, "allocated high precision Linux timer");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove log on success

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied

}
else
{
LOG_ERROR(GENERAL, "Linux timer allocation failed, use wait_unlock() only");
}
#endif
}

void thread_base::notify_abort() noexcept
Expand All @@ -1734,6 +1749,13 @@ bool thread_base::finalize(int) noexcept
// Report pending errors
error_code::error_report(0, 0, 0, 0);

#ifdef __linux__
if (m_timer != -1)
{
close(m_timer);
}
#endif

#ifdef _WIN32
ULONG64 cycles{};
QueryThreadCycleTime(GetCurrentThread(), &cycles);
Expand Down Expand Up @@ -1781,6 +1803,23 @@ void thread_ctrl::_wait_for(u64 usec, bool alert /* true */)
{
auto _this = g_tls_this_thread;

#ifdef __linux__
if (!alert && _this->m_timer != -1 && usec > 0 && usec <= 1000)
{
struct itimerspec timeout;
u64 missed;
u64 nsec = usec * 1000ull;

timeout.it_value.tv_nsec = (nsec % 1000000000ull);
timeout.it_value.tv_sec = nsec / 1000000000ull;
timeout.it_interval.tv_sec = 0;
timeout.it_interval.tv_nsec = 0;
timerfd_settime(_this->m_timer, 0, &timeout, NULL);
read(_this->m_timer, &missed, sizeof(missed));
return;
}
#endif

std::unique_lock lock(_this->m_mutex, std::defer_lock);

while (true)
Expand Down
5 changes: 5 additions & 0 deletions Utilities/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ class thread_base
using native_entry = void*(*)(void* arg);
#endif

#ifdef __linux__
// Linux thread timer
int m_timer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialize as -1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied.

#endif

// Thread handle (platform-specific)
atomic_t<std::uintptr_t> m_thread{0};

Expand Down
16 changes: 8 additions & 8 deletions rpcs3/Emu/Cell/lv2/sys_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,6 @@ struct lv2_obj
// Now scale the result
usec = (std::min<u64>(usec, max_usec) * g_cfg.core.clocks_scale) / 100;

#ifdef __linux__
// TODO: Confirm whether Apple or any BSD can benefit from this as well
constexpr u32 host_min_quantum = 50;
#else
// Host scheduler quantum for windows (worst case)
// NOTE: On ps3 this function has very high accuracy
constexpr u32 host_min_quantum = 500;
#endif
extern u64 get_system_time();

u64 passed = 0;
Expand All @@ -258,6 +250,14 @@ struct lv2_obj
while (usec >= passed)
{
remaining = usec - passed;
#ifdef __linux__
// NOTE: Assumption that timer initialization has succeeded
u64 host_min_quantum = is_usleep && remaining <= 1000 ? 16 : 50;
#else
// Host scheduler quantum for windows (worst case)
// NOTE: On ps3 this function has very high accuracy
constexpr u64 host_min_quantum = 500;
#endif

if (g_cfg.core.sleep_timers_accuracy < (is_usleep ? sleep_timers_accuracy_level::_usleep : sleep_timers_accuracy_level::_all_timers))
{
Expand Down