1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <getopt.h>
19#include <inttypes.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <time.h>
24#include <unistd.h>
25
26#include <sys/epoll.h>
27#include <sys/timerfd.h>
28
29#include <cutils/klog.h>
30
31#define NSEC_PER_SEC (1000*1000*1000)
32#define MSEC_PER_SEC 1000
33#define NSEC_PER_MSEC (NSEC_PER_SEC/MSEC_PER_SEC)
34
35long long timediff_ns(const struct timespec *a, const struct timespec *b) {
36    return ((long long)(a->tv_sec - b->tv_sec)) * NSEC_PER_SEC +
37            (a->tv_nsec - b->tv_nsec);
38}
39
40void usage(void)
41{
42    printf("usage: suspend_stress [ <options> ]\n"
43           "options:\n"
44           "  -a,--abort                abort test on late alarm\n"
45           "  -c,--count=<count>        number of times to suspend (default infinite)\n"
46           "  -t,--time=<seconds>       time to suspend for (default 5)\n"
47        );
48}
49
50int main(int argc, char **argv)
51{
52    int alarm_time = 5;
53    int count = -1;
54    bool abort_on_failure = false;
55
56    while (1) {
57        const static struct option long_options[] = {
58            {"abort", no_argument, 0, 'a'},
59            {"count", required_argument, 0, 'c'},
60            {"time", required_argument, 0, 't'},
61        };
62        int c = getopt_long(argc, argv, "ac:t:", long_options, NULL);
63        if (c < 0) {
64            break;
65        }
66
67        switch (c) {
68        case 'a':
69            abort_on_failure = true;
70            break;
71        case 'c':
72            count = strtoul(optarg, NULL, 0);
73            break;
74        case 't':
75            alarm_time = strtoul(optarg, NULL, 0);
76            break;
77        case '?':
78            usage();
79            exit(EXIT_FAILURE);
80        default:
81            abort();
82        }
83    }
84
85    klog_init();
86    klog_set_level(KLOG_INFO_LEVEL);
87
88    if (optind < argc) {
89        fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
90        usage();
91        exit(EXIT_FAILURE);
92    }
93
94    int fd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
95    if (fd < 0) {
96        perror("timerfd_create failed");
97        exit(EXIT_FAILURE);
98    }
99
100    struct itimerspec delay = itimerspec();
101    delay.it_value.tv_sec = alarm_time;
102    int i = 0;
103
104    int epoll_fd = epoll_create(1);
105    if (epoll_fd < 0) {
106        perror("epoll_create failed");
107        exit(EXIT_FAILURE);
108    }
109
110    struct epoll_event ev = epoll_event();
111    ev.events = EPOLLIN | EPOLLWAKEUP;
112    int ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
113    if (ret < 0) {
114        perror("epoll_ctl failed");
115        exit(EXIT_FAILURE);
116    }
117
118    while (count != 0) {
119        struct timespec expected_time;
120        struct timespec actual_time;
121        uint64_t fired = 0;
122
123        ret = timerfd_settime(fd, 0, &delay, NULL);
124        if (ret < 0) {
125            perror("timerfd_settime failed");
126            exit(EXIT_FAILURE);
127        }
128
129        ret = clock_gettime(CLOCK_BOOTTIME, &expected_time);
130        if (ret < 0) {
131            perror("failed to get time");
132            exit(EXIT_FAILURE);
133        }
134        expected_time.tv_sec += alarm_time;
135
136        ret = 0;
137        while (ret != 1) {
138            struct epoll_event out_ev;
139            ret = epoll_wait(epoll_fd, &out_ev, 1, -1);
140            if (ret < 0 && errno != EINTR) {
141                perror("epoll_wait failed");
142                exit(EXIT_FAILURE);
143            }
144        }
145
146        ssize_t bytes = read(fd, &fired, sizeof(fired));
147        if (bytes < 0) {
148            perror("read from timer fd failed");
149            exit(EXIT_FAILURE);
150        } else if (bytes < (ssize_t)sizeof(fired)) {
151            fprintf(stderr, "unexpected read from timer fd: %zd\n", bytes);
152        }
153
154        if (fired != 1) {
155            fprintf(stderr, "unexpected timer fd fired count: %" PRIu64 "\n", fired);
156        }
157
158        ret = clock_gettime(CLOCK_BOOTTIME, &actual_time);
159        if (ret < 0) {
160            perror("failed to get time");
161            exit(EXIT_FAILURE);
162        }
163
164        long long diff = timediff_ns(&actual_time, &expected_time);
165        if (llabs(diff) > NSEC_PER_SEC) {
166            fprintf(stderr, "alarm arrived %lld.%03lld seconds %s\n",
167                    llabs(diff) / NSEC_PER_SEC,
168                    (llabs(diff) / NSEC_PER_MSEC) % MSEC_PER_SEC,
169                    diff > 0 ? "late" : "early");
170            KLOG_ERROR("suspend_stress", "alarm arrived %lld.%03lld seconds %s\n",
171                    llabs(diff) / NSEC_PER_SEC,
172                    (llabs(diff) / NSEC_PER_MSEC) % MSEC_PER_SEC,
173                    diff > 0 ? "late" : "early");
174            if (abort_on_failure) {
175                exit(EXIT_FAILURE);
176            }
177        }
178
179        time_t t = time(NULL);
180        i += fired;
181        printf("timer fired: %d at boottime %lld.%.3ld, %s", i,
182                   (long long)actual_time.tv_sec,
183                   actual_time.tv_nsec / NSEC_PER_MSEC,
184                   ctime(&t));
185
186        KLOG_INFO("suspend_stress", "timer fired: %d at boottime %lld.%.3ld, %s", i,
187                   (long long)actual_time.tv_sec,
188                   actual_time.tv_nsec / NSEC_PER_MSEC,
189                   ctime(&t));
190
191        if (count > 0)
192            count--;
193    }
194    return 0;
195}
196