Path: blob/master/tools/power/cpupower/bench/benchmark.c
26292 views
// SPDX-License-Identifier: GPL-2.0-or-later1/* cpufreq-bench CPUFreq microbenchmark2*3* Copyright (C) 2008 Christian Kornacker <[email protected]>4*/56#include <stdio.h>7#include <unistd.h>8#include <math.h>910#include "config.h"11#include "system.h"12#include "benchmark.h"1314/* Print out progress if we log into a file */15#define show_progress(total_time, progress_time) \16if (config->output != stdout) { \17fprintf(stdout, "Progress: %02lu %%\r", \18(progress_time * 100) / total_time); \19fflush(stdout); \20}2122/**23* compute how many rounds of calculation we should do24* to get the given load time25*26* @param load aimed load time in µs27*28* @retval rounds of calculation29**/3031unsigned int calculate_timespace(long load, struct config *config)32{33int i;34long long now, then;35unsigned int estimated = GAUGECOUNT;36unsigned int rounds = 0;37unsigned int timed = 0;3839if (config->verbose)40printf("calibrating load of %lius, please wait...\n", load);4142/* get the initial calculation time for a specific number of rounds */43now = get_time();44ROUNDS(estimated);45then = get_time();4647timed = (unsigned int)(then - now);4849/* approximation of the wanted load time by comparing with the50* initial calculation time */51for (i = 0; i < 4; i++) {52rounds = (unsigned int)(load * estimated / timed);53dprintf("calibrating with %u rounds\n", rounds);54now = get_time();55ROUNDS(rounds);56then = get_time();5758timed = (unsigned int)(then - now);59estimated = rounds;60}61if (config->verbose)62printf("calibration done\n");6364return estimated;65}6667/**68* benchmark69* generates a specific sleep an load time with the performance70* governor and compares the used time for same calculations done71* with the configured powersave governor72*73* @param config config values for the benchmark74*75**/7677void start_benchmark(struct config *config)78{79unsigned int _round, cycle;80long long now, then;81long sleep_time = 0, load_time = 0;82long performance_time = 0, powersave_time = 0;83unsigned int calculations;84unsigned long total_time = 0, progress_time = 0;8586sleep_time = config->sleep;87load_time = config->load;8889/* For the progress bar */90for (_round = 1; _round <= config->rounds; _round++)91total_time += _round * (config->sleep + config->load);92total_time *= 2; /* powersave and performance cycles */9394for (_round = 0; _round < config->rounds; _round++) {95performance_time = 0LL;96powersave_time = 0LL;9798show_progress(total_time, progress_time);99100/* set the cpufreq governor to "performance" which disables101* P-State switching. */102if (set_cpufreq_governor("performance", config->cpu) != 0)103return;104105/* calibrate the calculation time. the resulting calculation106* _rounds should produce a load which matches the configured107* load time */108calculations = calculate_timespace(load_time, config);109110if (config->verbose)111printf("_round %i: doing %u cycles with %u calculations"112" for %lius\n", _round + 1, config->cycles,113calculations, load_time);114115fprintf(config->output, "%u %li %li ",116_round, load_time, sleep_time);117118if (config->verbose)119printf("average: %lius, rps:%li\n",120load_time / calculations,1211000000 * calculations / load_time);122123/* do some sleep/load cycles with the performance governor */124for (cycle = 0; cycle < config->cycles; cycle++) {125now = get_time();126usleep(sleep_time);127ROUNDS(calculations);128then = get_time();129performance_time += then - now - sleep_time;130if (config->verbose)131printf("performance cycle took %lius, "132"sleep: %lius, "133"load: %lius, rounds: %u\n",134(long)(then - now), sleep_time,135load_time, calculations);136}137fprintf(config->output, "%li ",138performance_time / config->cycles);139140progress_time += sleep_time + load_time;141show_progress(total_time, progress_time);142143/* set the powersave governor which activates P-State switching144* again */145if (set_cpufreq_governor(config->governor, config->cpu) != 0)146return;147148/* again, do some sleep/load cycles with the149* powersave governor */150for (cycle = 0; cycle < config->cycles; cycle++) {151now = get_time();152usleep(sleep_time);153ROUNDS(calculations);154then = get_time();155powersave_time += then - now - sleep_time;156if (config->verbose)157printf("powersave cycle took %lius, "158"sleep: %lius, "159"load: %lius, rounds: %u\n",160(long)(then - now), sleep_time,161load_time, calculations);162}163164progress_time += sleep_time + load_time;165166/* compare the average sleep/load cycles */167fprintf(config->output, "%li ",168powersave_time / config->cycles);169fprintf(config->output, "%.3f\n",170performance_time * 100.0 / powersave_time);171fflush(config->output);172173if (config->verbose)174printf("performance is at %.2f%%\n",175performance_time * 100.0 / powersave_time);176177sleep_time += config->sleep_step;178load_time += config->load_step;179}180}181182183