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