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) strtoll(s, NULL, 0)
33 #define RWBUFSIZE 16*1024*1024
34 char rw_buf[RWBUFSIZE];
36 static void nb_sleep(int usec)
42 static void nb_target_rate(struct child_struct *child, double rate)
46 if (child->rate.last_bytes == 0) {
47 child->rate.last_bytes = child->bytes;
48 child->rate.last_time = timeval_current();
53 tdelay = (child->bytes - child->rate.last_bytes)/(1.0e6*rate) -
54 timeval_elapsed(&child->rate.last_time);
56 tdelay = - timeval_elapsed(&child->rate.last_time);
58 if (tdelay > 0 && rate != 0) {
61 child->max_latency = MAX(child->max_latency, -tdelay);
64 child->rate.last_time = timeval_current();
65 child->rate.last_bytes = child->bytes;
68 static void nb_time_reset(struct child_struct *child)
70 child->starttime = timeval_current();
71 memset(&child->rate, 0, sizeof(child->rate));
74 static void nb_time_delay(struct child_struct *child, double targett)
76 double elapsed = timeval_elapsed(&child->starttime);
77 if (targett > elapsed) {
78 msleep(1000*(targett - elapsed));
79 } else if (elapsed - targett > child->max_latency) {
80 child->max_latency = MAX(elapsed - targett, child->max_latency);
84 static void finish_op(struct child_struct *child, struct op *op)
86 double t = timeval_elapsed(&child->lasttime);
89 if (t > op->max_latency) {
94 #define OP_LATENCY(opname) finish_op(child, &child->op.op_ ## opname)
96 /* here we parse "special" arguments that start with '*'
97 * '*' itself means a random 64 bit number, but this can be qualified as
99 * '*' a random 64 bit number
101 * '.../y' align the number as an integer multiple of y (( x = (x/y)*y))
105 * '*' : random 64 bit number
106 * '*%1024' : random number between 0 and 1023
107 * '* /1024' : random 64 bit number aligned to n*1024
108 * '*%1024/2 : random even number between 0 and 1023
111 * a special case is when the format starts with a '+' and is followed by
112 * a number, in which case we reuse the number from the previous line in the
113 * loadfile and add <number> to it :
114 * '+1024' : add 1024 to the value from the previous line in the loadfile
116 static uint64_t parse_special(const char *fmt, uint64_t prev_val)
123 val = strtoll(fmt+1, NULL, 0);
124 return prev_val + val;
128 num = (num <<32) | random();
131 while (*fmt != '\0') {
133 val = strtoll(fmt, NULL, 0);
135 printf("Illegal value in random number qualifier. Can not be zero\n");
150 printf("Unknown qualifier '%c' for randum number qualifier\n", q);
153 /* skip until the next token */
154 while (*fmt != '\0') {
173 static void child_op(struct child_struct *child, const char *opname,
174 const char *fname, const char *fname2,
175 char **params, const char *status)
177 static struct dbench_op prev_op;
181 child->lasttime = timeval_current();
189 for (i=0;i<sizeof(op.params)/sizeof(op.params[0]);i++) {
190 switch (params[i][0]) {
193 op.params[i] = parse_special(params[i], prev_op.params[i]);
196 op.params[i] = params[i]?ival(params[i]):0;
202 if (strcasecmp(op.op, "Sleep") == 0) {
203 nb_sleep(op.params[0]);
207 for (i=0;nb_ops->ops[i].name;i++) {
208 if (strcasecmp(op.op, nb_ops->ops[i].name) == 0) {
209 nb_ops->ops[i].fn(&op);
210 finish_op(child, &child->ops[i]);
215 printf("[%u] Unknown operation %s in pid %u\n",
216 child->line, op.op, (unsigned)getpid());
219 #define MAX_RND_STR 10
220 static char random_string[MAX_RND_STR][256];
222 static int store_random_string(unsigned int idx, char *str)
224 if (idx >= MAX_RND_STR) {
225 fprintf(stderr, "'idx' in RANDOMSTRING is too large. %u specified but %u is maximum\n", idx, MAX_RND_STR-1);
230 strncpy(random_string[idx], str, sizeof(random_string[0]));
235 static char *get_random_string(unsigned int idx)
237 return random_string[idx];
241 * This parses a line of the form :
242 * RANDOMSTRING <idx> <string>
243 * All subpatterns of the form [<characters>] in string are substituted for
244 * a randomly chosen character from the specified set.
246 * The end result is stored as string index <idx>
248 static int parse_randomstring(char *line)
251 char *pstart, *pend, rndc[2];
256 pstart = index(line, '[');
257 if (pstart == NULL) {
260 strncpy(str, pstart, sizeof(str));
262 pend = index(str, ']');
263 if (pstart == NULL) {
264 fprintf(stderr, "Unbalanced '[' in RANDOMSTRING : %s\n", line);
271 /* pick a random character */
272 num = strlen(str) - 2;
273 rndc[0] = str[random()%num + 1];
276 single_string_sub(line, str, rndc);
281 if (sscanf(line, "RANDOMSTRING %u %s\n", &idx, str) != 2) {
282 fprintf(stderr, "Invalid RANDOMSTRING line : [%s]\n", line);
285 /* remote initial " */
286 while (str[0] == '"') {
287 memcpy(str, str+1, sizeof(str)-1);
289 /* remote trailing " */
291 int len = strlen(str);
297 if (str[len-1] != '"') {
304 if (store_random_string(idx, str)) {
305 fprintf(stderr, "Failed to store randomstring idx:%d str:%s\n", idx, str);
313 /* run a test that simulates an approximate netbench client load */
314 #define MAX_PARM_LEN 1024
315 void child_run(struct child_struct *child0, const char *loadfile)
318 char line[MAX_PARM_LEN], fname[MAX_PARM_LEN], fname2[MAX_PARM_LEN];
319 char **sparams, **params;
323 pid_t parent = getppid();
325 struct child_struct *child;
327 unsigned loop_count = 0;
328 z_off_t loop_start = 0;
330 gzf = gzopen(loadfile, "r");
336 for (child=child0;child<child0+options.clients_per_process;child++) {
338 asprintf(&child->cname,"client%d", child->id);
341 sparams = calloc(20, sizeof(char *));
343 sparams[i] = malloc(MAX_PARM_LEN);
344 memset(sparams[i], 0, MAX_PARM_LEN);
348 for (child=child0;child<child0+options.clients_per_process;child++) {
349 nb_time_reset(child);
352 while (gzgets(gzf, line, sizeof(line)-1)) {
353 unsigned repeat_count = 1;
355 for (child=child0;child<child0+options.clients_per_process;child++) {
356 if (child->done) goto done;
363 if (kill(parent, 0) == -1) {
368 /* if this is a "LOOP <xxx>" line,
369 * remember the current file position and move to the next line
371 if (strncmp(line, "LOOP", 4) == 0) {
372 if (sscanf(line, "LOOP %u\n", &loop_count) != 1) {
373 fprintf(stderr, "Incorrect LOOP at line %d\n", child0->line);
377 for (child=child0;child<child0+options.clients_per_process;child++) {
380 loop_start = gztell(gzf);
381 gzgets(gzf, line, sizeof(line)-1);
385 if (strncmp(line, "ENDLOOP", 7) == 0) {
388 gzgets(gzf, line, sizeof(line)-1);
390 if (loop_count > 0) {
391 gzseek(gzf, loop_start, SEEK_SET);
394 gzgets(gzf, line, sizeof(line)-1);
398 /* if this is a "REPEAT <xxx>" line, just replace the
399 * currently read line with the next line
401 if (strncmp(line, "REPEAT", 6) == 0) {
402 if (sscanf(line, "REPEAT %u\n", &repeat_count) != 1) {
403 fprintf(stderr, "Incorrect REPEAT at line %d\n", child0->line);
407 for (child=child0;child<child0+options.clients_per_process;child++) {
410 gzgets(gzf, line, sizeof(line)-1);
415 if (strncmp(line, "WRITEPATTERN", 12) == 0) {
417 int count = RWBUFSIZE;
423 if (len > strlen(line +13)) {
424 len = strlen(line +13);
426 memcpy(ptr, line+13, len);
435 if (strncmp(line, "RANDOMSTRING", 12) == 0) {
437 if (parse_randomstring(line) != 0) {
438 fprintf(stderr, "Incorrect RANDOMSTRING at line %d\n", child0->line);
445 line[strlen(line)-1] = 0;
447 all_string_sub(line,"\\", "/");
448 all_string_sub(line," /", " ");
450 /* substitute all $<digit> stored strings */
451 while (have_random && (p = index(line, '$')) != NULL) {
456 if (idx >= MAX_RND_STR) {
457 fprintf(stderr, "$%d is an invalid filename/string\n", idx);
465 nstr = get_random_string(idx);
466 all_string_sub(line, sstr, nstr);
471 i<19 && next_token(&p, params[i], " ");
476 if (i < 2 || params[0][0] == '#') continue;
478 if (!strncmp(params[0],"SMB", 3)) {
479 printf("ERROR: You are using a dbench 1 load file\n");
483 if (i > 0 && isdigit(params[0][0])) {
484 targett = strtod(params[0], NULL);
491 if (strncmp(params[i-1], "NT_STATUS_", 10) != 0 &&
492 strncmp(params[i-1], "0x", 2) != 0 &&
493 strncmp(params[i-1], "SUCCESS", 7) != 0 &&
494 strncmp(params[i-1], "ERROR", 7) != 0 &&
495 strncmp(params[i-1], "*", 1) != 0) {
496 printf("Badly formed status at line %d\n", child->line);
500 status = params[i-1];
503 for (child=child0;child<child0+options.clients_per_process;child++) {
504 unsigned child_repeat_count = repeat_count;
510 if (i>1 && params[1][0] == '/') {
511 snprintf(fname, sizeof(fname), "%s%s", child->directory, params[1]);
512 all_string_sub(fname,"client1", child->cname);
515 if (i>2 && params[2][0] == '/') {
516 snprintf(fname2, sizeof(fname2), "%s%s", child->directory, params[2]);
517 all_string_sub(fname2,"client1", child->cname);
521 if (options.targetrate != 0 || targett == 0.0) {
522 nb_target_rate(child, options.targetrate);
524 nb_time_delay(child, targett);
526 while (child_repeat_count--) {
527 child_op(child, params[0], fname, fname2, params+pcount, status);
532 if (options.run_once) {
541 for (child=child0;child<child0+options.clients_per_process;child++) {
544 if (!options.skip_cleanup) {
545 nb_ops->cleanup(child);
547 child->cleanup_finished = 1;