^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 <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <math.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "config.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "system.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "benchmark.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* Print out progress if we log into a file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define show_progress(total_time, progress_time) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (config->output != stdout) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) fprintf(stdout, "Progress: %02lu %%\r", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) (progress_time * 100) / total_time); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) fflush(stdout); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * compute how many rounds of calculation we should do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * to get the given load time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @param load aimed load time in µs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @retval rounds of calculation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int calculate_timespace(long load, struct config *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) long long now, then;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned int estimated = GAUGECOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned int rounds = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int timed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) printf("calibrating load of %lius, please wait...\n", load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* get the initial calculation time for a specific number of rounds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) now = get_time();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ROUNDS(estimated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) then = get_time();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) timed = (unsigned int)(then - now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* approximation of the wanted load time by comparing with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * initial calculation time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) rounds = (unsigned int)(load * estimated / timed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) dprintf("calibrating with %u rounds\n", rounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) now = get_time();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ROUNDS(rounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) then = get_time();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) timed = (unsigned int)(then - now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) estimated = rounds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) printf("calibration done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return estimated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * benchmark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * generates a specific sleep an load time with the performance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * governor and compares the used time for same calculations done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * with the configured powersave governor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * @param config config values for the benchmark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) void start_benchmark(struct config *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned int _round, cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) long long now, then;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) long sleep_time = 0, load_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) long performance_time = 0, powersave_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned int calculations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) unsigned long total_time = 0, progress_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) sleep_time = config->sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) load_time = config->load;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* For the progress bar */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) for (_round = 1; _round <= config->rounds; _round++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) total_time += _round * (config->sleep + config->load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) total_time *= 2; /* powersave and performance cycles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) for (_round = 0; _round < config->rounds; _round++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) performance_time = 0LL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) powersave_time = 0LL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) show_progress(total_time, progress_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* set the cpufreq governor to "performance" which disables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * P-State switching. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (set_cpufreq_governor("performance", config->cpu) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* calibrate the calculation time. the resulting calculation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * _rounds should produce a load which matches the configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * load time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) calculations = calculate_timespace(load_time, config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) printf("_round %i: doing %u cycles with %u calculations"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) " for %lius\n", _round + 1, config->cycles,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) calculations, load_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) fprintf(config->output, "%u %li %li ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) _round, load_time, sleep_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) printf("average: %lius, rps:%li\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) load_time / calculations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 1000000 * calculations / load_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* do some sleep/load cycles with the performance governor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) for (cycle = 0; cycle < config->cycles; cycle++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) now = get_time();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) usleep(sleep_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ROUNDS(calculations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) then = get_time();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) performance_time += then - now - sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) printf("performance cycle took %lius, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) "sleep: %lius, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) "load: %lius, rounds: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) (long)(then - now), sleep_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) load_time, calculations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) fprintf(config->output, "%li ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) performance_time / config->cycles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) progress_time += sleep_time + load_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) show_progress(total_time, progress_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* set the powersave governor which activates P-State switching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (set_cpufreq_governor(config->governor, config->cpu) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* again, do some sleep/load cycles with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * powersave governor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) for (cycle = 0; cycle < config->cycles; cycle++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) now = get_time();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) usleep(sleep_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ROUNDS(calculations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) then = get_time();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) powersave_time += then - now - sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) printf("powersave cycle took %lius, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) "sleep: %lius, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) "load: %lius, rounds: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) (long)(then - now), sleep_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) load_time, calculations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) progress_time += sleep_time + load_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* compare the average sleep/load cycles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) fprintf(config->output, "%li ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) powersave_time / config->cycles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) fprintf(config->output, "%.3f\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) performance_time * 100.0 / powersave_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) fflush(config->output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (config->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) printf("performance is at %.2f%%\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) performance_time * 100.0 / powersave_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) sleep_time += config->sleep_step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) load_time += config->load_step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }