edfproc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "time.h"
  4. char * T = "10s";
  5. char * D = "5s";
  6. char * C = "1.5s";
  7. char * S = nil;
  8. char * s = "250ms";
  9. char * R = "";
  10. int verbose, debug, tset, dset, cset, sset;
  11. int nproc = 1;
  12. uvlong v;
  13. char *clonedev = "#R/realtime/clone";
  14. char missstr[] = "sys: deadline miss: runtime";
  15. int notefd;
  16. int
  17. rthandler(void*, char *s)
  18. {
  19. static int umpteenth;
  20. if (!umpteenth++)
  21. fprint(2, "note received: %s\n", s);
  22. if (strncmp(missstr, s, strlen(missstr)) == 0)
  23. return 1;
  24. /* On any other note */
  25. if (fprint(notefd, "remove") < 0)
  26. sysfatal("Could not remove task: %r");
  27. sysfatal("Removed task");
  28. return 1;
  29. }
  30. static int
  31. schedpars(char *period, char *deadline, char *cost)
  32. {
  33. int fd;
  34. if ((fd = open(clonedev, ORDWR)) < 0)
  35. sysfatal("%s: %r", clonedev);
  36. fprint(2, "T=%s D=%s C=%s procs=%d resources=%q %sadmit\n",
  37. period, deadline, cost, getpid(), R, verbose?"verbose ":"");
  38. if (fprint(fd, "T=%s D=%s C=%s procs=%d resources=%q %sadmit",
  39. period, deadline, cost, getpid(), R, verbose?"verbose ":"") < 0)
  40. sysfatal("%s: %r", clonedev);
  41. notefd = fd;
  42. atnotify(rthandler, 1);
  43. return fd;
  44. }
  45. static void
  46. usage(void)
  47. {
  48. fprint(2, "Usage: %s [-T period] [-D deadline] [-D cost] [-S sleepinterval] [-s sleeptime] [-R resources] [-v]\n", argv0);
  49. exits(nil);
  50. }
  51. void
  52. main(int argc, char *argv[])
  53. {
  54. Time sleepinterval;
  55. Time sleeptime;
  56. Time now, when;
  57. int i, j, fd;
  58. char *e;
  59. quotefmtinstall();
  60. ARGBEGIN {
  61. case 'T':
  62. T = EARGF(usage());
  63. tset++;
  64. break;
  65. case 'D':
  66. D = EARGF(usage());
  67. dset++;
  68. break;
  69. case 'C':
  70. C = EARGF(usage());
  71. cset++;
  72. break;
  73. case 'S':
  74. S = EARGF(usage());
  75. break;
  76. case 's':
  77. s = EARGF(usage());
  78. sset++;
  79. break;
  80. case 'R':
  81. R = EARGF(usage());
  82. break;
  83. case 'n':
  84. nproc = atoi(EARGF(usage()));
  85. if (nproc <= 0 || nproc > 10)
  86. sysfatal("%d processes?", nproc);
  87. break;
  88. case 'v':
  89. verbose++;
  90. break;
  91. case 'd':
  92. debug++;
  93. break;
  94. default:
  95. usage();
  96. }
  97. ARGEND;
  98. if (S){
  99. if (e = parsetime(&sleepinterval, S))
  100. sysfatal("%s: Sleep interval: %s\n", argv0, e);
  101. if (e = parsetime(&sleeptime, s))
  102. sysfatal("%s: Sleep time: %s\n", argv0, e);
  103. }
  104. sleeptime /= 1000000;
  105. if (S && sleeptime == 0)
  106. sysfatal("%s: Sleep time too small\n", argv0);
  107. if (tset && !dset) D = T;
  108. fd = schedpars(T, D, C);
  109. for (j = 1; j < nproc; j++){
  110. switch(fork()){
  111. case -1:
  112. sysfatal("fork: %r");
  113. case 0:
  114. fprint(2, "procs+=%d\n", getpid());
  115. if (fprint(fd, "procs+=%d", getpid()) < 0)
  116. sysfatal("%s: %r", clonedev);
  117. for (;;){
  118. for(i = 0; i < 10000; i++)
  119. v+=1LL;
  120. if (S){
  121. now = nsec();
  122. if (now >= when){
  123. sleep((long)sleeptime);
  124. when = now + sleepinterval;
  125. }
  126. }
  127. }
  128. default:
  129. break;
  130. }
  131. }
  132. when = nsec() + sleepinterval;
  133. for (;;){
  134. for(i = 0; i < 100000; i++)
  135. v+=1LL;
  136. if (verbose)
  137. fprint(2, ".");
  138. if (S){
  139. now = nsec();
  140. if (now >= when){
  141. sleep((long)sleeptime);
  142. when = now + sleepinterval;
  143. }
  144. }
  145. }
  146. }