res_counter.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #ifndef __RES_COUNTER_H__
  2. #define __RES_COUNTER_H__
  3. /*
  4. * Resource Counters
  5. * Contain common data types and routines for resource accounting
  6. *
  7. * Copyright 2007 OpenVZ SWsoft Inc
  8. *
  9. * Author: Pavel Emelianov <xemul@openvz.org>
  10. *
  11. * See Documentation/cgroups/resource_counter.txt for more
  12. * info about what this counter is.
  13. */
  14. #include <linux/cgroup.h>
  15. /*
  16. * The core object. the cgroup that wishes to account for some
  17. * resource may include this counter into its structures and use
  18. * the helpers described beyond
  19. */
  20. struct res_counter {
  21. /*
  22. * the current resource consumption level
  23. */
  24. unsigned long long usage;
  25. /*
  26. * the maximal value of the usage from the counter creation
  27. */
  28. unsigned long long max_usage;
  29. /*
  30. * the limit that usage cannot exceed
  31. */
  32. unsigned long long limit;
  33. /*
  34. * the limit that usage can be exceed
  35. */
  36. unsigned long long soft_limit;
  37. /*
  38. * the number of unsuccessful attempts to consume the resource
  39. */
  40. unsigned long long failcnt;
  41. /*
  42. * the lock to protect all of the above.
  43. * the routines below consider this to be IRQ-safe
  44. */
  45. spinlock_t lock;
  46. /*
  47. * Parent counter, used for hierarchial resource accounting
  48. */
  49. struct res_counter *parent;
  50. };
  51. #define RESOURCE_MAX (unsigned long long)LLONG_MAX
  52. /**
  53. * Helpers to interact with userspace
  54. * res_counter_read_u64() - returns the value of the specified member.
  55. * res_counter_read/_write - put/get the specified fields from the
  56. * res_counter struct to/from the user
  57. *
  58. * @counter: the counter in question
  59. * @member: the field to work with (see RES_xxx below)
  60. * @buf: the buffer to opeate on,...
  61. * @nbytes: its size...
  62. * @pos: and the offset.
  63. */
  64. u64 res_counter_read_u64(struct res_counter *counter, int member);
  65. ssize_t res_counter_read(struct res_counter *counter, int member,
  66. const char __user *buf, size_t nbytes, loff_t *pos,
  67. int (*read_strategy)(unsigned long long val, char *s));
  68. typedef int (*write_strategy_fn)(const char *buf, unsigned long long *val);
  69. int res_counter_memparse_write_strategy(const char *buf,
  70. unsigned long long *res);
  71. int res_counter_write(struct res_counter *counter, int member,
  72. const char *buffer, write_strategy_fn write_strategy);
  73. /*
  74. * the field descriptors. one for each member of res_counter
  75. */
  76. enum {
  77. RES_USAGE,
  78. RES_MAX_USAGE,
  79. RES_LIMIT,
  80. RES_FAILCNT,
  81. RES_SOFT_LIMIT,
  82. };
  83. /*
  84. * helpers for accounting
  85. */
  86. void res_counter_init(struct res_counter *counter, struct res_counter *parent);
  87. /*
  88. * charge - try to consume more resource.
  89. *
  90. * @counter: the counter
  91. * @val: the amount of the resource. each controller defines its own
  92. * units, e.g. numbers, bytes, Kbytes, etc
  93. *
  94. * returns 0 on success and <0 if the counter->usage will exceed the
  95. * counter->limit _locked call expects the counter->lock to be taken
  96. *
  97. * charge_nofail works the same, except that it charges the resource
  98. * counter unconditionally, and returns < 0 if the after the current
  99. * charge we are over limit.
  100. */
  101. int __must_check res_counter_charge_locked(struct res_counter *counter,
  102. unsigned long val);
  103. int __must_check res_counter_charge(struct res_counter *counter,
  104. unsigned long val, struct res_counter **limit_fail_at);
  105. int __must_check res_counter_charge_nofail(struct res_counter *counter,
  106. unsigned long val, struct res_counter **limit_fail_at);
  107. /*
  108. * uncharge - tell that some portion of the resource is released
  109. *
  110. * @counter: the counter
  111. * @val: the amount of the resource
  112. *
  113. * these calls check for usage underflow and show a warning on the console
  114. * _locked call expects the counter->lock to be taken
  115. */
  116. void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
  117. void res_counter_uncharge(struct res_counter *counter, unsigned long val);
  118. /**
  119. * res_counter_margin - calculate chargeable space of a counter
  120. * @cnt: the counter
  121. *
  122. * Returns the difference between the hard limit and the current usage
  123. * of resource counter @cnt.
  124. */
  125. static inline unsigned long long res_counter_margin(struct res_counter *cnt)
  126. {
  127. unsigned long long margin;
  128. unsigned long flags;
  129. spin_lock_irqsave(&cnt->lock, flags);
  130. if (cnt->limit > cnt->usage)
  131. margin = cnt->limit - cnt->usage;
  132. else
  133. margin = 0;
  134. spin_unlock_irqrestore(&cnt->lock, flags);
  135. return margin;
  136. }
  137. /**
  138. * Get the difference between the usage and the soft limit
  139. * @cnt: The counter
  140. *
  141. * Returns 0 if usage is less than or equal to soft limit
  142. * The difference between usage and soft limit, otherwise.
  143. */
  144. static inline unsigned long long
  145. res_counter_soft_limit_excess(struct res_counter *cnt)
  146. {
  147. unsigned long long excess;
  148. unsigned long flags;
  149. spin_lock_irqsave(&cnt->lock, flags);
  150. if (cnt->usage <= cnt->soft_limit)
  151. excess = 0;
  152. else
  153. excess = cnt->usage - cnt->soft_limit;
  154. spin_unlock_irqrestore(&cnt->lock, flags);
  155. return excess;
  156. }
  157. static inline void res_counter_reset_max(struct res_counter *cnt)
  158. {
  159. unsigned long flags;
  160. spin_lock_irqsave(&cnt->lock, flags);
  161. cnt->max_usage = cnt->usage;
  162. spin_unlock_irqrestore(&cnt->lock, flags);
  163. }
  164. static inline void res_counter_reset_failcnt(struct res_counter *cnt)
  165. {
  166. unsigned long flags;
  167. spin_lock_irqsave(&cnt->lock, flags);
  168. cnt->failcnt = 0;
  169. spin_unlock_irqrestore(&cnt->lock, flags);
  170. }
  171. static inline int res_counter_set_limit(struct res_counter *cnt,
  172. unsigned long long limit)
  173. {
  174. unsigned long flags;
  175. int ret = -EBUSY;
  176. spin_lock_irqsave(&cnt->lock, flags);
  177. if (cnt->usage <= limit) {
  178. cnt->limit = limit;
  179. ret = 0;
  180. }
  181. spin_unlock_irqrestore(&cnt->lock, flags);
  182. return ret;
  183. }
  184. static inline int
  185. res_counter_set_soft_limit(struct res_counter *cnt,
  186. unsigned long long soft_limit)
  187. {
  188. unsigned long flags;
  189. spin_lock_irqsave(&cnt->lock, flags);
  190. cnt->soft_limit = soft_limit;
  191. spin_unlock_irqrestore(&cnt->lock, flags);
  192. return 0;
  193. }
  194. #endif