dynamic_queue_limits.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Dynamic byte queue limits. See include/linux/dynamic_queue_limits.h
  3. *
  4. * Copyright (c) 2011, Tom Herbert <therbert@google.com>
  5. */
  6. #include <linux/types.h>
  7. #include <linux/kernel.h>
  8. #include <linux/jiffies.h>
  9. #include <linux/dynamic_queue_limits.h>
  10. #include <linux/compiler.h>
  11. #include <linux/export.h>
  12. #define POSDIFF(A, B) ((int)((A) - (B)) > 0 ? (A) - (B) : 0)
  13. #define AFTER_EQ(A, B) ((int)((A) - (B)) >= 0)
  14. /* Records completed count and recalculates the queue limit */
  15. void dql_completed(struct dql *dql, unsigned int count)
  16. {
  17. unsigned int inprogress, prev_inprogress, limit;
  18. unsigned int ovlimit, completed, num_queued;
  19. bool all_prev_completed;
  20. num_queued = ACCESS_ONCE(dql->num_queued);
  21. /* Can't complete more than what's in queue */
  22. BUG_ON(count > num_queued - dql->num_completed);
  23. completed = dql->num_completed + count;
  24. limit = dql->limit;
  25. ovlimit = POSDIFF(num_queued - dql->num_completed, limit);
  26. inprogress = num_queued - completed;
  27. prev_inprogress = dql->prev_num_queued - dql->num_completed;
  28. all_prev_completed = AFTER_EQ(completed, dql->prev_num_queued);
  29. if ((ovlimit && !inprogress) ||
  30. (dql->prev_ovlimit && all_prev_completed)) {
  31. /*
  32. * Queue considered starved if:
  33. * - The queue was over-limit in the last interval,
  34. * and there is no more data in the queue.
  35. * OR
  36. * - The queue was over-limit in the previous interval and
  37. * when enqueuing it was possible that all queued data
  38. * had been consumed. This covers the case when queue
  39. * may have becomes starved between completion processing
  40. * running and next time enqueue was scheduled.
  41. *
  42. * When queue is starved increase the limit by the amount
  43. * of bytes both sent and completed in the last interval,
  44. * plus any previous over-limit.
  45. */
  46. limit += POSDIFF(completed, dql->prev_num_queued) +
  47. dql->prev_ovlimit;
  48. dql->slack_start_time = jiffies;
  49. dql->lowest_slack = UINT_MAX;
  50. } else if (inprogress && prev_inprogress && !all_prev_completed) {
  51. /*
  52. * Queue was not starved, check if the limit can be decreased.
  53. * A decrease is only considered if the queue has been busy in
  54. * the whole interval (the check above).
  55. *
  56. * If there is slack, the amount of execess data queued above
  57. * the the amount needed to prevent starvation, the queue limit
  58. * can be decreased. To avoid hysteresis we consider the
  59. * minimum amount of slack found over several iterations of the
  60. * completion routine.
  61. */
  62. unsigned int slack, slack_last_objs;
  63. /*
  64. * Slack is the maximum of
  65. * - The queue limit plus previous over-limit minus twice
  66. * the number of objects completed. Note that two times
  67. * number of completed bytes is a basis for an upper bound
  68. * of the limit.
  69. * - Portion of objects in the last queuing operation that
  70. * was not part of non-zero previous over-limit. That is
  71. * "round down" by non-overlimit portion of the last
  72. * queueing operation.
  73. */
  74. slack = POSDIFF(limit + dql->prev_ovlimit,
  75. 2 * (completed - dql->num_completed));
  76. slack_last_objs = dql->prev_ovlimit ?
  77. POSDIFF(dql->prev_last_obj_cnt, dql->prev_ovlimit) : 0;
  78. slack = max(slack, slack_last_objs);
  79. if (slack < dql->lowest_slack)
  80. dql->lowest_slack = slack;
  81. if (time_after(jiffies,
  82. dql->slack_start_time + dql->slack_hold_time)) {
  83. limit = POSDIFF(limit, dql->lowest_slack);
  84. dql->slack_start_time = jiffies;
  85. dql->lowest_slack = UINT_MAX;
  86. }
  87. }
  88. /* Enforce bounds on limit */
  89. limit = clamp(limit, dql->min_limit, dql->max_limit);
  90. if (limit != dql->limit) {
  91. dql->limit = limit;
  92. ovlimit = 0;
  93. }
  94. dql->adj_limit = limit + completed;
  95. dql->prev_ovlimit = ovlimit;
  96. dql->prev_last_obj_cnt = dql->last_obj_cnt;
  97. dql->num_completed = completed;
  98. dql->prev_num_queued = num_queued;
  99. }
  100. EXPORT_SYMBOL(dql_completed);
  101. void dql_reset(struct dql *dql)
  102. {
  103. /* Reset all dynamic values */
  104. dql->limit = 0;
  105. dql->num_queued = 0;
  106. dql->num_completed = 0;
  107. dql->last_obj_cnt = 0;
  108. dql->prev_num_queued = 0;
  109. dql->prev_last_obj_cnt = 0;
  110. dql->prev_ovlimit = 0;
  111. dql->lowest_slack = UINT_MAX;
  112. dql->slack_start_time = jiffies;
  113. }
  114. EXPORT_SYMBOL(dql_reset);
  115. int dql_init(struct dql *dql, unsigned hold_time)
  116. {
  117. dql->max_limit = DQL_MAX_LIMIT;
  118. dql->min_limit = 0;
  119. dql->slack_hold_time = hold_time;
  120. dql_reset(dql);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(dql_init);