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