debuggers.hg

changeset 20675:2d9c58c29a94

cpuidle: fix the menu governor to enhance IO performance

this is a revised version of linux upstream commit
69d25870f20c4b2563304f2b79c5300dd60a067e:

"
cpuidle: fix the menu governor to boost IO performance

Fix the menu idle governor which balances power savings, energy
efficiency
and performance impact.

The reason for a reworked governor is that there have been
serious
performance issues reported with the existing code on Nehalem
server
systems.

To show this I'm sure Andrew wants to see benchmark results:
(benchmark is "fio", "no cstates" is using "idle=3Dpoll")

no cstates current linux new algorithm
1 disk 107 Mb/s 85 Mb/s 105 Mb/s
2 disks 215 Mb/s 123 Mb/s 209 Mb/s
12 disks 590 Mb/s 320 Mb/s 585 Mb/s

In various power benchmark measurements, no degredation was found
by our
measurement&diagnostics team. Obviously a small percentage more
power was
used in the "fio" benchmark, due to the much higher performance.

Signed-off-by: Arjan van de Ven <arjan@linux.intel.com>
Cc: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
Cc: Len Brown <lenb@kernel.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Yanmin Zhang <yanmin_zhang@linux.intel.com>
Acked-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
"

in Xen version, most logic is similar and with only one exception:
linux use nr_iowait and loadavg to track the pending I/O request,
which however is not visible to Xen. so Xen use the do_irq frequency
to estimate the I/O pressure. this is not as accurate as linux, and
the better approach is to convey guest latency requirement to
hypervisor by virtual C state. this can be the future enhancement.

the detail algorithm description is in code comment. with this new
algorithm, fio benchmark performance improve ~5% with 1 disk. and no
power degration is found in idle case.

