1 /* alarmtimer suspend test
2 * John Stultz (john.stultz@linaro.org)
3 * (C) Copyright Linaro 2013
4 * Licensed under the GPLv2
6 * This test makes sure the alarmtimer & RTC wakeup code is
10 * $ gcc alarmtimer-suspend.c -o alarmtimer-suspend -lrt
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.
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.
31 #include "../kselftest.h"
33 #define CLOCK_REALTIME 0
34 #define CLOCK_MONOTONIC 1
35 #define CLOCK_PROCESS_CPUTIME_ID 2
36 #define CLOCK_THREAD_CPUTIME_ID 3
37 #define CLOCK_MONOTONIC_RAW 4
38 #define CLOCK_REALTIME_COARSE 5
39 #define CLOCK_MONOTONIC_COARSE 6
40 #define CLOCK_BOOTTIME 7
41 #define CLOCK_REALTIME_ALARM 8
42 #define CLOCK_BOOTTIME_ALARM 9
43 #define CLOCK_HWSPECIFIC 10
45 #define NR_CLOCKIDS 12
48 #define NSEC_PER_SEC 1000000000ULL
49 #define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
51 #define SUSPEND_SECS 15
54 struct timespec start_time;
57 char *clockstring(int clockid)
61 return "CLOCK_REALTIME";
63 return "CLOCK_MONOTONIC";
64 case CLOCK_PROCESS_CPUTIME_ID:
65 return "CLOCK_PROCESS_CPUTIME_ID";
66 case CLOCK_THREAD_CPUTIME_ID:
67 return "CLOCK_THREAD_CPUTIME_ID";
68 case CLOCK_MONOTONIC_RAW:
69 return "CLOCK_MONOTONIC_RAW";
70 case CLOCK_REALTIME_COARSE:
71 return "CLOCK_REALTIME_COARSE";
72 case CLOCK_MONOTONIC_COARSE:
73 return "CLOCK_MONOTONIC_COARSE";
75 return "CLOCK_BOOTTIME";
76 case CLOCK_REALTIME_ALARM:
77 return "CLOCK_REALTIME_ALARM";
78 case CLOCK_BOOTTIME_ALARM:
79 return "CLOCK_BOOTTIME_ALARM";
83 return "UNKNOWN_CLOCKID";
87 long long timespec_sub(struct timespec a, struct timespec b)
89 long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
91 ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
97 void sigalarm(int signo)
102 clock_gettime(alarm_clock_id, &ts);
105 delta_ns = timespec_sub(start_time, ts);
106 delta_ns -= NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
108 printf("ALARM(%i): %ld:%ld latency: %lld ns ", alarmcount, ts.tv_sec,
109 ts.tv_nsec, delta_ns);
111 if (delta_ns > UNREASONABLE_LAT) {
122 struct itimerspec its1, its2;
124 struct sigaction act;
125 int signum = SIGRTMAX;
127 /* Set up signal handler: */
128 sigfillset(&act.sa_mask);
130 act.sa_handler = sigalarm;
131 sigaction(signum, &act, NULL);
134 memset(&se, 0, sizeof(se));
135 se.sigev_notify = SIGEV_SIGNAL;
136 se.sigev_signo = signum;
137 se.sigev_value.sival_int = 0;
139 for (alarm_clock_id = CLOCK_REALTIME_ALARM;
140 alarm_clock_id <= CLOCK_BOOTTIME_ALARM;
144 if (timer_create(alarm_clock_id, &se, &tm1) == -1) {
145 printf("timer_create failed, %s unsupported?\n",
146 clockstring(alarm_clock_id));
150 clock_gettime(alarm_clock_id, &start_time);
151 printf("Start time (%s): %ld:%ld\n", clockstring(alarm_clock_id),
152 start_time.tv_sec, start_time.tv_nsec);
153 printf("Setting alarm for every %i seconds\n", SUSPEND_SECS);
154 its1.it_value = start_time;
155 its1.it_value.tv_sec += SUSPEND_SECS;
156 its1.it_interval.tv_sec = SUSPEND_SECS;
157 its1.it_interval.tv_nsec = 0;
159 timer_settime(tm1, TIMER_ABSTIME, &its1, &its2);
161 while (alarmcount < 5)
162 sleep(1); /* First 5 alarms, do nothing */
164 printf("Starting suspend loops\n");
165 while (alarmcount < 10) {
169 ret = system("echo mem > /sys/power/state");
176 return ksft_exit_fail();
177 return ksft_exit_pass();