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