Re: [RFC,RFT,PATCH] cfq: autotuning support

From: Corrado Zoccolo
Date: Tue Nov 17 2009 - 13:21:56 EST


On Tue, Nov 17, 2009 at 5:52 PM, Vivek Goyal <vgoyal@xxxxxxxxxx> wrote:
> On Tue, Nov 17, 2009 at 03:51:14PM +0100, Corrado Zoccolo wrote:
>> Hi, this is my first attempt at autotuning cfq parameters, and should apply on top of for-2.6.33 branch.
>> Jeff and Vivek, if you can test this on your NCQ SSDs, it will help me to have your feedback (please include
>> the output of: 'grep -r . /sys/block/sdX/queue/iosched' after you run your tests).
>
> Sure, I will do some tests on this patch, may be later today.
>
> I had a quick look at the patch. Had couple of questions.
>
> - Service time seems to be the measure of on an average how much time it
> Âtook to service a read/write request (be it sequential or random read).
We sample both in the histogram, but then we try to extract the random
read service time.

> ÂIn auto tuning, you seem to be updating slice_idle dynamically based on
> Âservice time. So the intent seems to be that is service times are higher
> Âthen it is a slow media and we should have higher slice_idle otherwise
> Âa low slice_idle?

Yes. The target is to have slice_idle = the average seek cost.
So we will wait up to the average seek cost for a sequential request
before switching to an other queue and paying a possibly long seek.
For media that have really fast seek (less than 0.5ms), both when
reading and when writing, then we put slice_idle = 0. Both of your
SSDs should be in this category.

