resource_counter.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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)
  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).
  56. e. void 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".
  61. The _locked routines imply that the res_counter->lock is taken.
  62. 2.1 Other accounting routines
  63. There are more routines that may help you with common needs, like
  64. checking whether the limit is reached or resetting the max_usage
  65. value. They are all declared in include/linux/res_counter.h.
  66. 3. Analyzing the resource counter registrations
  67. a. If the failcnt value constantly grows, this means that the counter's
  68. limit is too tight. Either the group is misbehaving and consumes too
  69. many resources, or the configuration is not suitable for the group
  70. and the limit should be increased.
  71. b. The max_usage value can be used to quickly tune the group. One may
  72. set the limits to maximal values and either load the container with
  73. a common pattern or leave one for a while. After this the max_usage
  74. value shows the amount of memory the container would require during
  75. its common activity.
  76. Setting the limit a bit above this value gives a pretty good
  77. configuration that works in most of the cases.
  78. c. If the max_usage is much less than the limit, but the failcnt value
  79. is growing, then the group tries to allocate a big chunk of resource
  80. at once.
  81. d. If the max_usage is much less than the limit, but the failcnt value
  82. is 0, then this group is given too high limit, that it does not
  83. require. It is better to lower the limit a bit leaving more resource
  84. for other groups.
  85. 4. Communication with the control groups subsystem (cgroups)
  86. All the resource controllers that are using cgroups and resource counters
  87. should provide files (in the cgroup filesystem) to work with the resource
  88. counter fields. They are recommended to adhere to the following rules:
  89. a. File names
  90. Field name File name
  91. ---------------------------------------------------
  92. usage usage_in_<unit_of_measurement>
  93. max_usage max_usage_in_<unit_of_measurement>
  94. limit limit_in_<unit_of_measurement>
  95. failcnt failcnt
  96. lock no file :)
  97. b. Reading from file should show the corresponding field value in the
  98. appropriate format.
  99. c. Writing to file
  100. Field Expected behavior
  101. ----------------------------------
  102. usage prohibited
  103. max_usage reset to usage
  104. limit set the limit
  105. failcnt reset to zero
  106. 5. Usage example
  107. a. Declare a task group (take a look at cgroups subsystem for this) and
  108. fold a res_counter into it
  109. struct my_group {
  110. struct res_counter res;
  111. <other fields>
  112. }
  113. b. Put hooks in resource allocation/release paths
  114. int alloc_something(...)
  115. {
  116. if (res_counter_charge(res_counter_ptr, amount) < 0)
  117. return -ENOMEM;
  118. <allocate the resource and return to the caller>
  119. }
  120. void release_something(...)
  121. {
  122. res_counter_uncharge(res_counter_ptr, amount);
  123. <release the resource>
  124. }
  125. In order to keep the usage value self-consistent, both the
  126. "res_counter_ptr" and the "amount" in release_something() should be
  127. the same as they were in the alloc_something() when the releasing
  128. resource was allocated.
  129. c. Provide the way to read res_counter values and set them (the cgroups
  130. still can help with it).
  131. c. Compile and run :)