Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Dynamic queue limits (dql) - Definitions
0004  *
0005  * Copyright (c) 2011, Tom Herbert <therbert@google.com>
0006  *
0007  * This header file contains the definitions for dynamic queue limits (dql).
0008  * dql would be used in conjunction with a producer/consumer type queue
0009  * (possibly a HW queue).  Such a queue would have these general properties:
0010  *
0011  *   1) Objects are queued up to some limit specified as number of objects.
0012  *   2) Periodically a completion process executes which retires consumed
0013  *      objects.
0014  *   3) Starvation occurs when limit has been reached, all queued data has
0015  *      actually been consumed, but completion processing has not yet run
0016  *      so queuing new data is blocked.
0017  *   4) Minimizing the amount of queued data is desirable.
0018  *
0019  * The goal of dql is to calculate the limit as the minimum number of objects
0020  * needed to prevent starvation.
0021  *
0022  * The primary functions of dql are:
0023  *    dql_queued - called when objects are enqueued to record number of objects
0024  *    dql_avail - returns how many objects are available to be queued based
0025  *      on the object limit and how many objects are already enqueued
0026  *    dql_completed - called at completion time to indicate how many objects
0027  *      were retired from the queue
0028  *
0029  * The dql implementation does not implement any locking for the dql data
0030  * structures, the higher layer should provide this.  dql_queued should
0031  * be serialized to prevent concurrent execution of the function; this
0032  * is also true for  dql_completed.  However, dql_queued and dlq_completed  can
0033  * be executed concurrently (i.e. they can be protected by different locks).
0034  */
0035 
0036 #ifndef _LINUX_DQL_H
0037 #define _LINUX_DQL_H
0038 
0039 #ifdef __KERNEL__
0040 
0041 #include <asm/bug.h>
0042 
0043 struct dql {
0044     /* Fields accessed in enqueue path (dql_queued) */
0045     unsigned int    num_queued;     /* Total ever queued */
0046     unsigned int    adj_limit;      /* limit + num_completed */
0047     unsigned int    last_obj_cnt;       /* Count at last queuing */
0048 
0049     /* Fields accessed only by completion path (dql_completed) */
0050 
0051     unsigned int    limit ____cacheline_aligned_in_smp; /* Current limit */
0052     unsigned int    num_completed;      /* Total ever completed */
0053 
0054     unsigned int    prev_ovlimit;       /* Previous over limit */
0055     unsigned int    prev_num_queued;    /* Previous queue total */
0056     unsigned int    prev_last_obj_cnt;  /* Previous queuing cnt */
0057 
0058     unsigned int    lowest_slack;       /* Lowest slack found */
0059     unsigned long   slack_start_time;   /* Time slacks seen */
0060 
0061     /* Configuration */
0062     unsigned int    max_limit;      /* Max limit */
0063     unsigned int    min_limit;      /* Minimum limit */
0064     unsigned int    slack_hold_time;    /* Time to measure slack */
0065 };
0066 
0067 /* Set some static maximums */
0068 #define DQL_MAX_OBJECT (UINT_MAX / 16)
0069 #define DQL_MAX_LIMIT ((UINT_MAX / 2) - DQL_MAX_OBJECT)
0070 
0071 /*
0072  * Record number of objects queued. Assumes that caller has already checked
0073  * availability in the queue with dql_avail.
0074  */
0075 static inline void dql_queued(struct dql *dql, unsigned int count)
0076 {
0077     BUG_ON(count > DQL_MAX_OBJECT);
0078 
0079     dql->last_obj_cnt = count;
0080 
0081     /* We want to force a write first, so that cpu do not attempt
0082      * to get cache line containing last_obj_cnt, num_queued, adj_limit
0083      * in Shared state, but directly does a Request For Ownership
0084      * It is only a hint, we use barrier() only.
0085      */
0086     barrier();
0087 
0088     dql->num_queued += count;
0089 }
0090 
0091 /* Returns how many objects can be queued, < 0 indicates over limit. */
0092 static inline int dql_avail(const struct dql *dql)
0093 {
0094     return READ_ONCE(dql->adj_limit) - READ_ONCE(dql->num_queued);
0095 }
0096 
0097 /* Record number of completed objects and recalculate the limit. */
0098 void dql_completed(struct dql *dql, unsigned int count);
0099 
0100 /* Reset dql state */
0101 void dql_reset(struct dql *dql);
0102 
0103 /* Initialize dql state */
0104 void dql_init(struct dql *dql, unsigned int hold_time);
0105 
0106 #endif /* _KERNEL_ */
0107 
0108 #endif /* _LINUX_DQL_H */