Enabling memlock for rtpmidi on Ubuntu 20.10

  • by

In version 0.5.2 of McLaren Labs’ rtpmidi, we made the use of locked memory the default. Locked memory dedicates fixed RAM to the rtpmidi process, and prevents it from being swapped to disk. The use of locked memory (through the mlockall() function call) can be a benefit to realtime applications like MIDI and Audio, but its use can be to the detriment of other processes. So it should be used carefully.

On Ubuntu 18.04 and 20.04 enabling memlock as the default option seemed benign enough. On Ubuntu 20.10, however, this default caused problems. With memlock enabled, the process printed strange errors and aborted. Example:

***MEMORY-ERROR***: rtpmidi[2023]: GSlice: failed to allocate 8176 bytes (alignment: 8192): Cannot allocate memory

Ubuntu 20.10 (Groovy Gorilla) accounts for locked memory pages differently, and even though the memory limit is higher, with all things accounted for, it is not enough.

Checking the Limit

In BASH, you can use the ulimit command with the -a option to list all of the limits.

# Ubuntu 20.10 default
$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15499
max locked memory       (kbytes, -l) 503374
...

# Ubuntu 20.04 default
$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15499
max locked memory       (kbytes, -l) 65536

As you can see, the max-locked-memory limit is different in Ubuntu 20.04 and 20.10. The Ubuntu 20.10 team raised the default limit. We suspect this is because the newer kernel accounts for locked pages using a different algorithm, but we cannot confirm this presently.

But it is true that the two Operating System releases are using different kernels.

Ubuntu 20.04 - Kernel 5.4
Ubuntu 20.10 - Kernel 5.8

Changing the default limit

User limits are set at boot time and are specified in the file shown below. (Read this excellent article: https://stackoverflow.com/questions/46978524/mlockall-cannot-allocate-memory.)

/etc/security/limits.conf

To raise the default limit, add a new line to the end of the file for the user ID that should get the raised limit. Example:

MYUSERNAME - memlock 1000000

Then reboot. Re-run the 'ulimit -a command and you should see the raised limit.

Running rtpmidi with memlock

The -M option is provided to enable memlock. Enable locked pages like this:

$ /opt/rtpmid_0.6.1_xxx/bin/rtpmidi gui -M 1 ...

or disable it like this:

$ /opt/rtpmid_0.6.1_xxx/bin/rtpmidi gui -M 0 ...

Conclusion

McLaren Labs tries to distrbute its products with sensible defaults that “just work” but with the knobs available to optimize performance for users who are up to the task. This article described how to adjust the Linux memlock limit for one particular user, and described how to invoke rtpmidi with the memlock option.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.