resource_counter.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. The Resource Counter
  2. The resource counter, declared at include/linux/res_counter.h,
  3. is supposed to facilitate the resource management by controllers
  4. by providing common stuff for accounting.
  5. This "stuff" includes the res_counter structure and routines
  6. to work with it.
  7. 1. Crucial parts of the res_counter structure
  8. a. unsigned long long usage
  9. The usage value shows the amount of a resource that is consumed
  10. by a group at a given time. The units of measurement should be
  11. determined by the controller that uses this counter. E.g. it can
  12. be bytes, items or any other unit the controller operates on.
  13. b. unsigned long long max_usage
  14. The maximal value of the usage over time.
  15. This value is useful when gathering statistical information about
  16. the particular group, as it shows the actual resource requirements
  17. for a particular group, not just some usage snapshot.
  18. c. unsigned long long limit
  19. The maximal allowed amount of resource to consume by the group. In
  20. case the group requests for more resources, so that the usage value
  21. would exceed the limit, the resource allocation is rejected (see
  22. the next section).
  23. d. unsigned long long failcnt
  24. The failcnt stands for "failures counter". This is the number of
  25. resource allocation attempts that failed.
  26. c. spinlock_t lock
  27. Protects changes of the above values.
  28. 2. Basic accounting routines
  29. a. void res_counter_init(struct res_counter *rc,
  30. struct res_counter *rc_parent)
  31. Initializes the resource counter. As usual, should be the first
  32. routine called for a new counter.
  33. The struct res_counter *parent can be used to define a hierarchical
  34. child -> parent relationship directly in the res_counter structure,
  35. NULL can be used to define no relationship.
  36. c. int res_counter_charge(struct res_counter *rc, unsigned long val,
  37. struct res_counter **limit_fail_at)
  38. When a resource is about to be allocated it has to be accounted
  39. with the appropriate resource counter (controller should determine
  40. which one to use on its own). This operation is called "charging".
  41. This is not very important which operation - resource allocation
  42. or charging - is performed first, but
  43. * if the allocation is performed first, this may create a
  44. temporary resource over-usage by the time resource counter is
  45. charged;
  46. * if the charging is performed first, then it should be uncharged
  47. on error path (if the one is called).
  48. If the charging fails and a hierarchical dependency exists, the
  49. limit_fail_at parameter is set to the particular res_counter element
  50. where the charging failed.
  51. d. int res_counter_charge_locked
  52. (struct res_counter *rc, unsigned long val, bool force)
  53. The same as res_counter_charge(), but it must not acquire/release the
  54. res_counter->lock internally (it must be called with res_counter->lock
  55. held). The force parameter indicates whether we can bypass the limit.
  56. e. u64 res_counter_uncharge[_locked]
  57. (struct res_counter *rc, unsigned long val)
  58. When a resource is released (freed) it should be de-accounted
  59. from the resource counter it was accounted to. This is called
  60. "uncharging". The return value of this function indicate the amount
  61. of charges still present in the counter.
  62. The _locked routines imply that the res_counter->lock is taken.
  63. f. u64 res_counter_uncharge_until
  64. (struct res_counter *rc, struct res_counter *top,
  65. unsinged long val)
  66. Almost same as res_cunter_uncharge() but propagation of uncharge
  67. stops when rc == top. This is useful when kill a res_coutner in
  68. child cgroup.
  69. 2.1 Other accounting routines
  70. There are more routines that may help you with common needs, like
  71. checking whether the limit is reached or resetting the max_usage
  72. value. They are all declared in include/linux/res_counter.h.
  73. 3. Analyzing the resource counter registrations
  74. a. If the failcnt value constantly grows, this means that the counter's
  75. limit is too tight. Either the group is misbehaving and consumes too
  76. many resources, or the configuration is not suitable for the group
  77. and the limit should be increased.
  78. b. The max_usage value can be used to quickly tune the group. One may
  79. set the limits to maximal values and either load the container with
  80. a common pattern or leave one for a while. After this the max_usage
  81. value shows the amount of memory the container would require during
  82. its common activity.
  83. Setting the limit a bit above this value gives a pretty good
  84. configuration that works in most of the cases.
  85. c. If the max_usage is much less than the limit, but the failcnt value
  86. is growing, then the group tries to allocate a big chunk of resource
  87. at once.
  88. d. If the max_usage is much less than the limit, but the failcnt value
  89. is 0, then this group is given too high limit, that it does not
  90. require. It is better to lower the limit a bit leaving more resource
  91. for other groups.
  92. 4. Communication with the control groups subsystem (cgroups)
  93. All the resource controllers that are using cgroups and resource counters
  94. should provide files (in the cgroup filesystem) to work with the resource
  95. counter fields. They are recommended to adhere to the following rules:
  96. a. File names
  97. Field name File name
  98. ---------------------------------------------------
  99. usage usage_in_<unit_of_measurement>
  100. max_usage max_usage_in_<unit_of_measurement>
  101. limit limit_in_<unit_of_measurement>
  102. failcnt failcnt
  103. lock no file :)
  104. b. Reading from file should show the corresponding field value in the
  105. appropriate format.
  106. c. Writing to file
  107. Field Expected behavior
  108. ----------------------------------
  109. usage prohibited
  110. max_usage reset to usage
  111. limit set the limit
  112. failcnt reset to zero
  113. 5. Usage example
  114. a. Declare a task group (take a look at cgroups subsystem for this) and
  115. fold a res_counter into it
  116. struct my_group {
  117. struct res_counter res;
  118. <other fields>
  119. }
  120. b. Put hooks in resource allocation/release paths
  121. int alloc_something(...)
  122. {
  123. if (res_counter_charge(res_counter_ptr, amount) < 0)
  124. return -ENOMEM;
  125. <allocate the resource and return to the caller>
  126. }
  127. void release_something(...)
  128. {
  129. res_counter_uncharge(res_counter_ptr, amount);
  130. <release the resource>
  131. }
  132. In order to keep the usage value self-consistent, both the
  133. "res_counter_ptr" and the "amount" in release_something() should be
  134. the same as they were in the alloc_something() when the releasing
  135. resource was allocated.
  136. c. Provide the way to read res_counter values and set them (the cgroups
  137. still can help with it).
  138. c. Compile and run :)