alarmtimer-suspend.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #ifdef KTEST
  30. #include "../kselftest.h"
  31. #else
  32. static inline int ksft_exit_pass(void)
  33. {
  34. exit(0);
  35. }
  36. static inline int ksft_exit_fail(void)
  37. {
  38. exit(1);
  39. }
  40. #endif
  41. #define CLOCK_REALTIME 0
  42. #define CLOCK_MONOTONIC 1
  43. #define CLOCK_PROCESS_CPUTIME_ID 2
  44. #define CLOCK_THREAD_CPUTIME_ID 3
  45. #define CLOCK_MONOTONIC_RAW 4
  46. #define CLOCK_REALTIME_COARSE 5
  47. #define CLOCK_MONOTONIC_COARSE 6
  48. #define CLOCK_BOOTTIME 7
  49. #define CLOCK_REALTIME_ALARM 8
  50. #define CLOCK_BOOTTIME_ALARM 9
  51. #define CLOCK_HWSPECIFIC 10
  52. #define CLOCK_TAI 11
  53. #define NR_CLOCKIDS 12
  54. #define NSEC_PER_SEC 1000000000ULL
  55. #define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
  56. #define SUSPEND_SECS 15
  57. int alarmcount;
  58. int alarm_clock_id;
  59. struct timespec start_time;
  60. char *clockstring(int clockid)
  61. {
  62. switch (clockid) {
  63. case CLOCK_REALTIME:
  64. return "CLOCK_REALTIME";
  65. case CLOCK_MONOTONIC:
  66. return "CLOCK_MONOTONIC";
  67. case CLOCK_PROCESS_CPUTIME_ID:
  68. return "CLOCK_PROCESS_CPUTIME_ID";
  69. case CLOCK_THREAD_CPUTIME_ID:
  70. return "CLOCK_THREAD_CPUTIME_ID";
  71. case CLOCK_MONOTONIC_RAW:
  72. return "CLOCK_MONOTONIC_RAW";
  73. case CLOCK_REALTIME_COARSE:
  74. return "CLOCK_REALTIME_COARSE";
  75. case CLOCK_MONOTONIC_COARSE:
  76. return "CLOCK_MONOTONIC_COARSE";
  77. case CLOCK_BOOTTIME:
  78. return "CLOCK_BOOTTIME";
  79. case CLOCK_REALTIME_ALARM:
  80. return "CLOCK_REALTIME_ALARM";
  81. case CLOCK_BOOTTIME_ALARM:
  82. return "CLOCK_BOOTTIME_ALARM";
  83. case CLOCK_TAI:
  84. return "CLOCK_TAI";
  85. };
  86. return "UNKNOWN_CLOCKID";
  87. }
  88. long long timespec_sub(struct timespec a, struct timespec b)
  89. {
  90. long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
  91. ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
  92. return ret;
  93. }
  94. int final_ret = 0;
  95. void sigalarm(int signo)
  96. {
  97. long long delta_ns;
  98. struct timespec ts;
  99. clock_gettime(alarm_clock_id, &ts);
  100. alarmcount++;
  101. delta_ns = timespec_sub(start_time, ts);
  102. delta_ns -= NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
  103. printf("ALARM(%i): %ld:%ld latency: %lld ns ", alarmcount, ts.tv_sec,
  104. ts.tv_nsec, delta_ns);
  105. if (delta_ns > UNREASONABLE_LAT) {
  106. printf("[FAIL]\n");
  107. final_ret = -1;
  108. } else
  109. printf("[OK]\n");
  110. }
  111. int main(void)
  112. {
  113. timer_t tm1;
  114. struct itimerspec its1, its2;
  115. struct sigevent se;
  116. struct sigaction act;
  117. int signum = SIGRTMAX;
  118. /* Set up signal handler: */
  119. sigfillset(&act.sa_mask);
  120. act.sa_flags = 0;
  121. act.sa_handler = sigalarm;
  122. sigaction(signum, &act, NULL);
  123. /* Set up timer: */
  124. memset(&se, 0, sizeof(se));
  125. se.sigev_notify = SIGEV_SIGNAL;
  126. se.sigev_signo = signum;
  127. se.sigev_value.sival_int = 0;
  128. for (alarm_clock_id = CLOCK_REALTIME_ALARM;
  129. alarm_clock_id <= CLOCK_BOOTTIME_ALARM;
  130. alarm_clock_id++) {
  131. alarmcount = 0;
  132. if (timer_create(alarm_clock_id, &se, &tm1) == -1) {
  133. printf("timer_create failled, %s unspported?\n",
  134. clockstring(alarm_clock_id));
  135. break;
  136. }
  137. clock_gettime(alarm_clock_id, &start_time);
  138. printf("Start time (%s): %ld:%ld\n", clockstring(alarm_clock_id),
  139. start_time.tv_sec, start_time.tv_nsec);
  140. printf("Setting alarm for every %i seconds\n", SUSPEND_SECS);
  141. its1.it_value = start_time;
  142. its1.it_value.tv_sec += SUSPEND_SECS;
  143. its1.it_interval.tv_sec = SUSPEND_SECS;
  144. its1.it_interval.tv_nsec = 0;
  145. timer_settime(tm1, TIMER_ABSTIME, &its1, &its2);
  146. while (alarmcount < 5)
  147. sleep(1); /* First 5 alarms, do nothing */
  148. printf("Starting suspend loops\n");
  149. while (alarmcount < 10) {
  150. int ret;
  151. sleep(3);
  152. ret = system("echo mem > /sys/power/state");
  153. if (ret)
  154. break;
  155. }
  156. timer_delete(tm1);
  157. }
  158. if (final_ret)
  159. return ksft_exit_fail();
  160. return ksft_exit_pass();
  161. }