[dbench @ cvs-1:tridge-20070622101840-jlcfy1gphxfo0ts3]
[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
34 /* run a test that simulates an approximate netbench client load */
35 void child_run(struct child_struct *child, const char *loadfile)
36 {
37         int i;
38         char line[1024];
39         char *cname;
40         char **sparams, **params;
41         char *p;
42         const char *status;
43         char *fname = NULL;
44         char *fname2 = NULL;
45         FILE *f = fopen(loadfile, "r");
46         pid_t parent = getppid();
47         extern double targetrate;
48
49         child->line = 0;
50
51         asprintf(&cname,"client%d", child->id);
52
53         sparams = calloc(20, sizeof(char *));
54         for (i=0;i<20;i++) {
55                 sparams[i] = malloc(100);
56         }
57
58 again:
59         nb_time_reset(child);
60
61         while (fgets(line, sizeof(line)-1, f)) {
62                 params = sparams;
63
64                 if (child->done || kill(parent, 0) == -1) {
65                         goto done;
66                 }
67
68                 child->line++;
69
70                 line[strlen(line)-1] = 0;
71
72                 all_string_sub(line,"client1", cname);
73                 all_string_sub(line,"\\", "/");
74                 all_string_sub(line," /", " ");
75                 
76                 p = line;
77                 for (i=0; 
78                      i<19 && next_token(&p, params[i], " ");
79                      i++) ;
80
81                 params[i][0] = 0;
82
83                 if (i < 2 || params[0][0] == '#') continue;
84
85                 if (!strncmp(params[0],"SMB", 3)) {
86                         printf("ERROR: You are using a dbench 1 load file\n");
87                         exit(1);
88                 }
89
90                 if (i > 0 && isdigit(params[0][0])) {
91                         double targett = strtod(params[0], NULL);
92                         if (targetrate != 0) {
93                                 nb_target_rate(child, targetrate);
94                         } else {
95                                 nb_time_delay(child, targett);
96                         }
97                         params++;
98                         i--;
99                 }
100
101                 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0 &&
102                     strncmp(params[i-1], "0x", 2) != 0) {
103                         printf("Badly formed status at line %d\n", child->line);
104                         continue;
105                 }
106
107                 if (fname) {
108                         free(fname);
109                         fname = NULL;
110                 }
111                 if (fname2) {
112                         free(fname2);
113                         fname2 = NULL;
114                 }
115
116                 if (i>1 && params[1][0] == '/') {
117                         asprintf(&fname, "%s%s", child->directory, params[1]);
118                 }
119                 if (i>2 && params[2][0] == '/') {
120                         asprintf(&fname2, "%s%s", child->directory, params[2]);
121                 }
122
123                 status = params[i-1];
124
125                 if (!strcmp(params[0],"NTCreateX")) {
126                         nb_createx(child, fname, ival(params[2]), ival(params[3]), 
127                                    ival(params[4]), status);
128                 } else if (!strcmp(params[0],"Close")) {
129                         nb_close(child, ival(params[1]), status);
130                 } else if (!strcmp(params[0],"Rename")) {
131                         nb_rename(child, fname, fname2, status);
132                 } else if (!strcmp(params[0],"Unlink")) {
133                         nb_unlink(child, fname, ival(params[2]), status);
134                 } else if (!strcmp(params[0],"Deltree")) {
135                         nb_deltree(child, fname);
136                 } else if (!strcmp(params[0],"Rmdir")) {
137                         nb_rmdir(child, fname, status);
138                 } else if (!strcmp(params[0],"Mkdir")) {
139                         nb_mkdir(child, fname, status);
140                 } else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
141                         nb_qpathinfo(child, fname, ival(params[2]), status);
142                 } else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
143                         nb_qfileinfo(child, ival(params[1]), ival(params[2]), status);
144                 } else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
145                         nb_qfsinfo(child, ival(params[1]), status);
146                 } else if (!strcmp(params[0],"SET_FILE_INFORMATION")) {
147                         nb_sfileinfo(child, ival(params[1]), ival(params[2]), status);
148                 } else if (!strcmp(params[0],"FIND_FIRST")) {
149                         nb_findfirst(child, fname, ival(params[2]), 
150                                      ival(params[3]), ival(params[4]), status);
151                 } else if (!strcmp(params[0],"WriteX")) {
152                         nb_writex(child, ival(params[1]), 
153                                   ival(params[2]), ival(params[3]), ival(params[4]),
154                                   status);
155                 } else if (!strcmp(params[0],"LockX")) {
156                         nb_lockx(child, ival(params[1]), 
157                                  ival(params[2]), ival(params[3]), status);
158                 } else if (!strcmp(params[0],"UnlockX")) {
159                         nb_unlockx(child, ival(params[1]), 
160                                  ival(params[2]), ival(params[3]), status);
161                 } else if (!strcmp(params[0],"ReadX")) {
162                         nb_readx(child, ival(params[1]), 
163                                  ival(params[2]), ival(params[3]), ival(params[4]),
164                                  status);
165                 } else if (!strcmp(params[0],"Flush")) {
166                         nb_flush(child, ival(params[1]), status);
167                 } else if (!strcmp(params[0],"Sleep")) {
168                         nb_sleep(child, ival(params[1]), status);
169                 } else {
170                         printf("[%d] Unknown operation %s\n", child->line, params[0]);
171                 }
172         }
173
174         rewind(f);
175         goto again;
176
177 done:
178         child->cleanup = 1;
179         fclose(f);
180         nb_cleanup(child);
181 }