1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Floating proportions with flexible aging period
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2011, SUSE, Jan Kara <jack@suse.cz>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef _LINUX_FLEX_PROPORTIONS_H
9*4882a593Smuzhiyun #define _LINUX_FLEX_PROPORTIONS_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/percpu_counter.h>
12*4882a593Smuzhiyun #include <linux/spinlock.h>
13*4882a593Smuzhiyun #include <linux/seqlock.h>
14*4882a593Smuzhiyun #include <linux/gfp.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /*
17*4882a593Smuzhiyun * When maximum proportion of some event type is specified, this is the
18*4882a593Smuzhiyun * precision with which we allow limitting. Note that this creates an upper
19*4882a593Smuzhiyun * bound on the number of events per period like
20*4882a593Smuzhiyun * ULLONG_MAX >> FPROP_FRAC_SHIFT.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun #define FPROP_FRAC_SHIFT 10
23*4882a593Smuzhiyun #define FPROP_FRAC_BASE (1UL << FPROP_FRAC_SHIFT)
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun * ---- Global proportion definitions ----
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun struct fprop_global {
29*4882a593Smuzhiyun /* Number of events in the current period */
30*4882a593Smuzhiyun struct percpu_counter events;
31*4882a593Smuzhiyun /* Current period */
32*4882a593Smuzhiyun unsigned int period;
33*4882a593Smuzhiyun /* Synchronization with period transitions */
34*4882a593Smuzhiyun seqcount_t sequence;
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun int fprop_global_init(struct fprop_global *p, gfp_t gfp);
38*4882a593Smuzhiyun void fprop_global_destroy(struct fprop_global *p);
39*4882a593Smuzhiyun bool fprop_new_period(struct fprop_global *p, int periods);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun * ---- SINGLE ----
43*4882a593Smuzhiyun */
44*4882a593Smuzhiyun struct fprop_local_single {
45*4882a593Smuzhiyun /* the local events counter */
46*4882a593Smuzhiyun unsigned long events;
47*4882a593Smuzhiyun /* Period in which we last updated events */
48*4882a593Smuzhiyun unsigned int period;
49*4882a593Smuzhiyun raw_spinlock_t lock; /* Protect period and numerator */
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #define INIT_FPROP_LOCAL_SINGLE(name) \
53*4882a593Smuzhiyun { .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun int fprop_local_init_single(struct fprop_local_single *pl);
57*4882a593Smuzhiyun void fprop_local_destroy_single(struct fprop_local_single *pl);
58*4882a593Smuzhiyun void __fprop_inc_single(struct fprop_global *p, struct fprop_local_single *pl);
59*4882a593Smuzhiyun void fprop_fraction_single(struct fprop_global *p,
60*4882a593Smuzhiyun struct fprop_local_single *pl, unsigned long *numerator,
61*4882a593Smuzhiyun unsigned long *denominator);
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun static inline
fprop_inc_single(struct fprop_global * p,struct fprop_local_single * pl)64*4882a593Smuzhiyun void fprop_inc_single(struct fprop_global *p, struct fprop_local_single *pl)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun unsigned long flags;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun local_irq_save(flags);
69*4882a593Smuzhiyun __fprop_inc_single(p, pl);
70*4882a593Smuzhiyun local_irq_restore(flags);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun * ---- PERCPU ----
75*4882a593Smuzhiyun */
76*4882a593Smuzhiyun struct fprop_local_percpu {
77*4882a593Smuzhiyun /* the local events counter */
78*4882a593Smuzhiyun struct percpu_counter events;
79*4882a593Smuzhiyun /* Period in which we last updated events */
80*4882a593Smuzhiyun unsigned int period;
81*4882a593Smuzhiyun raw_spinlock_t lock; /* Protect period and numerator */
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun int fprop_local_init_percpu(struct fprop_local_percpu *pl, gfp_t gfp);
85*4882a593Smuzhiyun void fprop_local_destroy_percpu(struct fprop_local_percpu *pl);
86*4882a593Smuzhiyun void __fprop_inc_percpu(struct fprop_global *p, struct fprop_local_percpu *pl);
87*4882a593Smuzhiyun void __fprop_inc_percpu_max(struct fprop_global *p, struct fprop_local_percpu *pl,
88*4882a593Smuzhiyun int max_frac);
89*4882a593Smuzhiyun void fprop_fraction_percpu(struct fprop_global *p,
90*4882a593Smuzhiyun struct fprop_local_percpu *pl, unsigned long *numerator,
91*4882a593Smuzhiyun unsigned long *denominator);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun static inline
fprop_inc_percpu(struct fprop_global * p,struct fprop_local_percpu * pl)94*4882a593Smuzhiyun void fprop_inc_percpu(struct fprop_global *p, struct fprop_local_percpu *pl)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun unsigned long flags;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun local_irq_save(flags);
99*4882a593Smuzhiyun __fprop_inc_percpu(p, pl);
100*4882a593Smuzhiyun local_irq_restore(flags);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun #endif
104