alarmtimer-suspend.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* alarmtimer suspend test
  2. * John Stultz (john.stultz@linaro.org)
  3. * (C) Copyright Linaro 2013
  4. * Licensed under the GPLv2
  5. *
  6. * This test makes sure the alarmtimer & RTC wakeup code is
  7. * functioning.
  8. *
  9. * To build:
  10. * $ gcc alarmtimer-suspend.c -o alarmtimer-suspend -lrt
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <time.h>
  25. #include <string.h>
  26. #include <signal.h>
  27. #include <stdlib.h>
  28. #include <pthread.h>
  29. #include <include/vdso/time64.h>
  30. #include <errno.h>
  31. #include "kselftest.h"
  32. #define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
  33. #define SUSPEND_SECS 15
  34. int alarmcount;
  35. int alarm_clock_id;
  36. struct timespec start_time;
  37. char *clockstring(int clockid)
  38. {
  39. switch (clockid) {
  40. case CLOCK_REALTIME:
  41. return "CLOCK_REALTIME";
  42. case CLOCK_MONOTONIC:
  43. return "CLOCK_MONOTONIC";
  44. case CLOCK_PROCESS_CPUTIME_ID:
  45. return "CLOCK_PROCESS_CPUTIME_ID";
  46. case CLOCK_THREAD_CPUTIME_ID:
  47. return "CLOCK_THREAD_CPUTIME_ID";
  48. case CLOCK_MONOTONIC_RAW:
  49. return "CLOCK_MONOTONIC_RAW";
  50. case CLOCK_REALTIME_COARSE:
  51. return "CLOCK_REALTIME_COARSE";
  52. case CLOCK_MONOTONIC_COARSE:
  53. return "CLOCK_MONOTONIC_COARSE";
  54. case CLOCK_BOOTTIME:
  55. return "CLOCK_BOOTTIME";
  56. case CLOCK_REALTIME_ALARM:
  57. return "CLOCK_REALTIME_ALARM";
  58. case CLOCK_BOOTTIME_ALARM:
  59. return "CLOCK_BOOTTIME_ALARM";
  60. case CLOCK_TAI:
  61. return "CLOCK_TAI";
  62. }
  63. return "UNKNOWN_CLOCKID";
  64. }
  65. long long timespec_sub(struct timespec a, struct timespec b)
  66. {
  67. long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
  68. ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
  69. return ret;
  70. }
  71. int final_ret;
  72. void sigalarm(int signo)
  73. {
  74. long long delta_ns;
  75. struct timespec ts;
  76. clock_gettime(alarm_clock_id, &ts);
  77. alarmcount++;
  78. delta_ns = timespec_sub(start_time, ts);
  79. delta_ns -= NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
  80. printf("ALARM(%i): %ld:%ld latency: %lld ns ", alarmcount, ts.tv_sec,
  81. ts.tv_nsec, delta_ns);
  82. if (delta_ns > UNREASONABLE_LAT) {
  83. printf("[FAIL]\n");
  84. final_ret = -1;
  85. } else
  86. printf("[OK]\n");
  87. }
  88. int main(void)
  89. {
  90. timer_t tm1;
  91. struct itimerspec its1, its2;
  92. struct sigevent se;
  93. struct sigaction act;
  94. int signum = SIGRTMAX;
  95. /* Set up signal handler: */
  96. sigfillset(&act.sa_mask);
  97. act.sa_flags = 0;
  98. act.sa_handler = sigalarm;
  99. sigaction(signum, &act, NULL);
  100. /* Set up timer: */
  101. memset(&se, 0, sizeof(se));
  102. se.sigev_notify = SIGEV_SIGNAL;
  103. se.sigev_signo = signum;
  104. se.sigev_value.sival_int = 0;
  105. for (alarm_clock_id = CLOCK_REALTIME_ALARM;
  106. alarm_clock_id <= CLOCK_BOOTTIME_ALARM;
  107. alarm_clock_id++) {
  108. alarmcount = 0;
  109. if (timer_create(alarm_clock_id, &se, &tm1) == -1) {
  110. printf("timer_create failed, %s unsupported?: %s\n",
  111. clockstring(alarm_clock_id), strerror(errno));
  112. break;
  113. }
  114. clock_gettime(alarm_clock_id, &start_time);
  115. printf("Start time (%s): %ld:%ld\n", clockstring(alarm_clock_id),
  116. start_time.tv_sec, start_time.tv_nsec);
  117. printf("Setting alarm for every %i seconds\n", SUSPEND_SECS);
  118. its1.it_value = start_time;
  119. its1.it_value.tv_sec += SUSPEND_SECS;
  120. its1.it_interval.tv_sec = SUSPEND_SECS;
  121. its1.it_interval.tv_nsec = 0;
  122. timer_settime(tm1, TIMER_ABSTIME, &its1, &its2);
  123. while (alarmcount < 5)
  124. sleep(1); /* First 5 alarms, do nothing */
  125. printf("Starting suspend loops\n");
  126. while (alarmcount < 10) {
  127. int ret;
  128. sleep(3);
  129. ret = system("echo mem > /sys/power/state");
  130. if (ret)
  131. break;
  132. }
  133. timer_delete(tm1);
  134. }
  135. if (final_ret)
  136. ksft_exit_fail();
  137. ksft_exit_pass();
  138. }