File Index
Files and directories
Sign in
FILE

alarmtimer-suspend.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/timers/alarmtimer-suspend.c C
  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#include <include/vdso/time64.h>
 32#include <errno.h>
 33#include "kselftest.h"
 34
 35#define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
 36
 37#define SUSPEND_SECS 15
 38int alarmcount;
 39int alarm_clock_id;
 40struct timespec start_time;
 41
 42
 43char *clockstring(int clockid)
 44{
 45	switch (clockid) {
 46	case CLOCK_REALTIME:
 47		return "CLOCK_REALTIME";
 48	case CLOCK_MONOTONIC:
 49		return "CLOCK_MONOTONIC";
 50	case CLOCK_PROCESS_CPUTIME_ID:
 51		return "CLOCK_PROCESS_CPUTIME_ID";
 52	case CLOCK_THREAD_CPUTIME_ID:
 53		return "CLOCK_THREAD_CPUTIME_ID";
 54	case CLOCK_MONOTONIC_RAW:
 55		return "CLOCK_MONOTONIC_RAW";
 56	case CLOCK_REALTIME_COARSE:
 57		return "CLOCK_REALTIME_COARSE";
 58	case CLOCK_MONOTONIC_COARSE:
 59		return "CLOCK_MONOTONIC_COARSE";
 60	case CLOCK_BOOTTIME:
 61		return "CLOCK_BOOTTIME";
 62	case CLOCK_REALTIME_ALARM:
 63		return "CLOCK_REALTIME_ALARM";
 64	case CLOCK_BOOTTIME_ALARM:
 65		return "CLOCK_BOOTTIME_ALARM";
 66	case CLOCK_TAI:
 67		return "CLOCK_TAI";
 68	}
 69	return "UNKNOWN_CLOCKID";
 70}
 71
 72
 73long long timespec_sub(struct timespec a, struct timespec b)
 74{
 75	long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
 76
 77	ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
 78	return ret;
 79}
 80
 81int final_ret;
 82
 83void sigalarm(int signo)
 84{
 85	long long delta_ns;
 86	struct timespec ts;
 87
 88	clock_gettime(alarm_clock_id, &ts);
 89	alarmcount++;
 90
 91	delta_ns = timespec_sub(start_time, ts);
 92	delta_ns -= NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
 93
 94	printf("ALARM(%i): %ld:%ld latency: %lld ns ", alarmcount, ts.tv_sec,
 95							ts.tv_nsec, delta_ns);
 96
 97	if (delta_ns > UNREASONABLE_LAT) {
 98		printf("[FAIL]\n");
 99		final_ret = -1;
100	} else
101		printf("[OK]\n");
102
103}
104
105int main(void)
106{
107	timer_t tm1;
108	struct itimerspec its1, its2;
109	struct sigevent se;
110	struct sigaction act;
111	int signum = SIGRTMAX;
112
113	/* Set up signal handler: */
114	sigfillset(&act.sa_mask);
115	act.sa_flags = 0;
116	act.sa_handler = sigalarm;
117	sigaction(signum, &act, NULL);
118
119	/* Set up timer: */
120	memset(&se, 0, sizeof(se));
121	se.sigev_notify = SIGEV_SIGNAL;
122	se.sigev_signo = signum;
123	se.sigev_value.sival_int = 0;
124
125	for (alarm_clock_id = CLOCK_REALTIME_ALARM;
126			alarm_clock_id <= CLOCK_BOOTTIME_ALARM;
127			alarm_clock_id++) {
128
129		alarmcount = 0;
130		if (timer_create(alarm_clock_id, &se, &tm1) == -1) {
131			printf("timer_create failed, %s unsupported?: %s\n",
132					clockstring(alarm_clock_id), strerror(errno));
133			break;
134		}
135
136		clock_gettime(alarm_clock_id, &start_time);
137		printf("Start time (%s): %ld:%ld\n", clockstring(alarm_clock_id),
138				start_time.tv_sec, start_time.tv_nsec);
139		printf("Setting alarm for every %i seconds\n", SUSPEND_SECS);
140		its1.it_value = start_time;
141		its1.it_value.tv_sec += SUSPEND_SECS;
142		its1.it_interval.tv_sec = SUSPEND_SECS;
143		its1.it_interval.tv_nsec = 0;
144
145		timer_settime(tm1, TIMER_ABSTIME, &its1, &its2);
146
147		while (alarmcount < 5)
148			sleep(1); /* First 5 alarms, do nothing */
149
150		printf("Starting suspend loops\n");
151		while (alarmcount < 10) {
152			int ret;
153
154			sleep(3);
155			ret = system("echo mem > /sys/power/state");
156			if (ret)
157				break;
158		}
159		timer_delete(tm1);
160	}
161	if (final_ret)
162		ksft_exit_fail();
163	ksft_exit_pass();
164}
~/ Read-only file preview.