dynamic_queue_limits.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef _LINUX_DQL_H
  35. #define _LINUX_DQL_H
  36. #ifdef __KERNEL__
  37. struct dql {
  38. /* Fields accessed in enqueue path (dql_queued) */
  39. unsigned int num_queued; /* Total ever queued */
  40. unsigned int adj_limit; /* limit + num_completed */
  41. unsigned int last_obj_cnt; /* Count at last queuing */
  42. /* Fields accessed only by completion path (dql_completed) */
  43. unsigned int limit ____cacheline_aligned_in_smp; /* Current limit */
  44. unsigned int num_completed; /* Total ever completed */
  45. unsigned int prev_ovlimit; /* Previous over limit */
  46. unsigned int prev_num_queued; /* Previous queue total */
  47. unsigned int prev_last_obj_cnt; /* Previous queuing cnt */
  48. unsigned int lowest_slack; /* Lowest slack found */
  49. unsigned long slack_start_time; /* Time slacks seen */
  50. /* Configuration */
  51. unsigned int max_limit; /* Max limit */
  52. unsigned int min_limit; /* Minimum limit */
  53. unsigned int slack_hold_time; /* Time to measure slack */
  54. };
  55. /* Set some static maximums */
  56. #define DQL_MAX_OBJECT (UINT_MAX / 16)
  57. #define DQL_MAX_LIMIT ((UINT_MAX / 2) - DQL_MAX_OBJECT)
  58. /*
  59. * Record number of objects queued. Assumes that caller has already checked
  60. * availability in the queue with dql_avail.
  61. */
  62. static inline void dql_queued(struct dql *dql, unsigned int count)
  63. {
  64. BUG_ON(count > DQL_MAX_OBJECT);
  65. dql->num_queued += count;
  66. dql->last_obj_cnt = count;
  67. }
  68. /* Returns how many objects can be queued, < 0 indicates over limit. */
  69. static inline int dql_avail(const struct dql *dql)
  70. {
  71. return dql->adj_limit - dql->num_queued;
  72. }
  73. /* Record number of completed objects and recalculate the limit. */
  74. void dql_completed(struct dql *dql, unsigned int count);
  75. /* Reset dql state */
  76. void dql_reset(struct dql *dql);
  77. /* Initialize dql state */
  78. int dql_init(struct dql *dql, unsigned hold_time);
  79. #endif /* _KERNEL_ */
  80. #endif /* _LINUX_DQL_H */