add a webpage for the smb backend
[tridge/dbench.git] / child.c
1 /* 
2    dbench version 3
3
4    Copyright (C) Andrew Tridgell 1999-2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* This file links against either fileio.c to do operations against a
21    local filesystem (making dbench), or sockio.c to issue SMB-like
22    command packets over a socket (making tbench).
23
24    So, the pattern of operations and the control structure is the same
25    for both benchmarks, but the operations performed are different.
26 */
27
28 #include "dbench.h"
29 #include <zlib.h>
30
31 #define ival(s) strtol(s, NULL, 0)
32
33 static void nb_sleep(int usec)
34 {
35         usleep(usec);
36 }
37
38
39 static void nb_target_rate(struct child_struct *child, double rate)
40 {
41         double tdelay;
42
43         if (child->rate.last_bytes == 0) {
44                 child->rate.last_bytes = child->bytes;
45                 child->rate.last_time = timeval_current();
46                 return;
47         }
48
49         if (rate != 0) {
50                 tdelay = (child->bytes - child->rate.last_bytes)/(1.0e6*rate) - 
51                         timeval_elapsed(&child->rate.last_time);
52         } else {
53                 tdelay = - timeval_elapsed(&child->rate.last_time);
54         }
55         if (tdelay > 0 && rate != 0) {
56                 msleep(tdelay*1000);
57         } else {
58                 child->max_latency = MAX(child->max_latency, -tdelay);
59         }
60
61         child->rate.last_time = timeval_current();
62         child->rate.last_bytes = child->bytes;
63 }
64
65 static void nb_time_reset(struct child_struct *child)
66 {
67         child->starttime = timeval_current();   
68         memset(&child->rate, 0, sizeof(child->rate));
69 }
70
71 static void nb_time_delay(struct child_struct *child, double targett)
72 {
73         double elapsed = timeval_elapsed(&child->starttime);
74         if (targett > elapsed) {
75                 msleep(1000*(targett - elapsed));
76         } else if (elapsed - targett > child->max_latency) {
77                 child->max_latency = MAX(elapsed - targett, child->max_latency);
78         }
79 }
80
81 static void finish_op(struct child_struct *child, struct op *op)
82 {
83         double t = timeval_elapsed(&child->lasttime);
84         op->count++;
85         op->total_time += t;
86         if (t > op->max_latency) {
87                 op->max_latency = t;
88         }
89 }
90
91 #define OP_LATENCY(opname) finish_op(child, &child->op.op_ ## opname)
92
93 /*
94   one child operation
95  */
96 static void child_op(struct child_struct *child, const char *opname,
97                      const char *fname, const char *fname2, 
98                      char **params, const char *status)
99 {
100         struct dbench_op op;
101         unsigned i;
102
103         child->lasttime = timeval_current();
104
105         ZERO_STRUCT(op);
106         op.child = child;
107         op.op = opname;
108         op.fname = fname;
109         op.fname2 = fname2;
110         op.status = status;
111         for (i=0;i<sizeof(op.params)/sizeof(op.params[0]);i++) {
112                 if (!strcmp(params[i], "*")) {
113                         op.params[i] = random();
114                 } else {
115                         op.params[i] = params[i]?ival(params[i]):0;
116                 }
117         }
118
119         if (strcasecmp(op.op, "Sleep") == 0) {
120                 nb_sleep(op.params[0]);
121                 return;
122         }
123
124         for (i=0;nb_ops->ops[i].name;i++) {
125                 if (strcasecmp(op.op, nb_ops->ops[i].name) == 0) {
126                         nb_ops->ops[i].fn(&op);
127                         finish_op(child, &child->ops[i]);
128                         return;
129                 }
130         }
131
132         printf("[%u] Unknown operation %s in pid %u\n", 
133                child->line, op.op, (unsigned)getpid());
134 }
135
136
137 /* run a test that simulates an approximate netbench client load */
138 #define MAX_PARM_LEN 1024
139 void child_run(struct child_struct *child0, const char *loadfile)
140 {
141         int i;
142         char line[MAX_PARM_LEN], fname[MAX_PARM_LEN], fname2[MAX_PARM_LEN];
143         char **sparams, **params;
144         char *p;
145         const char *status;
146         gzFile *gzf;
147         pid_t parent = getppid();
148         double targett;
149         struct child_struct *child;
150
151         gzf = gzopen(loadfile, "r");
152         if (gzf == NULL) {
153                 perror(loadfile);
154                 exit(1);
155         }
156
157         for (child=child0;child<child0+options.clients_per_process;child++) {
158                 child->line = 0;
159                 asprintf(&child->cname,"client%d", child->id);
160         }
161
162         sparams = calloc(20, sizeof(char *));
163         for (i=0;i<20;i++) {
164                 sparams[i] = malloc(MAX_PARM_LEN);
165                 memset(sparams[i], 0, MAX_PARM_LEN);
166         }
167
168 again:
169         for (child=child0;child<child0+options.clients_per_process;child++) {
170                 nb_time_reset(child);
171         }
172
173         while (gzgets(gzf, line, sizeof(line)-1)) {
174                 params = sparams;
175
176                 if (kill(parent, 0) == -1) {
177                         exit(1);
178                 }
179
180                 for (child=child0;child<child0+options.clients_per_process;child++) {
181                         if (child->done) goto done;
182                         child->line++;
183                 }
184
185                 line[strlen(line)-1] = 0;
186
187                 all_string_sub(line,"\\", "/");
188                 all_string_sub(line," /", " ");
189                 
190                 p = line;
191                 for (i=0; 
192                      i<19 && next_token(&p, params[i], " ");
193                      i++) ;
194
195                 params[i][0] = 0;
196
197                 if (i < 2 || params[0][0] == '#') continue;
198
199                 if (!strncmp(params[0],"SMB", 3)) {
200                         printf("ERROR: You are using a dbench 1 load file\n");
201                         exit(1);
202                 }
203
204                 if (i > 0 && isdigit(params[0][0])) {
205                         targett = strtod(params[0], NULL);
206                         params++;
207                         i--;
208                 } else {
209                         targett = 0.0;
210                 }
211
212                 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0 &&
213                     strncmp(params[i-1], "0x", 2) != 0 &&
214                     strncmp(params[i-1], "SUCCESS", 7) != 0 &&
215                     strncmp(params[i-1], "ERROR", 7) != 0 &&
216                     strncmp(params[i-1], "*", 1) != 0) {
217                         printf("Badly formed status at line %d\n", child->line);
218                         continue;
219                 }
220
221                 status = params[i-1];
222                 
223                 for (child=child0;child<child0+options.clients_per_process;child++) {
224                         int pcount = 1;
225
226                         fname[0] = 0;
227                         fname2[0] = 0;
228
229                         if (i>1 && params[1][0] == '/') {
230                                 snprintf(fname, sizeof(fname), "%s%s", child->directory, params[1]);
231                                 all_string_sub(fname,"client1", child->cname);
232                                 pcount++;
233                         }
234                         if (i>2 && params[2][0] == '/') {
235                                 snprintf(fname2, sizeof(fname2), "%s%s", child->directory, params[2]);
236                                 all_string_sub(fname2,"client1", child->cname);
237                                 pcount++;
238                         }
239
240                         if (options.targetrate != 0 || targett == 0.0) {
241                                 nb_target_rate(child, options.targetrate);
242                         } else {
243                                 nb_time_delay(child, targett);
244                         }
245                         child_op(child, params[0], fname, fname2, params+pcount, status);
246                 }
247         }
248
249         if (options.run_once) {
250                 goto done;
251         }
252
253         gzrewind(gzf);
254         goto again;
255
256 done:
257         gzclose(gzf);
258         for (child=child0;child<child0+options.clients_per_process;child++) {
259                 child->cleanup = 1;
260                 fflush(stdout);
261                 if (!options.skip_cleanup) {
262                         nb_ops->cleanup(child);
263                 }
264                 child->cleanup_finished = 1;
265                 if(child->cname){
266                         free(child->cname);
267                         child->cname = NULL;
268                 }
269         }
270 }