1 /* cpufreq-bench CPUFreq microbenchmark
3 * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <sys/utsname.h>
28 #include <sys/types.h>
35 * converts priority string to priority
37 * @param str string that represents a scheduler priority
40 * @retval SCHED_ERR when the priority doesn't exit
43 enum sched_prio string_to_prio(const char *str)
45 if (strncasecmp("high", str, strlen(str)) == 0)
47 else if (strncasecmp("default", str, strlen(str)) == 0)
49 else if (strncasecmp("low", str, strlen(str)) == 0)
56 * create and open logfile
58 * @param dir directory in which the logfile should be created
60 * @retval logfile on success
61 * @retval NULL when the file can't be created
64 FILE *prepare_output(const char *dirname)
69 struct utsname sysdata;
72 dir = opendir(dirname);
74 if (mkdir(dirname, 0755)) {
76 fprintf(stderr, "error: Cannot create dir %s\n",
82 len = strlen(dirname) + 30;
83 filename = malloc(sizeof(char) * len);
89 if (uname(&sysdata) == 0) {
90 len += strlen(sysdata.nodename) + strlen(sysdata.release);
91 filename = realloc(filename, sizeof(char) * len);
98 snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
99 dirname, sysdata.nodename, sysdata.release, time(NULL));
101 snprintf(filename, len - 1, "%s/benchmark_%li.log",
102 dirname, time(NULL));
105 dprintf("logilename: %s\n", filename);
107 output = fopen(filename, "w+");
108 if (output == NULL) {
110 fprintf(stderr, "error: unable to open logfile\n");
114 fprintf(stdout, "Logfile: %s\n", filename);
116 fprintf(output, "#round load sleep performance powersave percentage\n");
125 * returns the default config
127 * @retval default config on success
128 * @retval NULL when the output file can't be created
131 struct config *prepare_default_config()
133 struct config *config = malloc(sizeof(struct config));
135 dprintf("loading defaults\n");
137 config->sleep = 500000;
138 config->load = 500000;
139 config->sleep_step = 500000;
140 config->load_step = 500000;
144 config->prio = SCHED_HIGH;
146 strncpy(config->governor, "ondemand", 8);
148 config->output = stdout;
150 #ifdef DEFAULT_CONFIG_FILE
151 if (prepare_config(DEFAULT_CONFIG_FILE, config))
158 * parses config file and returns the config to the caller
160 * @param path config file name
163 * @retval 0 on success
166 int prepare_config(const char *path, struct config *config)
169 char opt[16], val[32], *line = NULL;
172 if (config == NULL) {
173 fprintf(stderr, "error: config is NULL\n");
177 configfile = fopen(path, "r");
178 if (configfile == NULL) {
180 fprintf(stderr, "error: unable to read configfile\n");
185 while (getline(&line, &len, configfile) != -1) {
186 if (line[0] == '#' || line[0] == ' ' || line[0] == '\n')
189 if (sscanf(line, "%14s = %30s", opt, val) < 2)
192 dprintf("parsing: %s -> %s\n", opt, val);
194 if (strcmp("sleep", opt) == 0)
195 sscanf(val, "%li", &config->sleep);
197 else if (strcmp("load", opt) == 0)
198 sscanf(val, "%li", &config->load);
200 else if (strcmp("load_step", opt) == 0)
201 sscanf(val, "%li", &config->load_step);
203 else if (strcmp("sleep_step", opt) == 0)
204 sscanf(val, "%li", &config->sleep_step);
206 else if (strcmp("cycles", opt) == 0)
207 sscanf(val, "%u", &config->cycles);
209 else if (strcmp("rounds", opt) == 0)
210 sscanf(val, "%u", &config->rounds);
212 else if (strcmp("verbose", opt) == 0)
213 sscanf(val, "%u", &config->verbose);
215 else if (strcmp("output", opt) == 0)
216 config->output = prepare_output(val);
218 else if (strcmp("cpu", opt) == 0)
219 sscanf(val, "%u", &config->cpu);
221 else if (strcmp("governor", opt) == 0) {
222 strncpy(config->governor, val,
223 sizeof(config->governor));
224 config->governor[sizeof(config->governor) - 1] = '\0';
227 else if (strcmp("priority", opt) == 0) {
228 if (string_to_prio(val) != SCHED_ERR)
229 config->prio = string_to_prio(val);