4 Copyright (C) Andrew Tridgell 1999-2004
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.
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.
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/>.
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).
24 So, the pattern of operations and the control structure is the same
25 for both benchmarks, but the operations performed are different.
31 #define ival(s) strtol(s, NULL, 0)
33 static void nb_sleep(int usec)
39 static void nb_target_rate(struct child_struct *child, double rate)
43 if (child->rate.last_bytes == 0) {
44 child->rate.last_bytes = child->bytes;
45 child->rate.last_time = timeval_current();
50 tdelay = (child->bytes - child->rate.last_bytes)/(1.0e6*rate) -
51 timeval_elapsed(&child->rate.last_time);
53 tdelay = - timeval_elapsed(&child->rate.last_time);
55 if (tdelay > 0 && rate != 0) {
58 child->max_latency = MAX(child->max_latency, -tdelay);
61 child->rate.last_time = timeval_current();
62 child->rate.last_bytes = child->bytes;
65 static void nb_time_reset(struct child_struct *child)
67 child->starttime = timeval_current();
68 memset(&child->rate, 0, sizeof(child->rate));
71 static void nb_time_delay(struct child_struct *child, double targett)
73 double elapsed = timeval_elapsed(&child->starttime);
74 if (targett > elapsed) {
75 msleep(1000*(targett - elapsed));
76 } else if (elapsed - targett > child->max_latency) {
77 child->max_latency = MAX(elapsed - targett, child->max_latency);
81 static void finish_op(struct child_struct *child, struct op *op)
83 double t = timeval_elapsed(&child->lasttime);
86 if (t > op->max_latency) {
91 #define OP_LATENCY(opname) finish_op(child, &child->op.op_ ## opname)
96 static void child_op(struct child_struct *child, const char *opname,
97 const char *fname, const char *fname2,
98 char **params, const char *status)
103 child->lasttime = timeval_current();
111 for (i=0;i<sizeof(op.params)/sizeof(op.params[0]);i++) {
112 if (!strcmp(params[i], "*")) {
113 op.params[i] = random();
115 op.params[i] = params[i]?ival(params[i]):0;
119 if (strcasecmp(op.op, "Sleep") == 0) {
120 nb_sleep(op.params[0]);
124 for (i=0;nb_ops->ops[i].name;i++) {
125 if (strcasecmp(op.op, nb_ops->ops[i].name) == 0) {
126 nb_ops->ops[i].fn(&op);
127 finish_op(child, &child->ops[i]);
132 printf("[%u] Unknown operation %s in pid %u\n",
133 child->line, op.op, (unsigned)getpid());
137 /* run a test that simulates an approximate netbench client load */
138 #define MAX_PARM_LEN 1024
139 void child_run(struct child_struct *child0, const char *loadfile)
142 char line[MAX_PARM_LEN], fname[MAX_PARM_LEN], fname2[MAX_PARM_LEN];
143 char **sparams, **params;
147 pid_t parent = getppid();
149 struct child_struct *child;
151 gzf = gzopen(loadfile, "r");
157 for (child=child0;child<child0+options.clients_per_process;child++) {
159 asprintf(&child->cname,"client%d", child->id);
162 sparams = calloc(20, sizeof(char *));
164 sparams[i] = malloc(MAX_PARM_LEN);
165 memset(sparams[i], 0, MAX_PARM_LEN);
169 for (child=child0;child<child0+options.clients_per_process;child++) {
170 nb_time_reset(child);
173 while (gzgets(gzf, line, sizeof(line)-1)) {
176 if (kill(parent, 0) == -1) {
180 for (child=child0;child<child0+options.clients_per_process;child++) {
181 if (child->done) goto done;
185 line[strlen(line)-1] = 0;
187 all_string_sub(line,"\\", "/");
188 all_string_sub(line," /", " ");
192 i<19 && next_token(&p, params[i], " ");
197 if (i < 2 || params[0][0] == '#') continue;
199 if (!strncmp(params[0],"SMB", 3)) {
200 printf("ERROR: You are using a dbench 1 load file\n");
204 if (i > 0 && isdigit(params[0][0])) {
205 targett = strtod(params[0], NULL);
212 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0 &&
213 strncmp(params[i-1], "0x", 2) != 0 &&
214 strncmp(params[i-1], "SUCCESS", 7) != 0 &&
215 strncmp(params[i-1], "ERROR", 7) != 0 &&
216 strncmp(params[i-1], "*", 1) != 0) {
217 printf("Badly formed status at line %d\n", child->line);
221 status = params[i-1];
223 for (child=child0;child<child0+options.clients_per_process;child++) {
229 if (i>1 && params[1][0] == '/') {
230 snprintf(fname, sizeof(fname), "%s%s", child->directory, params[1]);
231 all_string_sub(fname,"client1", child->cname);
234 if (i>2 && params[2][0] == '/') {
235 snprintf(fname2, sizeof(fname2), "%s%s", child->directory, params[2]);
236 all_string_sub(fname2,"client1", child->cname);
240 if (options.targetrate != 0 || targett == 0.0) {
241 nb_target_rate(child, options.targetrate);
243 nb_time_delay(child, targett);
245 child_op(child, params[0], fname, fname2, params+pcount, status);
249 if (options.run_once) {
258 for (child=child0;child<child0+options.clients_per_process;child++) {
261 if (!options.skip_cleanup) {
262 nb_ops->cleanup(child);
264 child->cleanup_finished = 1;