1*1a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27fe2f639SDominik Brodowski /* cpufreq-bench CPUFreq microbenchmark
37fe2f639SDominik Brodowski *
47fe2f639SDominik Brodowski * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
57fe2f639SDominik Brodowski */
67fe2f639SDominik Brodowski
77fe2f639SDominik Brodowski #include <stdio.h>
87fe2f639SDominik Brodowski #include <stdlib.h>
97fe2f639SDominik Brodowski #include <stdarg.h>
107fe2f639SDominik Brodowski #include <string.h>
117fe2f639SDominik Brodowski #include <time.h>
127fe2f639SDominik Brodowski #include <dirent.h>
137fe2f639SDominik Brodowski
147fe2f639SDominik Brodowski #include <sys/utsname.h>
157fe2f639SDominik Brodowski #include <sys/types.h>
167fe2f639SDominik Brodowski #include <sys/stat.h>
177fe2f639SDominik Brodowski
187fe2f639SDominik Brodowski #include "parse.h"
197fe2f639SDominik Brodowski #include "config.h"
207fe2f639SDominik Brodowski
217fe2f639SDominik Brodowski /**
227fe2f639SDominik Brodowski * converts priority string to priority
237fe2f639SDominik Brodowski *
247fe2f639SDominik Brodowski * @param str string that represents a scheduler priority
257fe2f639SDominik Brodowski *
267fe2f639SDominik Brodowski * @retval priority
277fe2f639SDominik Brodowski * @retval SCHED_ERR when the priority doesn't exit
287fe2f639SDominik Brodowski **/
297fe2f639SDominik Brodowski
string_to_prio(const char * str)307fe2f639SDominik Brodowski enum sched_prio string_to_prio(const char *str)
317fe2f639SDominik Brodowski {
327fe2f639SDominik Brodowski if (strncasecmp("high", str, strlen(str)) == 0)
337fe2f639SDominik Brodowski return SCHED_HIGH;
347fe2f639SDominik Brodowski else if (strncasecmp("default", str, strlen(str)) == 0)
357fe2f639SDominik Brodowski return SCHED_DEFAULT;
367fe2f639SDominik Brodowski else if (strncasecmp("low", str, strlen(str)) == 0)
377fe2f639SDominik Brodowski return SCHED_LOW;
387fe2f639SDominik Brodowski else
397fe2f639SDominik Brodowski return SCHED_ERR;
407fe2f639SDominik Brodowski }
417fe2f639SDominik Brodowski
427fe2f639SDominik Brodowski /**
437fe2f639SDominik Brodowski * create and open logfile
447fe2f639SDominik Brodowski *
457fe2f639SDominik Brodowski * @param dir directory in which the logfile should be created
467fe2f639SDominik Brodowski *
477fe2f639SDominik Brodowski * @retval logfile on success
487fe2f639SDominik Brodowski * @retval NULL when the file can't be created
497fe2f639SDominik Brodowski **/
507fe2f639SDominik Brodowski
prepare_output(const char * dirname)517fe2f639SDominik Brodowski FILE *prepare_output(const char *dirname)
527fe2f639SDominik Brodowski {
537fe2f639SDominik Brodowski FILE *output = NULL;
547fe2f639SDominik Brodowski int len;
550b81561cSArjun Sreedharan char *filename, *filename_tmp;
567fe2f639SDominik Brodowski struct utsname sysdata;
577fe2f639SDominik Brodowski DIR *dir;
587fe2f639SDominik Brodowski
597fe2f639SDominik Brodowski dir = opendir(dirname);
607fe2f639SDominik Brodowski if (dir == NULL) {
617fe2f639SDominik Brodowski if (mkdir(dirname, 0755)) {
627fe2f639SDominik Brodowski perror("mkdir");
637fe2f639SDominik Brodowski fprintf(stderr, "error: Cannot create dir %s\n",
647fe2f639SDominik Brodowski dirname);
657fe2f639SDominik Brodowski return NULL;
667fe2f639SDominik Brodowski }
677fe2f639SDominik Brodowski }
687fe2f639SDominik Brodowski
697fe2f639SDominik Brodowski len = strlen(dirname) + 30;
707fe2f639SDominik Brodowski filename = malloc(sizeof(char) * len);
71983d9e06SColin Ian King if (!filename) {
72983d9e06SColin Ian King perror("malloc");
73983d9e06SColin Ian King goto out_dir;
74983d9e06SColin Ian King }
757fe2f639SDominik Brodowski
767fe2f639SDominik Brodowski if (uname(&sysdata) == 0) {
777fe2f639SDominik Brodowski len += strlen(sysdata.nodename) + strlen(sysdata.release);
780b81561cSArjun Sreedharan filename_tmp = realloc(filename, sizeof(*filename) * len);
797fe2f639SDominik Brodowski
800b81561cSArjun Sreedharan if (filename_tmp == NULL) {
810b81561cSArjun Sreedharan free(filename);
827fe2f639SDominik Brodowski perror("realloc");
83983d9e06SColin Ian King goto out_dir;
847fe2f639SDominik Brodowski }
857fe2f639SDominik Brodowski
860b81561cSArjun Sreedharan filename = filename_tmp;
877fe2f639SDominik Brodowski snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
887fe2f639SDominik Brodowski dirname, sysdata.nodename, sysdata.release, time(NULL));
897fe2f639SDominik Brodowski } else {
9002af3cb5SDominik Brodowski snprintf(filename, len - 1, "%s/benchmark_%li.log",
9102af3cb5SDominik Brodowski dirname, time(NULL));
927fe2f639SDominik Brodowski }
937fe2f639SDominik Brodowski
949fd0c404SColin Ian King dprintf("logfilename: %s\n", filename);
957fe2f639SDominik Brodowski
9602af3cb5SDominik Brodowski output = fopen(filename, "w+");
9702af3cb5SDominik Brodowski if (output == NULL) {
987fe2f639SDominik Brodowski perror("fopen");
997fe2f639SDominik Brodowski fprintf(stderr, "error: unable to open logfile\n");
100983d9e06SColin Ian King goto out;
1017fe2f639SDominik Brodowski }
1027fe2f639SDominik Brodowski
1037fe2f639SDominik Brodowski fprintf(stdout, "Logfile: %s\n", filename);
1047fe2f639SDominik Brodowski
1057fe2f639SDominik Brodowski fprintf(output, "#round load sleep performance powersave percentage\n");
106983d9e06SColin Ian King out:
107983d9e06SColin Ian King free(filename);
108983d9e06SColin Ian King out_dir:
109983d9e06SColin Ian King closedir(dir);
1107fe2f639SDominik Brodowski return output;
1117fe2f639SDominik Brodowski }
1127fe2f639SDominik Brodowski
1137fe2f639SDominik Brodowski /**
1147fe2f639SDominik Brodowski * returns the default config
1157fe2f639SDominik Brodowski *
1167fe2f639SDominik Brodowski * @retval default config on success
1177fe2f639SDominik Brodowski * @retval NULL when the output file can't be created
1187fe2f639SDominik Brodowski **/
1197fe2f639SDominik Brodowski
prepare_default_config()1207fe2f639SDominik Brodowski struct config *prepare_default_config()
1217fe2f639SDominik Brodowski {
1227fe2f639SDominik Brodowski struct config *config = malloc(sizeof(struct config));
1237fe2f639SDominik Brodowski
1247fe2f639SDominik Brodowski dprintf("loading defaults\n");
1257fe2f639SDominik Brodowski
1267fe2f639SDominik Brodowski config->sleep = 500000;
1277fe2f639SDominik Brodowski config->load = 500000;
1287fe2f639SDominik Brodowski config->sleep_step = 500000;
1297fe2f639SDominik Brodowski config->load_step = 500000;
1307fe2f639SDominik Brodowski config->cycles = 5;
1317fe2f639SDominik Brodowski config->rounds = 50;
1327fe2f639SDominik Brodowski config->cpu = 0;
1337fe2f639SDominik Brodowski config->prio = SCHED_HIGH;
1347fe2f639SDominik Brodowski config->verbose = 0;
1358a7e2d2eSAnders Roxell strncpy(config->governor, "ondemand", sizeof(config->governor));
1367fe2f639SDominik Brodowski
1377fe2f639SDominik Brodowski config->output = stdout;
1387fe2f639SDominik Brodowski
1397fe2f639SDominik Brodowski #ifdef DEFAULT_CONFIG_FILE
1407fe2f639SDominik Brodowski if (prepare_config(DEFAULT_CONFIG_FILE, config))
1417fe2f639SDominik Brodowski return NULL;
1427fe2f639SDominik Brodowski #endif
1437fe2f639SDominik Brodowski return config;
1447fe2f639SDominik Brodowski }
1457fe2f639SDominik Brodowski
1467fe2f639SDominik Brodowski /**
1477fe2f639SDominik Brodowski * parses config file and returns the config to the caller
1487fe2f639SDominik Brodowski *
1497fe2f639SDominik Brodowski * @param path config file name
1507fe2f639SDominik Brodowski *
1517fe2f639SDominik Brodowski * @retval 1 on error
1527fe2f639SDominik Brodowski * @retval 0 on success
1537fe2f639SDominik Brodowski **/
1547fe2f639SDominik Brodowski
prepare_config(const char * path,struct config * config)1557fe2f639SDominik Brodowski int prepare_config(const char *path, struct config *config)
1567fe2f639SDominik Brodowski {
1577fe2f639SDominik Brodowski size_t len = 0;
15813f6de52SRickard Strandqvist char opt[16], val[32], *line = NULL;
15913f6de52SRickard Strandqvist FILE *configfile;
1607fe2f639SDominik Brodowski
1617fe2f639SDominik Brodowski if (config == NULL) {
1627fe2f639SDominik Brodowski fprintf(stderr, "error: config is NULL\n");
1637fe2f639SDominik Brodowski return 1;
1647fe2f639SDominik Brodowski }
1657fe2f639SDominik Brodowski
16613f6de52SRickard Strandqvist configfile = fopen(path, "r");
1677fe2f639SDominik Brodowski if (configfile == NULL) {
1687fe2f639SDominik Brodowski perror("fopen");
1697fe2f639SDominik Brodowski fprintf(stderr, "error: unable to read configfile\n");
1707fe2f639SDominik Brodowski free(config);
1717fe2f639SDominik Brodowski return 1;
1727fe2f639SDominik Brodowski }
1737fe2f639SDominik Brodowski
17402af3cb5SDominik Brodowski while (getline(&line, &len, configfile) != -1) {
17513f6de52SRickard Strandqvist if (line[0] == '#' || line[0] == ' ' || line[0] == '\n')
1767fe2f639SDominik Brodowski continue;
1777fe2f639SDominik Brodowski
17813f6de52SRickard Strandqvist if (sscanf(line, "%14s = %30s", opt, val) < 2)
17913f6de52SRickard Strandqvist continue;
1807fe2f639SDominik Brodowski
1817fe2f639SDominik Brodowski dprintf("parsing: %s -> %s\n", opt, val);
1827fe2f639SDominik Brodowski
18313f6de52SRickard Strandqvist if (strcmp("sleep", opt) == 0)
1847fe2f639SDominik Brodowski sscanf(val, "%li", &config->sleep);
1857fe2f639SDominik Brodowski
18613f6de52SRickard Strandqvist else if (strcmp("load", opt) == 0)
1877fe2f639SDominik Brodowski sscanf(val, "%li", &config->load);
1887fe2f639SDominik Brodowski
18913f6de52SRickard Strandqvist else if (strcmp("load_step", opt) == 0)
1907fe2f639SDominik Brodowski sscanf(val, "%li", &config->load_step);
1917fe2f639SDominik Brodowski
19213f6de52SRickard Strandqvist else if (strcmp("sleep_step", opt) == 0)
1937fe2f639SDominik Brodowski sscanf(val, "%li", &config->sleep_step);
1947fe2f639SDominik Brodowski
19513f6de52SRickard Strandqvist else if (strcmp("cycles", opt) == 0)
1967fe2f639SDominik Brodowski sscanf(val, "%u", &config->cycles);
1977fe2f639SDominik Brodowski
19813f6de52SRickard Strandqvist else if (strcmp("rounds", opt) == 0)
1997fe2f639SDominik Brodowski sscanf(val, "%u", &config->rounds);
2007fe2f639SDominik Brodowski
20113f6de52SRickard Strandqvist else if (strcmp("verbose", opt) == 0)
2027fe2f639SDominik Brodowski sscanf(val, "%u", &config->verbose);
2037fe2f639SDominik Brodowski
20413f6de52SRickard Strandqvist else if (strcmp("output", opt) == 0)
2057fe2f639SDominik Brodowski config->output = prepare_output(val);
2067fe2f639SDominik Brodowski
20713f6de52SRickard Strandqvist else if (strcmp("cpu", opt) == 0)
2087fe2f639SDominik Brodowski sscanf(val, "%u", &config->cpu);
2097fe2f639SDominik Brodowski
21013f6de52SRickard Strandqvist else if (strcmp("governor", opt) == 0) {
21113f6de52SRickard Strandqvist strncpy(config->governor, val,
21213f6de52SRickard Strandqvist sizeof(config->governor));
21313f6de52SRickard Strandqvist config->governor[sizeof(config->governor) - 1] = '\0';
21413f6de52SRickard Strandqvist }
2157fe2f639SDominik Brodowski
21613f6de52SRickard Strandqvist else if (strcmp("priority", opt) == 0) {
2177fe2f639SDominik Brodowski if (string_to_prio(val) != SCHED_ERR)
2187fe2f639SDominik Brodowski config->prio = string_to_prio(val);
2197fe2f639SDominik Brodowski }
2207fe2f639SDominik Brodowski }
2217fe2f639SDominik Brodowski
2227fe2f639SDominik Brodowski free(line);
2237fe2f639SDominik Brodowski
2247fe2f639SDominik Brodowski return 0;
2257fe2f639SDominik Brodowski }
226