parse.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* cpufreq-bench CPUFreq microbenchmark
  2. *
  3. * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #include <dirent.h>
  25. #include <sys/utsname.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include "parse.h"
  29. #include "config.h"
  30. /**
  31. * converts priority string to priority
  32. *
  33. * @param str string that represents a scheduler priority
  34. *
  35. * @retval priority
  36. * @retval SCHED_ERR when the priority doesn't exit
  37. **/
  38. enum sched_prio string_to_prio(const char *str)
  39. {
  40. if (strncasecmp("high", str, strlen(str)) == 0)
  41. return SCHED_HIGH;
  42. else if (strncasecmp("default", str, strlen(str)) == 0)
  43. return SCHED_DEFAULT;
  44. else if (strncasecmp("low", str, strlen(str)) == 0)
  45. return SCHED_LOW;
  46. else
  47. return SCHED_ERR;
  48. }
  49. /**
  50. * create and open logfile
  51. *
  52. * @param dir directory in which the logfile should be created
  53. *
  54. * @retval logfile on success
  55. * @retval NULL when the file can't be created
  56. **/
  57. FILE *prepare_output(const char *dirname)
  58. {
  59. FILE *output = NULL;
  60. int len;
  61. char *filename, *filename_tmp;
  62. struct utsname sysdata;
  63. DIR *dir;
  64. dir = opendir(dirname);
  65. if (dir == NULL) {
  66. if (mkdir(dirname, 0755)) {
  67. perror("mkdir");
  68. fprintf(stderr, "error: Cannot create dir %s\n",
  69. dirname);
  70. return NULL;
  71. }
  72. }
  73. len = strlen(dirname) + 30;
  74. filename = malloc(sizeof(char) * len);
  75. if (!filename) {
  76. perror("malloc");
  77. goto out_dir;
  78. }
  79. if (uname(&sysdata) == 0) {
  80. len += strlen(sysdata.nodename) + strlen(sysdata.release);
  81. filename_tmp = realloc(filename, sizeof(*filename) * len);
  82. if (filename_tmp == NULL) {
  83. free(filename);
  84. perror("realloc");
  85. goto out_dir;
  86. }
  87. filename = filename_tmp;
  88. snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
  89. dirname, sysdata.nodename, sysdata.release, time(NULL));
  90. } else {
  91. snprintf(filename, len - 1, "%s/benchmark_%li.log",
  92. dirname, time(NULL));
  93. }
  94. dprintf("logfilename: %s\n", filename);
  95. output = fopen(filename, "w+");
  96. if (output == NULL) {
  97. perror("fopen");
  98. fprintf(stderr, "error: unable to open logfile\n");
  99. goto out;
  100. }
  101. fprintf(stdout, "Logfile: %s\n", filename);
  102. fprintf(output, "#round load sleep performance powersave percentage\n");
  103. out:
  104. free(filename);
  105. out_dir:
  106. closedir(dir);
  107. return output;
  108. }
  109. /**
  110. * returns the default config
  111. *
  112. * @retval default config on success
  113. * @retval NULL when the output file can't be created
  114. **/
  115. struct config *prepare_default_config()
  116. {
  117. struct config *config = malloc(sizeof(struct config));
  118. dprintf("loading defaults\n");
  119. config->sleep = 500000;
  120. config->load = 500000;
  121. config->sleep_step = 500000;
  122. config->load_step = 500000;
  123. config->cycles = 5;
  124. config->rounds = 50;
  125. config->cpu = 0;
  126. config->prio = SCHED_HIGH;
  127. config->verbose = 0;
  128. strncpy(config->governor, "ondemand", 8);
  129. config->output = stdout;
  130. #ifdef DEFAULT_CONFIG_FILE
  131. if (prepare_config(DEFAULT_CONFIG_FILE, config))
  132. return NULL;
  133. #endif
  134. return config;
  135. }
  136. /**
  137. * parses config file and returns the config to the caller
  138. *
  139. * @param path config file name
  140. *
  141. * @retval 1 on error
  142. * @retval 0 on success
  143. **/
  144. int prepare_config(const char *path, struct config *config)
  145. {
  146. size_t len = 0;
  147. char opt[16], val[32], *line = NULL;
  148. FILE *configfile;
  149. if (config == NULL) {
  150. fprintf(stderr, "error: config is NULL\n");
  151. return 1;
  152. }
  153. configfile = fopen(path, "r");
  154. if (configfile == NULL) {
  155. perror("fopen");
  156. fprintf(stderr, "error: unable to read configfile\n");
  157. free(config);
  158. return 1;
  159. }
  160. while (getline(&line, &len, configfile) != -1) {
  161. if (line[0] == '#' || line[0] == ' ' || line[0] == '\n')
  162. continue;
  163. if (sscanf(line, "%14s = %30s", opt, val) < 2)
  164. continue;
  165. dprintf("parsing: %s -> %s\n", opt, val);
  166. if (strcmp("sleep", opt) == 0)
  167. sscanf(val, "%li", &config->sleep);
  168. else if (strcmp("load", opt) == 0)
  169. sscanf(val, "%li", &config->load);
  170. else if (strcmp("load_step", opt) == 0)
  171. sscanf(val, "%li", &config->load_step);
  172. else if (strcmp("sleep_step", opt) == 0)
  173. sscanf(val, "%li", &config->sleep_step);
  174. else if (strcmp("cycles", opt) == 0)
  175. sscanf(val, "%u", &config->cycles);
  176. else if (strcmp("rounds", opt) == 0)
  177. sscanf(val, "%u", &config->rounds);
  178. else if (strcmp("verbose", opt) == 0)
  179. sscanf(val, "%u", &config->verbose);
  180. else if (strcmp("output", opt) == 0)
  181. config->output = prepare_output(val);
  182. else if (strcmp("cpu", opt) == 0)
  183. sscanf(val, "%u", &config->cpu);
  184. else if (strcmp("governor", opt) == 0) {
  185. strncpy(config->governor, val,
  186. sizeof(config->governor));
  187. config->governor[sizeof(config->governor) - 1] = '\0';
  188. }
  189. else if (strcmp("priority", opt) == 0) {
  190. if (string_to_prio(val) != SCHED_ERR)
  191. config->prio = string_to_prio(val);
  192. }
  193. }
  194. free(line);
  195. return 0;
  196. }