dynamic_queue_limits.c 4.3 KB

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