1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* cpufreq-bench CPUFreq microbenchmark
4 * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
14 #include <sys/utsname.h>
15 #include <sys/types.h>
22 * converts priority string to priority
24 * @param str string that represents a scheduler priority
27 * @retval SCHED_ERR when the priority doesn't exit
30 enum sched_prio
string_to_prio(const char *str
)
32 if (strncasecmp("high", str
, strlen(str
)) == 0)
34 else if (strncasecmp("default", str
, strlen(str
)) == 0)
36 else if (strncasecmp("low", str
, strlen(str
)) == 0)
43 * create and open logfile
45 * @param dir directory in which the logfile should be created
47 * @retval logfile on success
48 * @retval NULL when the file can't be created
51 FILE *prepare_output(const char *dirname
)
55 char *filename
, *filename_tmp
;
56 struct utsname sysdata
;
59 dir
= opendir(dirname
);
61 if (mkdir(dirname
, 0755)) {
63 fprintf(stderr
, "error: Cannot create dir %s\n",
69 len
= strlen(dirname
) + 30;
70 filename
= malloc(sizeof(char) * len
);
76 if (uname(&sysdata
) == 0) {
77 len
+= strlen(sysdata
.nodename
) + strlen(sysdata
.release
);
78 filename_tmp
= realloc(filename
, sizeof(*filename
) * len
);
80 if (filename_tmp
== NULL
) {
86 filename
= filename_tmp
;
87 snprintf(filename
, len
- 1, "%s/benchmark_%s_%s_%li.log",
88 dirname
, sysdata
.nodename
, sysdata
.release
, time(NULL
));
90 snprintf(filename
, len
- 1, "%s/benchmark_%li.log",
94 dprintf("logfilename: %s\n", filename
);
96 output
= fopen(filename
, "w+");
99 fprintf(stderr
, "error: unable to open logfile\n");
103 fprintf(stdout
, "Logfile: %s\n", filename
);
105 fprintf(output
, "#round load sleep performance powersave percentage\n");
114 * returns the default config
116 * @retval default config on success
117 * @retval NULL when the output file can't be created
120 struct config
*prepare_default_config()
122 struct config
*config
= malloc(sizeof(struct config
));
124 dprintf("loading defaults\n");
126 config
->sleep
= 500000;
127 config
->load
= 500000;
128 config
->sleep_step
= 500000;
129 config
->load_step
= 500000;
133 config
->prio
= SCHED_HIGH
;
135 strncpy(config
->governor
, "ondemand", sizeof(config
->governor
));
137 config
->output
= stdout
;
139 #ifdef DEFAULT_CONFIG_FILE
140 if (prepare_config(DEFAULT_CONFIG_FILE
, config
))
147 * parses config file and returns the config to the caller
149 * @param path config file name
152 * @retval 0 on success
155 int prepare_config(const char *path
, struct config
*config
)
158 char opt
[16], val
[32], *line
= NULL
;
161 if (config
== NULL
) {
162 fprintf(stderr
, "error: config is NULL\n");
166 configfile
= fopen(path
, "r");
167 if (configfile
== NULL
) {
169 fprintf(stderr
, "error: unable to read configfile\n");
174 while (getline(&line
, &len
, configfile
) != -1) {
175 if (line
[0] == '#' || line
[0] == ' ' || line
[0] == '\n')
178 if (sscanf(line
, "%14s = %30s", opt
, val
) < 2)
181 dprintf("parsing: %s -> %s\n", opt
, val
);
183 if (strcmp("sleep", opt
) == 0)
184 sscanf(val
, "%li", &config
->sleep
);
186 else if (strcmp("load", opt
) == 0)
187 sscanf(val
, "%li", &config
->load
);
189 else if (strcmp("load_step", opt
) == 0)
190 sscanf(val
, "%li", &config
->load_step
);
192 else if (strcmp("sleep_step", opt
) == 0)
193 sscanf(val
, "%li", &config
->sleep_step
);
195 else if (strcmp("cycles", opt
) == 0)
196 sscanf(val
, "%u", &config
->cycles
);
198 else if (strcmp("rounds", opt
) == 0)
199 sscanf(val
, "%u", &config
->rounds
);
201 else if (strcmp("verbose", opt
) == 0)
202 sscanf(val
, "%u", &config
->verbose
);
204 else if (strcmp("output", opt
) == 0)
205 config
->output
= prepare_output(val
);
207 else if (strcmp("cpu", opt
) == 0)
208 sscanf(val
, "%u", &config
->cpu
);
210 else if (strcmp("governor", opt
) == 0) {
211 strncpy(config
->governor
, val
,
212 sizeof(config
->governor
));
213 config
->governor
[sizeof(config
->governor
) - 1] = '\0';
216 else if (strcmp("priority", opt
) == 0) {
217 if (string_to_prio(val
) != SCHED_ERR
)
218 config
->prio
= string_to_prio(val
);