summaryrefslogtreecommitdiffstats
path: root/tools/power/cpupower/bench/parse.c
blob: f503fb53824ede11ab4c7ade033d877088ff3a21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*  cpufreq-bench CPUFreq microbenchmark
 *
 *  Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#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;
	char *filename;
	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);

	if (uname(&sysdata) == 0) {
		len += strlen(sysdata.nodename) + strlen(sysdata.release);
		filename = realloc(filename, sizeof(char) * len);

		if (filename == NULL) {
			perror("realloc");
			return NULL;
		}

		snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
			dirname, sysdata.nodename, sysdata.release, time(NULL));
	} else {
		snprintf(filename, len - 1, "%s/benchmark_%li.log",
			dirname, time(NULL));
	}

	dprintf("logilename: %s\n", filename);

	output = fopen(filename, "w+");
	if (output == NULL) {
		perror("fopen");
		fprintf(stderr, "error: unable to open logfile\n");
	}

	fprintf(stdout, "Logfile: %s\n", filename);

	free(filename);
	fprintf(output, "#round load sleep performance powersave percentage\n");
	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;
	config->cpu = 0;
	config->prio = SCHED_HIGH;
	config->verbose = 0;
	strncpy(config->governor, "ondemand", 8);

	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;
	char opt[16], val[32], *line = NULL;
	FILE *configfile;

	if (config == NULL) {
		fprintf(stderr, "error: config is NULL\n");
		return 1;
	}

	configfile = fopen(path, "r");
	if (configfile == NULL) {
		perror("fopen");
		fprintf(stderr, "error: unable to read configfile\n");
		free(config);
		return 1;
	}

	while (getline(&line, &len, configfile) != -1) {
		if (line[0] == '#' || line[0] == ' ' || line[0] == '\n')
			continue;

		if (sscanf(line, "%14s = %30s", opt, val) < 2)
			continue;

		dprintf("parsing: %s -> %s\n", opt, val);

		if (strcmp("sleep", opt) == 0)
			sscanf(val, "%li", &config->sleep);

		else if (strcmp("load", opt) == 0)
			sscanf(val, "%li", &config->load);

		else if (strcmp("load_step", opt) == 0)
			sscanf(val, "%li", &config->load_step);

		else if (strcmp("sleep_step", opt) == 0)
			sscanf(val, "%li", &config->sleep_step);

		else if (strcmp("cycles", opt) == 0)
			sscanf(val, "%u", &config->cycles);

		else if (strcmp("rounds", opt) == 0)
			sscanf(val, "%u", &config->rounds);

		else if (strcmp("verbose", opt) == 0)
			sscanf(val, "%u", &config->verbose);

		else if (strcmp("output", opt) == 0)
			config->output = prepare_output(val); 

		else if (strcmp("cpu", opt) == 0)
			sscanf(val, "%u", &config->cpu);

		else if (strcmp("governor", opt) == 0) {
			strncpy(config->governor, val,
					sizeof(config->governor));
			config->governor[sizeof(config->governor) - 1] = '\0';
		}

		else if (strcmp("priority", opt) == 0) {
			if (string_to_prio(val) != SCHED_ERR)
				config->prio = string_to_prio(val);
		}
	}

	free(line);

	return 0;
}