1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* cpufreq-bench CPUFreq microbenchmark
4 * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
10 #include <sys/types.h>
22 * returns time since epoch in µs
27 long long int get_time()
31 gettimeofday(&now
, NULL
);
33 return (long long int)(now
.tv_sec
* 1000000LL + now
.tv_usec
);
37 * sets the cpufreq governor
39 * @param governor cpufreq governor name
40 * @param cpu cpu for which the governor should be set
42 * @retval 0 on success
43 * @retval -1 when failed
46 int set_cpufreq_governor(char *governor
, unsigned int cpu
)
49 dprintf("set %s as cpufreq governor\n", governor
);
51 if (cpupower_is_cpu_online(cpu
) != 1) {
52 perror("cpufreq_cpu_exists");
53 fprintf(stderr
, "error: cpu %u does not exist\n", cpu
);
57 if (cpufreq_modify_policy_governor(cpu
, governor
) != 0) {
58 perror("cpufreq_modify_policy_governor");
59 fprintf(stderr
, "error: unable to set %s governor\n", governor
);
67 * sets cpu affinity for the process
69 * @param cpu cpu# to which the affinity should be set
71 * @retval 0 on success
72 * @retval -1 when setting the affinity failed
75 int set_cpu_affinity(unsigned int cpu
)
80 CPU_SET(cpu
, &cpuset
);
82 dprintf("set affinity to cpu #%u\n", cpu
);
84 if (sched_setaffinity(getpid(), sizeof(cpu_set_t
), &cpuset
) < 0) {
85 perror("sched_setaffinity");
86 fprintf(stderr
, "warning: unable to set cpu affinity\n");
94 * sets the process priority parameter
96 * @param priority priority value
98 * @retval 0 on success
99 * @retval -1 when setting the priority failed
102 int set_process_priority(int priority
)
104 struct sched_param param
;
106 dprintf("set scheduler priority to %i\n", priority
);
108 param
.sched_priority
= priority
;
110 if (sched_setscheduler(0, SCHEDULER
, ¶m
) < 0) {
111 perror("sched_setscheduler");
112 fprintf(stderr
, "warning: unable to set scheduler priority\n");
120 * notifies the user that the benchmark may run some time
122 * @param config benchmark config values
126 void prepare_user(const struct config
*config
)
128 unsigned long sleep_time
= 0;
129 unsigned long load_time
= 0;
132 for (round
= 0; round
< config
->rounds
; round
++) {
133 sleep_time
+= 2 * config
->cycles
*
134 (config
->sleep
+ config
->sleep_step
* round
);
135 load_time
+= 2 * config
->cycles
*
136 (config
->load
+ config
->load_step
* round
) +
137 (config
->load
+ config
->load_step
* round
* 4);
140 if (config
->verbose
|| config
->output
!= stdout
)
141 printf("approx. test duration: %im\n",
142 (int)((sleep_time
+ load_time
) / 60000000));
146 * sets up the cpu affinity and scheduler priority
148 * @param config benchmark config values
152 void prepare_system(const struct config
*config
)
155 printf("set cpu affinity to cpu #%u\n", config
->cpu
);
157 set_cpu_affinity(config
->cpu
);
159 switch (config
->prio
) {
162 printf("high priority condition requested\n");
164 set_process_priority(PRIORITY_HIGH
);
168 printf("low priority condition requested\n");
170 set_process_priority(PRIORITY_LOW
);
174 printf("default priority condition requested\n");
176 set_process_priority(PRIORITY_DEFAULT
);