linux/tools/testing/selftests/timers/alarmtimer-suspend.c
<<
>>
Prefs
   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
  23
  24#include <stdio.h>
  25#include <unistd.h>
  26#include <time.h>
  27#include <string.h>
  28#include <signal.h>
  29#include <stdlib.h>
  30#include <pthread.h>
  31#ifdef KTEST
  32#include "../kselftest.h"
  33#else
  34static inline int ksft_exit_pass(void)
  35{
  36        exit(0);
  37}
  38static inline int ksft_exit_fail(void)
  39{
  40        exit(1);
  41}
  42#endif
  43
  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
  55#define CLOCK_TAI                       11
  56#define NR_CLOCKIDS                     12
  57
  58
  59#define NSEC_PER_SEC 1000000000ULL
  60#define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
  61
  62#define SUSPEND_SECS 15
  63int alarmcount;
  64int alarm_clock_id;
  65struct timespec start_time;
  66
  67
  68char *clockstring(int clockid)
  69{
  70        switch (clockid) {
  71        case CLOCK_REALTIME:
  72                return "CLOCK_REALTIME";
  73        case CLOCK_MONOTONIC:
  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";
  85        case CLOCK_BOOTTIME:
  86                return "CLOCK_BOOTTIME";
  87        case CLOCK_REALTIME_ALARM:
  88                return "CLOCK_REALTIME_ALARM";
  89        case CLOCK_BOOTTIME_ALARM:
  90                return "CLOCK_BOOTTIME_ALARM";
  91        case CLOCK_TAI:
  92                return "CLOCK_TAI";
  93        };
  94        return "UNKNOWN_CLOCKID";
  95}
  96
  97
  98long long timespec_sub(struct timespec a, struct timespec b)
  99{
 100        long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
 101
 102        ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
 103        return ret;
 104}
 105
 106int final_ret = 0;
 107
 108void sigalarm(int signo)
 109{
 110        long long delta_ns;
 111        struct timespec ts;
 112
 113        clock_gettime(alarm_clock_id, &ts);
 114        alarmcount++;
 115
 116        delta_ns = timespec_sub(start_time, ts);
 117        delta_ns -= NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
 118
 119        printf("ALARM(%i): %ld:%ld latency: %lld ns ", alarmcount, ts.tv_sec,
 120                                                        ts.tv_nsec, delta_ns);
 121
 122        if (delta_ns > UNREASONABLE_LAT) {
 123                printf("[FAIL]\n");
 124                final_ret = -1;
 125        } else
 126                printf("[OK]\n");
 127
 128}
 129
 130int main(void)
 131{
 132        timer_t tm1;
 133        struct itimerspec its1, its2;
 134        struct sigevent se;
 135        struct sigaction act;
 136        int signum = SIGRTMAX;
 137
 138        /* Set up signal handler: */
 139        sigfillset(&act.sa_mask);
 140        act.sa_flags = 0;
 141        act.sa_handler = sigalarm;
 142        sigaction(signum, &act, NULL);
 143
 144        /* Set up timer: */
 145        memset(&se, 0, sizeof(se));
 146        se.sigev_notify = SIGEV_SIGNAL;
 147        se.sigev_signo = signum;
 148        se.sigev_value.sival_int = 0;
 149
 150        for (alarm_clock_id = CLOCK_REALTIME_ALARM;
 151                        alarm_clock_id <= CLOCK_BOOTTIME_ALARM;
 152                        alarm_clock_id++) {
 153
 154                alarmcount = 0;
 155                if (timer_create(alarm_clock_id, &se, &tm1) == -1) {
 156                        printf("timer_create failed, %s unsupported?\n",
 157                                        clockstring(alarm_clock_id));
 158                        break;
 159                }
 160
 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;
 169
 170                timer_settime(tm1, TIMER_ABSTIME, &its1, &its2);
 171
 172                while (alarmcount < 5)
 173                        sleep(1); /* First 5 alarms, do nothing */
 174
 175                printf("Starting suspend loops\n");
 176                while (alarmcount < 10) {
 177                        int ret;
 178
 179                        sleep(3);
 180                        ret = system("echo mem > /sys/power/state");
 181                        if (ret)
 182                                break;
 183                }
 184                timer_delete(tm1);
 185        }
 186        if (final_ret)
 187                return ksft_exit_fail();
 188        return ksft_exit_pass();
 189}
 190