parse.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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;
  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 (uname(&sysdata) == 0) {
  76. len += strlen(sysdata.nodename) + strlen(sysdata.release);
  77. filename = realloc(filename, sizeof(char) * len);
  78. if (filename == NULL) {
  79. perror("realloc");
  80. return NULL;
  81. }
  82. snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
  83. dirname, sysdata.nodename, sysdata.release, time(NULL));
  84. } else {
  85. snprintf(filename, len - 1, "%s/benchmark_%li.log",
  86. dirname, time(NULL));
  87. }
  88. dprintf("logilename: %s\n", filename);
  89. output = fopen(filename, "w+");
  90. if (output == NULL) {
  91. perror("fopen");
  92. fprintf(stderr, "error: unable to open logfile\n");
  93. }
  94. fprintf(stdout, "Logfile: %s\n", filename);
  95. free(filename);
  96. fprintf(output, "#round load sleep performance powersave percentage\n");
  97. return output;
  98. }
  99. /**
  100. * returns the default config
  101. *
  102. * @retval default config on success
  103. * @retval NULL when the output file can't be created
  104. **/
  105. struct config *prepare_default_config()
  106. {
  107. struct config *config = malloc(sizeof(struct config));
  108. dprintf("loading defaults\n");
  109. config->sleep = 500000;
  110. config->load = 500000;
  111. config->sleep_step = 500000;
  112. config->load_step = 500000;
  113. config->cycles = 5;
  114. config->rounds = 50;
  115. config->cpu = 0;
  116. config->prio = SCHED_HIGH;
  117. config->verbose = 0;
  118. strncpy(config->governor, "ondemand", 8);
  119. config->output = stdout;
  120. #ifdef DEFAULT_CONFIG_FILE
  121. if (prepare_config(DEFAULT_CONFIG_FILE, config))
  122. return NULL;
  123. #endif
  124. return config;
  125. }
  126. /**
  127. * parses config file and returns the config to the caller
  128. *
  129. * @param path config file name
  130. *
  131. * @retval 1 on error
  132. * @retval 0 on success
  133. **/
  134. int prepare_config(const char *path, struct config *config)
  135. {
  136. size_t len = 0;
  137. char opt[16], val[32], *line = NULL;
  138. FILE *configfile;
  139. if (config == NULL) {
  140. fprintf(stderr, "error: config is NULL\n");
  141. return 1;
  142. }
  143. configfile = fopen(path, "r");
  144. if (configfile == NULL) {
  145. perror("fopen");
  146. fprintf(stderr, "error: unable to read configfile\n");
  147. free(config);
  148. return 1;
  149. }
  150. while (getline(&line, &len, configfile) != -1) {
  151. if (line[0] == '#' || line[0] == ' ' || line[0] == '\n')
  152. continue;
  153. if (sscanf(line, "%14s = %30s", opt, val) < 2)
  154. continue;
  155. dprintf("parsing: %s -> %s\n", opt, val);
  156. if (strcmp("sleep", opt) == 0)
  157. sscanf(val, "%li", &config->sleep);
  158. else if (strcmp("load", opt) == 0)
  159. sscanf(val, "%li", &config->load);
  160. else if (strcmp("load_step", opt) == 0)
  161. sscanf(val, "%li", &config->load_step);
  162. else if (strcmp("sleep_step", opt) == 0)
  163. sscanf(val, "%li", &config->sleep_step);
  164. else if (strcmp("cycles", opt) == 0)
  165. sscanf(val, "%u", &config->cycles);
  166. else if (strcmp("rounds", opt) == 0)
  167. sscanf(val, "%u", &config->rounds);
  168. else if (strcmp("verbose", opt) == 0)
  169. sscanf(val, "%u", &config->verbose);
  170. else if (strcmp("output", opt) == 0)
  171. config->output = prepare_output(val);
  172. else if (strcmp("cpu", opt) == 0)
  173. sscanf(val, "%u", &config->cpu);
  174. else if (strcmp("governor", opt) == 0) {
  175. strncpy(config->governor, val,
  176. sizeof(config->governor));
  177. config->governor[sizeof(config->governor) - 1] = '\0';
  178. }
  179. else if (strcmp("priority", opt) == 0) {
  180. if (string_to_prio(val) != SCHED_ERR)
  181. config->prio = string_to_prio(val);
  182. }
  183. }
  184. free(line);
  185. return 0;
  186. }