flex_proportions.h 2.8 KB

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