portclock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "ureg.h"
  8. struct Timers
  9. {
  10. Lock;
  11. Timer *head;
  12. };
  13. static Timers timers[MAXMACH];
  14. ulong intrcount[MAXMACH];
  15. ulong fcallcount[MAXMACH];
  16. static vlong
  17. tadd(Timers *tt, Timer *nt)
  18. {
  19. Timer *t, **last;
  20. /* Called with tt locked */
  21. assert(nt->tt == nil);
  22. switch(nt->tmode){
  23. default:
  24. panic("timer");
  25. break;
  26. case Trelative:
  27. if(nt->tns <= 0)
  28. nt->tns = 1;
  29. nt->twhen = fastticks(nil) + ns2fastticks(nt->tns);
  30. break;
  31. case Tperiodic:
  32. assert(nt->tns >= 100000); /* At least 100 µs period */
  33. if(nt->twhen == 0){
  34. /* look for another timer at same frequency for combining */
  35. for(t = tt->head; t; t = t->tnext){
  36. if(t->tmode == Tperiodic && t->tns == nt->tns)
  37. break;
  38. }
  39. if (t)
  40. nt->twhen = t->twhen;
  41. else
  42. nt->twhen = fastticks(nil);
  43. }
  44. nt->twhen += ns2fastticks(nt->tns);
  45. break;
  46. }
  47. for(last = &tt->head; t = *last; last = &t->tnext){
  48. if(t->twhen > nt->twhen)
  49. break;
  50. }
  51. nt->tnext = *last;
  52. *last = nt;
  53. nt->tt = tt;
  54. if(last == &tt->head)
  55. return nt->twhen;
  56. return 0;
  57. }
  58. static uvlong
  59. tdel(Timer *dt)
  60. {
  61. Timer *t, **last;
  62. Timers *tt;
  63. tt = dt->tt;
  64. if (tt == nil)
  65. return 0;
  66. for(last = &tt->head; t = *last; last = &t->tnext){
  67. if(t == dt){
  68. assert(dt->tt);
  69. dt->tt = nil;
  70. *last = t->tnext;
  71. break;
  72. }
  73. }
  74. if(last == &tt->head && tt->head)
  75. return tt->head->twhen;
  76. return 0;
  77. }
  78. /* add or modify a timer */
  79. void
  80. timeradd(Timer *nt)
  81. {
  82. Timers *tt;
  83. vlong when;
  84. /* Must lock Timer struct before Timers struct */
  85. ilock(nt);
  86. if(tt = nt->tt){
  87. ilock(tt);
  88. tdel(nt);
  89. iunlock(tt);
  90. }
  91. tt = &timers[m->machno];
  92. ilock(tt);
  93. when = tadd(tt, nt);
  94. if(when)
  95. timerset(when);
  96. iunlock(tt);
  97. iunlock(nt);
  98. }
  99. void
  100. timerdel(Timer *dt)
  101. {
  102. Timers *tt;
  103. uvlong when;
  104. ilock(dt);
  105. if(tt = dt->tt){
  106. ilock(tt);
  107. when = tdel(dt);
  108. if(when && tt == &timers[m->machno])
  109. timerset(tt->head->twhen);
  110. iunlock(tt);
  111. }
  112. iunlock(dt);
  113. }
  114. void
  115. hzclock(Ureg *ur)
  116. {
  117. m->ticks++;
  118. if(m->proc)
  119. m->proc->pc = ur->pc;
  120. if(m->flushmmu){
  121. if(up)
  122. flushmmu();
  123. m->flushmmu = 0;
  124. }
  125. accounttime();
  126. kmapinval();
  127. if(kproftimer != nil)
  128. kproftimer(ur->pc);
  129. if((active.machs&(1<<m->machno)) == 0)
  130. return;
  131. if(active.exiting) {
  132. print("someone's exiting\n");
  133. exit(0);
  134. }
  135. checkalarms();
  136. if(up && up->state == Running)
  137. hzsched(); /* in proc.c */
  138. }
  139. void
  140. timerintr(Ureg *u, Tval)
  141. {
  142. Timer *t;
  143. Timers *tt;
  144. uvlong when, now;
  145. int callhzclock;
  146. static int sofar;
  147. intrcount[m->machno]++;
  148. callhzclock = 0;
  149. tt = &timers[m->machno];
  150. now = fastticks(nil);
  151. ilock(tt);
  152. while(t = tt->head){
  153. /*
  154. * No need to ilock t here: any manipulation of t
  155. * requires tdel(t) and this must be done with a
  156. * lock to tt held. We have tt, so the tdel will
  157. * wait until we're done
  158. */
  159. when = t->twhen;
  160. if(when > now){
  161. timerset(when);
  162. iunlock(tt);
  163. if(callhzclock)
  164. hzclock(u);
  165. return;
  166. }
  167. tt->head = t->tnext;
  168. assert(t->tt == tt);
  169. t->tt = nil;
  170. fcallcount[m->machno]++;
  171. iunlock(tt);
  172. if(t->tf)
  173. (*t->tf)(u, t);
  174. else
  175. callhzclock++;
  176. ilock(tt);
  177. if(t->tmode == Tperiodic)
  178. tadd(tt, t);
  179. }
  180. iunlock(tt);
  181. }
  182. void
  183. timersinit(void)
  184. {
  185. Timer *t;
  186. /*
  187. * T->tf == nil means the HZ clock for this processor.
  188. */
  189. todinit();
  190. t = malloc(sizeof(*t));
  191. t->tmode = Tperiodic;
  192. t->tt = nil;
  193. t->tns = 1000000000/HZ;
  194. t->tf = nil;
  195. timeradd(t);
  196. }
  197. Timer*
  198. addclock0link(void (*f)(void), int ms)
  199. {
  200. Timer *nt;
  201. uvlong when;
  202. /* Synchronize to hztimer if ms is 0 */
  203. nt = malloc(sizeof(Timer));
  204. if(ms == 0)
  205. ms = 1000/HZ;
  206. nt->tns = (vlong)ms*1000000LL;
  207. nt->tmode = Tperiodic;
  208. nt->tt = nil;
  209. nt->tf = (void (*)(Ureg*, Timer*))f;
  210. ilock(&timers[0]);
  211. when = tadd(&timers[0], nt);
  212. if(when)
  213. timerset(when);
  214. iunlock(&timers[0]);
  215. return nt;
  216. }
  217. /*
  218. * This tk2ms avoids overflows that the macro version is prone to.
  219. * It is a LOT slower so shouldn't be used if you're just converting
  220. * a delta.
  221. */
  222. ulong
  223. tk2ms(ulong ticks)
  224. {
  225. uvlong t, hz;
  226. t = ticks;
  227. hz = HZ;
  228. t *= 1000L;
  229. t = t/hz;
  230. ticks = t;
  231. return ticks;
  232. }
  233. ulong
  234. ms2tk(ulong ms)
  235. {
  236. /* avoid overflows at the cost of precision */
  237. if(ms >= 1000000000/HZ)
  238. return (ms/1000)*HZ;
  239. return (ms*HZ+500)/1000;
  240. }