Thanks
Corrado
> Thanks
> Vivek
>
>
>>
>> The patch introduces code to sample the request service time distribution, and analyze it,
>> in order to compute the following cfq parameters:
>> * slice_idle, is computed as the expected service time of random request
>> * cfq_slice[1] (i.e. the slice for sync queues)
>> * cfq_slice[0] (i.e. the slice for async queues)
>>
>> The sync and async slices are scaled from default values proportionally to the new computed slice_idle.
>>
>> Autotuning will be enabled by default only on kernels compiled with HZ >= 500.
>> With smaller HZ, I don't think jiffies is reliable to estimate those parameters.
>>
>> Signed-off-by: Corrado Zoccolo <czoccolo@xxxxxxxxx>
>> ---
>> Âblock/cfq-iosched.c | Â166 ++++++++++++++++++++++++++++++++++++++++++++++++++-
>> Â1 files changed, 163 insertions(+), 3 deletions(-)
>>
>> diff --git a/block/cfq-iosched.c b/block/cfq-iosched.c
>> index 6925ab9..d786a0b 100644
>> --- a/block/cfq-iosched.c
>> +++ b/block/cfq-iosched.c
>> @@ -32,6 +32,12 @@ static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
>> Âstatic const int cfq_hist_divisor = 4;
>>
>> Â/*
>> + * Number of samples to collect before updating autotune
>> + * higher # makes the measurements more stable
>> + */
>> +#define CFQ_AUTOTUNE_SAMPLES (10)
>> +
>> +/*
>> Â * offset from end of service tree
>> Â */
>> Â#define CFQ_IDLE_DELAY Â Â Â Â Â Â Â (HZ / 5)
>> @@ -201,6 +207,21 @@ struct cfq_data {
>> Â Â Â unsigned int hw_tag_samples;
>>
>> Â Â Â /*
>> + Â Â Â* disk performance measurements
>> + Â Â Â*/
>> + Â Â unsigned long observation_start;
>> + Â Â /*
>> + Â Â Â* measures are split (READ vs WRITE)
>> + Â Â Â*/
>> + Â Â unsigned long processed_rq[2];
>> + Â Â /*
>> + Â Â Â* We store an histogram of samples for the service time
>> + Â Â Â* in log scale [0..5]; [6] is a count, that is reset every
>> + Â Â Â* time autotuning is done (i.e. every CFQ_AUTOTUNE_SAMPLES)
>> + Â Â Â*/
>> + Â Â unsigned int serv_time_samples[2][7];
>> +
>> + Â Â /*
>> Â Â Â Â* idle window management
>> Â Â Â Â*/
>> Â Â Â struct timer_list idle_slice_timer;
>> @@ -228,6 +249,7 @@ struct cfq_data {
>> Â Â Â unsigned int cfq_slice_async_rq;
>> Â Â Â unsigned int cfq_slice_idle;
>> Â Â Â unsigned int cfq_latency;
>> + Â Â unsigned int cfq_autotune;
>>
>> Â Â Â struct list_head cic_list;
>>
>> @@ -891,6 +913,12 @@ static void cfq_activate_request(struct request_queue *q, struct request *rq)
>> Â{
>> Â Â Â struct cfq_data *cfqd = q->elevator->elevator_data;
>>
>> + Â Â if (!rq_in_driver(cfqd)) {
>> + Â Â Â Â Â Â cfqd->observation_start = jiffies;
>> + Â Â Â Â Â Â cfqd->processed_rq[0] = 0;
>> + Â Â Â Â Â Â cfqd->processed_rq[1] = 0;
>> + Â Â }
>> +
>> Â Â Â cfqd->rq_in_driver[rq_is_sync(rq)]++;
>> Â Â Â cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â rq_in_driver(cfqd));
>> @@ -2562,14 +2590,120 @@ static void cfq_update_hw_tag(struct cfq_data *cfqd)
>> Â Â Â Â Â Â Â cfqd->hw_tag = 0;
>> Â}
>>
>> +
>> +/*
>> + * Update the histogram to compute service time
>> + * Returns true if we collected enough samples to re-run autotune
>> + */
>> +static bool cfq_update_stime(unsigned samples[6], unsigned long stime)
>> +{
>> + Â Â unsigned idx = (stime > 15) + (stime > 7) + (stime > 3)
>> + Â Â Â Â Â Â Â Â Â Â+ (stime > 1) + (stime > 0);
>> + Â Â samples[idx]++;
>> + Â Â if (samples[idx] > (1U<<31))
>> + Â Â Â Â Â Â for (idx = 0; idx < 6; ++idx) {
>> + Â Â Â Â Â Â Â Â Â Â samples[idx]++;
>> + Â Â Â Â Â Â Â Â Â Â samples[idx] >>= 1;
>> + Â Â Â Â Â Â }
>> +
>> + Â Â if (samples[6]++ < CFQ_AUTOTUNE_SAMPLES)
>> + Â Â Â Â Â Â return false;
>> + Â Â samples[6] = 0;
>> + Â Â return true;
>> +}
>> +
>> +/*
>> + * Currently, we measure only service time for pure READ or WRITE requests
>> + * and we update autotune when we have collected enough READ requests
>> + */
>> +static bool cfq_update_perf_measures(struct cfq_data *cfqd, unsigned long now)
>> +{
>> + Â Â unsigned long tot_proc = cfqd->processed_rq[0] + cfqd->processed_rq[1];
>> + Â Â unsigned long obstime = now - cfqd->observation_start;
>> + Â Â unsigned long stime = obstime / tot_proc;
>> +
>> + Â Â cfqd->observation_start = now;
>> +
>> + Â Â if (!cfqd->processed_rq[READ])
>> + Â Â Â Â Â Â cfq_update_stime(cfqd->serv_time_samples[WRITE], stime);
>> + Â Â if (!cfqd->processed_rq[WRITE])
>> + Â Â Â Â Â Â return cfq_update_stime(cfqd->serv_time_samples[READ], stime);
>> + Â Â return false;
>> +}
>> +
>> +/*
>> + * Compute service time from the sampled distribution in the histogram
>> + * The real service time distribution is a super-position of two distinct
>> + * distributions:
>> + * the one for sequential requests (usually this has a small mean)
>> + * the one for random requests (usually with a larger mean)
>> + * and we want to identify the random request one
>> + */
>> +static unsigned cfq_service_time(unsigned samples[6])
>> +{
>> + Â Â unsigned last_max = 0, i;
>> + Â Â /* Random request service time corresponds to the
>> + Â Â Â* largest maximum in the histogram */
>> + Â Â for (i = 1; i < 6; ++i)
>> + Â Â Â Â Â Â if (samples[i] > samples[i-1])
>> + Â Â Â Â Â Â Â Â Â Â last_max = i;
>> + Â Â /*
>> + Â Â Â* Unfortunately, if sequential requests overwhelm
>> + Â Â Â* random ones, and the two peaks are too near,
>> + Â Â Â* the second peak could be masked by the tail of the first.
>> + Â Â Â* To catch this, we check if the tail has enough weight,
>> + Â Â Â* and in this case we take the next bin as maximum
>> + Â Â Â*/
>> + Â Â if (last_max < 5) {
>> + Â Â Â Â Â Â unsigned total = 0;
>> + Â Â Â Â Â Â for (i = last_max + 1; i < 6; ++i)
>> + Â Â Â Â Â Â Â Â Â Â total += samples[i];
>> + Â Â Â Â Â Â if (total > samples[last_max])
>> + Â Â Â Â Â Â Â Â Â Â ++last_max;
>> + Â Â }
>> + Â Â if (!last_max)
>> + Â Â Â Â Â Â return 0;
>> + Â Â return 1U << last_max;
>> +}
>> +
>> +static void cfq_update_autotune(struct cfq_data *cfqd)
>> +{
>> + Â Â unsigned long base, baseW;
>> + Â Â if (!cfqd->cfq_autotune)
>> + Â Â Â Â Â Â return;
>> + Â Â base = cfq_service_time(cfqd->serv_time_samples[READ]);
>> + Â Â baseW = cfq_service_time(cfqd->serv_time_samples[WRITE]);
>> +
>> + Â Â /* Compute slice_idle */
>> + Â Â if (!base)
>> + Â Â Â Â Â Â base = baseW;
>> + Â Â if (base > cfq_slice_idle)
>> + Â Â Â Â Â Â base = min_t(unsigned long,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â(base + cfq_slice_idle) / 2, 2 * cfq_slice_idle);
>> +
>> + Â Â cfqd->cfq_slice_idle = base;
>> +
>> + Â Â /* Compute derived cfq_slice[*]
>> + Â Â Â* Note: those cannot be 0
>> + Â Â Â*/
>> + Â Â if (!base)
>> + Â Â Â Â Â Â base = 1;
>> +
>> + Â Â if (baseW)
>> + Â Â Â Â Â Â baseW = min(base, baseW * cfq_slice_sync / cfq_slice_async);
>> + Â Â else
>> + Â Â Â Â Â Â baseW = base;
>> +
>> + Â Â cfqd->cfq_slice[1] = base * cfq_slice_sync / cfq_slice_idle;
>> + Â Â cfqd->cfq_slice[0] = baseW * cfq_slice_async / cfq_slice_idle;
>> +}
>> +
>> Âstatic void cfq_completed_request(struct request_queue *q, struct request *rq)
>> Â{
>> Â Â Â struct cfq_queue *cfqq = RQ_CFQQ(rq);
>> Â Â Â struct cfq_data *cfqd = cfqq->cfqd;
>> Â Â Â const int sync = rq_is_sync(rq);
>> - Â Â unsigned long now;
>> -
>> - Â Â now = jiffies;
>> + Â Â unsigned long now = jiffies;
>> Â Â Â cfq_log_cfqq(cfqd, cfqq, "complete");
>>
>> Â Â Â cfq_update_hw_tag(cfqd);
>> @@ -2578,6 +2712,10 @@ static void cfq_completed_request(struct request_queue *q, struct request *rq)
>> Â Â Â WARN_ON(!cfqq->dispatched);
>> Â Â Â cfqd->rq_in_driver[sync]--;
>> Â Â Â cfqq->dispatched--;
>> + Â Â cfqd->processed_rq[rq_data_dir(rq)]++;
>> +
>> + Â Â if (!rq_in_driver(cfqd) && cfq_update_perf_measures(cfqd, now))
>> + Â Â Â Â Â Â cfq_update_autotune(cfqd);
>>
>> Â Â Â if (cfq_cfqq_sync(cfqq))
>> Â Â Â Â Â Â Â cfqd->sync_flight--;
>> @@ -2966,6 +3104,7 @@ static void *cfq_init_queue(struct request_queue *q)
>> Â Â Â cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
>> Â Â Â cfqd->cfq_slice_idle = cfq_slice_idle;
>> Â Â Â cfqd->cfq_latency = 1;
>> + Â Â cfqd->cfq_autotune = (HZ >= 500);
>> Â Â Â cfqd->hw_tag = -1;
>> Â Â Â cfqd->last_end_sync_rq = jiffies;
>> Â Â Â return cfqd;
>> @@ -3002,6 +3141,23 @@ fail:
>> Â/*
>> Â * sysfs parts below -->
>> Â */
>> +static ssize_t autotune_stats_show(struct elevator_queue *e, char *page)
>> +{
>> + Â Â struct cfq_data *cfqd = e->elevator_data;
>> + Â Â int pos = 0, i, j;
>> +#if HZ < 500
>> + Â Â pos += sprintf(page, "Autotune may work incorrectly with HZ < 500\n");
>> +#endif
>> + Â Â for (j = 0; j < 2; ++j) {
>> + Â Â Â Â Â Â for (i = 0; i < 6; ++i)
>> + Â Â Â Â Â Â Â Â Â Â pos += sprintf(page+pos, "[%2u]",
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â cfqd->serv_time_samples[j][i]);
>> + Â Â Â Â Â Â page[pos++] = '\n';
>> + Â Â }
>> + Â Â page[pos] = '\0';
>> + Â Â return pos;
>> +}
>> +
>> Âstatic ssize_t
>> Âcfq_var_show(unsigned int var, char *page)
>> Â{
>> @@ -3036,6 +3192,7 @@ SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
>> ÂSHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
>> ÂSHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
>> ÂSHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
>> +SHOW_FUNCTION(cfq_autotune_show, cfqd->cfq_autotune, 0);
>> Â#undef SHOW_FUNCTION
>>
>> Â#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) Â Â Â Â Â Â Â Â Â Â Â\
>> @@ -3068,6 +3225,7 @@ STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
>> ÂSTORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
>> Â Â Â Â Â Â Â UINT_MAX, 0);
>> ÂSTORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
>> +STORE_FUNCTION(cfq_autotune_store, &cfqd->cfq_autotune, 0, 1, 0);
>> Â#undef STORE_FUNCTION
>>
>> Â#define CFQ_ATTR(name) \
>> @@ -3084,6 +3242,8 @@ static struct elv_fs_entry cfq_attrs[] = {
>> Â Â Â CFQ_ATTR(slice_async_rq),
>> Â Â Â CFQ_ATTR(slice_idle),
>> Â Â Â CFQ_ATTR(low_latency),
>> + Â Â CFQ_ATTR(autotune),
>> + Â Â __ATTR_RO(autotune_stats),
>> Â Â Â __ATTR_NULL
>> Â};
>>
>> --
>> 1.6.2.5
>>
>



--
__________________________________________________________________________

dott. Corrado Zoccolo mailto:czoccolo@xxxxxxxxx
PhD - Department of Computer Science - University of Pisa, Italy
--------------------------------------------------------------------------
The self-confidence of a warrior is not the self-confidence of the average
man. The average man seeks certainty in the eyes of the onlooker and calls
that self-confidence. The warrior seeks impeccability in his own eyes and
calls that humbleness.
Tales of Power - C. Castaneda
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/