Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Floating proportions with flexible aging period
0004  *
0005  *  Copyright (C) 2011, SUSE, Jan Kara <jack@suse.cz>
0006  */
0007 
0008 #ifndef _LINUX_FLEX_PROPORTIONS_H
0009 #define _LINUX_FLEX_PROPORTIONS_H
0010 
0011 #include <linux/percpu_counter.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/seqlock.h>
0014 #include <linux/gfp.h>
0015 
0016 /*
0017  * When maximum proportion of some event type is specified, this is the
0018  * precision with which we allow limitting. Note that this creates an upper
0019  * bound on the number of events per period like
0020  *   ULLONG_MAX >> FPROP_FRAC_SHIFT.
0021  */
0022 #define FPROP_FRAC_SHIFT 10
0023 #define FPROP_FRAC_BASE (1UL << FPROP_FRAC_SHIFT)
0024 
0025 /*
0026  * ---- Global proportion definitions ----
0027  */
0028 struct fprop_global {
0029     /* Number of events in the current period */
0030     struct percpu_counter events;
0031     /* Current period */
0032     unsigned int period;
0033     /* Synchronization with period transitions */
0034     seqcount_t sequence;
0035 };
0036 
0037 int fprop_global_init(struct fprop_global *p, gfp_t gfp);
0038 void fprop_global_destroy(struct fprop_global *p);
0039 bool fprop_new_period(struct fprop_global *p, int periods);
0040 
0041 /*
0042  *  ---- SINGLE ----
0043  */
0044 struct fprop_local_single {
0045     /* the local events counter */
0046     unsigned long events;
0047     /* Period in which we last updated events */
0048     unsigned int period;
0049     raw_spinlock_t lock;    /* Protect period and numerator */
0050 };
0051 
0052 #define INIT_FPROP_LOCAL_SINGLE(name)           \
0053 {   .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock),    \
0054 }
0055 
0056 int fprop_local_init_single(struct fprop_local_single *pl);
0057 void fprop_local_destroy_single(struct fprop_local_single *pl);
0058 void __fprop_inc_single(struct fprop_global *p, struct fprop_local_single *pl);
0059 void fprop_fraction_single(struct fprop_global *p,
0060     struct fprop_local_single *pl, unsigned long *numerator,
0061     unsigned long *denominator);
0062 
0063 static inline
0064 void fprop_inc_single(struct fprop_global *p, struct fprop_local_single *pl)
0065 {
0066     unsigned long flags;
0067 
0068     local_irq_save(flags);
0069     __fprop_inc_single(p, pl);
0070     local_irq_restore(flags);
0071 }
0072 
0073 /*
0074  * ---- PERCPU ----
0075  */
0076 struct fprop_local_percpu {
0077     /* the local events counter */
0078     struct percpu_counter events;
0079     /* Period in which we last updated events */
0080     unsigned int period;
0081     raw_spinlock_t lock;    /* Protect period and numerator */
0082 };
0083 
0084 int fprop_local_init_percpu(struct fprop_local_percpu *pl, gfp_t gfp);
0085 void fprop_local_destroy_percpu(struct fprop_local_percpu *pl);
0086 void __fprop_add_percpu(struct fprop_global *p, struct fprop_local_percpu *pl,
0087         long nr);
0088 void __fprop_add_percpu_max(struct fprop_global *p,
0089         struct fprop_local_percpu *pl, int max_frac, long nr);
0090 void fprop_fraction_percpu(struct fprop_global *p,
0091     struct fprop_local_percpu *pl, unsigned long *numerator,
0092     unsigned long *denominator);
0093 
0094 static inline
0095 void fprop_inc_percpu(struct fprop_global *p, struct fprop_local_percpu *pl)
0096 {
0097     unsigned long flags;
0098 
0099     local_irq_save(flags);
0100     __fprop_add_percpu(p, pl, 1);
0101     local_irq_restore(flags);
0102 }
0103 
0104 #endif