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