18c2ecf20Sopenharmony_ci/* Measure nanosleep timer latency
28c2ecf20Sopenharmony_ci *              by: john stultz (john.stultz@linaro.org)
38c2ecf20Sopenharmony_ci *		(C) Copyright Linaro 2013
48c2ecf20Sopenharmony_ci *              Licensed under the GPLv2
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  To build:
78c2ecf20Sopenharmony_ci *	$ gcc nsleep-lat.c -o nsleep-lat -lrt
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *   This program is free software: you can redistribute it and/or modify
108c2ecf20Sopenharmony_ci *   it under the terms of the GNU General Public License as published by
118c2ecf20Sopenharmony_ci *   the Free Software Foundation, either version 2 of the License, or
128c2ecf20Sopenharmony_ci *   (at your option) any later version.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *   This program is distributed in the hope that it will be useful,
158c2ecf20Sopenharmony_ci *   but WITHOUT ANY WARRANTY; without even the implied warranty of
168c2ecf20Sopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
178c2ecf20Sopenharmony_ci *   GNU General Public License for more details.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <stdio.h>
218c2ecf20Sopenharmony_ci#include <stdlib.h>
228c2ecf20Sopenharmony_ci#include <time.h>
238c2ecf20Sopenharmony_ci#include <sys/time.h>
248c2ecf20Sopenharmony_ci#include <sys/timex.h>
258c2ecf20Sopenharmony_ci#include <string.h>
268c2ecf20Sopenharmony_ci#include <signal.h>
278c2ecf20Sopenharmony_ci#include "../kselftest.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define NSEC_PER_SEC 1000000000ULL
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define CLOCK_REALTIME			0
358c2ecf20Sopenharmony_ci#define CLOCK_MONOTONIC			1
368c2ecf20Sopenharmony_ci#define CLOCK_PROCESS_CPUTIME_ID	2
378c2ecf20Sopenharmony_ci#define CLOCK_THREAD_CPUTIME_ID		3
388c2ecf20Sopenharmony_ci#define CLOCK_MONOTONIC_RAW		4
398c2ecf20Sopenharmony_ci#define CLOCK_REALTIME_COARSE		5
408c2ecf20Sopenharmony_ci#define CLOCK_MONOTONIC_COARSE		6
418c2ecf20Sopenharmony_ci#define CLOCK_BOOTTIME			7
428c2ecf20Sopenharmony_ci#define CLOCK_REALTIME_ALARM		8
438c2ecf20Sopenharmony_ci#define CLOCK_BOOTTIME_ALARM		9
448c2ecf20Sopenharmony_ci#define CLOCK_HWSPECIFIC		10
458c2ecf20Sopenharmony_ci#define CLOCK_TAI			11
468c2ecf20Sopenharmony_ci#define NR_CLOCKIDS			12
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define UNSUPPORTED 0xf00f
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cichar *clockstring(int clockid)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	switch (clockid) {
538c2ecf20Sopenharmony_ci	case CLOCK_REALTIME:
548c2ecf20Sopenharmony_ci		return "CLOCK_REALTIME";
558c2ecf20Sopenharmony_ci	case CLOCK_MONOTONIC:
568c2ecf20Sopenharmony_ci		return "CLOCK_MONOTONIC";
578c2ecf20Sopenharmony_ci	case CLOCK_PROCESS_CPUTIME_ID:
588c2ecf20Sopenharmony_ci		return "CLOCK_PROCESS_CPUTIME_ID";
598c2ecf20Sopenharmony_ci	case CLOCK_THREAD_CPUTIME_ID:
608c2ecf20Sopenharmony_ci		return "CLOCK_THREAD_CPUTIME_ID";
618c2ecf20Sopenharmony_ci	case CLOCK_MONOTONIC_RAW:
628c2ecf20Sopenharmony_ci		return "CLOCK_MONOTONIC_RAW";
638c2ecf20Sopenharmony_ci	case CLOCK_REALTIME_COARSE:
648c2ecf20Sopenharmony_ci		return "CLOCK_REALTIME_COARSE";
658c2ecf20Sopenharmony_ci	case CLOCK_MONOTONIC_COARSE:
668c2ecf20Sopenharmony_ci		return "CLOCK_MONOTONIC_COARSE";
678c2ecf20Sopenharmony_ci	case CLOCK_BOOTTIME:
688c2ecf20Sopenharmony_ci		return "CLOCK_BOOTTIME";
698c2ecf20Sopenharmony_ci	case CLOCK_REALTIME_ALARM:
708c2ecf20Sopenharmony_ci		return "CLOCK_REALTIME_ALARM";
718c2ecf20Sopenharmony_ci	case CLOCK_BOOTTIME_ALARM:
728c2ecf20Sopenharmony_ci		return "CLOCK_BOOTTIME_ALARM";
738c2ecf20Sopenharmony_ci	case CLOCK_TAI:
748c2ecf20Sopenharmony_ci		return "CLOCK_TAI";
758c2ecf20Sopenharmony_ci	};
768c2ecf20Sopenharmony_ci	return "UNKNOWN_CLOCKID";
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistruct timespec timespec_add(struct timespec ts, unsigned long long ns)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	ts.tv_nsec += ns;
828c2ecf20Sopenharmony_ci	while (ts.tv_nsec >= NSEC_PER_SEC) {
838c2ecf20Sopenharmony_ci		ts.tv_nsec -= NSEC_PER_SEC;
848c2ecf20Sopenharmony_ci		ts.tv_sec++;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci	return ts;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cilong long timespec_sub(struct timespec a, struct timespec b)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
958c2ecf20Sopenharmony_ci	return ret;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciint nanosleep_lat_test(int clockid, long long ns)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	struct timespec start, end, target;
1018c2ecf20Sopenharmony_ci	long long latency = 0;
1028c2ecf20Sopenharmony_ci	int i, count;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	target.tv_sec = ns/NSEC_PER_SEC;
1058c2ecf20Sopenharmony_ci	target.tv_nsec = ns%NSEC_PER_SEC;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	if (clock_gettime(clockid, &start))
1088c2ecf20Sopenharmony_ci		return UNSUPPORTED;
1098c2ecf20Sopenharmony_ci	if (clock_nanosleep(clockid, 0, &target, NULL))
1108c2ecf20Sopenharmony_ci		return UNSUPPORTED;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	count = 10;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	/* First check relative latency */
1158c2ecf20Sopenharmony_ci	clock_gettime(clockid, &start);
1168c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++)
1178c2ecf20Sopenharmony_ci		clock_nanosleep(clockid, 0, &target, NULL);
1188c2ecf20Sopenharmony_ci	clock_gettime(clockid, &end);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	if (((timespec_sub(start, end)/count)-ns) > UNRESONABLE_LATENCY) {
1218c2ecf20Sopenharmony_ci		printf("Large rel latency: %lld ns :", (timespec_sub(start, end)/count)-ns);
1228c2ecf20Sopenharmony_ci		return -1;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/* Next check absolute latency */
1268c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
1278c2ecf20Sopenharmony_ci		clock_gettime(clockid, &start);
1288c2ecf20Sopenharmony_ci		target = timespec_add(start, ns);
1298c2ecf20Sopenharmony_ci		clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL);
1308c2ecf20Sopenharmony_ci		clock_gettime(clockid, &end);
1318c2ecf20Sopenharmony_ci		latency += timespec_sub(target, end);
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	if (latency/count > UNRESONABLE_LATENCY) {
1358c2ecf20Sopenharmony_ci		printf("Large abs latency: %lld ns :", latency/count);
1368c2ecf20Sopenharmony_ci		return -1;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return 0;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciint main(int argc, char **argv)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	long long length;
1478c2ecf20Sopenharmony_ci	int clockid, ret;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		/* Skip cputime clockids since nanosleep won't increment cputime */
1528c2ecf20Sopenharmony_ci		if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
1538c2ecf20Sopenharmony_ci				clockid == CLOCK_THREAD_CPUTIME_ID ||
1548c2ecf20Sopenharmony_ci				clockid == CLOCK_HWSPECIFIC)
1558c2ecf20Sopenharmony_ci			continue;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci		printf("nsleep latency %-26s ", clockstring(clockid));
1588c2ecf20Sopenharmony_ci		fflush(stdout);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci		length = 10;
1618c2ecf20Sopenharmony_ci		while (length <= (NSEC_PER_SEC * 10)) {
1628c2ecf20Sopenharmony_ci			ret = nanosleep_lat_test(clockid, length);
1638c2ecf20Sopenharmony_ci			if (ret)
1648c2ecf20Sopenharmony_ci				break;
1658c2ecf20Sopenharmony_ci			length *= 100;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci		}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		if (ret == UNSUPPORTED) {
1708c2ecf20Sopenharmony_ci			printf("[UNSUPPORTED]\n");
1718c2ecf20Sopenharmony_ci			continue;
1728c2ecf20Sopenharmony_ci		}
1738c2ecf20Sopenharmony_ci		if (ret < 0) {
1748c2ecf20Sopenharmony_ci			printf("[FAILED]\n");
1758c2ecf20Sopenharmony_ci			return ksft_exit_fail();
1768c2ecf20Sopenharmony_ci		}
1778c2ecf20Sopenharmony_ci		printf("[OK]\n");
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci	return ksft_exit_pass();
1808c2ecf20Sopenharmony_ci}
181