alarmtimer-suspend.c 4.3 KB

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