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) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*  cpufreq-bench CPUFreq microbenchmark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *  Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <cpupower.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "config.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "system.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * returns time since epoch in µs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * @retval time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) long long int get_time()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct timeval now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	gettimeofday(&now, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return (long long int)(now.tv_sec * 1000000LL + now.tv_usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * sets the cpufreq governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * @param governor cpufreq governor name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * @param cpu cpu for which the governor should be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * @retval 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * @retval -1 when failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) int set_cpufreq_governor(char *governor, unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	dprintf("set %s as cpufreq governor\n", governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (cpupower_is_cpu_online(cpu) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		perror("cpufreq_cpu_exists");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		fprintf(stderr, "error: cpu %u does not exist\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (cpufreq_modify_policy_governor(cpu, governor) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		perror("cpufreq_modify_policy_governor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		fprintf(stderr, "error: unable to set %s governor\n", governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * sets cpu affinity for the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * @param cpu cpu# to which the affinity should be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * @retval 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * @retval -1 when setting the affinity failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) int set_cpu_affinity(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	cpu_set_t cpuset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	CPU_ZERO(&cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	CPU_SET(cpu, &cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	dprintf("set affinity to cpu #%u\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		perror("sched_setaffinity");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		fprintf(stderr, "warning: unable to set cpu affinity\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return -1;
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  * sets the process priority parameter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * @param priority priority value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * @retval 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * @retval -1 when setting the priority failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int set_process_priority(int priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct sched_param param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	dprintf("set scheduler priority to %i\n", priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	param.sched_priority = priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (sched_setscheduler(0, SCHEDULER, &param) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		perror("sched_setscheduler");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		fprintf(stderr, "warning: unable to set scheduler priority\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * notifies the user that the benchmark may run some time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * @param config benchmark config values
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) void prepare_user(const struct config *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	unsigned long sleep_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	unsigned long load_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	unsigned int round;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	for (round = 0; round < config->rounds; round++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		sleep_time +=  2 * config->cycles *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			(config->sleep + config->sleep_step * round);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		load_time += 2 * config->cycles *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			(config->load + config->load_step * round) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			(config->load + config->load_step * round * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (config->verbose || config->output != stdout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		printf("approx. test duration: %im\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		       (int)((sleep_time + load_time) / 60000000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * sets up the cpu affinity and scheduler priority
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  * @param config benchmark config values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) void prepare_system(const struct config *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		printf("set cpu affinity to cpu #%u\n", config->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	set_cpu_affinity(config->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	switch (config->prio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	case SCHED_HIGH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			printf("high priority condition requested\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		set_process_priority(PRIORITY_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	case SCHED_LOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			printf("low priority condition requested\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		set_process_priority(PRIORITY_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			printf("default priority condition requested\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		set_process_priority(PRIORITY_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)