2 * Floating proportions with flexible aging period
4 * Copyright (C) 2011, SUSE, Jan Kara <jack@suse.cz>
7 #ifndef _LINUX_FLEX_PROPORTIONS_H
8 #define _LINUX_FLEX_PROPORTIONS_H
10 #include <linux/percpu_counter.h>
11 #include <linux/spinlock.h>
12 #include <linux/seqlock.h>
13 #include <linux/gfp.h>
16 * When maximum proportion of some event type is specified, this is the
17 * precision with which we allow limitting. Note that this creates an upper
18 * bound on the number of events per period like
19 * ULLONG_MAX >> FPROP_FRAC_SHIFT.
21 #define FPROP_FRAC_SHIFT 10
22 #define FPROP_FRAC_BASE (1UL << FPROP_FRAC_SHIFT)
25 * ---- Global proportion definitions ----
28 /* Number of events in the current period */
29 struct percpu_counter events
;
32 /* Synchronization with period transitions */
36 int fprop_global_init(struct fprop_global
*p
, gfp_t gfp
);
37 void fprop_global_destroy(struct fprop_global
*p
);
38 bool fprop_new_period(struct fprop_global
*p
, int periods
);
43 struct fprop_local_single
{
44 /* the local events counter */
46 /* Period in which we last updated events */
48 raw_spinlock_t lock
; /* Protect period and numerator */
51 #define INIT_FPROP_LOCAL_SINGLE(name) \
52 { .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
55 int fprop_local_init_single(struct fprop_local_single
*pl
);
56 void fprop_local_destroy_single(struct fprop_local_single
*pl
);
57 void __fprop_inc_single(struct fprop_global
*p
, struct fprop_local_single
*pl
);
58 void fprop_fraction_single(struct fprop_global
*p
,
59 struct fprop_local_single
*pl
, unsigned long *numerator
,
60 unsigned long *denominator
);
63 void fprop_inc_single(struct fprop_global
*p
, struct fprop_local_single
*pl
)
67 local_irq_save(flags
);
68 __fprop_inc_single(p
, pl
);
69 local_irq_restore(flags
);
75 struct fprop_local_percpu
{
76 /* the local events counter */
77 struct percpu_counter events
;
78 /* Period in which we last updated events */
80 raw_spinlock_t lock
; /* Protect period and numerator */
83 int fprop_local_init_percpu(struct fprop_local_percpu
*pl
, gfp_t gfp
);
84 void fprop_local_destroy_percpu(struct fprop_local_percpu
*pl
);
85 void __fprop_inc_percpu(struct fprop_global
*p
, struct fprop_local_percpu
*pl
);
86 void __fprop_inc_percpu_max(struct fprop_global
*p
, struct fprop_local_percpu
*pl
,
88 void fprop_fraction_percpu(struct fprop_global
*p
,
89 struct fprop_local_percpu
*pl
, unsigned long *numerator
,
90 unsigned long *denominator
);
93 void fprop_inc_percpu(struct fprop_global
*p
, struct fprop_local_percpu
*pl
)
97 local_irq_save(flags
);
98 __fprop_inc_percpu(p
, pl
);
99 local_irq_restore(flags
);