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