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