Signed-off-by: Yu Ke <ke.yu@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Dec 14 07:54:53 2009 +0000 (2009-12-14)
parents 3d505c9f1b73
children db8a985693f7
files xen/arch/x86/acpi/cpuidle_menu.c xen/arch/x86/hpet.c xen/arch/x86/irq.c xen/include/asm-x86/irq.h xen/include/xen/cpuidle.h
line diff
     1.1 --- a/xen/arch/x86/acpi/cpuidle_menu.c	Mon Dec 14 07:52:22 2009 +0000
     1.2 +++ b/xen/arch/x86/acpi/cpuidle_menu.c	Mon Dec 14 07:54:53 2009 +0000
     1.3 @@ -30,23 +30,147 @@
     1.4  #include <xen/acpi.h>
     1.5  #include <xen/timer.h>
     1.6  #include <xen/cpuidle.h>
     1.7 +#include <asm/irq.h>
     1.8  
     1.9 -#define BREAK_FUZZ      4       /* 4 us */
    1.10 -#define PRED_HISTORY_PCT   50
    1.11 -#define USEC_PER_SEC 1000000
    1.12 +#define BUCKETS 6
    1.13 +#define RESOLUTION 1024
    1.14 +#define DECAY 4
    1.15 +#define MAX_INTERESTING 50000
    1.16 +
    1.17 +/*
    1.18 + * Concepts and ideas behind the menu governor
    1.19 + *
    1.20 + * For the menu governor, there are 3 decision factors for picking a C
    1.21 + * state:
    1.22 + * 1) Energy break even point
    1.23 + * 2) Performance impact
    1.24 + * 3) Latency tolerance (TBD: from guest virtual C state)
    1.25 + * These these three factors are treated independently.
    1.26 + *
    1.27 + * Energy break even point
    1.28 + * -----------------------
    1.29 + * C state entry and exit have an energy cost, and a certain amount of time in
    1.30 + * the  C state is required to actually break even on this cost. CPUIDLE
    1.31 + * provides us this duration in the "target_residency" field. So all that we
    1.32 + * need is a good prediction of how long we'll be idle. Like the traditional
    1.33 + * menu governor, we start with the actual known "next timer event" time.
    1.34 + *
    1.35 + * Since there are other source of wakeups (interrupts for example) than
    1.36 + * the next timer event, this estimation is rather optimistic. To get a
    1.37 + * more realistic estimate, a correction factor is applied to the estimate,
    1.38 + * that is based on historic behavior. For example, if in the past the actual
    1.39 + * duration always was 50% of the next timer tick, the correction factor will
    1.40 + * be 0.5.
    1.41 + *
    1.42 + * menu uses a running average for this correction factor, however it uses a
    1.43 + * set of factors, not just a single factor. This stems from the realization
    1.44 + * that the ratio is dependent on the order of magnitude of the expected
    1.45 + * duration; if we expect 500 milliseconds of idle time the likelihood of
    1.46 + * getting an interrupt very early is much higher than if we expect 50 micro
    1.47 + * seconds of idle time.
    1.48 + * For this reason we keep an array of 6 independent factors, that gets
    1.49 + * indexed based on the magnitude of the expected duration
    1.50 + *
    1.51 + * Limiting Performance Impact
    1.52 + * ---------------------------
    1.53 + * C states, especially those with large exit latencies, can have a real
    1.54 + * noticable impact on workloads, which is not acceptable for most sysadmins,
    1.55 + * and in addition, less performance has a power price of its own.
    1.56 + *
    1.57 + * As a general rule of thumb, menu assumes that the following heuristic
    1.58 + * holds:
    1.59 + *     The busier the system, the less impact of C states is acceptable
    1.60 + *
    1.61 + * This rule-of-thumb is implemented using average interrupt interval:
    1.62 + * If the exit latency times multiplier is longer than the average
    1.63 + * interrupt interval, the C state is not considered a candidate
    1.64 + * for selection due to a too high performance impact. So the smaller
    1.65 + * the average interrupt interval is, the smaller C state latency should be
    1.66 + * and thus the less likely a busy CPU will hit such a deep C state.
    1.67 + *
    1.68 + */
    1.69 +
    1.70 +struct perf_factor{
    1.71 +    s_time_t    time_stamp;
    1.72 +    s_time_t    duration;
    1.73 +    unsigned int irq_count_stamp;
    1.74 +    unsigned int irq_sum;
    1.75 +};
    1.76  
    1.77  struct menu_device
    1.78  {
    1.79      int             last_state_idx;
    1.80      unsigned int    expected_us;
    1.81 -    unsigned int    predicted_us;
    1.82 -    unsigned int    current_predicted_us;
    1.83 -    unsigned int    last_measured_us;
    1.84 -    unsigned int    elapsed_us;
    1.85 +    u64             predicted_us;
    1.86 +    unsigned int    measured_us;
    1.87 +    unsigned int    exit_us;
    1.88 +    unsigned int    bucket;
    1.89 +    u64             correction_factor[BUCKETS];
    1.90 +    struct perf_factor pf;
    1.91  };
    1.92  
    1.93  static DEFINE_PER_CPU(struct menu_device, menu_devices);
    1.94  
    1.95 +static inline int which_bucket(unsigned int duration)
    1.96 +{
    1.97 +   int bucket = 0;
    1.98 +
    1.99 +   if (duration < 10)
   1.100 +       return bucket;
   1.101 +   if (duration < 100)
   1.102 +       return bucket + 1;
   1.103 +   if (duration < 1000)
   1.104 +       return bucket + 2;
   1.105 +   if (duration < 10000)
   1.106 +       return bucket + 3;
   1.107 +   if (duration < 100000)
   1.108 +       return bucket + 4;
   1.109 +   return bucket + 5;
   1.110 +}
   1.111 +
   1.112 +/*
   1.113 + * Return the average interrupt interval to take I/O performance
   1.114 + * requirements into account. The smaller the average interrupt
   1.115 + * interval to be, the more busy I/O activity, and thus the higher
   1.116 + * the barrier to go to an expensive C state.
   1.117 + */
   1.118 +
   1.119 +/* 5 milisec sampling period */
   1.120 +#define SAMPLING_PERIOD     5000000
   1.121 +
   1.122 +/* for I/O interrupt, we give 8x multiplier compared to C state latency*/
   1.123 +#define IO_MULTIPLIER       8
   1.124 +
   1.125 +static inline s_time_t avg_intr_interval_us(void)
   1.126 +{
   1.127 +    struct menu_device *data = &__get_cpu_var(menu_devices);
   1.128 +    s_time_t    duration, now;
   1.129 +    s_time_t    avg_interval;
   1.130 +    unsigned int irq_sum;
   1.131 +
   1.132 +    now = NOW();
   1.133 +    duration = (data->pf.duration + (now - data->pf.time_stamp)
   1.134 +            * (DECAY - 1)) / DECAY;
   1.135 +
   1.136 +    irq_sum = (data->pf.irq_sum + (this_cpu(irq_count) - data->pf.irq_count_stamp)
   1.137 +            * (DECAY - 1)) / DECAY;
   1.138 +
   1.139 +    if (irq_sum == 0)
   1.140 +        /* no irq recently, so return a big enough interval: 1 sec */
   1.141 +        avg_interval = 1000000;
   1.142 +    else
   1.143 +        avg_interval = duration / irq_sum / 1000; /* in us */
   1.144 +
   1.145 +    if ( duration >= SAMPLING_PERIOD){
   1.146 +        data->pf.time_stamp = now;
   1.147 +        data->pf.duration = duration;
   1.148 +        data->pf.irq_count_stamp= this_cpu(irq_count);
   1.149 +        data->pf.irq_sum = irq_sum;
   1.150 +    }
   1.151 +
   1.152 +    return avg_interval;
   1.153 +}
   1.154 +
   1.155  static unsigned int get_sleep_length_us(void)
   1.156  {
   1.157      s_time_t us = (this_cpu(timer_deadline_start) - NOW()) / 1000;
   1.158 @@ -62,57 +186,86 @@ static int menu_select(struct acpi_proce
   1.159  {
   1.160      struct menu_device *data = &__get_cpu_var(menu_devices);
   1.161      int i;
   1.162 +    s_time_t    io_interval;
   1.163  
   1.164 -    /* determine the expected residency time */
   1.165 +    /*  TBD: Change to 0 if C0(polling mode) support is added later*/
   1.166 +    data->last_state_idx = CPUIDLE_DRIVER_STATE_START;
   1.167 +    data->exit_us = 0;
   1.168 +
   1.169 +    /* determine the expected residency time, round up */
   1.170      data->expected_us = get_sleep_length_us();
   1.171  
   1.172 -    /* Recalculate predicted_us based on prediction_history_pct */
   1.173 -    data->predicted_us *= PRED_HISTORY_PCT;
   1.174 -    data->predicted_us += (100 - PRED_HISTORY_PCT) *
   1.175 -        data->current_predicted_us;
   1.176 -    data->predicted_us /= 100;
   1.177 +    data->bucket = which_bucket(data->expected_us);
   1.178 +
   1.179 +    io_interval = avg_intr_interval_us();
   1.180 +
   1.181 +    /*
   1.182 +     * if the correction factor is 0 (eg first time init or cpu hotplug
   1.183 +     * etc), we actually want to start out with a unity factor.
   1.184 +     */
   1.185 +    if (data->correction_factor[data->bucket] == 0)
   1.186 +        data->correction_factor[data->bucket] = RESOLUTION * DECAY;
   1.187 +
   1.188 +    /* Make sure to round up for half microseconds */
   1.189 +    data->predicted_us = DIV_ROUND(
   1.190 +            data->expected_us * data->correction_factor[data->bucket],
   1.191 +            RESOLUTION * DECAY);
   1.192  
   1.193      /* find the deepest idle state that satisfies our constraints */
   1.194 -    for ( i = 2; i < power->count; i++ )
   1.195 +    for ( i = CPUIDLE_DRIVER_STATE_START + 1; i < power->count; i++ )
   1.196      {
   1.197          struct acpi_processor_cx *s = &power->states[i];
   1.198  
   1.199 -        if ( s->target_residency > data->expected_us + s->latency )
   1.200 +        if (s->target_residency > data->predicted_us)
   1.201              break;
   1.202 -        if ( s->target_residency > data->predicted_us )
   1.203 +        if (s->latency * IO_MULTIPLIER > io_interval)
   1.204              break;
   1.205          /* TBD: we need to check the QoS requirment in future */
   1.206 +        data->exit_us = s->latency;
   1.207 +        data->last_state_idx = i;
   1.208      }
   1.209  
   1.210 -    data->last_state_idx = i - 1;
   1.211 -    return i - 1;
   1.212 +    return data->last_state_idx;
   1.213  }
   1.214  
   1.215  static void menu_reflect(struct acpi_processor_power *power)
   1.216  {
   1.217      struct menu_device *data = &__get_cpu_var(menu_devices);
   1.218 -    struct acpi_processor_cx *target = &power->states[data->last_state_idx];
   1.219 -    unsigned int last_residency; 
   1.220 +    unsigned int last_idle_us = power->last_residency;
   1.221      unsigned int measured_us;
   1.222 +    u64 new_factor;
   1.223 +
   1.224 +    measured_us = last_idle_us;
   1.225  
   1.226 -    last_residency = power->last_residency;
   1.227 -    measured_us = last_residency + data->elapsed_us;
   1.228 +    /*
   1.229 +     * We correct for the exit latency; we are assuming here that the
   1.230 +     * exit latency happens after the event that we're interested in.
   1.231 +     */
   1.232 +    if (measured_us > data->exit_us)
   1.233 +        measured_us -= data->exit_us;
   1.234  
   1.235 -    /* if wrapping, set to max uint (-1) */
   1.236 -    measured_us = data->elapsed_us <= measured_us ? measured_us : -1;
   1.237 +    /* update our correction ratio */
   1.238 +
   1.239 +    new_factor = data->correction_factor[data->bucket]
   1.240 +        * (DECAY - 1) / DECAY;
   1.241  
   1.242 -    /* Predict time remaining until next break event */
   1.243 -    data->current_predicted_us = max(measured_us, data->last_measured_us);
   1.244 +    if (data->expected_us > 0 && data->measured_us < MAX_INTERESTING)
   1.245 +        new_factor += RESOLUTION * measured_us / data->expected_us;
   1.246 +    else
   1.247 +        /*
   1.248 +         * we were idle so long that we count it as a perfect
   1.249 +         * prediction
   1.250 +         */
   1.251 +        new_factor += RESOLUTION;
   1.252  
   1.253 -    /* Distinguish between expected & non-expected events */
   1.254 -    if ( last_residency + BREAK_FUZZ
   1.255 -         < data->expected_us + target->latency )
   1.256 -    {
   1.257 -        data->last_measured_us = measured_us;
   1.258 -        data->elapsed_us = 0;
   1.259 -    }
   1.260 -    else
   1.261 -        data->elapsed_us = measured_us;
   1.262 +    /*
   1.263 +     * We don't want 0 as factor; we always want at least
   1.264 +     * a tiny bit of estimated time.
   1.265 +     */
   1.266 +    if (new_factor == 0)
   1.267 +        new_factor = 1;
   1.268 +
   1.269 +    data->correction_factor[data->bucket] = new_factor;
   1.270  }
   1.271  
   1.272  static int menu_enable_device(struct acpi_processor_power *power)
     2.1 --- a/xen/arch/x86/hpet.c	Mon Dec 14 07:52:22 2009 +0000
     2.2 +++ b/xen/arch/x86/hpet.c	Mon Dec 14 07:54:53 2009 +0000
     2.3 @@ -211,6 +211,9 @@ static void hpet_interrupt_handler(int i
     2.4          struct cpu_user_regs *regs)
     2.5  {
     2.6      struct hpet_event_channel *ch = (struct hpet_event_channel *)data;
     2.7 +
     2.8 +    this_cpu(irq_count)--;
     2.9 +
    2.10      if ( !ch->event_handler )
    2.11      {
    2.12          printk(XENLOG_WARNING "Spurious HPET timer interrupt on HPET timer %d\n", ch->idx);
    2.13 @@ -692,6 +695,8 @@ int hpet_broadcast_is_available(void)
    2.14  
    2.15  int hpet_legacy_irq_tick(void)
    2.16  {
    2.17 +    this_cpu(irq_count)--;
    2.18 +
    2.19      if ( !legacy_hpet_event.event_handler )
    2.20          return 0;
    2.21      legacy_hpet_event.event_handler(&legacy_hpet_event);
     3.1 --- a/xen/arch/x86/irq.c	Mon Dec 14 07:52:22 2009 +0000
     3.2 +++ b/xen/arch/x86/irq.c	Mon Dec 14 07:54:53 2009 +0000
     3.3 @@ -517,6 +517,8 @@ void irq_set_affinity(int irq, cpumask_t
     3.4      cpus_copy(desc->pending_mask, mask);
     3.5  }
     3.6  
     3.7 +DEFINE_PER_CPU(unsigned int, irq_count);
     3.8 +
     3.9  asmlinkage void do_IRQ(struct cpu_user_regs *regs)
    3.10  {
    3.11      struct irqaction *action;
    3.12 @@ -528,6 +530,8 @@ asmlinkage void do_IRQ(struct cpu_user_r
    3.13      
    3.14      perfc_incr(irqs);
    3.15  
    3.16 +    this_cpu(irq_count)++;
    3.17 +
    3.18      if (irq < 0) {
    3.19          ack_APIC_irq();
    3.20          printk("%s: %d.%d No irq handler for vector (irq %d)\n",
     4.1 --- a/xen/include/asm-x86/irq.h	Mon Dec 14 07:52:22 2009 +0000
     4.2 +++ b/xen/include/asm-x86/irq.h	Mon Dec 14 07:54:53 2009 +0000
     4.3 @@ -105,6 +105,8 @@ extern unsigned long io_apic_irqs;
     4.4  extern atomic_t irq_err_count;
     4.5  extern atomic_t irq_mis_count;
     4.6  
     4.7 +DECLARE_PER_CPU(unsigned int, irq_count);
     4.8 +
     4.9  int pirq_shared(struct domain *d , int irq);
    4.10  
    4.11  int map_domain_pirq(struct domain *d, int pirq, int irq, int type,
     5.1 --- a/xen/include/xen/cpuidle.h	Mon Dec 14 07:52:22 2009 +0000
     5.2 +++ b/xen/include/xen/cpuidle.h	Mon Dec 14 07:54:53 2009 +0000
     5.3 @@ -86,4 +86,6 @@ struct cpuidle_governor
     5.4  extern struct cpuidle_governor *cpuidle_current_governor;
     5.5  void cpuidle_disable_deep_cstate(void);
     5.6  
     5.7 +#define CPUIDLE_DRIVER_STATE_START  1
     5.8 +
     5.9  #endif /* _XEN_CPUIDLE_H */