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

Mixer worker threads do not have rtprio on Linux #5065

Open
JohannesLorenz opened this issue Jun 29, 2019 · 10 comments
Open

Mixer worker threads do not have rtprio on Linux #5065

JohannesLorenz opened this issue Jun 29, 2019 · 10 comments

Comments

@JohannesLorenz
Copy link
Contributor

Calling

$ ps -o cmd,pid,pri,ni,rtprio -T $(pidof lmms)   # note: -T lists the threads
CMD                           PID PRI  NI RTPRIO
install/bin/lmms             8895  90   -     50
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   -      0
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   0      -
install/bin/lmms             8895  19   0      -

shows that currently only 1 thread (well, 2, for some reasons, but I run this on an >=4 core) has rtprio (realtime priority). Our worker threads do not have rtprio.

The desired behavior is that all worker threads have rtprio. E.g. if you add something like

if (jack_client_create_thread (client, &tid,
        jack_client_real_time_priority (client),
        jack_is_realtime (client),
        &worker_thread, NULL))
{
        fprintf (stderr, "failed to create realtime worker thread\n");
}

to the simple client example, you can see two rtprio threads in the ps output. Many thanks to @rgareus who found this solution.

This should be fixed for jack, and also for ALSA (in the way jack does it when it uses ALSA as backend).

Background:
Realtime priority lets the kernel schedule threads before any non realtime priority threads. If a thread that shall process audio does not have this priority, it may get interrupted ("preemption") by another thread, which can cause lag when you play music while your CPU is busy.

@PhysSong PhysSong changed the title Mixer worker threads do not have rtprio Mixer worker threads do not have rtprio on Linux Jul 15, 2019
@PhysSong
Copy link
Member

It seems like we have a relevant code in main.cpp, but that's for the main thread. We may do something similar for mixer worker threads.
FYI, you can get the limit of the RT priority by calling getrlimit with RLIMIT_RTPRIO, or from the Bash builtin ulimit -r. In my case, the default value was 95. It's also possible to use a binary search instead.

Related: QTBUG-3879

@PhysSong
Copy link
Member

PhysSong commented Jul 15, 2019

The Qt bug says Mac is not affected, but I can't confirm it. Windows have a different scheduling system, but I can't find bugs on Windows.

@x42
Copy link

x42 commented Jul 19, 2019

MacOS/X mach kernel offers time-constraint scheduling. pthread_setschedparam isn't relevant for audio I/O. It's relatively straight forward to set a pthread's priority though:

thread_policy_set (pthread_mach_thread_np (thread_id), THREAD_TIME_CONSTRAINT_POLICY,...)

However if you use JACK, you should really use the libjack API to create helper threads.

@PhysSong
Copy link
Member

@x42 Thanks for the information. We may try something similar to what JACK does.

@PhysSong
Copy link
Member

This should be fixed for jack, and also for ALSA (in the way jack does it when it uses ALSA as backend).

However if you use JACK, you should really use the libjack API to create helper threads.

I strongly think that the fix should be independent of backends. I believe many users are using backends rather than JACK, and we should cover those cases.

@x42
Copy link

x42 commented Jul 28, 2019

Keep in mind that if you use JACK, your application's internal helper rt-threads should not exceed the priority of jack's main process callback.

The straight forward solution is to have each backend provide the appropriate thread-start method. I don't think you can find a common, backend independent cross-platform solution for this.

@PhysSong
Copy link
Member

I don't think you can find a common, backend independent cross-platform solution for this.

Do you have any reasons? No one can know if it's possible before really trying to implement it.

@x42
Copy link

x42 commented Jul 28, 2019 via email

@PhysSong
Copy link
Member

https://github.com/Ardour/ardour/tree/master/libs/backends might come handy for inspiration.

Thank you for the references(https://github.com/Ardour/ardour/blob/master/libs/pbd/pthread_utils.cc as well). LMMS may need slightly different implementations, but that will definitely help. 👍

@JohannesLorenz
Copy link
Contributor Author

The straight forward solution is to have each backend provide the appropriate thread-start method.

That's also what I would have proposed. E.g. AudioAlsa::startThread, AudioJack::startThread ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

3 participants