clock.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-12 Bernard first version
  9. * 2006-05-27 Bernard add support for same priority thread schedule
  10. * 2006-08-10 Bernard remove the last rt_schedule in rt_tick_increase
  11. * 2010-03-08 Bernard remove rt_passed_second
  12. * 2010-05-20 Bernard fix the tick exceeds the maximum limits
  13. * 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca
  14. * 2011-06-26 Bernard add rt_tick_set function.
  15. * 2018-11-22 Jesven add per cpu tick
  16. * 2020-12-29 Meco Man implement rt_tick_get_millisecond()
  17. * 2021-06-01 Meco Man add critical section projection for rt_tick_increase()
  18. * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  19. * 2023-10-16 RiceChen fix: only the main core detection rt_timer_check(), in SMP mode
  20. */
  21. #include <rthw.h>
  22. #include <rtthread.h>
  23. #include <rtatomic.h>
  24. #if defined(RT_USING_SMART) && defined(RT_USING_VDSO)
  25. #include <vdso.h>
  26. #endif
  27. #ifdef RT_USING_SMP
  28. #define rt_tick rt_cpu_index(0)->tick
  29. #else
  30. static volatile rt_atomic_t rt_tick = 0;
  31. #endif /* RT_USING_SMP */
  32. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  33. static void (*rt_tick_hook)(void);
  34. /**
  35. * @addtogroup Hook
  36. */
  37. /**@{*/
  38. /**
  39. * @brief This function will set a hook function, which will be invoked when tick increase
  40. *
  41. *
  42. * @param hook the hook function
  43. */
  44. void rt_tick_sethook(void (*hook)(void))
  45. {
  46. rt_tick_hook = hook;
  47. }
  48. /**@}*/
  49. #endif /* RT_USING_HOOK */
  50. /**
  51. * @addtogroup Clock
  52. */
  53. /**@{*/
  54. /**
  55. * @brief This function will return current tick from operating system startup.
  56. *
  57. * @return Return current tick.
  58. */
  59. rt_tick_t rt_tick_get(void)
  60. {
  61. /* return the global tick */
  62. return (rt_tick_t)rt_atomic_load(&(rt_tick));
  63. }
  64. RTM_EXPORT(rt_tick_get);
  65. /**
  66. * @brief This function will set current tick.
  67. *
  68. * @param tick is the value that you will set.
  69. */
  70. void rt_tick_set(rt_tick_t tick)
  71. {
  72. rt_atomic_store(&(rt_tick), tick);
  73. }
  74. #ifdef RT_USING_CPU_USAGE_TRACER
  75. static void _update_process_times(rt_tick_t tick)
  76. {
  77. struct rt_thread *thread = rt_thread_self();
  78. struct rt_cpu *pcpu = rt_cpu_self();
  79. if (!LWP_IS_USER_MODE(thread))
  80. {
  81. thread->user_time += tick;
  82. pcpu->cpu_stat.user += tick;
  83. }
  84. else
  85. {
  86. thread->system_time += tick;
  87. if (thread == pcpu->idle_thread)
  88. {
  89. pcpu->cpu_stat.idle += tick;
  90. }
  91. else
  92. {
  93. pcpu->cpu_stat.system += tick;
  94. }
  95. }
  96. }
  97. #else
  98. #define _update_process_times(tick)
  99. #endif /* RT_USING_CPU_USAGE_TRACER */
  100. /**
  101. * @brief This function will notify kernel there is one tick passed.
  102. * Normally, this function is invoked by clock ISR.
  103. */
  104. void rt_tick_increase(void)
  105. {
  106. RT_ASSERT(rt_interrupt_get_nest() > 0);
  107. RT_OBJECT_HOOK_CALL(rt_tick_hook, ());
  108. /* tracing cpu usage */
  109. _update_process_times(1);
  110. /* increase the global tick */
  111. #ifdef RT_USING_SMP
  112. /* get percpu and increase the tick */
  113. rt_atomic_add(&(rt_cpu_self()->tick), 1);
  114. #else
  115. rt_atomic_add(&(rt_tick), 1);
  116. #endif /* RT_USING_SMP */
  117. /* check time slice */
  118. rt_sched_tick_increase(1);
  119. /* check timer */
  120. #ifdef RT_USING_SMP
  121. if (rt_cpu_get_id() != 0)
  122. {
  123. return;
  124. }
  125. #endif
  126. rt_timer_check();
  127. }
  128. /**
  129. * @brief This function will notify kernel there is n tick passed.
  130. * Normally, this function is invoked by clock ISR.
  131. */
  132. void rt_tick_increase_tick(rt_tick_t tick)
  133. {
  134. RT_ASSERT(rt_interrupt_get_nest() > 0);
  135. RT_OBJECT_HOOK_CALL(rt_tick_hook, ());
  136. /* tracing cpu usage */
  137. _update_process_times(tick);
  138. /* increase the global tick */
  139. #ifdef RT_USING_SMP
  140. /* get percpu and increase the tick */
  141. rt_atomic_add(&(rt_cpu_self()->tick), tick);
  142. #else
  143. rt_atomic_add(&(rt_tick), tick);
  144. #endif /* RT_USING_SMP */
  145. /* check time slice */
  146. rt_sched_tick_increase(tick);
  147. /* check timer */
  148. #ifdef RT_USING_SMP
  149. if (rt_cpu_get_id() != 0)
  150. {
  151. return;
  152. }
  153. #endif
  154. rt_timer_check();
  155. #ifdef RT_USING_VDSO
  156. rt_vdso_update_glob_time();
  157. #endif
  158. }
  159. /**
  160. * @brief This function will calculate the tick from millisecond.
  161. *
  162. * @param ms is the specified millisecond.
  163. * - Negative Number wait forever
  164. * - Zero not wait
  165. * - Max 0x7fffffff
  166. *
  167. * @return Return the calculated tick.
  168. */
  169. rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
  170. {
  171. rt_tick_t tick;
  172. if (ms < 0)
  173. {
  174. tick = (rt_tick_t)RT_WAITING_FOREVER;
  175. }
  176. else
  177. {
  178. tick = RT_TICK_PER_SECOND * (ms / 1000);
  179. tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
  180. }
  181. /* return the calculated tick */
  182. return tick;
  183. }
  184. RTM_EXPORT(rt_tick_from_millisecond);
  185. /**
  186. * @brief This function will return the passed millisecond from boot.
  187. *
  188. * @note if the value of RT_TICK_PER_SECOND is lower than 1000 or
  189. * is not an integral multiple of 1000, this function will not
  190. * provide the correct 1ms-based tick.
  191. *
  192. * @return Return passed millisecond from boot.
  193. */
  194. rt_weak rt_tick_t rt_tick_get_millisecond(void)
  195. {
  196. #if RT_TICK_PER_SECOND == 0 /* make cppcheck happy*/
  197. #error "RT_TICK_PER_SECOND must be greater than zero"
  198. #endif
  199. #if 1000 % RT_TICK_PER_SECOND == 0u
  200. return rt_tick_get() * (1000u / RT_TICK_PER_SECOND);
  201. #else
  202. #warning "rt-thread cannot provide a correct 1ms-based tick any longer,\
  203. please redefine this function in another file by using a high-precision hard-timer."
  204. return 0;
  205. #endif /* 1000 % RT_TICK_PER_SECOND == 0u */
  206. }
  207. /**@}*/