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.
32 #include "../kselftest.h"
34 static inline int ksft_exit_pass(void)
38 static inline int ksft_exit_fail(void)
44 #define CLOCK_REALTIME 0
45 #define CLOCK_MONOTONIC 1
46 #define CLOCK_PROCESS_CPUTIME_ID 2
47 #define CLOCK_THREAD_CPUTIME_ID 3
48 #define CLOCK_MONOTONIC_RAW 4
49 #define CLOCK_REALTIME_COARSE 5
50 #define CLOCK_MONOTONIC_COARSE 6
51 #define CLOCK_BOOTTIME 7
52 #define CLOCK_REALTIME_ALARM 8
53 #define CLOCK_BOOTTIME_ALARM 9
54 #define CLOCK_HWSPECIFIC 10
56 #define NR_CLOCKIDS 12
59 #define NSEC_PER_SEC 1000000000ULL
60 #define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
62 #define SUSPEND_SECS 15
65 struct timespec start_time
;
68 char *clockstring(int clockid
)
72 return "CLOCK_REALTIME";
74 return "CLOCK_MONOTONIC";
75 case CLOCK_PROCESS_CPUTIME_ID
:
76 return "CLOCK_PROCESS_CPUTIME_ID";
77 case CLOCK_THREAD_CPUTIME_ID
:
78 return "CLOCK_THREAD_CPUTIME_ID";
79 case CLOCK_MONOTONIC_RAW
:
80 return "CLOCK_MONOTONIC_RAW";
81 case CLOCK_REALTIME_COARSE
:
82 return "CLOCK_REALTIME_COARSE";
83 case CLOCK_MONOTONIC_COARSE
:
84 return "CLOCK_MONOTONIC_COARSE";
86 return "CLOCK_BOOTTIME";
87 case CLOCK_REALTIME_ALARM
:
88 return "CLOCK_REALTIME_ALARM";
89 case CLOCK_BOOTTIME_ALARM
:
90 return "CLOCK_BOOTTIME_ALARM";
94 return "UNKNOWN_CLOCKID";
98 long long timespec_sub(struct timespec a
, struct timespec b
)
100 long long ret
= NSEC_PER_SEC
* b
.tv_sec
+ b
.tv_nsec
;
102 ret
-= NSEC_PER_SEC
* a
.tv_sec
+ a
.tv_nsec
;
108 void sigalarm(int signo
)
113 clock_gettime(alarm_clock_id
, &ts
);
116 delta_ns
= timespec_sub(start_time
, ts
);
117 delta_ns
-= NSEC_PER_SEC
* SUSPEND_SECS
* alarmcount
;
119 printf("ALARM(%i): %ld:%ld latency: %lld ns ", alarmcount
, ts
.tv_sec
,
120 ts
.tv_nsec
, delta_ns
);
122 if (delta_ns
> UNREASONABLE_LAT
) {
133 struct itimerspec its1
, its2
;
135 struct sigaction act
;
136 int signum
= SIGRTMAX
;
138 /* Set up signal handler: */
139 sigfillset(&act
.sa_mask
);
141 act
.sa_handler
= sigalarm
;
142 sigaction(signum
, &act
, NULL
);
145 memset(&se
, 0, sizeof(se
));
146 se
.sigev_notify
= SIGEV_SIGNAL
;
147 se
.sigev_signo
= signum
;
148 se
.sigev_value
.sival_int
= 0;
150 for (alarm_clock_id
= CLOCK_REALTIME_ALARM
;
151 alarm_clock_id
<= CLOCK_BOOTTIME_ALARM
;
155 if (timer_create(alarm_clock_id
, &se
, &tm1
) == -1) {
156 printf("timer_create failled, %s unspported?\n",
157 clockstring(alarm_clock_id
));
161 clock_gettime(alarm_clock_id
, &start_time
);
162 printf("Start time (%s): %ld:%ld\n", clockstring(alarm_clock_id
),
163 start_time
.tv_sec
, start_time
.tv_nsec
);
164 printf("Setting alarm for every %i seconds\n", SUSPEND_SECS
);
165 its1
.it_value
= start_time
;
166 its1
.it_value
.tv_sec
+= SUSPEND_SECS
;
167 its1
.it_interval
.tv_sec
= SUSPEND_SECS
;
168 its1
.it_interval
.tv_nsec
= 0;
170 timer_settime(tm1
, TIMER_ABSTIME
, &its1
, &its2
);
172 while (alarmcount
< 5)
173 sleep(1); /* First 5 alarms, do nothing */
175 printf("Starting suspend loops\n");
176 while (alarmcount
< 10) {
180 ret
= system("echo mem > /sys/power/state");
187 return ksft_exit_fail();
188 return ksft_exit_pass();