timers-howto.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ===================================================================
  2. delays - Information on the various kernel delay / sleep mechanisms
  3. ===================================================================
  4. This document seeks to answer the common question: "What is the
  5. RightWay (TM) to insert a delay?"
  6. This question is most often faced by driver writers who have to
  7. deal with hardware delays and who may not be the most intimately
  8. familiar with the inner workings of the Linux Kernel.
  9. Inserting Delays
  10. ----------------
  11. The first, and most important, question you need to ask is "Is my
  12. code in an atomic context?" This should be followed closely by "Does
  13. it really need to delay in atomic context?" If so...
  14. ATOMIC CONTEXT:
  15. You must use the `*delay` family of functions. These
  16. functions use the jiffie estimation of clock speed
  17. and will busy wait for enough loop cycles to achieve
  18. the desired delay:
  19. ndelay(unsigned long nsecs)
  20. udelay(unsigned long usecs)
  21. mdelay(unsigned long msecs)
  22. udelay is the generally preferred API; ndelay-level
  23. precision may not actually exist on many non-PC devices.
  24. mdelay is macro wrapper around udelay, to account for
  25. possible overflow when passing large arguments to udelay.
  26. In general, use of mdelay is discouraged and code should
  27. be refactored to allow for the use of msleep.
  28. NON-ATOMIC CONTEXT:
  29. You should use the `*sleep[_range]` family of functions.
  30. There are a few more options here, while any of them may
  31. work correctly, using the "right" sleep function will
  32. help the scheduler, power management, and just make your
  33. driver better :)
  34. -- Backed by busy-wait loop:
  35. udelay(unsigned long usecs)
  36. -- Backed by hrtimers:
  37. usleep_range(unsigned long min, unsigned long max)
  38. -- Backed by jiffies / legacy_timers
  39. msleep(unsigned long msecs)
  40. msleep_interruptible(unsigned long msecs)
  41. Unlike the `*delay` family, the underlying mechanism
  42. driving each of these calls varies, thus there are
  43. quirks you should be aware of.
  44. SLEEPING FOR "A FEW" USECS ( < ~10us? ):
  45. * Use udelay
  46. - Why not usleep?
  47. On slower systems, (embedded, OR perhaps a speed-
  48. stepped PC!) the overhead of setting up the hrtimers
  49. for usleep *may* not be worth it. Such an evaluation
  50. will obviously depend on your specific situation, but
  51. it is something to be aware of.
  52. SLEEPING FOR ~USECS OR SMALL MSECS ( 10us - 20ms):
  53. * Use usleep_range
  54. - Why not msleep for (1ms - 20ms)?
  55. Explained originally here:
  56. http://lkml.org/lkml/2007/8/3/250
  57. msleep(1~20) may not do what the caller intends, and
  58. will often sleep longer (~20 ms actual sleep for any
  59. value given in the 1~20ms range). In many cases this
  60. is not the desired behavior.
  61. - Why is there no "usleep" / What is a good range?
  62. Since usleep_range is built on top of hrtimers, the
  63. wakeup will be very precise (ish), thus a simple
  64. usleep function would likely introduce a large number
  65. of undesired interrupts.
  66. With the introduction of a range, the scheduler is
  67. free to coalesce your wakeup with any other wakeup
  68. that may have happened for other reasons, or at the
  69. worst case, fire an interrupt for your upper bound.
  70. The larger a range you supply, the greater a chance
  71. that you will not trigger an interrupt; this should
  72. be balanced with what is an acceptable upper bound on
  73. delay / performance for your specific code path. Exact
  74. tolerances here are very situation specific, thus it
  75. is left to the caller to determine a reasonable range.
  76. SLEEPING FOR LARGER MSECS ( 10ms+ )
  77. * Use msleep or possibly msleep_interruptible
  78. - What's the difference?
  79. msleep sets the current task to TASK_UNINTERRUPTIBLE
  80. whereas msleep_interruptible sets the current task to
  81. TASK_INTERRUPTIBLE before scheduling the sleep. In
  82. short, the difference is whether the sleep can be ended
  83. early by a signal. In general, just use msleep unless
  84. you know you have a need for the interruptible variant.
  85. FLEXIBLE SLEEPING (any delay, uninterruptible)
  86. * Use fsleep