1 /* cpufreq-bench CPUFreq microbenchmark
3 * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
34 * returns time since epoch in µs
39 long long int get_time()
43 gettimeofday(&now, NULL);
45 return (long long int)(now.tv_sec * 1000000LL + now.tv_usec);
49 * sets the cpufreq governor
51 * @param governor cpufreq governor name
52 * @param cpu cpu for which the governor should be set
54 * @retval 0 on success
55 * @retval -1 when failed
58 int set_cpufreq_governor(char *governor, unsigned int cpu)
61 dprintf("set %s as cpufreq governor\n", governor);
63 if (cpufreq_cpu_exists(cpu) != 0) {
64 perror("cpufreq_cpu_exists");
65 fprintf(stderr, "error: cpu %u does not exist\n", cpu);
69 if (cpufreq_modify_policy_governor(cpu, governor) != 0) {
70 perror("cpufreq_modify_policy_governor");
71 fprintf(stderr, "error: unable to set %s governor\n", governor);
79 * sets cpu affinity for the process
81 * @param cpu cpu# to which the affinity should be set
83 * @retval 0 on success
84 * @retval -1 when setting the affinity failed
87 int set_cpu_affinity(unsigned int cpu)
92 CPU_SET(cpu, &cpuset);
94 dprintf("set affinity to cpu #%u\n", cpu);
96 if (sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset) < 0) {
97 perror("sched_setaffinity");
98 fprintf(stderr, "warning: unable to set cpu affinity\n");
106 * sets the process priority parameter
108 * @param priority priority value
110 * @retval 0 on success
111 * @retval -1 when setting the priority failed
114 int set_process_priority(int priority)
116 struct sched_param param;
118 dprintf("set scheduler priority to %i\n", priority);
120 param.sched_priority = priority;
122 if (sched_setscheduler(0, SCHEDULER, ¶m) < 0) {
123 perror("sched_setscheduler");
124 fprintf(stderr, "warning: unable to set scheduler priority\n");
132 * notifies the user that the benchmark may run some time
134 * @param config benchmark config values
138 void prepare_user(const struct config *config)
140 unsigned long sleep_time = 0;
141 unsigned long load_time = 0;
144 for (round = 0; round < config->rounds; round++) {
145 sleep_time += 2 * config->cycles *
146 (config->sleep + config->sleep_step * round);
147 load_time += 2 * config->cycles *
148 (config->load + config->load_step * round) +
149 (config->load + config->load_step * round * 4);
152 if (config->verbose || config->output != stdout)
153 printf("approx. test duration: %im\n",
154 (int)((sleep_time + load_time) / 60000000));
158 * sets up the cpu affinity and scheduler priority
160 * @param config benchmark config values
164 void prepare_system(const struct config *config)
167 printf("set cpu affinity to cpu #%u\n", config->cpu);
169 set_cpu_affinity(config->cpu);
171 switch (config->prio) {
174 printf("high priority condition requested\n");
176 set_process_priority(PRIORITY_HIGH);
180 printf("low priority condition requested\n");
182 set_process_priority(PRIORITY_LOW);
186 printf("default priority condition requested\n");
188 set_process_priority(PRIORITY_DEFAULT);