Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 17: Data Synchronization


17.11.2 Thresholds


When does the kernel regard a queue as being congested and when does it give the ‘‘all clear‘‘? The
answer is surprisingly easy — a simple check is made to ascertain whether certain minimum and maxi-
mum limit values (or thresholds) for requests have been exceeded in a specific queue.

The kernel doesnotuse fixed constants to do this. Instead, it defines the limit values in relation to the
system’s main memory because the number of blockrequests is scaled accordingly.

Recall from Chapter 6 that each block device is equipped with a request queue defined bystruct
request. The fields that are interesting forour purposes are reproduced below:

<blkdev.h>
struct request_queue
{
...
unsigned long nr_requests; /* Max # of requests */
unsigned int nr_congestion_on;
unsigned int nr_congestion_off;
unsigned int nr_batching;
...
}

Thenr_requestselement is used to define the number of request structures per queue. Typ-
ically, this number is set toBLKDEV_MAX_RQ, which equates to 128 but can be changed using
/sys/block/<device>/queue/nr_requests. A lower bound on the number of requests is given by
BLKDEV_MIN_RQ, which equates to 4.

❑ nr_congestion_ondenotes the limit value at which a queue is regarded as congested. There
must be fewer freerequeststructures than specified by the value for this state to occur.
❑ nr_congestion_off(note the ‘‘off‘‘) also specifies a limit value at which a queue is regarded
asno longercongested. When there are more freerequests than indicated by this number, the
kernel regards the queue as free.

The functionsqueue_congestion_on_thresholdandqueue_congestion_off_thresholdare provided
to read the current threshold values. Although the functions are trivial, they must be used instead of
reading the values directly. Should the implementation change in future kernel versions, the user will
nevertheless be able to enjoy the same interface and will not require modifications.

The congestion thresholds are computed byblk_congestion_threshold:

block/ll_rw_blk.c
static void blk_queue_congestion_threshold(struct request_queue *q)

Figure 17-9 displays the congestion thresholds that are computed for a request queue with a given
length. The values forcongestion_onandcongestion_offdiffer slightly. This minor difference
(known ashysteresisin the kernel sources, a term borrowed from physics) prevents queues from
switching constantly between both states when the number of requests is close to the congestion
threshold.
Free download pdf