res_counter.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. */
  12. #include <linux/cgroup.h>
  13. /*
  14. * The core object. the cgroup that wishes to account for some
  15. * resource may include this counter into its structures and use
  16. * the helpers described beyond
  17. */
  18. struct res_counter {
  19. /*
  20. * the current resource consumption level
  21. */
  22. unsigned long usage;
  23. /*
  24. * the limit that usage cannot exceed
  25. */
  26. unsigned long limit;
  27. /*
  28. * the number of unsuccessful attempts to consume the resource
  29. */
  30. unsigned long failcnt;
  31. /*
  32. * the lock to protect all of the above.
  33. * the routines below consider this to be IRQ-safe
  34. */
  35. spinlock_t lock;
  36. };
  37. /*
  38. * Helpers to interact with userspace
  39. * res_counter_read/_write - put/get the specified fields from the
  40. * res_counter struct to/from the user
  41. *
  42. * @counter: the counter in question
  43. * @member: the field to work with (see RES_xxx below)
  44. * @buf: the buffer to opeate on,...
  45. * @nbytes: its size...
  46. * @pos: and the offset.
  47. */
  48. ssize_t res_counter_read(struct res_counter *counter, int member,
  49. const char __user *buf, size_t nbytes, loff_t *pos);
  50. ssize_t res_counter_write(struct res_counter *counter, int member,
  51. const char __user *buf, size_t nbytes, loff_t *pos);
  52. /*
  53. * the field descriptors. one for each member of res_counter
  54. */
  55. enum {
  56. RES_USAGE,
  57. RES_LIMIT,
  58. RES_FAILCNT,
  59. };
  60. /*
  61. * helpers for accounting
  62. */
  63. void res_counter_init(struct res_counter *counter);
  64. /*
  65. * charge - try to consume more resource.
  66. *
  67. * @counter: the counter
  68. * @val: the amount of the resource. each controller defines its own
  69. * units, e.g. numbers, bytes, Kbytes, etc
  70. *
  71. * returns 0 on success and <0 if the counter->usage will exceed the
  72. * counter->limit _locked call expects the counter->lock to be taken
  73. */
  74. int res_counter_charge_locked(struct res_counter *counter, unsigned long val);
  75. int res_counter_charge(struct res_counter *counter, unsigned long val);
  76. /*
  77. * uncharge - tell that some portion of the resource is released
  78. *
  79. * @counter: the counter
  80. * @val: the amount of the resource
  81. *
  82. * these calls check for usage underflow and show a warning on the console
  83. * _locked call expects the counter->lock to be taken
  84. */
  85. void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
  86. void res_counter_uncharge(struct res_counter *counter, unsigned long val);
  87. static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
  88. {
  89. if (cnt->usage < cnt->limit)
  90. return true;
  91. return false;
  92. }
  93. /*
  94. * Helper function to detect if the cgroup is within it's limit or
  95. * not. It's currently called from cgroup_rss_prepare()
  96. */
  97. static inline bool res_counter_check_under_limit(struct res_counter *cnt)
  98. {
  99. bool ret;
  100. unsigned long flags;
  101. spin_lock_irqsave(&cnt->lock, flags);
  102. ret = res_counter_limit_check_locked(cnt);
  103. spin_unlock_irqrestore(&cnt->lock, flags);
  104. return ret;
  105. }
  106. #endif