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