xref: /openbmc/linux/tools/power/cpupower/bench/main.c (revision e3d786a3)
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 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <getopt.h>
25 #include <errno.h>
26 
27 #include "config.h"
28 #include "system.h"
29 #include "benchmark.h"
30 
31 static struct option long_options[] = {
32 	{"output",	1,	0,	'o'},
33 	{"sleep",	1,	0,	's'},
34 	{"load",	1,	0,	'l'},
35 	{"verbose",	0,	0,	'v'},
36 	{"cpu",		1,	0,	'c'},
37 	{"governor",	1,	0,	'g'},
38 	{"prio",	1,	0,	'p'},
39 	{"file",	1,	0,	'f'},
40 	{"cycles",	1,	0,	'n'},
41 	{"rounds",	1,	0,	'r'},
42 	{"load-step",	1,	0,	'x'},
43 	{"sleep-step",	1,	0,	'y'},
44 	{"help",	0,	0,	'h'},
45 	{0, 0, 0, 0}
46 };
47 
48 /*******************************************************************
49  usage
50 *******************************************************************/
51 
52 void usage()
53 {
54 	printf("usage: ./bench\n");
55 	printf("Options:\n");
56 	printf(" -l, --load=<long int>\t\tinitial load time in us\n");
57 	printf(" -s, --sleep=<long int>\t\tinitial sleep time in us\n");
58 	printf(" -x, --load-step=<long int>\ttime to be added to load time, in us\n");
59 	printf(" -y, --sleep-step=<long int>\ttime to be added to sleep time, in us\n");
60 	printf(" -c, --cpu=<cpu #>\t\t\tCPU Nr. to use, starting at 0\n");
61 	printf(" -p, --prio=<priority>\t\t\tscheduler priority, HIGH, LOW or DEFAULT\n");
62 	printf(" -g, --governor=<governor>\t\tcpufreq governor to test\n");
63 	printf(" -n, --cycles=<int>\t\t\tload/sleep cycles\n");
64 	printf(" -r, --rounds<int>\t\t\tload/sleep rounds\n");
65 	printf(" -f, --file=<configfile>\t\tconfig file to use\n");
66 	printf(" -o, --output=<dir>\t\t\toutput path. Filename will be OUTPUTPATH/benchmark_TIMESTAMP.log\n");
67 	printf(" -v, --verbose\t\t\t\tverbose output on/off\n");
68 	printf(" -h, --help\t\t\t\tPrint this help screen\n");
69 	exit(1);
70 }
71 
72 /*******************************************************************
73  main
74 *******************************************************************/
75 
76 int main(int argc, char **argv)
77 {
78 	int c;
79 	int option_index = 0;
80 	struct config *config = NULL;
81 
82 	config = prepare_default_config();
83 
84 	if (config == NULL)
85 		return EXIT_FAILURE;
86 
87 	while (1) {
88 		c = getopt_long (argc, argv, "hg:o:s:l:vc:p:f:n:r:x:y:",
89 				long_options, &option_index);
90 		if (c == -1)
91 			break;
92 
93 		switch (c) {
94 		case 'o':
95 			if (config->output != NULL)
96 				fclose(config->output);
97 
98 			config->output = prepare_output(optarg);
99 
100 			if (config->output == NULL)
101 				return EXIT_FAILURE;
102 
103 			dprintf("user output path -> %s\n", optarg);
104 			break;
105 		case 's':
106 			sscanf(optarg, "%li", &config->sleep);
107 			dprintf("user sleep time -> %s\n", optarg);
108 			break;
109 		case 'l':
110 			sscanf(optarg, "%li", &config->load);
111 			dprintf("user load time -> %s\n", optarg);
112 			break;
113 		case 'c':
114 			sscanf(optarg, "%u", &config->cpu);
115 			dprintf("user cpu -> %s\n", optarg);
116 			break;
117 		case 'g':
118 			strncpy(config->governor, optarg, 14);
119 			dprintf("user governor -> %s\n", optarg);
120 			break;
121 		case 'p':
122 			if (string_to_prio(optarg) != SCHED_ERR) {
123 				config->prio = string_to_prio(optarg);
124 				dprintf("user prio -> %s\n", optarg);
125 			} else {
126 				if (config != NULL) {
127 					if (config->output != NULL)
128 						fclose(config->output);
129 					free(config);
130 				}
131 				usage();
132 			}
133 			break;
134 		case 'n':
135 			sscanf(optarg, "%u", &config->cycles);
136 			dprintf("user cycles -> %s\n", optarg);
137 			break;
138 		case 'r':
139 			sscanf(optarg, "%u", &config->rounds);
140 			dprintf("user rounds -> %s\n", optarg);
141 			break;
142 		case 'x':
143 			sscanf(optarg, "%li", &config->load_step);
144 			dprintf("user load_step -> %s\n", optarg);
145 			break;
146 		case 'y':
147 			sscanf(optarg, "%li", &config->sleep_step);
148 			dprintf("user sleep_step -> %s\n", optarg);
149 			break;
150 		case 'f':
151 			if (prepare_config(optarg, config))
152 				return EXIT_FAILURE;
153 			break;
154 		case 'v':
155 			config->verbose = 1;
156 			dprintf("verbose output enabled\n");
157 			break;
158 		case 'h':
159 		case '?':
160 		default:
161 			if (config != NULL) {
162 				if (config->output != NULL)
163 					fclose(config->output);
164 				free(config);
165 			}
166 			usage();
167 		}
168 	}
169 
170 	if (config->verbose) {
171 		printf("starting benchmark with parameters:\n");
172 		printf("config:\n\t"
173 		       "sleep=%li\n\t"
174 		       "load=%li\n\t"
175 		       "sleep_step=%li\n\t"
176 		       "load_step=%li\n\t"
177 		       "cpu=%u\n\t"
178 		       "cycles=%u\n\t"
179 		       "rounds=%u\n\t"
180 		       "governor=%s\n\n",
181 		       config->sleep,
182 		       config->load,
183 		       config->sleep_step,
184 		       config->load_step,
185 		       config->cpu,
186 		       config->cycles,
187 		       config->rounds,
188 		       config->governor);
189 	}
190 
191 	prepare_user(config);
192 	prepare_system(config);
193 	start_benchmark(config);
194 
195 	if (config->output != stdout)
196 		fclose(config->output);
197 
198 	free(config);
199 
200 	return EXIT_SUCCESS;
201 }
202 
203