resource_counter.txt 6.4 KB

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