proportions.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * FLoating proportions
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  5. *
  6. * This file contains the public data structure and API definitions.
  7. */
  8. #ifndef _LINUX_PROPORTIONS_H
  9. #define _LINUX_PROPORTIONS_H
  10. #include <linux/percpu_counter.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/mutex.h>
  13. struct prop_global {
  14. /*
  15. * The period over which we differentiate
  16. *
  17. * period = 2^shift
  18. */
  19. int shift;
  20. /*
  21. * The total event counter aka 'time'.
  22. *
  23. * Treated as an unsigned long; the lower 'shift - 1' bits are the
  24. * counter bits, the remaining upper bits the period counter.
  25. */
  26. struct percpu_counter events;
  27. };
  28. /*
  29. * global proportion descriptor
  30. *
  31. * this is needed to consitently flip prop_global structures.
  32. */
  33. struct prop_descriptor {
  34. int index;
  35. struct prop_global pg[2];
  36. struct mutex mutex; /* serialize the prop_global switch */
  37. };
  38. int prop_descriptor_init(struct prop_descriptor *pd, int shift);
  39. void prop_change_shift(struct prop_descriptor *pd, int new_shift);
  40. /*
  41. * ----- PERCPU ------
  42. */
  43. struct prop_local_percpu {
  44. /*
  45. * the local events counter
  46. */
  47. struct percpu_counter events;
  48. /*
  49. * snapshot of the last seen global state
  50. */
  51. int shift;
  52. unsigned long period;
  53. raw_spinlock_t lock; /* protect the snapshot state */
  54. };
  55. int prop_local_init_percpu(struct prop_local_percpu *pl);
  56. void prop_local_destroy_percpu(struct prop_local_percpu *pl);
  57. void __prop_inc_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl);
  58. void prop_fraction_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl,
  59. long *numerator, long *denominator);
  60. static inline
  61. void prop_inc_percpu(struct prop_descriptor *pd, struct prop_local_percpu *pl)
  62. {
  63. unsigned long flags;
  64. local_irq_save(flags);
  65. __prop_inc_percpu(pd, pl);
  66. local_irq_restore(flags);
  67. }
  68. /*
  69. * Limit the time part in order to ensure there are some bits left for the
  70. * cycle counter and fraction multiply.
  71. */
  72. #if BITS_PER_LONG == 32
  73. #define PROP_MAX_SHIFT (3*BITS_PER_LONG/4)
  74. #else
  75. #define PROP_MAX_SHIFT (BITS_PER_LONG/2)
  76. #endif
  77. #define PROP_FRAC_SHIFT (BITS_PER_LONG - PROP_MAX_SHIFT - 1)
  78. #define PROP_FRAC_BASE (1UL << PROP_FRAC_SHIFT)
  79. void __prop_inc_percpu_max(struct prop_descriptor *pd,
  80. struct prop_local_percpu *pl, long frac);
  81. /*
  82. * ----- SINGLE ------
  83. */
  84. struct prop_local_single {
  85. /*
  86. * the local events counter
  87. */
  88. unsigned long events;
  89. /*
  90. * snapshot of the last seen global state
  91. * and a lock protecting this state
  92. */
  93. unsigned long period;
  94. int shift;
  95. raw_spinlock_t lock; /* protect the snapshot state */
  96. };
  97. #define INIT_PROP_LOCAL_SINGLE(name) \
  98. { .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  99. }
  100. int prop_local_init_single(struct prop_local_single *pl);
  101. void prop_local_destroy_single(struct prop_local_single *pl);
  102. void __prop_inc_single(struct prop_descriptor *pd, struct prop_local_single *pl);
  103. void prop_fraction_single(struct prop_descriptor *pd, struct prop_local_single *pl,
  104. long *numerator, long *denominator);
  105. static inline
  106. void prop_inc_single(struct prop_descriptor *pd, struct prop_local_single *pl)
  107. {
  108. unsigned long flags;
  109. local_irq_save(flags);
  110. __prop_inc_single(pd, pl);
  111. local_irq_restore(flags);
  112. }
  113. #endif /* _LINUX_PROPORTIONS_H */