• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
timediff_ns(const struct timespec * a,const struct timespec * b)35 long 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 
usage(void)40 void 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 
main(int argc,char ** argv)50 int 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_set_level(KLOG_INFO_LEVEL);
86 
87     if (optind < argc) {
88         fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
89         usage();
90         exit(EXIT_FAILURE);
91     }
92 
93     int fd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0);
94     if (fd < 0) {
95         perror("timerfd_create failed");
96         exit(EXIT_FAILURE);
97     }
98 
99     struct itimerspec delay = itimerspec();
100     delay.it_value.tv_sec = alarm_time;
101     int i = 0;
102 
103     int epoll_fd = epoll_create(1);
104     if (epoll_fd < 0) {
105         perror("epoll_create failed");
106         exit(EXIT_FAILURE);
107     }
108 
109     struct epoll_event ev = epoll_event();
110     ev.events = EPOLLIN | EPOLLWAKEUP;
111     int ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
112     if (ret < 0) {
113         perror("epoll_ctl failed");
114         exit(EXIT_FAILURE);
115     }
116 
117     while (count != 0) {
118         struct timespec expected_time;
119         struct timespec actual_time;
120         uint64_t fired = 0;
121 
122         ret = timerfd_settime(fd, 0, &delay, NULL);
123         if (ret < 0) {
124             perror("timerfd_settime failed");
125             exit(EXIT_FAILURE);
126         }
127 
128         ret = clock_gettime(CLOCK_BOOTTIME, &expected_time);
129         if (ret < 0) {
130             perror("failed to get time");
131             exit(EXIT_FAILURE);
132         }
133         expected_time.tv_sec += alarm_time;
134 
135         ret = 0;
136         while (ret != 1) {
137             struct epoll_event out_ev;
138             ret = epoll_wait(epoll_fd, &out_ev, 1, -1);
139             if (ret < 0 && errno != EINTR) {
140                 perror("epoll_wait failed");
141                 exit(EXIT_FAILURE);
142             }
143         }
144 
145         ssize_t bytes = read(fd, &fired, sizeof(fired));
146         if (bytes < 0) {
147             perror("read from timer fd failed");
148             exit(EXIT_FAILURE);
149         } else if (bytes < (ssize_t)sizeof(fired)) {
150             fprintf(stderr, "unexpected read from timer fd: %zd\n", bytes);
151         }
152 
153         if (fired != 1) {
154             fprintf(stderr, "unexpected timer fd fired count: %" PRIu64 "\n", fired);
155         }
156 
157         ret = clock_gettime(CLOCK_BOOTTIME, &actual_time);
158         if (ret < 0) {
159             perror("failed to get time");
160             exit(EXIT_FAILURE);
161         }
162 
163         long long diff = timediff_ns(&actual_time, &expected_time);
164         if (llabs(diff) > NSEC_PER_SEC) {
165             fprintf(stderr, "alarm arrived %lld.%03lld seconds %s\n",
166                     llabs(diff) / NSEC_PER_SEC,
167                     (llabs(diff) / NSEC_PER_MSEC) % MSEC_PER_SEC,
168                     diff > 0 ? "late" : "early");
169             KLOG_ERROR("suspend_stress", "alarm arrived %lld.%03lld seconds %s\n",
170                     llabs(diff) / NSEC_PER_SEC,
171                     (llabs(diff) / NSEC_PER_MSEC) % MSEC_PER_SEC,
172                     diff > 0 ? "late" : "early");
173             if (abort_on_failure) {
174                 exit(EXIT_FAILURE);
175             }
176         }
177 
178         time_t t = time(NULL);
179         i += fired;
180         printf("timer fired: %d at boottime %lld.%.3ld, %s", i,
181                    (long long)actual_time.tv_sec,
182                    actual_time.tv_nsec / NSEC_PER_MSEC,
183                    ctime(&t));
184 
185         KLOG_INFO("suspend_stress", "timer fired: %d at boottime %lld.%.3ld, %s", i,
186                    (long long)actual_time.tv_sec,
187                    actual_time.tv_nsec / NSEC_PER_MSEC,
188                    ctime(&t));
189 
190         if (count > 0)
191             count--;
192     }
193     return 0;
194 }
195