2 Copyright (C) by Andrew Tridgell <tridge@samba.org> 1999, 2001
3 Copyright (C) 2001 by Martin Pool <mbp@samba.org>
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* TODO: We could try allowing for different flavours of synchronous
21 operation: data sync and so on. Linux apparently doesn't make any
22 distinction, however, and for practical purposes it probably
23 doesn't matter. On NFSv4 it might be interesting, since the client
24 can choose what kind it wants for each OPEN operation. */
28 int sync_open = 0, sync_dirs = 0;
29 char *tcp_options = TCP_OPTIONS;
30 static int timelimit = 600, warmup;
31 static const char *directory = ".";
32 static char *loadfile = DATADIR "/client.txt";
33 static struct timeval tv_start;
34 static struct timeval tv_end;
40 static FILE *open_loadfile(void)
44 if ((f = fopen(loadfile, "rt")) != NULL)
48 "dbench: error opening '%s': %s\n", loadfile,
55 static struct child_struct *children;
57 static void sigcont(int sig)
62 static void sig_alarm(int sig)
64 double total_bytes = 0;
67 int nprocs = children[0].nprocs;
70 static int in_cleanup;
74 for (i=0;i<nprocs;i++) {
75 total_bytes += children[i].bytes - children[i].bytes_done_warmup;
76 if (children[i].bytes == 0) {
79 total_lines += children[i].line;
82 t = timeval_elapsed(&tv_start);
84 if (!in_warmup && warmup>0 && t > warmup) {
85 tv_start = timeval_current();
87 for (i=0;i<nprocs;i++) {
88 children[i].bytes_done_warmup = children[i].bytes;
94 } else if (!in_warmup && !in_cleanup && t > timelimit) {
95 for (i=0;i<nprocs;i++) {
98 tv_end = timeval_current();
106 printf("%4d %8d %7.2f MB/sec warmup %3.0f sec \n",
107 nprocs, total_lines/nprocs,
108 1.0e-6 * total_bytes / t, t);
109 } else if (in_cleanup) {
110 printf("%4d %8d %7.2f MB/sec cleanup %3.0f sec \n",
111 nprocs, total_lines/nprocs,
112 1.0e-6 * total_bytes / t, t);
114 printf("%4d %8d %7.2f MB/sec execute %3.0f sec \n",
115 nprocs, total_lines/nprocs,
116 1.0e-6 * total_bytes / t, t);
121 signal(SIGALRM, sig_alarm);
125 /* this creates the specified number of child processes and runs fn()
127 static void create_procs(int nprocs, void (*fn)(struct child_struct *, const char *))
134 load = open_loadfile();
139 signal(SIGCONT, sigcont);
145 "create %d procs? you must be kidding.\n",
150 children = shm_setup(sizeof(struct child_struct)*nprocs);
152 printf("Failed to setup shared memory\n");
156 memset(children, 0, sizeof(*children)*nprocs);
158 for (i=0;i<nprocs;i++) {
160 children[i].nprocs = nprocs;
161 children[i].cleanup = 0;
162 children[i].directory = directory;
165 for (i=0;i<nprocs;i++) {
168 nb_setup(&children[i]);
169 children[i].status = getpid();
171 fn(&children[i], loadfile);
176 tv = timeval_current();
179 for (i=0;i<nprocs;i++) {
180 if (children[i].status) synccount++;
182 if (synccount == nprocs) break;
184 } while (timeval_elapsed(&tv) < 30);
186 if (synccount != nprocs) {
187 printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
191 printf("%d clients started\n", nprocs);
195 tv_start = timeval_current();
197 signal(SIGALRM, sig_alarm);
200 for (i=0;i<nprocs;) {
201 if (waitpid(0, &status, 0) == -1) continue;
202 if (WEXITSTATUS(status) != 0) {
203 printf("Child failed with status %d\n",
204 WEXITSTATUS(status));
217 static void show_usage(void)
219 printf("usage: dbench [OPTIONS] nprocs\n" \
220 "usage: tbench [OPTIONS] nprocs <server>\n" \
222 " -v show version\n" \
223 " -t timelimit run time in seconds (default 600)\n" \
224 " -D directory base directory to run in\n" \
225 " -c loadfile set location of the loadfile\n" \
226 " -s synchronous file IO\n" \
227 " -S synchronous directories (mkdir, unlink...)\n" \
228 " -x enable EA support\n" \
229 " -T options set socket options for tbench\n");
235 static int process_opts(int argc, char **argv,
239 extern int sync_open;
242 while ((c = getopt(argc, argv, "vc:sST:t:xD:")) != -1)
254 tcp_options = optarg;
257 timelimit = atoi(optarg);
269 printf("EA suppport not compiled in\n");
274 if (isprint (optopt))
275 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
278 "Unknown option character `\\x%x'.\n",
288 *nprocs = atoi(argv[optind++]);
291 server = argv[optind++];
298 int main(int argc, char *argv[])
301 double total_bytes = 0;
305 printf("dbench version %s - Copyright Andrew Tridgell 1999-2004\n\n", VERSION);
307 if (!process_opts(argc, argv, &nprocs))
310 warmup = timelimit / 5;
312 printf("Running for %d seconds with load '%s' and minimum warmup %d secs\n",
313 timelimit, loadfile, warmup);
315 create_procs(nprocs, child_run);
317 for (i=0;i<nprocs;i++) {
318 total_bytes += children[i].bytes - children[i].bytes_done_warmup;
321 t = timeval_elapsed2(&tv_start, &tv_end);
323 printf("Throughput %g MB/sec%s%s %d procs\n",
324 1.0e-6 * total_bytes / t,
325 sync_open ? " (sync open)" : "",
326 sync_dirs ? " (sync dirs)" : "", nprocs);