Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 1 | /* cpufreq-bench CPUFreq microbenchmark |
| 2 | * |
| 3 | * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de> |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <string.h> |
| 24 | #include <time.h> |
| 25 | #include <dirent.h> |
| 26 | |
| 27 | #include <sys/utsname.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | |
| 31 | #include "parse.h" |
| 32 | #include "config.h" |
| 33 | |
| 34 | /** |
| 35 | * converts priority string to priority |
| 36 | * |
| 37 | * @param str string that represents a scheduler priority |
| 38 | * |
| 39 | * @retval priority |
| 40 | * @retval SCHED_ERR when the priority doesn't exit |
| 41 | **/ |
| 42 | |
| 43 | enum sched_prio string_to_prio(const char *str) |
| 44 | { |
| 45 | if (strncasecmp("high", str, strlen(str)) == 0) |
| 46 | return SCHED_HIGH; |
| 47 | else if (strncasecmp("default", str, strlen(str)) == 0) |
| 48 | return SCHED_DEFAULT; |
| 49 | else if (strncasecmp("low", str, strlen(str)) == 0) |
| 50 | return SCHED_LOW; |
| 51 | else |
| 52 | return SCHED_ERR; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * create and open logfile |
| 57 | * |
| 58 | * @param dir directory in which the logfile should be created |
| 59 | * |
| 60 | * @retval logfile on success |
| 61 | * @retval NULL when the file can't be created |
| 62 | **/ |
| 63 | |
| 64 | FILE *prepare_output(const char *dirname) |
| 65 | { |
| 66 | FILE *output = NULL; |
| 67 | int len; |
| 68 | char *filename; |
| 69 | struct utsname sysdata; |
| 70 | DIR *dir; |
| 71 | |
| 72 | dir = opendir(dirname); |
| 73 | if (dir == NULL) { |
| 74 | if (mkdir(dirname, 0755)) { |
| 75 | perror("mkdir"); |
| 76 | fprintf(stderr, "error: Cannot create dir %s\n", |
| 77 | dirname); |
| 78 | return NULL; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | len = strlen(dirname) + 30; |
| 83 | filename = malloc(sizeof(char) * len); |
| 84 | |
| 85 | if (uname(&sysdata) == 0) { |
| 86 | len += strlen(sysdata.nodename) + strlen(sysdata.release); |
| 87 | filename = realloc(filename, sizeof(char) * len); |
| 88 | |
Dominik Brodowski | 02af3cb | 2011-04-19 19:20:12 +0200 | [diff] [blame] | 89 | if (filename == NULL) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 90 | perror("realloc"); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Dominik Brodowski | 02af3cb | 2011-04-19 19:20:12 +0200 | [diff] [blame] | 94 | snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log", |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 95 | dirname, sysdata.nodename, sysdata.release, time(NULL)); |
| 96 | } else { |
Dominik Brodowski | 02af3cb | 2011-04-19 19:20:12 +0200 | [diff] [blame] | 97 | snprintf(filename, len - 1, "%s/benchmark_%li.log", |
| 98 | dirname, time(NULL)); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | dprintf("logilename: %s\n", filename); |
| 102 | |
Dominik Brodowski | 02af3cb | 2011-04-19 19:20:12 +0200 | [diff] [blame] | 103 | output = fopen(filename, "w+"); |
| 104 | if (output == NULL) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 105 | perror("fopen"); |
| 106 | fprintf(stderr, "error: unable to open logfile\n"); |
| 107 | } |
| 108 | |
| 109 | fprintf(stdout, "Logfile: %s\n", filename); |
| 110 | |
| 111 | free(filename); |
| 112 | fprintf(output, "#round load sleep performance powersave percentage\n"); |
| 113 | return output; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * returns the default config |
| 118 | * |
| 119 | * @retval default config on success |
| 120 | * @retval NULL when the output file can't be created |
| 121 | **/ |
| 122 | |
| 123 | struct config *prepare_default_config() |
| 124 | { |
| 125 | struct config *config = malloc(sizeof(struct config)); |
| 126 | |
| 127 | dprintf("loading defaults\n"); |
| 128 | |
| 129 | config->sleep = 500000; |
| 130 | config->load = 500000; |
| 131 | config->sleep_step = 500000; |
| 132 | config->load_step = 500000; |
| 133 | config->cycles = 5; |
| 134 | config->rounds = 50; |
Dominik Brodowski | 02af3cb | 2011-04-19 19:20:12 +0200 | [diff] [blame] | 135 | config->cpu = 0; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 136 | config->prio = SCHED_HIGH; |
| 137 | config->verbose = 0; |
| 138 | strncpy(config->governor, "ondemand", 8); |
| 139 | |
| 140 | config->output = stdout; |
| 141 | |
| 142 | #ifdef DEFAULT_CONFIG_FILE |
| 143 | if (prepare_config(DEFAULT_CONFIG_FILE, config)) |
| 144 | return NULL; |
| 145 | #endif |
| 146 | return config; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * parses config file and returns the config to the caller |
| 151 | * |
| 152 | * @param path config file name |
| 153 | * |
| 154 | * @retval 1 on error |
| 155 | * @retval 0 on success |
| 156 | **/ |
| 157 | |
| 158 | int prepare_config(const char *path, struct config *config) |
| 159 | { |
| 160 | size_t len = 0; |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 161 | char opt[16], val[32], *line = NULL; |
| 162 | FILE *configfile; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 163 | |
| 164 | if (config == NULL) { |
| 165 | fprintf(stderr, "error: config is NULL\n"); |
| 166 | return 1; |
| 167 | } |
| 168 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 169 | configfile = fopen(path, "r"); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 170 | if (configfile == NULL) { |
| 171 | perror("fopen"); |
Dominik Brodowski | 02af3cb | 2011-04-19 19:20:12 +0200 | [diff] [blame] | 172 | fprintf(stderr, "error: unable to read configfile\n"); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 173 | free(config); |
| 174 | return 1; |
| 175 | } |
| 176 | |
Dominik Brodowski | 02af3cb | 2011-04-19 19:20:12 +0200 | [diff] [blame] | 177 | while (getline(&line, &len, configfile) != -1) { |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 178 | if (line[0] == '#' || line[0] == ' ' || line[0] == '\n') |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 179 | continue; |
| 180 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 181 | if (sscanf(line, "%14s = %30s", opt, val) < 2) |
| 182 | continue; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 183 | |
| 184 | dprintf("parsing: %s -> %s\n", opt, val); |
| 185 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 186 | if (strcmp("sleep", opt) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 187 | sscanf(val, "%li", &config->sleep); |
| 188 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 189 | else if (strcmp("load", opt) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 190 | sscanf(val, "%li", &config->load); |
| 191 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 192 | else if (strcmp("load_step", opt) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 193 | sscanf(val, "%li", &config->load_step); |
| 194 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 195 | else if (strcmp("sleep_step", opt) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 196 | sscanf(val, "%li", &config->sleep_step); |
| 197 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 198 | else if (strcmp("cycles", opt) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 199 | sscanf(val, "%u", &config->cycles); |
| 200 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 201 | else if (strcmp("rounds", opt) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 202 | sscanf(val, "%u", &config->rounds); |
| 203 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 204 | else if (strcmp("verbose", opt) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 205 | sscanf(val, "%u", &config->verbose); |
| 206 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 207 | else if (strcmp("output", opt) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 208 | config->output = prepare_output(val); |
| 209 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 210 | else if (strcmp("cpu", opt) == 0) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 211 | sscanf(val, "%u", &config->cpu); |
| 212 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 213 | else if (strcmp("governor", opt) == 0) { |
| 214 | strncpy(config->governor, val, |
| 215 | sizeof(config->governor)); |
| 216 | config->governor[sizeof(config->governor) - 1] = '\0'; |
| 217 | } |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 218 | |
Rickard Strandqvist | 13f6de5 | 2014-07-29 18:12:19 +0200 | [diff] [blame] | 219 | else if (strcmp("priority", opt) == 0) { |
Dominik Brodowski | 02af3cb | 2011-04-19 19:20:12 +0200 | [diff] [blame] | 220 | if (string_to_prio(val) != SCHED_ERR) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 221 | config->prio = string_to_prio(val); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | free(line); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 226 | |
| 227 | return 0; |
| 228 | } |