5

Ubuntu has ionice, but as far as I can tell, it does absolutely nothing.

I suspect this is because Ubuntu replaced cfq with deadline and deadline doesn't support priorities.

Is there any possible way to have prioritized I/O on Ubuntu anymore?

EDIT: The context is that I have a database restore that easily consumes all my I/O and renders my system unusable until it has finished. I'd like it to remain usable for other tasks.

Paul Draper
  • 240
  • 3
  • 16
  • 1
    What happens if you renice the database restore process? Will it still consume all you I/O? – sudodus Jan 30 '20 at 20:43
  • Did you try ionice -c idle myprocess? Could you describe the steps and your hadware (inxi -SMD -! 31 -y 80)? iotop while running? – Pablo Bianchi Feb 06 '20 at 05:17
  • you can change the scheduler with sudo echo cfq > /sys/block/<blockdevice>/queue/scheduler for an immediate change or for a more permanent one, add elevator=cfq to your grub cmd_line. AFAIK cfq is the only scheduler that supports io priorities. – Robert Riedl Feb 06 '20 at 11:01

2 Answers2

3

This answer might be helpfull for Ubuntu 20.04, which lacks the cfq scheduler: https://askubuntu.com/a/1350605

From that answer:

Ubuntu 20.04 is using Kernel 5.4 (with HWE: 5.8) with only the mq-deadline scheduler compiled in.

You can check it by viewing /sys/block/sda/queue/scheduler. The active scheduler has square brackets around. If there are other schedulers compiled in the kernel they are also shown.

Example:

# cat /sys/block/sda/queue/scheduler
[mq-deadline] none

uname -r

5.4.0-26-generic

The mq-deadline scheduler does not support the mechanism used by ionice at the moment, see: https://unix.stackexchange.com/a/160081/27458

Solution: Switch to BFQ scheduler

The bfq scheduler does not need to be compiled in the kernel, it can be loaded afterwards using a kernel module.

Switch to the BFQ scheduler:

# modprobe  "bfq"
# echo "bfq" > /sys/block/sda/queue/scheduler
# echo "bfq" > /etc/modules-load.d/bfq.conf
# echo 'ACTION=="add|change", KERNEL=="sd*[!0-9]|sr*", ATTR{queue/scheduler}="bfq"' > /etc/udev/rules.d/60-scheduler.rules

Check:

# cat /sys/block/sda/queue/scheduler
mq-deadline [bfq] none

It is probably a good idea to also do a reboot and check again.

BFQ not available on "virtual" kernel

If you are using a "virtual" kernel, you will probably not have the bfq kernel module available because it does not include the linux-modules-extra-5.xxx package.

You can solve this by switching to the "generic-HWE" kernel:

# sudo apt-get install linux-generic-hwe-20.04  linux-tools-generic-hwe-20.04
# reboot

After reboot you should be on kernel 5.8.0-xxx-generic. You can check this:

# uname -r
5.8.0-59-generic

Now you can apply the above solution.

Alternative solution: Systemd scope

If you don't want to switch the IO scheduler, you can use a Systemd scope with a lower IO weight.

Create a file /usr/local/bin/mh_ionice with contents:

#!/bin/bash
if (( EUID == 0 )); then USERMODE=''; else USERMODE='--user'; fi
systemd-run \
  --collect \
  --quiet \
  --scope \
  $USERMODE \
  --nice=19 \
  --property="IOAccounting=yes" \
  --property="IOWeight=1" \
  "$@"

Make it executable:

chmod 755 /usr/local/bin/mh_ionice

Now you can run:

mh_ionice  [heavy_command] [arg] [arg] [arg]
Airstriker
  • 131
  • 4
1

You have to change your scheduler from deadlineto cfq.

You can do that per blockdevice and non permanent via

sudo echo cfq > /sys/block/<blockdevice>/queue

Or for the whole system, permanent via grub, with adding the elevator=cfqparameter to the grub commandline options.

This is an excellent post explaining how you can do that.

What the drawbacks of such a change are is better covered elsewhere.

The TL;DR is

According to the test results, each scheduler has different advantages over others. CFQ scheduler is suitable for the systems that require balanced I/O access and do not need process prioritization. Deadline scheduler has better performance on read-intensive works. Noop is for the systems on the cloud or hypervisors. BFQ performs better on interactive use-case scenarios. Noop is the simplest scheduler and it is considered to have the potential for optimized new implementations targeting SSD block devices.

Robert Riedl
  • 4,351