[dbench @ tridge@samba.org-20070715124531-g2327o7r0y3eo3q1]
[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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /* This file links against either fileio.c to do operations against a
22    local filesystem (making dbench), or sockio.c to issue SMB-like
23    command packets over a socket (making tbench).
24
25    So, the pattern of operations and the control structure is the same
26    for both benchmarks, but the operations performed are different.
27 */
28
29 #include "dbench.h"
30
31 #define ival(s) strtol(s, NULL, 0)
32
33 static void nb_target_rate(struct child_struct *child, double rate)
34 {
35         static double last_bytes;
36         static struct timeval last_time;
37         double tdelay;
38
39         if (last_bytes == 0) {
40                 last_bytes = child->bytes;
41                 last_time = timeval_current();
42                 return;
43         }
44
45         if (rate != 0) {
46                 tdelay = (child->bytes - last_bytes)/(1.0e6*rate) - timeval_elapsed(&last_time);
47         } else {
48                 tdelay = - timeval_elapsed(&last_time);
49         }
50         if (tdelay > 0 && rate != 0) {
51                 msleep(tdelay*1000);
52         } else {
53                 child->max_latency = MAX(child->max_latency, -tdelay);
54         }
55
56         last_time = timeval_current();
57         last_bytes = child->bytes;
58 }
59
60 static void nb_time_reset(struct child_struct *child)
61 {
62         child->starttime = timeval_current();   
63 }
64
65 static void nb_time_delay(struct child_struct *child, double targett)
66 {
67         double elapsed = timeval_elapsed(&child->starttime);
68         if (targett > elapsed) {
69                 msleep(1000*(targett - elapsed));
70         } else if (elapsed - targett > child->max_latency) {
71                 child->max_latency = MAX(elapsed - targett, child->max_latency);
72         }
73 }
74
75
76
77 /* run a test that simulates an approximate netbench client load */
78 void child_run(struct child_struct *child, const char *loadfile)
79 {
80         int i;
81         char line[1024];
82         char *cname;
83         char **sparams, **params;
84         char *p;
85         const char *status;
86         char *fname = NULL;
87         char *fname2 = NULL;
88         FILE *f = fopen(loadfile, "r");
89         pid_t parent = getppid();
90         double targett;
91
92         child->line = 0;
93
94         asprintf(&cname,"client%d", child->id);
95
96         sparams = calloc(20, sizeof(char *));
97         for (i=0;i<20;i++) {
98                 sparams[i] = malloc(100);
99         }
100
101 again:
102         nb_time_reset(child);
103
104         while (fgets(line, sizeof(line)-1, f)) {
105                 params = sparams;
106
107                 if (child->done || kill(parent, 0) == -1) {
108                         goto done;
109                 }
110
111                 child->line++;
112
113                 line[strlen(line)-1] = 0;
114
115                 all_string_sub(line,"client1", cname);
116                 all_string_sub(line,"\\", "/");
117                 all_string_sub(line," /", " ");
118                 
119                 p = line;
120                 for (i=0; 
121                      i<19 && next_token(&p, params[i], " ");
122                      i++) ;
123
124                 params[i][0] = 0;
125
126                 if (i < 2 || params[0][0] == '#') continue;
127
128                 if (!strncmp(params[0],"SMB", 3)) {
129                         printf("ERROR: You are using a dbench 1 load file\n");
130                         exit(1);
131                 }
132
133                 if (i > 0 && isdigit(params[0][0])) {
134                         targett = strtod(params[0], NULL);
135                         params++;
136                         i--;
137                 } else {
138                         targett = 0.0;
139                 }
140
141                 if (options.targetrate != 0 || targett == 0.0) {
142                         nb_target_rate(child, options.targetrate);
143                 } else {
144                         nb_time_delay(child, targett);
145                 }
146                 child->lasttime = timeval_current();
147
148                 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0 &&
149                     strncmp(params[i-1], "0x", 2) != 0) {
150                         printf("Badly formed status at line %d\n", child->line);
151                         continue;
152                 }
153
154                 if (fname) {
155                         free(fname);
156                         fname = NULL;
157                 }
158                 if (fname2) {
159                         free(fname2);
160                         fname2 = NULL;
161                 }
162
163                 if (i>1 && params[1][0] == '/') {
164                         asprintf(&fname, "%s%s", child->directory, params[1]);
165                 }
166                 if (i>2 && params[2][0] == '/') {
167                         asprintf(&fname2, "%s%s", child->directory, params[2]);
168                 }
169
170                 status = params[i-1];
171
172                 if (!strcmp(params[0],"NTCreateX")) {
173                         nb_createx(child, fname, ival(params[2]), ival(params[3]), 
174                                    ival(params[4]), status);
175                 } else if (!strcmp(params[0],"Close")) {
176                         nb_close(child, ival(params[1]), status);
177                 } else if (!strcmp(params[0],"Rename")) {
178                         nb_rename(child, fname, fname2, status);
179                 } else if (!strcmp(params[0],"Unlink")) {
180                         nb_unlink(child, fname, ival(params[2]), status);
181                 } else if (!strcmp(params[0],"Deltree")) {
182                         nb_deltree(child, fname);
183                 } else if (!strcmp(params[0],"Rmdir")) {
184                         nb_rmdir(child, fname, status);
185                 } else if (!strcmp(params[0],"Mkdir")) {
186                         nb_mkdir(child, fname, status);
187                 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
188                         nb_qpathinfo(child, fname, ival(params[2]), status);
189                 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
190                         nb_qfileinfo(child, ival(params[1]), ival(params[2]), status);
191                 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
192                         nb_qfsinfo(child, ival(params[1]), status);
193                 } else if (!strcmp(params[0],"SET_FILE_INFORMATION")) {
194                         nb_sfileinfo(child, ival(params[1]), ival(params[2]), status);
195                 } else if (!strcmp(params[0],"FIND_FIRST")) {
196                         nb_findfirst(child, fname, ival(params[2]), 
197                                      ival(params[3]), ival(params[4]), status);
198                 } else if (!strcmp(params[0],"WriteX")) {
199                         nb_writex(child, ival(params[1]), 
200                                   ival(params[2]), ival(params[3]), ival(params[4]),
201                                   status);
202                 } else if (!strcmp(params[0],"LockX")) {
203                         nb_lockx(child, ival(params[1]), 
204                                  ival(params[2]), ival(params[3]), status);
205                 } else if (!strcmp(params[0],"UnlockX")) {
206                         nb_unlockx(child, ival(params[1]), 
207                                  ival(params[2]), ival(params[3]), status);
208                 } else if (!strcmp(params[0],"ReadX")) {
209                         nb_readx(child, ival(params[1]), 
210                                  ival(params[2]), ival(params[3]), ival(params[4]),
211                                  status);
212                 } else if (!strcmp(params[0],"Flush")) {
213                         nb_flush(child, ival(params[1]), status);
214                 } else if (!strcmp(params[0],"Sleep")) {
215                         nb_sleep(child, ival(params[1]), status);
216                 } else {
217                         printf("[%d] Unknown operation %s\n", child->line, params[0]);
218                 }
219         }
220
221         rewind(f);
222         goto again;
223
224 done:
225         child->cleanup = 1;
226         fclose(f);
227         nb_cleanup(child);
228 }