parse.c 4.8 KB

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