Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* Measure nanosleep timer latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  *              by: john stultz (john.stultz@linaro.org)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *		(C) Copyright Linaro 2013
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *              Licensed under the GPLv2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  To build:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	$ gcc nsleep-lat.c -o nsleep-lat -lrt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *   This program is free software: you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *   it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *   the Free Software Foundation, either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *   (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *   This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *   GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <sys/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "../kselftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define NSEC_PER_SEC 1000000000ULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define CLOCK_REALTIME			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define CLOCK_MONOTONIC			1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define CLOCK_PROCESS_CPUTIME_ID	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define CLOCK_THREAD_CPUTIME_ID		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define CLOCK_MONOTONIC_RAW		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define CLOCK_REALTIME_COARSE		5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define CLOCK_MONOTONIC_COARSE		6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define CLOCK_BOOTTIME			7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define CLOCK_REALTIME_ALARM		8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define CLOCK_BOOTTIME_ALARM		9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define CLOCK_HWSPECIFIC		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define CLOCK_TAI			11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define NR_CLOCKIDS			12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define UNSUPPORTED 0xf00f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) char *clockstring(int clockid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	switch (clockid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	case CLOCK_REALTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return "CLOCK_REALTIME";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	case CLOCK_MONOTONIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return "CLOCK_MONOTONIC";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	case CLOCK_PROCESS_CPUTIME_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return "CLOCK_PROCESS_CPUTIME_ID";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	case CLOCK_THREAD_CPUTIME_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return "CLOCK_THREAD_CPUTIME_ID";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	case CLOCK_MONOTONIC_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return "CLOCK_MONOTONIC_RAW";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	case CLOCK_REALTIME_COARSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return "CLOCK_REALTIME_COARSE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	case CLOCK_MONOTONIC_COARSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		return "CLOCK_MONOTONIC_COARSE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	case CLOCK_BOOTTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return "CLOCK_BOOTTIME";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	case CLOCK_REALTIME_ALARM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return "CLOCK_REALTIME_ALARM";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	case CLOCK_BOOTTIME_ALARM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		return "CLOCK_BOOTTIME_ALARM";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	case CLOCK_TAI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		return "CLOCK_TAI";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return "UNKNOWN_CLOCKID";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) struct timespec timespec_add(struct timespec ts, unsigned long long ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	ts.tv_nsec += ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	while (ts.tv_nsec >= NSEC_PER_SEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		ts.tv_nsec -= NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		ts.tv_sec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) long long timespec_sub(struct timespec a, struct timespec b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) int nanosleep_lat_test(int clockid, long long ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct timespec start, end, target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	long long latency = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int i, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	target.tv_sec = ns/NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	target.tv_nsec = ns%NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (clock_gettime(clockid, &start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return UNSUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (clock_nanosleep(clockid, 0, &target, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return UNSUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	count = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* First check relative latency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	clock_gettime(clockid, &start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		clock_nanosleep(clockid, 0, &target, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	clock_gettime(clockid, &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (((timespec_sub(start, end)/count)-ns) > UNRESONABLE_LATENCY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		printf("Large rel latency: %lld ns :", (timespec_sub(start, end)/count)-ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	/* Next check absolute latency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		clock_gettime(clockid, &start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		target = timespec_add(start, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		clock_gettime(clockid, &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		latency += timespec_sub(target, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (latency/count > UNRESONABLE_LATENCY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		printf("Large abs latency: %lld ns :", latency/count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	long long length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	int clockid, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		/* Skip cputime clockids since nanosleep won't increment cputime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				clockid == CLOCK_THREAD_CPUTIME_ID ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				clockid == CLOCK_HWSPECIFIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		printf("nsleep latency %-26s ", clockstring(clockid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		length = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		while (length <= (NSEC_PER_SEC * 10)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			ret = nanosleep_lat_test(clockid, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			length *= 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		if (ret == UNSUPPORTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			printf("[UNSUPPORTED]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			printf("[FAILED]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			return ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		printf("[OK]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return ksft_exit_pass();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }