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 #define MAX_PARM_LEN 1024
134 void child_run(struct child_struct *child0, const char *loadfile)
137 char line[MAX_PARM_LEN], fname[MAX_PARM_LEN], fname2[MAX_PARM_LEN];
138 char **sparams, **params;
142 pid_t parent = getppid();
144 struct child_struct *child;
146 for (child=child0;child<child0+options.clients_per_process;child++) {
148 asprintf(&child->cname,"client%d", child->id);
151 sparams = calloc(20, sizeof(char *));
153 sparams[i] = malloc(MAX_PARM_LEN);
154 memset(sparams[i], 0, MAX_PARM_LEN);
157 f = fopen(loadfile, "r");
164 for (child=child0;child<child0+options.clients_per_process;child++) {
165 nb_time_reset(child);
168 while (fgets(line, sizeof(line)-1, f)) {
171 if (kill(parent, 0) == -1) {
175 for (child=child0;child<child0+options.clients_per_process;child++) {
176 if (child->done) goto done;
180 line[strlen(line)-1] = 0;
182 all_string_sub(line,"\\", "/");
183 all_string_sub(line," /", " ");
187 i<19 && next_token(&p, params[i], " ");
192 if (i < 2 || params[0][0] == '#') continue;
194 if (!strncmp(params[0],"SMB", 3)) {
195 printf("ERROR: You are using a dbench 1 load file\n");
199 if (i > 0 && isdigit(params[0][0])) {
200 targett = strtod(params[0], NULL);
207 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0 &&
208 strncmp(params[i-1], "0x", 2) != 0 &&
209 strncmp(params[i-1], "*", 1) != 0) {
210 printf("Badly formed status at line %d\n", child->line);
214 status = params[i-1];
216 for (child=child0;child<child0+options.clients_per_process;child++) {
222 if (i>1 && params[1][0] == '/') {
223 snprintf(fname, sizeof(fname), "%s%s", child->directory, params[1]);
224 all_string_sub(fname,"client1", child->cname);
227 if (i>2 && params[2][0] == '/') {
228 snprintf(fname2, sizeof(fname2), "%s%s", child->directory, params[2]);
229 all_string_sub(fname2,"client1", child->cname);
233 if (options.targetrate != 0 || targett == 0.0) {
234 nb_target_rate(child, options.targetrate);
236 nb_time_delay(child, targett);
238 child_op(child, params[0], fname, fname2, params+pcount, status);
242 if (options.run_once) {
251 for (child=child0;child<child0+options.clients_per_process;child++) {
254 if (!options.skip_cleanup) {
255 nb_ops->cleanup(child);
257 child->cleanup_finished = 1;