[dbench @ cvs-1:tridge-19990618011122-wdx5f7p6o8rk21xn]
[tridge/dbench.git] / dbench.c
1 /* 
2    dbench version 1
3    Copyright (C) Andrew Tridgell 1999
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "dbench.h"
21
22 /* this creates the specified number of child processes and runs fn() in all of them */
23 static double create_procs(int nprocs, void (*fn)(int ))
24 {
25         int i, status;
26         volatile int *child_status;
27         int synccount;
28
29         start_timer();
30
31         synccount = 0;
32
33         child_status = (volatile int *)shm_setup(sizeof(int)*nprocs);
34         if (!child_status) {
35                 printf("Failed to setup shared memory\n");
36                 return end_timer();
37         }
38
39         memset((void *)child_status, 0, sizeof(int)*nprocs);
40
41         for (i=0;i<nprocs;i++) {
42                 if (fork() == 0) {
43                         nb_setup(i);
44
45                         child_status[i] = getpid();
46
47                         while (child_status[i]) sleep(1);
48
49                         setbuffer(stdout, NULL, 0);
50
51                         fn(i);
52                         _exit(0);
53                 }
54         }
55
56         do {
57                 synccount = 0;
58                 for (i=0;i<nprocs;i++) {
59                         if (child_status[i]) synccount++;
60                 }
61                 if (synccount == nprocs) break;
62                 sleep(1);
63         } while (end_timer() < 30);
64
65         if (synccount != nprocs) {
66                 printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
67                 return end_timer();
68         }
69
70         /* start the client load */
71         start_timer();
72
73         for (i=0;i<nprocs;i++) {
74                 child_status[i] = 0;
75         }
76
77         printf("%d clients started\n", nprocs);
78
79         for (i=0;i<nprocs;i++) {
80                 waitpid(0, &status, 0);
81                 printf("*");
82         }
83         printf("\n");
84         return end_timer();
85 }
86
87
88
89
90  int main(int argc, char *argv[])
91 {
92         extern char *server;
93         int nprocs;
94         double t;
95
96         if (argc < 2) {
97                 printf("usage: dbench nprocs\n");
98                 exit(1);
99         }
100
101         nprocs = atoi(argv[1]);
102
103         if (argc > 2) {
104                 server = argv[2];
105         }
106
107         t = create_procs(nprocs, child_run);
108
109         /* to produce a netbench result we scale accoding to the
110            netbench measured throughput for the run that produced the
111            sniff that was used to produce client.txt. That run used 2
112            clients and ran for 660 seconds to produce a result of
113            4MBit/sec. */
114         printf("Throughput %g MB/sec (NB=%g MB/sec  %g MBit/sec)\n", 
115                132*nprocs/t, 0.5*0.5*nprocs*660/t, 2*nprocs*660/t);
116         return 0;
117 }