converted all backends to new common format
[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
30 #define ival(s) strtol(s, NULL, 0)
31
32 static void nb_sleep(int usec)
33 {
34         usleep(usec);
35 }
36
37
38 static void nb_target_rate(struct child_struct *child, double rate)
39 {
40         double tdelay;
41
42         if (child->rate.last_bytes == 0) {
43                 child->rate.last_bytes = child->bytes;
44                 child->rate.last_time = timeval_current();
45                 return;
46         }
47
48         if (rate != 0) {
49                 tdelay = (child->bytes - child->rate.last_bytes)/(1.0e6*rate) - 
50                         timeval_elapsed(&child->rate.last_time);
51         } else {
52                 tdelay = - timeval_elapsed(&child->rate.last_time);
53         }
54         if (tdelay > 0 && rate != 0) {
55                 msleep(tdelay*1000);
56         } else {
57                 child->max_latency = MAX(child->max_latency, -tdelay);
58         }
59
60         child->rate.last_time = timeval_current();
61         child->rate.last_bytes = child->bytes;
62 }
63
64 static void nb_time_reset(struct child_struct *child)
65 {
66         child->starttime = timeval_current();   
67         memset(&child->rate, 0, sizeof(child->rate));
68 }
69
70 static void nb_time_delay(struct child_struct *child, double targett)
71 {
72         double elapsed = timeval_elapsed(&child->starttime);
73         if (targett > elapsed) {
74                 msleep(1000*(targett - elapsed));
75         } else if (elapsed - targett > child->max_latency) {
76                 child->max_latency = MAX(elapsed - targett, child->max_latency);
77         }
78 }
79
80 static void finish_op(struct child_struct *child, struct op *op)
81 {
82         double t = timeval_elapsed(&child->lasttime);
83         op->count++;
84         op->total_time += t;
85         if (t > op->max_latency) {
86                 op->max_latency = t;
87         }
88 }
89
90 #define OP_LATENCY(opname) finish_op(child, &child->op.op_ ## opname)
91
92 /*
93   one child operation
94  */
95 static void child_op(struct child_struct *child, const char *opname,
96                      const char *fname, const char *fname2, 
97                      char **params, const char *status)
98 {
99         struct dbench_op op;
100         unsigned i;
101
102         child->lasttime = timeval_current();
103
104         ZERO_STRUCT(op);
105         op.child = child;
106         op.op = opname;
107         op.fname = fname;
108         op.fname2 = fname2;
109         op.status = status;
110         for (i=0;i<sizeof(op.params)/sizeof(op.params[0]);i++) {
111                 op.params[i] = params[i]?ival(params[i]):0;
112         }
113
114         if (strcasecmp(op.op, "Sleep") == 0) {
115                 nb_sleep(op.params[0]);
116                 return;
117         }
118
119         for (i=0;nb_ops.ops[i].name;i++) {
120                 if (strcasecmp(op.op, nb_ops.ops[i].name) == 0) {
121                         nb_ops.ops[i].fn(&op);
122                         finish_op(child, &child->ops[i]);
123                         return;
124                 }
125         }
126
127         printf("[%u] Unknown operation %s in pid %u\n", 
128                child->line, op.op, (unsigned)getpid());
129 }
130
131
132 /* run a test that simulates an approximate netbench client load */
133 void child_run(struct child_struct *child0, const char *loadfile)
134 {
135         int i;
136         char line[1024], fname[1024], fname2[1024];
137         char **sparams, **params;
138         char *p;
139         const char *status;
140         FILE *f;
141         pid_t parent = getppid();
142         double targett;
143         struct child_struct *child;
144
145         for (child=child0;child<child0+options.clients_per_process;child++) {
146                 child->line = 0;
147                 asprintf(&child->cname,"client%d", child->id);
148         }
149
150         sparams = calloc(20, sizeof(char *));
151         for (i=0;i<20;i++) {
152                 sparams[i] = malloc(100);
153         }
154
155         f = fopen(loadfile, "r");
156         if (f == NULL) {
157                 perror(loadfile);
158                 exit(1);
159         }
160
161 again:
162         for (child=child0;child<child0+options.clients_per_process;child++) {
163                 nb_time_reset(child);
164         }
165
166         while (fgets(line, sizeof(line)-1, f)) {
167                 params = sparams;
168
169                 if (kill(parent, 0) == -1) {
170                         exit(1);
171                 }
172
173                 for (child=child0;child<child0+options.clients_per_process;child++) {
174                         if (child->done) goto done;
175                         child->line++;
176                 }
177
178                 line[strlen(line)-1] = 0;
179
180                 all_string_sub(line,"\\", "/");
181                 all_string_sub(line," /", " ");
182                 
183                 p = line;
184                 for (i=0; 
185                      i<19 && next_token(&p, params[i], " ");
186                      i++) ;
187
188                 params[i][0] = 0;
189
190                 if (i < 2 || params[0][0] == '#') continue;
191
192                 if (!strncmp(params[0],"SMB", 3)) {
193                         printf("ERROR: You are using a dbench 1 load file\n");
194                         exit(1);
195                 }
196
197                 if (i > 0 && isdigit(params[0][0])) {
198                         targett = strtod(params[0], NULL);
199                         params++;
200                         i--;
201                 } else {
202                         targett = 0.0;
203                 }
204
205                 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0 &&
206                     strncmp(params[i-1], "0x", 2) != 0) {
207                         printf("Badly formed status at line %d\n", child->line);
208                         continue;
209                 }
210
211                 status = params[i-1];
212                 
213                 for (child=child0;child<child0+options.clients_per_process;child++) {
214                         int pcount = 1;
215
216                         fname[0] = 0;
217                         fname2[0] = 0;
218
219                         if (i>1 && params[1][0] == '/') {
220                                 snprintf(fname, sizeof(fname), "%s%s", child->directory, params[1]);
221                                 all_string_sub(fname,"client1", child->cname);
222                                 pcount++;
223                         }
224                         if (i>2 && params[2][0] == '/') {
225                                 snprintf(fname2, sizeof(fname2), "%s%s", child->directory, params[2]);
226                                 all_string_sub(fname2,"client1", child->cname);
227                                 pcount++;
228                         }
229
230                         if (options.targetrate != 0 || targett == 0.0) {
231                                 nb_target_rate(child, options.targetrate);
232                         } else {
233                                 nb_time_delay(child, targett);
234                         }
235                         child_op(child, params[0], fname, fname2, params+pcount, status);
236                 }
237         }
238
239         rewind(f);
240         goto again;
241
242 done:
243         fclose(f);
244         for (child=child0;child<child0+options.clients_per_process;child++) {
245                 child->cleanup = 1;
246                 fflush(stdout);
247                 if (!options.skip_cleanup) {
248                         nb_ops.cleanup(child);
249                 }
250                 child->cleanup_finished = 1;
251         }
252 }