2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-03-30 22:30:11 +08:00
|
|
|
/* cpufreq-bench CPUFreq microbenchmark
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "parse.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* converts priority string to priority
|
|
|
|
*
|
|
|
|
* @param str string that represents a scheduler priority
|
|
|
|
*
|
|
|
|
* @retval priority
|
|
|
|
* @retval SCHED_ERR when the priority doesn't exit
|
|
|
|
**/
|
|
|
|
|
|
|
|
enum sched_prio string_to_prio(const char *str)
|
|
|
|
{
|
|
|
|
if (strncasecmp("high", str, strlen(str)) == 0)
|
|
|
|
return SCHED_HIGH;
|
|
|
|
else if (strncasecmp("default", str, strlen(str)) == 0)
|
|
|
|
return SCHED_DEFAULT;
|
|
|
|
else if (strncasecmp("low", str, strlen(str)) == 0)
|
|
|
|
return SCHED_LOW;
|
|
|
|
else
|
|
|
|
return SCHED_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create and open logfile
|
|
|
|
*
|
|
|
|
* @param dir directory in which the logfile should be created
|
|
|
|
*
|
|
|
|
* @retval logfile on success
|
|
|
|
* @retval NULL when the file can't be created
|
|
|
|
**/
|
|
|
|
|
|
|
|
FILE *prepare_output(const char *dirname)
|
|
|
|
{
|
|
|
|
FILE *output = NULL;
|
|
|
|
int len;
|
2016-04-29 15:00:14 +08:00
|
|
|
char *filename, *filename_tmp;
|
2011-03-30 22:30:11 +08:00
|
|
|
struct utsname sysdata;
|
|
|
|
DIR *dir;
|
|
|
|
|
|
|
|
dir = opendir(dirname);
|
|
|
|
if (dir == NULL) {
|
|
|
|
if (mkdir(dirname, 0755)) {
|
|
|
|
perror("mkdir");
|
|
|
|
fprintf(stderr, "error: Cannot create dir %s\n",
|
|
|
|
dirname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(dirname) + 30;
|
|
|
|
filename = malloc(sizeof(char) * len);
|
2016-04-28 21:24:37 +08:00
|
|
|
if (!filename) {
|
|
|
|
perror("malloc");
|
|
|
|
goto out_dir;
|
|
|
|
}
|
2011-03-30 22:30:11 +08:00
|
|
|
|
|
|
|
if (uname(&sysdata) == 0) {
|
|
|
|
len += strlen(sysdata.nodename) + strlen(sysdata.release);
|
2016-04-29 15:00:14 +08:00
|
|
|
filename_tmp = realloc(filename, sizeof(*filename) * len);
|
2011-03-30 22:30:11 +08:00
|
|
|
|
2016-04-29 15:00:14 +08:00
|
|
|
if (filename_tmp == NULL) {
|
|
|
|
free(filename);
|
2011-03-30 22:30:11 +08:00
|
|
|
perror("realloc");
|
2016-04-28 21:24:37 +08:00
|
|
|
goto out_dir;
|
2011-03-30 22:30:11 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 15:00:14 +08:00
|
|
|
filename = filename_tmp;
|
2011-04-20 01:20:12 +08:00
|
|
|
snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
|
2011-03-30 22:30:11 +08:00
|
|
|
dirname, sysdata.nodename, sysdata.release, time(NULL));
|
|
|
|
} else {
|
2011-04-20 01:20:12 +08:00
|
|
|
snprintf(filename, len - 1, "%s/benchmark_%li.log",
|
|
|
|
dirname, time(NULL));
|
2011-03-30 22:30:11 +08:00
|
|
|
}
|
|
|
|
|
2018-05-03 17:09:10 +08:00
|
|
|
dprintf("logfilename: %s\n", filename);
|
2011-03-30 22:30:11 +08:00
|
|
|
|
2011-04-20 01:20:12 +08:00
|
|
|
output = fopen(filename, "w+");
|
|
|
|
if (output == NULL) {
|
2011-03-30 22:30:11 +08:00
|
|
|
perror("fopen");
|
|
|
|
fprintf(stderr, "error: unable to open logfile\n");
|
2016-04-28 21:24:37 +08:00
|
|
|
goto out;
|
2011-03-30 22:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stdout, "Logfile: %s\n", filename);
|
|
|
|
|
|
|
|
fprintf(output, "#round load sleep performance powersave percentage\n");
|
2016-04-28 21:24:37 +08:00
|
|
|
out:
|
|
|
|
free(filename);
|
|
|
|
out_dir:
|
|
|
|
closedir(dir);
|
2011-03-30 22:30:11 +08:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the default config
|
|
|
|
*
|
|
|
|
* @retval default config on success
|
|
|
|
* @retval NULL when the output file can't be created
|
|
|
|
**/
|
|
|
|
|
|
|
|
struct config *prepare_default_config()
|
|
|
|
{
|
|
|
|
struct config *config = malloc(sizeof(struct config));
|
|
|
|
|
|
|
|
dprintf("loading defaults\n");
|
|
|
|
|
|
|
|
config->sleep = 500000;
|
|
|
|
config->load = 500000;
|
|
|
|
config->sleep_step = 500000;
|
|
|
|
config->load_step = 500000;
|
|
|
|
config->cycles = 5;
|
|
|
|
config->rounds = 50;
|
2011-04-20 01:20:12 +08:00
|
|
|
config->cpu = 0;
|
2011-03-30 22:30:11 +08:00
|
|
|
config->prio = SCHED_HIGH;
|
|
|
|
config->verbose = 0;
|
2018-08-28 17:31:18 +08:00
|
|
|
strncpy(config->governor, "ondemand", sizeof(config->governor));
|
2011-03-30 22:30:11 +08:00
|
|
|
|
|
|
|
config->output = stdout;
|
|
|
|
|
|
|
|
#ifdef DEFAULT_CONFIG_FILE
|
|
|
|
if (prepare_config(DEFAULT_CONFIG_FILE, config))
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parses config file and returns the config to the caller
|
|
|
|
*
|
|
|
|
* @param path config file name
|
|
|
|
*
|
|
|
|
* @retval 1 on error
|
|
|
|
* @retval 0 on success
|
|
|
|
**/
|
|
|
|
|
|
|
|
int prepare_config(const char *path, struct config *config)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
2014-07-30 00:12:19 +08:00
|
|
|
char opt[16], val[32], *line = NULL;
|
|
|
|
FILE *configfile;
|
2011-03-30 22:30:11 +08:00
|
|
|
|
|
|
|
if (config == NULL) {
|
|
|
|
fprintf(stderr, "error: config is NULL\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
configfile = fopen(path, "r");
|
2011-03-30 22:30:11 +08:00
|
|
|
if (configfile == NULL) {
|
|
|
|
perror("fopen");
|
2011-04-20 01:20:12 +08:00
|
|
|
fprintf(stderr, "error: unable to read configfile\n");
|
2011-03-30 22:30:11 +08:00
|
|
|
free(config);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-20 01:20:12 +08:00
|
|
|
while (getline(&line, &len, configfile) != -1) {
|
2014-07-30 00:12:19 +08:00
|
|
|
if (line[0] == '#' || line[0] == ' ' || line[0] == '\n')
|
2011-03-30 22:30:11 +08:00
|
|
|
continue;
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
if (sscanf(line, "%14s = %30s", opt, val) < 2)
|
|
|
|
continue;
|
2011-03-30 22:30:11 +08:00
|
|
|
|
|
|
|
dprintf("parsing: %s -> %s\n", opt, val);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
if (strcmp("sleep", opt) == 0)
|
2011-03-30 22:30:11 +08:00
|
|
|
sscanf(val, "%li", &config->sleep);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("load", opt) == 0)
|
2011-03-30 22:30:11 +08:00
|
|
|
sscanf(val, "%li", &config->load);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("load_step", opt) == 0)
|
2011-03-30 22:30:11 +08:00
|
|
|
sscanf(val, "%li", &config->load_step);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("sleep_step", opt) == 0)
|
2011-03-30 22:30:11 +08:00
|
|
|
sscanf(val, "%li", &config->sleep_step);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("cycles", opt) == 0)
|
2011-03-30 22:30:11 +08:00
|
|
|
sscanf(val, "%u", &config->cycles);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("rounds", opt) == 0)
|
2011-03-30 22:30:11 +08:00
|
|
|
sscanf(val, "%u", &config->rounds);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("verbose", opt) == 0)
|
2011-03-30 22:30:11 +08:00
|
|
|
sscanf(val, "%u", &config->verbose);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("output", opt) == 0)
|
2011-03-30 22:30:11 +08:00
|
|
|
config->output = prepare_output(val);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("cpu", opt) == 0)
|
2011-03-30 22:30:11 +08:00
|
|
|
sscanf(val, "%u", &config->cpu);
|
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("governor", opt) == 0) {
|
|
|
|
strncpy(config->governor, val,
|
|
|
|
sizeof(config->governor));
|
|
|
|
config->governor[sizeof(config->governor) - 1] = '\0';
|
|
|
|
}
|
2011-03-30 22:30:11 +08:00
|
|
|
|
2014-07-30 00:12:19 +08:00
|
|
|
else if (strcmp("priority", opt) == 0) {
|
2011-04-20 01:20:12 +08:00
|
|
|
if (string_to_prio(val) != SCHED_ERR)
|
2011-03-30 22:30:11 +08:00
|
|
|
config->prio = string_to_prio(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|