alarm.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * This file is part of Jehanne.
  3. *
  4. * Copyright (C) 2015 Giacomo Tesio <giacomo@tesio.it>
  5. *
  6. * Jehanne is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 2 of the License.
  9. *
  10. * Jehanne is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <u.h>
  19. #include <lib9.h>
  20. /* Test alarm(2):
  21. * - alarms replace each other (the last one wins)
  22. * - alarms are received as notes
  23. * - alarms are received (more or less) on time
  24. *
  25. * Given we have two clocks at work here, we can use one to
  26. * check the other:
  27. * - nsec() is an high resolution clock from RDTSC instruction
  28. * (see sysproc.c, portclock.c and tod.c in sys/src/9/port)
  29. * - sys->ticks incremented by hzclock (in portclock.c) that
  30. * is used by alarm (and sleep and the kernel scheduler)
  31. */
  32. int verbose = 0;
  33. int64_t alarmReceived;
  34. int
  35. printFirst(void *v, char *s)
  36. {
  37. /* just not exit, please */
  38. if(strcmp(s, "alarm") == 0){
  39. alarmReceived = nsec();
  40. if(verbose)
  41. fprint(2, "%d: noted: %s at %lld\n", getpid(), s, nsec());
  42. atnotify(printFirst, 0);
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. int
  48. failOnSecond(void *v, char *s)
  49. {
  50. /* just not exit, please */
  51. if(strcmp(s, "alarm") == 0){
  52. print("FAIL\n");
  53. exits("FAIL");
  54. }
  55. return 0;
  56. }
  57. void
  58. main(void)
  59. {
  60. int64_t a2000, a500;
  61. if (!atnotify(printFirst, 1) || !atnotify(failOnSecond, 1)){
  62. fprint(2, "%r\n");
  63. exits("atnotify fails");
  64. }
  65. sys_alarm(2000);
  66. a2000 = nsec();
  67. sys_alarm(500);
  68. a500 = nsec();
  69. sys_rendezvous(&a500, (void*)0x123);
  70. if(verbose)
  71. fprint(2, "%d: set alarm(2000)@%lld then alarm(500)@%lld; received after %lld nanosecond\n", getpid(), a2000, a500, alarmReceived-a500);
  72. if(alarmReceived-a500 > 700 * 1000 * 1000){
  73. print("FAIL\n");
  74. exits("FAIL");
  75. }
  76. print("PASS\n");
  77. exits("PASS");
  78. }