res_counter.h 3.6 KB

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