100 bytes is much too small for a buffer to hold an NFS filename.
[amitay/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 #define MAX_PARM_LEN 1024
134 void child_run(struct child_struct *child0, const char *loadfile)
135 {
136         int i;
137         char line[MAX_PARM_LEN], fname[MAX_PARM_LEN], fname2[MAX_PARM_LEN];
138         char **sparams, **params;
139         char *p;
140         const char *status;
141         FILE *f;
142         pid_t parent = getppid();
143         double targett;
144         struct child_struct *child;
145
146         for (child=child0;child<child0+options.clients_per_process;child++) {
147                 child->line = 0;
148                 asprintf(&child->cname,"client%d", child->id);
149         }
150
151         sparams = calloc(20, sizeof(char *));
152         for (i=0;i<20;i++) {
153                 sparams[i] = malloc(MAX_PARM_LEN);
154         }
155
156         f = fopen(loadfile, "r");
157         if (f == NULL) {
158                 perror(loadfile);
159                 exit(1);
160         }
161
162 again:
163         for (child=child0;child<child0+options.clients_per_process;child++) {
164                 nb_time_reset(child);
165         }
166
167         while (fgets(line, sizeof(line)-1, f)) {
168                 params = sparams;
169
170                 if (kill(parent, 0) == -1) {
171                         exit(1);
172                 }
173
174                 for (child=child0;child<child0+options.clients_per_process;child++) {
175                         if (child->done) goto done;
176                         child->line++;
177                 }
178
179                 line[strlen(line)-1] = 0;
180
181                 all_string_sub(line,"\\", "/");
182                 all_string_sub(line," /", " ");
183                 
184                 p = line;
185                 for (i=0; 
186                      i<19 && next_token(&p, params[i], " ");
187                      i++) ;
188
189                 params[i][0] = 0;
190
191                 if (i < 2 || params[0][0] == '#') continue;
192
193                 if (!strncmp(params[0],"SMB", 3)) {
194                         printf("ERROR: You are using a dbench 1 load file\n");
195                         exit(1);
196                 }
197
198                 if (i > 0 && isdigit(params[0][0])) {
199                         targett = strtod(params[0], NULL);
200                         params++;
201                         i--;
202                 } else {
203                         targett = 0.0;
204                 }
205
206                 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0 &&
207                     strncmp(params[i-1], "0x", 2) != 0 &&
208                     strncmp(params[i-1], "*", 1) != 0) {
209                         printf("Badly formed status at line %d\n", child->line);
210                         continue;
211                 }
212
213                 status = params[i-1];
214                 
215                 for (child=child0;child<child0+options.clients_per_process;child++) {
216                         int pcount = 1;
217
218                         fname[0] = 0;
219                         fname2[0] = 0;
220
221                         if (i>1 && params[1][0] == '/') {
222                                 snprintf(fname, sizeof(fname), "%s%s", child->directory, params[1]);
223                                 all_string_sub(fname,"client1", child->cname);
224                                 pcount++;
225                         }
226                         if (i>2 && params[2][0] == '/') {
227                                 snprintf(fname2, sizeof(fname2), "%s%s", child->directory, params[2]);
228                                 all_string_sub(fname2,"client1", child->cname);
229                                 pcount++;
230                         }
231
232                         if (options.targetrate != 0 || targett == 0.0) {
233                                 nb_target_rate(child, options.targetrate);
234                         } else {
235                                 nb_time_delay(child, targett);
236                         }
237                         child_op(child, params[0], fname, fname2, params+pcount, status);
238                 }
239         }
240
241         if (options.run_once) {
242                 goto done;
243         }
244
245         rewind(f);
246         goto again;
247
248 done:
249         fclose(f);
250         for (child=child0;child<child0+options.clients_per_process;child++) {
251                 child->cleanup = 1;
252                 fflush(stdout);
253                 if (!options.skip_cleanup) {
254                         nb_ops->cleanup(child);
255                 }
256                 child->cleanup_finished = 1;
257         }
258 }