# Disk Performance

====================================================================================

### I/O Schedulers

In Linux, the I/O scheduler is responsible for determining the order in which block I/O operations are submitted to storage devices. The scheduler affects the performance and behavior of disk I/O operations, impacting both throughput and latency. Different I/O schedulers are available, each optimized for specific workloads and scenarios.

By default, I/O tasks are scheduled by fifo - first in first out.

#### Common Linux I/O Schedulers

1. CFQ (Completely Fair Queuing) - Provides a balanced approach to I/O scheduling, aiming to give each process a fair share of the I/O bandwidth.
2. Deadline - Designed to prevent starvation of I/O operations by imposing deadlines on requests.
3. NOOP - Implements a simple FIFO (First-In, First-Out) queue, essentially a passthrough scheduler.
4. BFQ (Budget Fair Queuing) - Aims to provide predictable I/O performance by distributing I/O bandwidth according to budgets assigned to tasks.
5. MQ Deadline (Multiqueue Deadline) - Similar to the Deadline scheduler but designed for multiqueue block devices.
6. Kyber - A relatively new scheduler designed to work well with modern hardware and to provide low latency.
7. BFQ (Budget Fair Queuing) - Bypasses software I/O scheduling, relying entirely on hardware-level I/O management.

#### Changing the scheduler

The scheduler is set on a per disk basis, not per filesystem.

##### (none persistent) method:

The scheduler can be set within the below file:

`/sys/block/diskname/queue/scheduler`

Within this file, you'll likely see a number of the potential I/O scheduler types. Tne one surrounded by \[\] is the currently selected scheduler, ie:

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

##### (persistent) method:

To persistently set a disk scheduler, we'll need to alter the grub configuration

`/etc/default/grub`

append the below to the line prefaced with 'GRUB\_CMDLINE\_LINUX='

```
elevator=schedulertype
```

Save the new GRUB configuration:

```
update-grub2
```

or

```
grub-mkconfig
```

====================================================================================

### Disk Performance Troubleshooting Tools

\------------------------------------------------------------------------------------------------------------------------------------------------

#### sar

\------------------------------------------------------------------------------------------------------------------------------------------------

#### lsof

We can use the lsof command to check what parts of a disk a process is accessing.

```
lsof -p pid
```

```
lsof -c command
```

\------------------------------------------------------------------------------------------------------------------------------------------------

#### systat tools

The `sysstat` package includes a collection of performance monitoring tools for Unix-like systems.

#### iotop

```
iotop [options]
```

<table border="1" id="bkmrk--a-aggregate-disk-io" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr><td>-a</td><td>aggregate disk IO over time (duration of command)</td></tr></tbody></table>

##### iostat

Similar to iotop. Provides statistical information about I/O device loading. It reports on CPU utilization, device I/O statistics, and system throughput, making it useful for overall system performance analysis.

```
iostat
```

##### ioping

ioping is basically the ping command but for disks. It is used to measure the IO latency of storage devices - basically it measures how long a storage device will take to respond to an I/O request.

```
ioping [options] target
```

<table border="1" id="bkmrk--c-specify-a-number-" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr><td>-c</td><td>specify a number of IO requests to make</td></tr><tr><td>-i</td><td>interval between requests</td></tr><tr><td>-s</td><td>request size (default 4kb)</td></tr><tr><td>-q</td><td>suppress regular output, only show statistics</td></tr></tbody></table>

====================================================================================