timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Copyright (c) 2017-2019 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * This implementation is based on "Hashed and Hierarchical Timing Wheels:
  19. * Efficient Data Structures for Implementing a Timer Facility" by George
  20. * Varghese and Tony Lauck. Specifically, it implements scheme 6.1.2.
  21. *
  22. */
  23. #include <assert.h>
  24. #include <stdalign.h>
  25. #include <stdbool.h>
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. #include <kern/atomic.h>
  29. #include <kern/clock.h>
  30. #include <kern/init.h>
  31. #include <kern/hash.h>
  32. #include <kern/hlist.h>
  33. #include <kern/macros.h>
  34. #include <kern/panic.h>
  35. #include <kern/percpu.h>
  36. #include <kern/spinlock.h>
  37. #include <kern/thread.h>
  38. #include <kern/timer.h>
  39. #include <kern/work.h>
  40. #include <machine/boot.h>
  41. #include <machine/cpu.h>
  42. // Timer states.
  43. #define TIMER_TS_READY 1
  44. #define TIMER_TS_SCHEDULED 2
  45. #define TIMER_TS_RUNNING 3
  46. #define TIMER_TS_DONE 4
  47. // Timer flags.
  48. #define TIMER_TF_DETACHED 0x1
  49. #define TIMER_TF_INTR 0x2
  50. #define TIMER_TF_HIGH_PRIO 0x4
  51. #define TIMER_TF_CANCELED 0x8
  52. #define TIMER_INVALID_CPU ((unsigned int)-1)
  53. #define TIMER_HTABLE_SIZE 2048
  54. #if !ISP2(TIMER_HTABLE_SIZE)
  55. #error "hash table size must be a power of two"
  56. #endif
  57. #define TIMER_HTABLE_MASK (TIMER_HTABLE_SIZE - 1)
  58. struct timer_bucket
  59. {
  60. struct hlist timers;
  61. };
  62. /*
  63. * The hash table bucket matching the last time member has already been
  64. * processed, and the next periodic event resumes from the next bucket.
  65. *
  66. * Locking order: interrupts -> timer_cpu_data.
  67. */
  68. struct timer_cpu_data
  69. {
  70. unsigned int cpu;
  71. struct spinlock lock;
  72. uint64_t last_time;
  73. struct timer_bucket htable[TIMER_HTABLE_SIZE];
  74. };
  75. static struct timer_cpu_data timer_cpu_data __percpu;
  76. static struct timer_cpu_data*
  77. timer_cpu_data_acquire (cpu_flags_t *flags)
  78. {
  79. thread_preempt_disable ();
  80. _Auto cpu_data = cpu_local_ptr (timer_cpu_data);
  81. spinlock_lock_intr_save (&cpu_data->lock, flags);
  82. thread_preempt_enable_no_resched ();
  83. return (cpu_data);
  84. }
  85. static struct timer_cpu_data*
  86. timer_lock_cpu_data (struct timer *timer, cpu_flags_t *flags)
  87. {
  88. while (1)
  89. {
  90. uint32_t cpu = atomic_load_rlx (&timer->cpu);
  91. if (cpu == TIMER_INVALID_CPU)
  92. return (NULL);
  93. _Auto cpu_data = percpu_ptr (timer_cpu_data, cpu);
  94. spinlock_lock_intr_save (&cpu_data->lock, flags);
  95. if (cpu == atomic_load_rlx (&timer->cpu))
  96. return (cpu_data);
  97. spinlock_unlock_intr_restore (&cpu_data->lock, *flags);
  98. }
  99. }
  100. static void
  101. timer_unlock_cpu_data (struct timer_cpu_data *cpu_data, cpu_flags_t flags)
  102. {
  103. spinlock_unlock_intr_restore (&cpu_data->lock, flags);
  104. }
  105. // Timer state functions.
  106. static bool
  107. timer_ready (const struct timer *timer)
  108. {
  109. return (timer->state == TIMER_TS_READY);
  110. }
  111. static void
  112. timer_set_ready (struct timer *timer)
  113. {
  114. timer->state = TIMER_TS_READY;
  115. }
  116. static bool
  117. timer_scheduled (const struct timer *timer)
  118. {
  119. return (timer->state == TIMER_TS_SCHEDULED);
  120. }
  121. static void
  122. timer_set_scheduled (struct timer *timer, uint32_t cpu)
  123. {
  124. atomic_store (&timer->cpu, cpu, ATOMIC_RELAXED);
  125. timer->state = TIMER_TS_SCHEDULED;
  126. }
  127. static bool
  128. timer_running (const struct timer *timer)
  129. {
  130. return (timer->state == TIMER_TS_RUNNING);
  131. }
  132. static void
  133. timer_set_running (struct timer *timer)
  134. {
  135. timer->state = TIMER_TS_RUNNING;
  136. }
  137. static bool
  138. timer_done (const struct timer *timer)
  139. {
  140. return (timer->state == TIMER_TS_DONE);
  141. }
  142. static void
  143. timer_set_done (struct timer *timer)
  144. {
  145. timer->state = TIMER_TS_DONE;
  146. }
  147. // Timer flags functions.
  148. static bool
  149. timer_detached (const struct timer *timer)
  150. {
  151. return ((timer->flags & TIMER_TF_DETACHED) != 0);
  152. }
  153. static void
  154. timer_set_detached (struct timer *timer)
  155. {
  156. timer->flags |= TIMER_TF_DETACHED;
  157. }
  158. static bool
  159. timer_is_intr (const struct timer *timer)
  160. {
  161. return ((timer->flags & TIMER_TF_INTR) != 0);
  162. }
  163. static void
  164. timer_set_intr (struct timer *timer)
  165. {
  166. timer->flags |= TIMER_TF_INTR;
  167. }
  168. static bool
  169. timer_is_high_prio (const struct timer *timer)
  170. {
  171. return ((timer->flags & TIMER_TF_HIGH_PRIO) != 0);
  172. }
  173. static void
  174. timer_set_high_prio (struct timer *timer)
  175. {
  176. timer->flags |= TIMER_TF_HIGH_PRIO;
  177. }
  178. static bool
  179. timer_canceled (const struct timer *timer)
  180. {
  181. return ((timer->flags & TIMER_TF_CANCELED) != 0);
  182. }
  183. static void
  184. timer_set_canceled (struct timer *timer)
  185. {
  186. timer->flags |= TIMER_TF_CANCELED;
  187. }
  188. static void
  189. timer_set_time (struct timer *timer, uint64_t ticks)
  190. {
  191. timer->ticks = ticks;
  192. }
  193. static bool
  194. timer_occurred (const struct timer *timer, uint64_t ref)
  195. {
  196. return (clock_time_occurred (timer_get_time (timer), ref));
  197. }
  198. static uint32_t
  199. timer_hash (uint64_t ticks)
  200. {
  201. return (hash_u64 (ticks));
  202. }
  203. static void
  204. timer_run (struct timer *timer)
  205. {
  206. assert (timer_running (timer));
  207. timer->fn (timer);
  208. if (timer_detached (timer))
  209. return;
  210. cpu_flags_t cpu_flags;
  211. _Auto cpu_data = timer_lock_cpu_data (timer, &cpu_flags);
  212. /*
  213. * The timer handler may have :
  214. * - rescheduled itself
  215. * - been canceled
  216. * - none of the above
  217. *
  218. * If the handler didn't call a timer function, or if the timer was
  219. * canceled, set the state to done and wake up the joiner, if any.
  220. *
  221. * If the handler rescheduled the timer, nothing must be done. This
  222. * is also true if the timer was canceled after being rescheduled by
  223. * the handler (in this case, cancellation won't wait for a signal).
  224. * These cases can be identified by checking if the timer state is
  225. * different from running.
  226. */
  227. if (timer_running (timer))
  228. {
  229. timer_set_done (timer);
  230. thread_wakeup (timer->joiner);
  231. }
  232. timer_unlock_cpu_data (cpu_data, cpu_flags);
  233. }
  234. static void
  235. timer_run_work (struct work *work)
  236. {
  237. timer_run (structof (work, struct timer, work));
  238. }
  239. static void
  240. timer_process (struct timer *timer)
  241. {
  242. if (timer_is_intr (timer))
  243. {
  244. timer_run (timer);
  245. return;
  246. }
  247. int work_flags = timer_is_high_prio (timer) ? WORK_HIGHPRIO : 0;
  248. work_init (&timer->work, timer_run_work);
  249. work_schedule (&timer->work, work_flags);
  250. }
  251. static void
  252. timer_bucket_init (struct timer_bucket *bucket)
  253. {
  254. hlist_init (&bucket->timers);
  255. }
  256. static void
  257. timer_bucket_add (struct timer_bucket *bucket, struct timer *timer)
  258. {
  259. hlist_insert_head (&bucket->timers, &timer->node);
  260. }
  261. static void
  262. timer_bucket_remove (struct timer_bucket *bucket __unused, struct timer *timer)
  263. {
  264. hlist_remove (&timer->node);
  265. }
  266. static void
  267. timer_cpu_data_init (struct timer_cpu_data *cpu_data, unsigned int cpu)
  268. {
  269. cpu_data->cpu = cpu;
  270. spinlock_init (&cpu_data->lock);
  271. // See periodic event handling.
  272. cpu_data->last_time = clock_get_time () - 1;
  273. for (size_t i = 0; i < ARRAY_SIZE (cpu_data->htable); i++)
  274. timer_bucket_init (&cpu_data->htable[i]);
  275. }
  276. static struct timer_bucket*
  277. timer_cpu_data_get_bucket (struct timer_cpu_data *cpu_data, uint64_t ticks)
  278. {
  279. uint32_t index = timer_hash (ticks) & TIMER_HTABLE_MASK;
  280. assert (index < ARRAY_SIZE (cpu_data->htable));
  281. return (&cpu_data->htable[index]);
  282. }
  283. static void
  284. timer_cpu_data_add (struct timer_cpu_data *cpu_data, struct timer *timer)
  285. {
  286. assert (timer_ready (timer));
  287. _Auto bucket = timer_cpu_data_get_bucket (cpu_data, timer->ticks);
  288. timer_bucket_add (bucket, timer);
  289. }
  290. static void
  291. timer_cpu_data_remove (struct timer_cpu_data *cpu_data, struct timer *timer)
  292. {
  293. assert (timer_scheduled (timer));
  294. _Auto bucket = timer_cpu_data_get_bucket (cpu_data, timer->ticks);
  295. timer_bucket_remove (bucket, timer);
  296. }
  297. static void
  298. timer_bucket_filter (struct timer_bucket *bucket, uint64_t now,
  299. struct hlist *timers)
  300. {
  301. struct timer *timer, *tmp;
  302. hlist_for_each_entry_safe (&bucket->timers, timer, tmp, node)
  303. {
  304. assert (timer_scheduled (timer));
  305. if (!timer_occurred (timer, now))
  306. continue;
  307. hlist_remove (&timer->node);
  308. timer_set_running (timer);
  309. hlist_insert_head (timers, &timer->node);
  310. }
  311. }
  312. static int __init
  313. timer_bootstrap (void)
  314. {
  315. timer_cpu_data_init (cpu_local_ptr (timer_cpu_data), 0);
  316. return (0);
  317. }
  318. INIT_OP_DEFINE (timer_bootstrap,
  319. INIT_OP_DEP (cpu_setup, true),
  320. INIT_OP_DEP (spinlock_setup, true));
  321. static int __init
  322. timer_setup (void)
  323. {
  324. for (uint32_t cpu = 1; cpu < cpu_count (); cpu++)
  325. timer_cpu_data_init (percpu_ptr (timer_cpu_data, cpu), cpu);
  326. return (0);
  327. }
  328. INIT_OP_DEFINE (timer_setup,
  329. INIT_OP_DEP (cpu_mp_probe, true),
  330. INIT_OP_DEP (spinlock_setup, true));
  331. void
  332. timer_init (struct timer *timer, timer_fn_t fn, int flags)
  333. {
  334. timer->fn = fn;
  335. timer->cpu = TIMER_INVALID_CPU;
  336. timer->state = TIMER_TS_READY;
  337. timer->flags = 0;
  338. timer->joiner = NULL;
  339. if (flags & TIMER_DETACHED)
  340. timer_set_detached (timer);
  341. if (flags & TIMER_INTR)
  342. timer_set_intr (timer);
  343. else if (flags & TIMER_HIGH_PRIO)
  344. timer_set_high_prio (timer);
  345. }
  346. void
  347. timer_schedule (struct timer *timer, uint64_t ticks)
  348. {
  349. cpu_flags_t cpu_flags;
  350. _Auto cpu_data = timer_lock_cpu_data (timer, &cpu_flags);
  351. if (! cpu_data)
  352. cpu_data = timer_cpu_data_acquire (&cpu_flags);
  353. else
  354. {
  355. if (timer_canceled (timer))
  356. goto out;
  357. /*
  358. * If called from the handler, the timer is running. If rescheduled
  359. * after completion, it's done.
  360. */
  361. if (timer_running (timer) || timer_done (timer))
  362. timer_set_ready (timer);
  363. }
  364. timer_set_time (timer, ticks);
  365. if (timer_occurred (timer, cpu_data->last_time))
  366. ticks = cpu_data->last_time + 1;
  367. timer_cpu_data_add (cpu_data, timer);
  368. timer_set_scheduled (timer, cpu_data->cpu);
  369. out:
  370. timer_unlock_cpu_data (cpu_data, cpu_flags);
  371. }
  372. void
  373. timer_cancel (struct timer *timer)
  374. {
  375. assert (!timer_detached (timer));
  376. cpu_flags_t cpu_flags;
  377. _Auto cpu_data = timer_lock_cpu_data (timer, &cpu_flags);
  378. assert (!timer->joiner);
  379. timer_set_canceled (timer);
  380. if (timer_scheduled (timer))
  381. timer_cpu_data_remove (cpu_data, timer);
  382. else
  383. {
  384. timer->joiner = thread_self ();
  385. while (!timer_done (timer))
  386. {
  387. if (timer_is_intr (timer))
  388. {
  389. timer_unlock_cpu_data (cpu_data, cpu_flags);
  390. atomic_spin_nop ();
  391. cpu_data = timer_lock_cpu_data (timer, &cpu_flags);
  392. }
  393. else
  394. thread_sleep (&cpu_data->lock, timer, "tmr_cncl");
  395. }
  396. assert (timer_done (timer));
  397. timer->joiner = NULL;
  398. }
  399. timer_set_ready (timer);
  400. timer_unlock_cpu_data (cpu_data, cpu_flags);
  401. }
  402. void
  403. timer_report_periodic_event (void)
  404. {
  405. assert (thread_check_intr_context ());
  406. uint64_t now = clock_get_time ();
  407. struct hlist timers;
  408. hlist_init (&timers);
  409. _Auto cpu_data = cpu_local_ptr (timer_cpu_data);
  410. spinlock_lock (&cpu_data->lock);
  411. for (uint64_t ticks = cpu_data->last_time + 1;
  412. clock_time_occurred (ticks, now);
  413. ticks++)
  414. {
  415. _Auto bucket = timer_cpu_data_get_bucket (cpu_data, ticks);
  416. timer_bucket_filter (bucket, now, &timers);
  417. }
  418. cpu_data->last_time = now;
  419. spinlock_unlock (&cpu_data->lock);
  420. while (!hlist_empty (&timers))
  421. {
  422. _Auto timer = hlist_first_entry (&timers, struct timer, node);
  423. hlist_remove (&timer->node);
  424. timer_process (timer);
  425. }
  426. }