thread 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. include("/sys/src/libthread/sched.acid");
  2. defn labstk(l)
  3. {
  4. if objtype == "386" then
  5. _stk(longjmp, *l, linkreg(0), 0);
  6. else
  7. _stk(*(l+4), *l, linkreg(0), 0);
  8. }
  9. defn lablstk(l)
  10. {
  11. if objtype == "386" then
  12. _stk(longjmp, *l, linkreg(0), 1);
  13. else
  14. _stk(*(l+4), *l, linkreg(0), 1);
  15. }
  16. defn altfmt(A){
  17. local i, s, yes;
  18. complex Alt A;
  19. s = "alt(";
  20. s = s + "tag(*" + itoa(A.tag\X) + "=" + itoa(*A.tag\X) + ") ";
  21. i = 0;
  22. yes = 0;
  23. while A.op != CHANEND && A.op != CHANNOBLK do{
  24. if A.op != CHANNOP then{
  25. if yes then s = s + ", ";
  26. s = s + itoa(i);
  27. s = s + ": ";
  28. if A.op == CHANSND then s = s + "send";
  29. if A.op == CHANRCV then s = s + "recv";
  30. s = s + "(channel(";
  31. s = s + itoa(A.c);
  32. s = s + "))";
  33. yes = 1;
  34. }
  35. i = i + 1;
  36. A = (Alt)(A + sizeofAlt);
  37. }
  38. if A.op==CHANNOBLK then{
  39. if yes then s = s + ", ";
  40. s = s + "noblock";
  41. }
  42. s = s + ")";
  43. return s;
  44. }
  45. defn alt(A){
  46. print(altfmt(A), "\n");
  47. }
  48. defn thread(T){
  49. complex Thread T;
  50. local A, yes, i, P;
  51. P = (Proc)T.proc;
  52. if T.cmdname != 0 then{
  53. print("\t", *(T.cmdname\s), ":\n");
  54. }
  55. print("\t", T\X, "\t", T.id);
  56. if T.state == Running then{
  57. print("\t\tRunning ");
  58. } else if T.state == Ready then{
  59. print("\t\tReady ");
  60. } else if T.state == Rendezvous then{
  61. print("\t\tRendezvous ");
  62. }else{
  63. print("\t\tBad state ", T.state\X, " ");
  64. }
  65. if T.chan == Chanalt then{
  66. print("\t\t", altfmt(T.alt), "\n");
  67. } else if T.chan == Chansend then{
  68. A = (Alt)T.alt;
  69. print("\t\tsend(Channel(", A.c\X, "))");
  70. } else if T.chan == Chanrecv then{
  71. A = (Alt)T.alt;
  72. print("\t\trecv(Channel(", A.c\X, "))");
  73. }
  74. if T.moribund == 1 then{
  75. print(" (Moribund)");
  76. }
  77. print("\n");
  78. }
  79. defn pthreads(P){
  80. complex Proc P;
  81. local T, Tq;
  82. mainpid = pid;
  83. setproc(P.pid);
  84. Tq = (Tqueue)P.threads;
  85. T = (Thread)Tq.$head;
  86. while T != 0 do{
  87. thread(T);
  88. T = T.nextt;
  89. }
  90. setproc(mainpid);
  91. }
  92. defn threads(){
  93. local P;
  94. P = (Proc)_threadpq.$head;
  95. while P != 0 do{
  96. if P != (Proc)_threadpq.$head then print("\n");
  97. lproc(P);
  98. P = P.next;
  99. }
  100. }
  101. defn stacks(){
  102. local P;
  103. mainpid = pid;
  104. P = (Proc)_threadpq.$head;
  105. while P != 0 do{
  106. print("=========================================================\n");
  107. proc(P);
  108. setproc(P.pid);
  109. if P.thread==0 then{
  110. print("=== thread scheduler stack\n");
  111. stk();
  112. }
  113. threadstks(P);
  114. P = P.next;
  115. }
  116. setproc(mainpid);
  117. }
  118. defn stacksizes(){
  119. local P, T, Tq, top, sp;
  120. mainpid = pid;
  121. P = (Proc)_threadpq.$head;
  122. while P != 0 do{
  123. P = (Proc)P;
  124. Tq = (Tqueue)P.threads;
  125. T = (Thread)Tq.$head;
  126. while T != 0 do{
  127. top = T.stk+T.stksize;
  128. if T.state==Running then {
  129. sp = *SP;
  130. }else{
  131. sp = *(T.sched);
  132. }
  133. sp = *(T.sched);
  134. print(top-sp\D, "\n");
  135. T = T.nextt;
  136. }
  137. P = P.next;
  138. }
  139. setproc(mainpid);
  140. }
  141. defn lproc(P){
  142. proc(P);
  143. pthreads(P);
  144. }
  145. defn threadstks(P){
  146. complex Proc P;
  147. local T, Tq;
  148. mainpid = pid;
  149. setproc(P.pid);
  150. Tq = (Tqueue)P.threads;
  151. T = (Thread)Tq.$head;
  152. while T != 0 do{
  153. print("=============================\n");
  154. thread(T);
  155. print("\n");
  156. threadstk(T);
  157. T = T.nextt;
  158. }
  159. setproc(mainpid);
  160. }
  161. defn proc(P){
  162. complex Proc P;
  163. print("Proc *p=", P\X, ", p->pid=", P.pid\D);
  164. if P.thread==0 then{
  165. print(", p->state=Sched, p->tag=", P\X);
  166. }else{
  167. print(", p->state=Running");
  168. }
  169. print("\n");
  170. }
  171. defn procs(){
  172. local P;
  173. P = (Proc)_threadpq.$head;
  174. while P != 0 do{
  175. proc(P);
  176. P = P.next;
  177. }
  178. }
  179. defn threadlstk(T){
  180. complex Thread T;
  181. local P, mainpid;
  182. P = (Proc)T.proc;
  183. mainpid = pid;
  184. setproc(P.pid);
  185. if T.state == Running then{
  186. lstk();
  187. } else {
  188. lablstk(T.sched);
  189. }
  190. setproc(mainpid);
  191. }
  192. defn threadstk(T){
  193. complex Thread T;
  194. local P, mainpid;
  195. P = (Proc)T.proc;
  196. mainpid = pid;
  197. setproc(P.pid);
  198. if T.state == Running then{
  199. stk();
  200. } else {
  201. labstk(T.sched);
  202. }
  203. setproc(mainpid);
  204. }
  205. defn tqueue(Q) {
  206. complex Tqueue Q;
  207. while Q != 0 do {
  208. print(Q.$head\X, " ");
  209. Q = *(Q.$tail);
  210. }
  211. print("#\n");
  212. }
  213. defn channel(C) {
  214. complex Channel C;
  215. local i, p;
  216. print("channel ", C\X);
  217. if C.freed then {
  218. print(" (moribund)");
  219. }
  220. print("\n");
  221. print("\telementsize=", C.e\D, " buffersize=", C.s, "\n");
  222. if C.s then {
  223. print("\t", C.n\D, " values in channel:\n");
  224. print("\t");
  225. p = C.v+C.e*(C.f%C.s);
  226. loop 1,C.n do {
  227. if C.e==4 then {
  228. print((*p)\X, " ");
  229. }else {
  230. print("data(", (*p)\X, ") ");
  231. }
  232. p = p+C.e;
  233. if p == C.v+C.s*C.e then {
  234. p = C.v;
  235. }
  236. }
  237. }
  238. print("\n");
  239. print(C.nentry\D, " queue slots:\n");
  240. i=0;
  241. loop 1,C.nentry do {
  242. if C.qentry[i] then
  243. print("\t", altfmt(C.qentry[i]), "\n");
  244. else
  245. print("\t<empty>\n");
  246. i=i+1;
  247. }
  248. }
  249. print("/sys/lib/acid/thread");