aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/linux/dynamic_queue_limits.h97
-rw-r--r--lib/Kconfig3
-rw-r--r--lib/Makefile2
-rw-r--r--lib/dynamic_queue_limits.c133
4 files changed, 235 insertions, 0 deletions
diff --git a/include/linux/dynamic_queue_limits.h b/include/linux/dynamic_queue_limits.h
new file mode 100644
index 000000000000..5621547d631b
--- /dev/null
+++ b/include/linux/dynamic_queue_limits.h
@@ -0,0 +1,97 @@
1/*
2 * Dynamic queue limits (dql) - Definitions
3 *
4 * Copyright (c) 2011, Tom Herbert <therbert@google.com>
5 *
6 * This header file contains the definitions for dynamic queue limits (dql).
7 * dql would be used in conjunction with a producer/consumer type queue
8 * (possibly a HW queue). Such a queue would have these general properties:
9 *
10 * 1) Objects are queued up to some limit specified as number of objects.
11 * 2) Periodically a completion process executes which retires consumed
12 * objects.
13 * 3) Starvation occurs when limit has been reached, all queued data has
14 * actually been consumed, but completion processing has not yet run
15 * so queuing new data is blocked.
16 * 4) Minimizing the amount of queued data is desirable.
17 *
18 * The goal of dql is to calculate the limit as the minimum number of objects
19 * needed to prevent starvation.
20 *
21 * The primary functions of dql are:
22 * dql_queued - called when objects are enqueued to record number of objects
23 * dql_avail - returns how many objects are available to be queued based
24 * on the object limit and how many objects are already enqueued
25 * dql_completed - called at completion time to indicate how many objects
26 * were retired from the queue
27 *
28 * The dql implementation does not implement any locking for the dql data
29 * structures, the higher layer should provide this. dql_queued should
30 * be serialized to prevent concurrent execution of the function; this
31 * is also true for dql_completed. However, dql_queued and dlq_completed can
32 * be executed concurrently (i.e. they can be protected by different locks).
33 */
34
35#ifndef _LINUX_DQL_H
36#define _LINUX_DQL_H
37
38#ifdef __KERNEL__
39
40struct dql {
41 /* Fields accessed in enqueue path (dql_queued) */
42 unsigned int num_queued; /* Total ever queued */
43 unsigned int adj_limit; /* limit + num_completed */
44 unsigned int last_obj_cnt; /* Count at last queuing */
45
46 /* Fields accessed only by completion path (dql_completed) */
47
48 unsigned int limit ____cacheline_aligned_in_smp; /* Current limit */
49 unsigned int num_completed; /* Total ever completed */
50
51 unsigned int prev_ovlimit; /* Previous over limit */
52 unsigned int prev_num_queued; /* Previous queue total */
53 unsigned int prev_last_obj_cnt; /* Previous queuing cnt */
54
55 unsigned int lowest_slack; /* Lowest slack found */
56 unsigned long slack_start_time; /* Time slacks seen */
57
58 /* Configuration */
59 unsigned int max_limit; /* Max limit */
60 unsigned int min_limit; /* Minimum limit */
61 unsigned int slack_hold_time; /* Time to measure slack */
62};
63
64/* Set some static maximums */
65#define DQL_MAX_OBJECT (UINT_MAX / 16)
66#define DQL_MAX_LIMIT ((UINT_MAX / 2) - DQL_MAX_OBJECT)
67
68/*
69 * Record number of objects queued. Assumes that caller has already checked
70 * availability in the queue with dql_avail.
71 */
72static inline void dql_queued(struct dql *dql, unsigned int count)
73{
74 BUG_ON(count > DQL_MAX_OBJECT);
75
76 dql->num_queued += count;
77 dql->last_obj_cnt = count;
78}
79
80/* Returns how many objects can be queued, < 0 indicates over limit. */
81static inline int dql_avail(const struct dql *dql)
82{
83 return dql->adj_limit - dql->num_queued;
84}
85
86/* Record number of completed objects and recalculate the limit. */
87void dql_completed(struct dql *dql, unsigned int count);
88
89/* Reset dql state */
90void dql_reset(struct dql *dql);
91
92/* Initialize dql state */
93int dql_init(struct dql *dql, unsigned hold_time);
94
95#endif /* _KERNEL_ */
96
97#endif /* _LINUX_DQL_H */
diff --git a/lib/Kconfig b/lib/Kconfig
index 32f3e5ae2be5..63b5782732ed 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -244,6 +244,9 @@ config CPU_RMAP
244 bool 244 bool
245 depends on SMP 245 depends on SMP
246 246
247config DQL
248 bool
249
247# 250#
248# Netlink attribute parsing support is select'ed if needed 251# Netlink attribute parsing support is select'ed if needed
249# 252#
diff --git a/lib/Makefile b/lib/Makefile
index a4da283f5dc0..ff00d4dcb7ed 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -115,6 +115,8 @@ obj-$(CONFIG_CPU_RMAP) += cpu_rmap.o
115 115
116obj-$(CONFIG_CORDIC) += cordic.o 116obj-$(CONFIG_CORDIC) += cordic.o
117 117
118obj-$(CONFIG_DQL) += dynamic_queue_limits.o
119
118hostprogs-y := gen_crc32table 120hostprogs-y := gen_crc32table
119clean-files := crc32table.h 121clean-files := crc32table.h
120 122
diff --git a/lib/dynamic_queue_limits.c b/lib/dynamic_queue_limits.c
new file mode 100644
index 000000000000..3d1bdcdd7db4
--- /dev/null
+++ b/lib/dynamic_queue_limits.c
@@ -0,0 +1,133 @@
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/dynamic_queue_limits.h>
11
12#define POSDIFF(A, B) ((A) > (B) ? (A) - (B) : 0)
13
14/* Records completed count and recalculates the queue limit */
15void dql_completed(struct dql *dql, unsigned int count)
16{
17 unsigned int inprogress, prev_inprogress, limit;
18 unsigned int ovlimit, all_prev_completed, completed;
19
20 /* Can't complete more than what's in queue */
21 BUG_ON(count > dql->num_queued - dql->num_completed);
22
23 completed = dql->num_completed + count;
24 limit = dql->limit;
25 ovlimit = POSDIFF(dql->num_queued - dql->num_completed, limit);
26 inprogress = dql->num_queued - completed;
27 prev_inprogress = dql->prev_num_queued - dql->num_completed;
28 all_prev_completed = POSDIFF(completed, dql->prev_num_queued);
29
30 if ((ovlimit && !inprogress) ||
31 (dql->prev_ovlimit && all_prev_completed)) {
32 /*
33 * Queue considered starved if:
34 * - The queue was over-limit in the last interval,
35 * and there is no more data in the queue.
36 * OR
37 * - The queue was over-limit in the previous interval and
38 * when enqueuing it was possible that all queued data
39 * had been consumed. This covers the case when queue
40 * may have becomes starved between completion processing
41 * running and next time enqueue was scheduled.
42 *
43 * When queue is starved increase the limit by the amount
44 * of bytes both sent and completed in the last interval,
45 * plus any previous over-limit.
46 */
47 limit += POSDIFF(completed, dql->prev_num_queued) +
48 dql->prev_ovlimit;
49 dql->slack_start_time = jiffies;
50 dql->lowest_slack = UINT_MAX;
51 } else if (inprogress && prev_inprogress && !all_prev_completed) {
52 /*
53 * Queue was not starved, check if the limit can be decreased.
54 * A decrease is only considered if the queue has been busy in
55 * the whole interval (the check above).
56 *
57 * If there is slack, the amount of execess data queued above
58 * the the amount needed to prevent starvation, the queue limit
59 * can be decreased. To avoid hysteresis we consider the
60 * minimum amount of slack found over several iterations of the
61 * completion routine.
62 */
63 unsigned int slack, slack_last_objs;
64
65 /*
66 * Slack is the maximum of
67 * - The queue limit plus previous over-limit minus twice
68 * the number of objects completed. Note that two times
69 * number of completed bytes is a basis for an upper bound
70 * of the limit.
71 * - Portion of objects in the last queuing operation that
72 * was not part of non-zero previous over-limit. That is
73 * "round down" by non-overlimit portion of the last
74 * queueing operation.
75 */
76 slack = POSDIFF(limit + dql->prev_ovlimit,
77 2 * (completed - dql->num_completed));
78 slack_last_objs = dql->prev_ovlimit ?
79 POSDIFF(dql->prev_last_obj_cnt, dql->prev_ovlimit) : 0;
80
81 slack = max(slack, slack_last_objs);
82
83 if (slack < dql->lowest_slack)
84 dql->lowest_slack = slack;
85
86 if (time_after(jiffies,
87 dql->slack_start_time + dql->slack_hold_time)) {
88 limit = POSDIFF(limit, dql->lowest_slack);
89 dql->slack_start_time = jiffies;
90 dql->lowest_slack = UINT_MAX;
91 }
92 }
93
94 /* Enforce bounds on limit */
95 limit = clamp(limit, dql->min_limit, dql->max_limit);
96
97 if (limit != dql->limit) {
98 dql->limit = limit;
99 ovlimit = 0;
100 }
101
102 dql->adj_limit = limit + completed;
103 dql->prev_ovlimit = ovlimit;
104 dql->prev_last_obj_cnt = dql->last_obj_cnt;
105 dql->num_completed = completed;
106 dql->prev_num_queued = dql->num_queued;
107}
108EXPORT_SYMBOL(dql_completed);
109
110void dql_reset(struct dql *dql)
111{
112 /* Reset all dynamic values */
113 dql->limit = 0;
114 dql->num_queued = 0;
115 dql->num_completed = 0;
116 dql->last_obj_cnt = 0;
117 dql->prev_num_queued = 0;
118 dql->prev_last_obj_cnt = 0;
119 dql->prev_ovlimit = 0;
120 dql->lowest_slack = UINT_MAX;
121 dql->slack_start_time = jiffies;
122}
123EXPORT_SYMBOL(dql_reset);
124
125int dql_init(struct dql *dql, unsigned hold_time)
126{
127 dql->max_limit = DQL_MAX_LIMIT;
128 dql->min_limit = 0;
129 dql->slack_hold_time = hold_time;
130 dql_reset(dql);
131 return 0;
132}
133EXPORT_SYMBOL(dql_init);