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.
30 #define ival(s) strtol(s, NULL, 0)
32 static void nb_sleep(int usec)
38 static void nb_target_rate(struct child_struct *child, double rate)
42 if (child->rate.last_bytes == 0) {
43 child->rate.last_bytes = child->bytes;
44 child->rate.last_time = timeval_current();
49 tdelay = (child->bytes - child->rate.last_bytes)/(1.0e6*rate) -
50 timeval_elapsed(&child->rate.last_time);
52 tdelay = - timeval_elapsed(&child->rate.last_time);
54 if (tdelay > 0 && rate != 0) {
57 child->max_latency = MAX(child->max_latency, -tdelay);
60 child->rate.last_time = timeval_current();
61 child->rate.last_bytes = child->bytes;
64 static void nb_time_reset(struct child_struct *child)
66 child->starttime = timeval_current();
67 memset(&child->rate, 0, sizeof(child->rate));
70 static void nb_time_delay(struct child_struct *child, double targett)
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);
80 static void finish_op(struct child_struct *child, struct op *op)
82 double t = timeval_elapsed(&child->lasttime);
85 if (t > op->max_latency) {
90 #define OP_LATENCY(opname) finish_op(child, &child->op.op_ ## opname)
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)
102 child->lasttime = timeval_current();
110 for (i=0;i<sizeof(op.params)/sizeof(op.params[0]);i++) {
111 op.params[i] = params[i]?ival(params[i]):0;
114 if (strcasecmp(op.op, "Sleep") == 0) {
115 nb_sleep(op.params[0]);
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]);
127 printf("[%u] Unknown operation %s in pid %u\n",
128 child->line, op.op, (unsigned)getpid());
132 /* run a test that simulates an approximate netbench client load */
133 void child_run(struct child_struct *child0, const char *loadfile)
136 char line[1024], fname[1024], fname2[1024];
137 char **sparams, **params;
141 pid_t parent = getppid();
143 struct child_struct *child;
145 for (child=child0;child<child0+options.clients_per_process;child++) {
147 asprintf(&child->cname,"client%d", child->id);
150 sparams = calloc(20, sizeof(char *));
152 sparams[i] = malloc(100);
155 f = fopen(loadfile, "r");
162 for (child=child0;child<child0+options.clients_per_process;child++) {
163 nb_time_reset(child);
166 while (fgets(line, sizeof(line)-1, f)) {
169 if (kill(parent, 0) == -1) {
173 for (child=child0;child<child0+options.clients_per_process;child++) {
174 if (child->done) goto done;
178 line[strlen(line)-1] = 0;
180 all_string_sub(line,"\\", "/");
181 all_string_sub(line," /", " ");
185 i<19 && next_token(&p, params[i], " ");
190 if (i < 2 || params[0][0] == '#') continue;
192 if (!strncmp(params[0],"SMB", 3)) {
193 printf("ERROR: You are using a dbench 1 load file\n");
197 if (i > 0 && isdigit(params[0][0])) {
198 targett = strtod(params[0], NULL);
205 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0 &&
206 strncmp(params[i-1], "0x", 2) != 0 &&
207 strncmp(params[i-1], "*", 1) != 0) {
208 printf("Badly formed status at line %d\n", child->line);
212 status = params[i-1];
214 for (child=child0;child<child0+options.clients_per_process;child++) {
220 if (i>1 && params[1][0] == '/') {
221 snprintf(fname, sizeof(fname), "%s%s", child->directory, params[1]);
222 all_string_sub(fname,"client1", child->cname);
225 if (i>2 && params[2][0] == '/') {
226 snprintf(fname2, sizeof(fname2), "%s%s", child->directory, params[2]);
227 all_string_sub(fname2,"client1", child->cname);
231 if (options.targetrate != 0 || targett == 0.0) {
232 nb_target_rate(child, options.targetrate);
234 nb_time_delay(child, targett);
236 child_op(child, params[0], fname, fname2, params+pcount, status);
240 if (options.run_once) {
249 for (child=child0;child<child0+options.clients_per_process;child++) {
252 if (!options.skip_cleanup) {
253 nb_ops->cleanup(child);
255 child->cleanup_finished = 1;