Back to home page

LXR

 
 

    


0001 /*
0002  * Dynamic byte queue limits.  See include/linux/dynamic_queue_limits.h
0003  *
0004  * Copyright (c) 2011, Tom Herbert <therbert@google.com>
0005  */
0006 #include <linux/types.h>
0007 #include <linux/kernel.h>
0008 #include <linux/jiffies.h>
0009 #include <linux/dynamic_queue_limits.h>
0010 #include <linux/compiler.h>
0011 #include <linux/export.h>
0012 
0013 #define POSDIFF(A, B) ((int)((A) - (B)) > 0 ? (A) - (B) : 0)
0014 #define AFTER_EQ(A, B) ((int)((A) - (B)) >= 0)
0015 
0016 /* Records completed count and recalculates the queue limit */
0017 void dql_completed(struct dql *dql, unsigned int count)
0018 {
0019     unsigned int inprogress, prev_inprogress, limit;
0020     unsigned int ovlimit, completed, num_queued;
0021     bool all_prev_completed;
0022 
0023     num_queued = ACCESS_ONCE(dql->num_queued);
0024 
0025     /* Can't complete more than what's in queue */
0026     BUG_ON(count > num_queued - dql->num_completed);
0027 
0028     completed = dql->num_completed + count;
0029     limit = dql->limit;
0030     ovlimit = POSDIFF(num_queued - dql->num_completed, limit);
0031     inprogress = num_queued - completed;
0032     prev_inprogress = dql->prev_num_queued - dql->num_completed;
0033     all_prev_completed = AFTER_EQ(completed, dql->prev_num_queued);
0034 
0035     if ((ovlimit && !inprogress) ||
0036         (dql->prev_ovlimit && all_prev_completed)) {
0037         /*
0038          * Queue considered starved if:
0039          *   - The queue was over-limit in the last interval,
0040          *     and there is no more data in the queue.
0041          *  OR
0042          *   - The queue was over-limit in the previous interval and
0043          *     when enqueuing it was possible that all queued data
0044          *     had been consumed.  This covers the case when queue
0045          *     may have becomes starved between completion processing
0046          *     running and next time enqueue was scheduled.
0047          *
0048          *     When queue is starved increase the limit by the amount
0049          *     of bytes both sent and completed in the last interval,
0050          *     plus any previous over-limit.
0051          */
0052         limit += POSDIFF(completed, dql->prev_num_queued) +
0053              dql->prev_ovlimit;
0054         dql->slack_start_time = jiffies;
0055         dql->lowest_slack = UINT_MAX;
0056     } else if (inprogress && prev_inprogress && !all_prev_completed) {
0057         /*
0058          * Queue was not starved, check if the limit can be decreased.
0059          * A decrease is only considered if the queue has been busy in
0060          * the whole interval (the check above).
0061          *
0062          * If there is slack, the amount of execess data queued above
0063          * the the amount needed to prevent starvation, the queue limit
0064          * can be decreased.  To avoid hysteresis we consider the
0065          * minimum amount of slack found over several iterations of the
0066          * completion routine.
0067          */
0068         unsigned int slack, slack_last_objs;
0069 
0070         /*
0071          * Slack is the maximum of
0072          *   - The queue limit plus previous over-limit minus twice
0073          *     the number of objects completed.  Note that two times
0074          *     number of completed bytes is a basis for an upper bound
0075          *     of the limit.
0076          *   - Portion of objects in the last queuing operation that
0077          *     was not part of non-zero previous over-limit.  That is
0078          *     "round down" by non-overlimit portion of the last
0079          *     queueing operation.
0080          */
0081         slack = POSDIFF(limit + dql->prev_ovlimit,
0082             2 * (completed - dql->num_completed));
0083         slack_last_objs = dql->prev_ovlimit ?
0084             POSDIFF(dql->prev_last_obj_cnt, dql->prev_ovlimit) : 0;
0085 
0086         slack = max(slack, slack_last_objs);
0087 
0088         if (slack < dql->lowest_slack)
0089             dql->lowest_slack = slack;
0090 
0091         if (time_after(jiffies,
0092                    dql->slack_start_time + dql->slack_hold_time)) {
0093             limit = POSDIFF(limit, dql->lowest_slack);
0094             dql->slack_start_time = jiffies;
0095             dql->lowest_slack = UINT_MAX;
0096         }
0097     }
0098 
0099     /* Enforce bounds on limit */
0100     limit = clamp(limit, dql->min_limit, dql->max_limit);
0101 
0102     if (limit != dql->limit) {
0103         dql->limit = limit;
0104         ovlimit = 0;
0105     }
0106 
0107     dql->adj_limit = limit + completed;
0108     dql->prev_ovlimit = ovlimit;
0109     dql->prev_last_obj_cnt = dql->last_obj_cnt;
0110     dql->num_completed = completed;
0111     dql->prev_num_queued = num_queued;
0112 }
0113 EXPORT_SYMBOL(dql_completed);
0114 
0115 void dql_reset(struct dql *dql)
0116 {
0117     /* Reset all dynamic values */
0118     dql->limit = 0;
0119     dql->num_queued = 0;
0120     dql->num_completed = 0;
0121     dql->last_obj_cnt = 0;
0122     dql->prev_num_queued = 0;
0123     dql->prev_last_obj_cnt = 0;
0124     dql->prev_ovlimit = 0;
0125     dql->lowest_slack = UINT_MAX;
0126     dql->slack_start_time = jiffies;
0127 }
0128 EXPORT_SYMBOL(dql_reset);
0129 
0130 int dql_init(struct dql *dql, unsigned hold_time)
0131 {
0132     dql->max_limit = DQL_MAX_LIMIT;
0133     dql->min_limit = 0;
0134     dql->slack_hold_time = hold_time;
0135     dql_reset(dql);
0136     return 0;
0137 }
0138 EXPORT_SYMBOL(dql_init);