1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* cpufreq-bench CPUFreq microbenchmark
4 * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
13 #include "benchmark.h"
15 /* Print out progress if we log into a file */
16 #define show_progress(total_time, progress_time) \
17 if (config->output != stdout) { \
18 fprintf(stdout, "Progress: %02lu %%\r", \
19 (progress_time * 100) / total_time); \
24 * compute how many rounds of calculation we should do
25 * to get the given load time
27 * @param load aimed load time in µs
29 * @retval rounds of calculation
32 unsigned int calculate_timespace(long load
, struct config
*config
)
36 unsigned int estimated
= GAUGECOUNT
;
37 unsigned int rounds
= 0;
38 unsigned int timed
= 0;
41 printf("calibrating load of %lius, please wait...\n", load
);
43 /* get the initial calculation time for a specific number of rounds */
48 timed
= (unsigned int)(then
- now
);
50 /* approximation of the wanted load time by comparing with the
51 * initial calculation time */
52 for (i
= 0; i
< 4; i
++) {
53 rounds
= (unsigned int)(load
* estimated
/ timed
);
54 dprintf("calibrating with %u rounds\n", rounds
);
59 timed
= (unsigned int)(then
- now
);
63 printf("calibration done\n");
70 * generates a specific sleep an load time with the performance
71 * governor and compares the used time for same calculations done
72 * with the configured powersave governor
74 * @param config config values for the benchmark
78 void start_benchmark(struct config
*config
)
80 unsigned int _round
, cycle
;
82 long sleep_time
= 0, load_time
= 0;
83 long performance_time
= 0, powersave_time
= 0;
84 unsigned int calculations
;
85 unsigned long total_time
= 0, progress_time
= 0;
87 sleep_time
= config
->sleep
;
88 load_time
= config
->load
;
90 /* For the progress bar */
91 for (_round
= 1; _round
<= config
->rounds
; _round
++)
92 total_time
+= _round
* (config
->sleep
+ config
->load
);
93 total_time
*= 2; /* powersave and performance cycles */
95 for (_round
= 0; _round
< config
->rounds
; _round
++) {
96 performance_time
= 0LL;
99 show_progress(total_time
, progress_time
);
101 /* set the cpufreq governor to "performance" which disables
102 * P-State switching. */
103 if (set_cpufreq_governor("performance", config
->cpu
) != 0)
106 /* calibrate the calculation time. the resulting calculation
107 * _rounds should produce a load which matches the configured
109 calculations
= calculate_timespace(load_time
, config
);
112 printf("_round %i: doing %u cycles with %u calculations"
113 " for %lius\n", _round
+ 1, config
->cycles
,
114 calculations
, load_time
);
116 fprintf(config
->output
, "%u %li %li ",
117 _round
, load_time
, sleep_time
);
120 printf("average: %lius, rps:%li\n",
121 load_time
/ calculations
,
122 1000000 * calculations
/ load_time
);
124 /* do some sleep/load cycles with the performance governor */
125 for (cycle
= 0; cycle
< config
->cycles
; cycle
++) {
128 ROUNDS(calculations
);
130 performance_time
+= then
- now
- sleep_time
;
132 printf("performance cycle took %lius, "
134 "load: %lius, rounds: %u\n",
135 (long)(then
- now
), sleep_time
,
136 load_time
, calculations
);
138 fprintf(config
->output
, "%li ",
139 performance_time
/ config
->cycles
);
141 progress_time
+= sleep_time
+ load_time
;
142 show_progress(total_time
, progress_time
);
144 /* set the powersave governor which activates P-State switching
146 if (set_cpufreq_governor(config
->governor
, config
->cpu
) != 0)
149 /* again, do some sleep/load cycles with the
150 * powersave governor */
151 for (cycle
= 0; cycle
< config
->cycles
; cycle
++) {
154 ROUNDS(calculations
);
156 powersave_time
+= then
- now
- sleep_time
;
158 printf("powersave cycle took %lius, "
160 "load: %lius, rounds: %u\n",
161 (long)(then
- now
), sleep_time
,
162 load_time
, calculations
);
165 progress_time
+= sleep_time
+ load_time
;
167 /* compare the average sleep/load cycles */
168 fprintf(config
->output
, "%li ",
169 powersave_time
/ config
->cycles
);
170 fprintf(config
->output
, "%.3f\n",
171 performance_time
* 100.0 / powersave_time
);
172 fflush(config
->output
);
175 printf("performance is at %.2f%%\n",
176 performance_time
* 100.0 / powersave_time
);
178 sleep_time
+= config
->sleep_step
;
179 load_time
+= config
->load_step
;