timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon
  24. * the rights to redistribute these changes.
  25. */
  26. #include <mach/kern_return.h>
  27. #include <mach/port.h>
  28. #include <kern/queue.h>
  29. #include <kern/thread.h>
  30. #include <mach/time_value.h>
  31. #include <kern/timer.h>
  32. #include <kern/cpu_number.h>
  33. #include <kern/assert.h>
  34. #include <kern/macros.h>
  35. timer_t current_timer[NCPUS];
  36. timer_data_t kernel_timer[NCPUS];
  37. /*
  38. * init_timers initializes all non-thread timers and puts the
  39. * service routine on the callout queue. All timers must be
  40. * serviced by the callout routine once an hour.
  41. */
  42. void init_timers(void)
  43. {
  44. int i;
  45. timer_t this_timer;
  46. /*
  47. * Initialize all the kernel timers and start the one
  48. * for this cpu (master) slaves start theirs later.
  49. */
  50. this_timer = &kernel_timer[0];
  51. for ( i=0 ; i<NCPUS ; i++, this_timer++) {
  52. timer_init(this_timer);
  53. current_timer[i] = (timer_t) 0;
  54. }
  55. start_timer(&kernel_timer[cpu_number()]);
  56. }
  57. /*
  58. * timer_init initializes a single timer.
  59. */
  60. void timer_init(timer_t this_timer)
  61. {
  62. this_timer->low_bits = 0;
  63. this_timer->high_bits = 0;
  64. this_timer->tstamp = 0;
  65. this_timer->high_bits_check = 0;
  66. }
  67. #if STAT_TIME
  68. #else /* STAT_TIME */
  69. #ifdef MACHINE_TIMER_ROUTINES
  70. /*
  71. * Machine-dependent code implements the timer routines.
  72. */
  73. #else /* MACHINE_TIMER_ROUTINES */
  74. /*
  75. * start_timer starts the given timer for this cpu. It is called
  76. * exactly once for each cpu during the boot sequence.
  77. */
  78. void
  79. start_timer(timer_t timer)
  80. {
  81. timer->tstamp = get_timestamp();
  82. current_timer[cpu_number()] = timer;
  83. }
  84. /*
  85. * time_trap_uentry does trap entry timing. Caller must lock out
  86. * interrupts and take a timestamp. ts is a timestamp taken after
  87. * interrupts were locked out. Must only be called if trap was
  88. * from user mode.
  89. */
  90. void
  91. time_trap_uentry(unsigned ts)
  92. {
  93. int elapsed;
  94. int mycpu;
  95. timer_t mytimer;
  96. /*
  97. * Calculate elapsed time.
  98. */
  99. mycpu = cpu_number();
  100. mytimer = current_timer[mycpu];
  101. elapsed = ts - mytimer->tstamp;
  102. #ifdef TIMER_MAX
  103. if (elapsed < 0) elapsed += TIMER_MAX;
  104. #endif /* TIMER_MAX */
  105. /*
  106. * Update current timer.
  107. */
  108. mytimer->low_bits += elapsed;
  109. mytimer->tstamp = 0;
  110. if (mytimer->low_bits & TIMER_LOW_FULL) {
  111. timer_normalize(mytimer);
  112. }
  113. /*
  114. * Record new timer.
  115. */
  116. mytimer = &(active_threads[mycpu]->system_timer);
  117. current_timer[mycpu] = mytimer;
  118. mytimer->tstamp = ts;
  119. }
  120. /*
  121. * time_trap_uexit does trap exit timing. Caller must lock out
  122. * interrupts and take a timestamp. ts is a timestamp taken after
  123. * interrupts were locked out. Must only be called if returning to
  124. * user mode.
  125. */
  126. void
  127. time_trap_uexit(int ts)
  128. {
  129. int elapsed;
  130. int mycpu;
  131. timer_t mytimer;
  132. /*
  133. * Calculate elapsed time.
  134. */
  135. mycpu = cpu_number();
  136. mytimer = current_timer[mycpu];
  137. elapsed = ts - mytimer->tstamp;
  138. #ifdef TIMER_MAX
  139. if (elapsed < 0) elapsed += TIMER_MAX;
  140. #endif /* TIMER_MAX */
  141. /*
  142. * Update current timer.
  143. */
  144. mytimer->low_bits += elapsed;
  145. mytimer->tstamp = 0;
  146. if (mytimer->low_bits & TIMER_LOW_FULL) {
  147. timer_normalize(mytimer); /* SYSTEMMODE */
  148. }
  149. mytimer = &(active_threads[mycpu]->user_timer);
  150. /*
  151. * Record new timer.
  152. */
  153. current_timer[mycpu] = mytimer;
  154. mytimer->tstamp = ts;
  155. }
  156. /*
  157. * time_int_entry does interrupt entry timing. Caller must lock out
  158. * interrupts and take a timestamp. ts is a timestamp taken after
  159. * interrupts were locked out. new_timer is the new timer to
  160. * switch to. This routine returns the currently running timer,
  161. * which MUST be pushed onto the stack by the caller, or otherwise
  162. * saved for time_int_exit.
  163. */
  164. timer_t
  165. time_int_entry(
  166. unsigned ts,
  167. timer_t new_timer)
  168. {
  169. int elapsed;
  170. int mycpu;
  171. timer_t mytimer;
  172. /*
  173. * Calculate elapsed time.
  174. */
  175. mycpu = cpu_number();
  176. mytimer = current_timer[mycpu];
  177. elapsed = ts - mytimer->tstamp;
  178. #ifdef TIMER_MAX
  179. if (elapsed < 0) elapsed += TIMER_MAX;
  180. #endif /* TIMER_MAX */
  181. /*
  182. * Update current timer.
  183. */
  184. mytimer->low_bits += elapsed;
  185. mytimer->tstamp = 0;
  186. /*
  187. * Switch to new timer, and save old one on stack.
  188. */
  189. new_timer->tstamp = ts;
  190. current_timer[mycpu] = new_timer;
  191. return(mytimer);
  192. }
  193. /*
  194. * time_int_exit does interrupt exit timing. Caller must lock out
  195. * interrupts and take a timestamp. ts is a timestamp taken after
  196. * interrupts were locked out. old_timer is the timer value pushed
  197. * onto the stack or otherwise saved after time_int_entry returned
  198. * it.
  199. */
  200. void
  201. time_int_exit(
  202. unsigned ts,
  203. timer_t old_timer)
  204. {
  205. int elapsed;
  206. int mycpu;
  207. timer_t mytimer;
  208. /*
  209. * Calculate elapsed time.
  210. */
  211. mycpu = cpu_number();
  212. mytimer = current_timer[mycpu];
  213. elapsed = ts - mytimer->tstamp;
  214. #ifdef TIMER_MAX
  215. if (elapsed < 0) elapsed += TIMER_MAX;
  216. #endif /* TIMER_MAX */
  217. /*
  218. * Update current timer.
  219. */
  220. mytimer->low_bits += elapsed;
  221. mytimer->tstamp = 0;
  222. /*
  223. * If normalization requested, do it.
  224. */
  225. if (mytimer->low_bits & TIMER_LOW_FULL) {
  226. timer_normalize(mytimer);
  227. }
  228. if (old_timer->low_bits & TIMER_LOW_FULL) {
  229. timer_normalize(old_timer);
  230. }
  231. /*
  232. * Start timer that was running before interrupt.
  233. */
  234. old_timer->tstamp = ts;
  235. current_timer[mycpu] = old_timer;
  236. }
  237. /*
  238. * timer_switch switches to a new timer. The machine
  239. * dependent routine/macro get_timestamp must return a timestamp.
  240. * Caller must lock out interrupts.
  241. */
  242. void
  243. timer_switch(timer_t new_timer)
  244. {
  245. int elapsed;
  246. int mycpu;
  247. timer_t mytimer;
  248. unsigned ts;
  249. /*
  250. * Calculate elapsed time.
  251. */
  252. mycpu = cpu_number();
  253. mytimer = current_timer[mycpu];
  254. ts = get_timestamp();
  255. elapsed = ts - mytimer->tstamp;
  256. #ifdef TIMER_MAX
  257. if (elapsed < 0) elapsed += TIMER_MAX;
  258. #endif /* TIMER_MAX */
  259. /*
  260. * Update current timer.
  261. */
  262. mytimer->low_bits += elapsed;
  263. mytimer->tstamp = 0;
  264. /*
  265. * Normalization check
  266. */
  267. if (mytimer->low_bits & TIMER_LOW_FULL) {
  268. timer_normalize(mytimer);
  269. }
  270. /*
  271. * Record new timer.
  272. */
  273. current_timer[mycpu] = new_timer;
  274. new_timer->tstamp = ts;
  275. }
  276. #endif /* MACHINE_TIMER_ROUTINES */
  277. #endif /* STAT_TIME */
  278. /*
  279. * timer_normalize normalizes the value of a timer. It is
  280. * called only rarely, to make sure low_bits never overflows.
  281. */
  282. void timer_normalize(timer_t timer)
  283. {
  284. unsigned int high_increment;
  285. /*
  286. * Calculate high_increment, then write high check field first
  287. * followed by low and high. timer_grab() reads these fields in
  288. * reverse order so if high and high check match, we know
  289. * that the values read are ok.
  290. */
  291. high_increment = timer->low_bits/TIMER_HIGH_UNIT;
  292. timer->high_bits_check += high_increment;
  293. timer->low_bits %= TIMER_HIGH_UNIT;
  294. timer->high_bits += high_increment;
  295. }
  296. /*
  297. * timer_grab() retrieves the value of a timer.
  298. *
  299. * Critical scheduling code uses TIMER_DELTA macro in timer.h
  300. * (called from thread_timer_delta in sched.h).
  301. *
  302. * Keep coherent with db_time_grab below.
  303. */
  304. static void timer_grab(
  305. timer_t timer,
  306. timer_save_t save)
  307. {
  308. #if MACH_ASSERT
  309. unsigned int passes=0;
  310. #endif
  311. do {
  312. (save)->high = (timer)->high_bits;
  313. (save)->low = (timer)->low_bits;
  314. /*
  315. * If the timer was normalized while we were doing this,
  316. * the high_bits value read above and the high_bits check
  317. * value will not match because high_bits_check is the first
  318. * field touched by the normalization procedure, and
  319. * high_bits is the last.
  320. *
  321. * Additions to timer only touch low bits and
  322. * are therefore atomic with respect to this.
  323. */
  324. #if MACH_ASSERT
  325. passes++;
  326. assert((passes < 10000) ? (1) : ((timer->high_bits_check = save->high), 0));
  327. #endif
  328. } while ( (save)->high != (timer)->high_bits_check);
  329. }
  330. /*
  331. *
  332. * Db_timer_grab(): used by db_thread_read_times. An nonblocking
  333. * version of db_thread_get_times. Keep coherent with timer_grab
  334. * above.
  335. *
  336. */
  337. void db_timer_grab(
  338. timer_t timer,
  339. timer_save_t save)
  340. {
  341. /* Don't worry about coherency */
  342. (save)->high = (timer)->high_bits;
  343. (save)->low = (timer)->low_bits;
  344. }
  345. /*
  346. * timer_read reads the value of a timer into a time_value_t. If the
  347. * timer was modified during the read, retry. The value returned
  348. * is accurate to the last update; time accumulated by a running
  349. * timer since its last timestamp is not included.
  350. */
  351. void
  352. timer_read(
  353. timer_t timer,
  354. time_value_t *tv)
  355. {
  356. timer_save_data_t temp;
  357. timer_grab(timer,&temp);
  358. /*
  359. * Normalize the result
  360. */
  361. #ifdef TIMER_ADJUST
  362. TIMER_ADJUST(&temp);
  363. #endif /* TIMER_ADJUST */
  364. tv->seconds = temp.high + temp.low/1000000;
  365. tv->microseconds = temp.low%1000000;
  366. }
  367. /*
  368. * thread_read_times reads the user and system times from a thread.
  369. * Time accumulated since last timestamp is not included. Should
  370. * be called at splsched() to avoid having user and system times
  371. * be out of step. Doesn't care if caller locked thread.
  372. *
  373. * Needs to be kept coherent with thread_read_times ahead.
  374. */
  375. void thread_read_times(
  376. thread_t thread,
  377. time_value_t *user_time_p,
  378. time_value_t *system_time_p)
  379. {
  380. timer_save_data_t temp;
  381. timer_t timer;
  382. timer = &thread->user_timer;
  383. timer_grab(timer, &temp);
  384. #ifdef TIMER_ADJUST
  385. TIMER_ADJUST(&temp);
  386. #endif /* TIMER_ADJUST */
  387. user_time_p->seconds = temp.high + temp.low/1000000;
  388. user_time_p->microseconds = temp.low % 1000000;
  389. timer = &thread->system_timer;
  390. timer_grab(timer, &temp);
  391. #ifdef TIMER_ADJUST
  392. TIMER_ADJUST(&temp);
  393. #endif /* TIMER_ADJUST */
  394. system_time_p->seconds = temp.high + temp.low/1000000;
  395. system_time_p->microseconds = temp.low % 1000000;
  396. }
  397. /*
  398. * Db_thread_read_times: A version of thread_read_times that
  399. * can be called by the debugger. This version does not call
  400. * timer_grab, which can block. Please keep it up to date with
  401. * thread_read_times above.
  402. *
  403. */
  404. void db_thread_read_times(
  405. thread_t thread,
  406. time_value_t *user_time_p,
  407. time_value_t *system_time_p)
  408. {
  409. timer_save_data_t temp;
  410. timer_t timer;
  411. timer = &thread->user_timer;
  412. db_timer_grab(timer, &temp);
  413. #ifdef TIMER_ADJUST
  414. TIMER_ADJUST(&temp);
  415. #endif /* TIMER_ADJUST */
  416. user_time_p->seconds = temp.high + temp.low/1000000;
  417. user_time_p->microseconds = temp.low % 1000000;
  418. timer = &thread->system_timer;
  419. timer_grab(timer, &temp);
  420. #ifdef TIMER_ADJUST
  421. TIMER_ADJUST(&temp);
  422. #endif /* TIMER_ADJUST */
  423. system_time_p->seconds = temp.high + temp.low/1000000;
  424. system_time_p->microseconds = temp.low % 1000000;
  425. }
  426. /*
  427. * timer_delta takes the difference of a saved timer value
  428. * and the current one, and updates the saved value to current.
  429. * The difference is returned as a function value. See
  430. * TIMER_DELTA macro (timer.h) for optimization to this.
  431. */
  432. unsigned
  433. timer_delta(
  434. timer_t timer,
  435. timer_save_t save)
  436. {
  437. timer_save_data_t new_save;
  438. unsigned result;
  439. timer_grab(timer,&new_save);
  440. result = (new_save.high - save->high) * TIMER_HIGH_UNIT +
  441. new_save.low - save->low;
  442. save->high = new_save.high;
  443. save->low = new_save.low;
  444. return(result);
  445. }