2 * Floating proportions with flexible aging period
4 * Copyright (C) 2011, SUSE, Jan Kara <jack@suse.cz>
6 * The goal of this code is: Given different types of event, measure proportion
7 * of each type of event over time. The proportions are measured with
8 * exponentially decaying history to give smooth transitions. A formula
9 * expressing proportion of event of type 'j' is:
11 * p_{j} = (\Sum_{i>=0} x_{i,j}/2^{i+1})/(\Sum_{i>=0} x_i/2^{i+1})
13 * Where x_{i,j} is j's number of events in i-th last time period and x_i is
14 * total number of events in i-th last time period.
16 * Note that p_{j}'s are normalised, i.e.
20 * This formula can be straightforwardly computed by maintaing denominator
21 * (let's call it 'd') and for each event type its numerator (let's call it
22 * 'n_j'). When an event of type 'j' happens, we simply need to do:
25 * When a new period is declared, we could do:
30 * To avoid iteration over all event types, we instead shift numerator of event
31 * j lazily when someone asks for a proportion of event j or when event j
32 * occurs. This can bit trivially implemented by remembering last period in
33 * which something happened with proportion of type j.
35 #include <linux/flex_proportions.h>
37 int fprop_global_init(struct fprop_global
*p
, gfp_t gfp
)
42 /* Use 1 to avoid dealing with periods with 0 events... */
43 err
= percpu_counter_init(&p
->events
, 1, gfp
);
46 seqcount_init(&p
->sequence
);
50 void fprop_global_destroy(struct fprop_global
*p
)
52 percpu_counter_destroy(&p
->events
);
56 * Declare @periods new periods. It is upto the caller to make sure period
57 * transitions cannot happen in parallel.
59 * The function returns true if the proportions are still defined and false
60 * if aging zeroed out all events. This can be used to detect whether declaring
61 * further periods has any effect.
63 bool fprop_new_period(struct fprop_global
*p
, int periods
)
68 local_irq_save(flags
);
69 events
= percpu_counter_sum(&p
->events
);
71 * Don't do anything if there are no events.
74 local_irq_restore(flags
);
77 write_seqcount_begin(&p
->sequence
);
79 events
-= events
>> periods
;
80 /* Use addition to avoid losing events happening between sum and set */
81 percpu_counter_add(&p
->events
, -events
);
83 write_seqcount_end(&p
->sequence
);
84 local_irq_restore(flags
);
93 int fprop_local_init_single(struct fprop_local_single
*pl
)
97 raw_spin_lock_init(&pl
->lock
);
101 void fprop_local_destroy_single(struct fprop_local_single
*pl
)
105 static void fprop_reflect_period_single(struct fprop_global
*p
,
106 struct fprop_local_single
*pl
)
108 unsigned int period
= p
->period
;
111 /* Fast path - period didn't change */
112 if (pl
->period
== period
)
114 raw_spin_lock_irqsave(&pl
->lock
, flags
);
115 /* Someone updated pl->period while we were spinning? */
116 if (pl
->period
>= period
) {
117 raw_spin_unlock_irqrestore(&pl
->lock
, flags
);
120 /* Aging zeroed our fraction? */
121 if (period
- pl
->period
< BITS_PER_LONG
)
122 pl
->events
>>= period
- pl
->period
;
126 raw_spin_unlock_irqrestore(&pl
->lock
, flags
);
129 /* Event of type pl happened */
130 void __fprop_inc_single(struct fprop_global
*p
, struct fprop_local_single
*pl
)
132 fprop_reflect_period_single(p
, pl
);
134 percpu_counter_add(&p
->events
, 1);
137 /* Return fraction of events of type pl */
138 void fprop_fraction_single(struct fprop_global
*p
,
139 struct fprop_local_single
*pl
,
140 unsigned long *numerator
, unsigned long *denominator
)
146 seq
= read_seqcount_begin(&p
->sequence
);
147 fprop_reflect_period_single(p
, pl
);
149 den
= percpu_counter_read_positive(&p
->events
);
150 } while (read_seqcount_retry(&p
->sequence
, seq
));
153 * Make fraction <= 1 and denominator > 0 even in presence of percpu
169 #define PROP_BATCH (8*(1+ilog2(nr_cpu_ids)))
171 int fprop_local_init_percpu(struct fprop_local_percpu
*pl
, gfp_t gfp
)
175 err
= percpu_counter_init(&pl
->events
, 0, gfp
);
179 raw_spin_lock_init(&pl
->lock
);
183 void fprop_local_destroy_percpu(struct fprop_local_percpu
*pl
)
185 percpu_counter_destroy(&pl
->events
);
188 static void fprop_reflect_period_percpu(struct fprop_global
*p
,
189 struct fprop_local_percpu
*pl
)
191 unsigned int period
= p
->period
;
194 /* Fast path - period didn't change */
195 if (pl
->period
== period
)
197 raw_spin_lock_irqsave(&pl
->lock
, flags
);
198 /* Someone updated pl->period while we were spinning? */
199 if (pl
->period
>= period
) {
200 raw_spin_unlock_irqrestore(&pl
->lock
, flags
);
203 /* Aging zeroed our fraction? */
204 if (period
- pl
->period
< BITS_PER_LONG
) {
205 s64 val
= percpu_counter_read(&pl
->events
);
207 if (val
< (nr_cpu_ids
* PROP_BATCH
))
208 val
= percpu_counter_sum(&pl
->events
);
210 __percpu_counter_add(&pl
->events
,
211 -val
+ (val
>> (period
-pl
->period
)), PROP_BATCH
);
213 percpu_counter_set(&pl
->events
, 0);
215 raw_spin_unlock_irqrestore(&pl
->lock
, flags
);
218 /* Event of type pl happened */
219 void __fprop_inc_percpu(struct fprop_global
*p
, struct fprop_local_percpu
*pl
)
221 fprop_reflect_period_percpu(p
, pl
);
222 __percpu_counter_add(&pl
->events
, 1, PROP_BATCH
);
223 percpu_counter_add(&p
->events
, 1);
226 void fprop_fraction_percpu(struct fprop_global
*p
,
227 struct fprop_local_percpu
*pl
,
228 unsigned long *numerator
, unsigned long *denominator
)
234 seq
= read_seqcount_begin(&p
->sequence
);
235 fprop_reflect_period_percpu(p
, pl
);
236 num
= percpu_counter_read_positive(&pl
->events
);
237 den
= percpu_counter_read_positive(&p
->events
);
238 } while (read_seqcount_retry(&p
->sequence
, seq
));
241 * Make fraction <= 1 and denominator > 0 even in presence of percpu
255 * Like __fprop_inc_percpu() except that event is counted only if the given
256 * type has fraction smaller than @max_frac/FPROP_FRAC_BASE
258 void __fprop_inc_percpu_max(struct fprop_global
*p
,
259 struct fprop_local_percpu
*pl
, int max_frac
)
261 if (unlikely(max_frac
< FPROP_FRAC_BASE
)) {
262 unsigned long numerator
, denominator
;
264 fprop_fraction_percpu(p
, pl
, &numerator
, &denominator
);
266 (((u64
)denominator
) * max_frac
) >> FPROP_FRAC_SHIFT
)
269 fprop_reflect_period_percpu(p
, pl
);
270 __percpu_counter_add(&pl
->events
, 1, PROP_BATCH
);
271 percpu_counter_add(&p
->events
, 1);
This page took 0.035219 seconds and 5 git commands to generate.