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