timers-howto.txt 3.8 KB

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