parse.c 5.3 KB

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