summaryrefslogtreecommitdiff
path: root/source4/torture/nbench/nbench.c
blob: f671a5b0dec0393959b648c7cbf5f785a1025a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* 
   Unix SMB/CIFS implementation.
   SMB torture tester - NBENCH test
   Copyright (C) Andrew Tridgell 1997-2004
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "libcli/libcli.h"
#include "torture/ui.h"
#include "torture/util.h"
#include "torture/torture.h"
#include "system/filesys.h"
#include "pstring.h"

#include "torture/nbench/proto.h"

int nbench_line_count = 0;
static int timelimit = 600;
static int warmup;
static const char *loadfile;
static int read_only;

#define ival(s) strtoll(s, NULL, 0)

/* run a test that simulates an approximate netbench client load */
static BOOL run_netbench(struct torture_context *tctx, struct smbcli_state *cli, int client)
{
	int torture_nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
	int i;
	pstring line;
	char *cname;
	FILE *f;
	const char **params;
	BOOL correct = True;

	if (torture_nprocs == 1) {
		if (!read_only && !torture_setup_dir(cli, "\\clients")) {
			return False;
		}
	}

	nb_setup(cli, client);

	asprintf(&cname, "client%d", client+1);

	f = fopen(loadfile, "r");

	if (!f) {
		perror(loadfile);
		return False;
	}

again:
	while (fgets(line, sizeof(line)-1, f)) {
		NTSTATUS status;

		nbench_line_count++;

		line[strlen(line)-1] = 0;

		all_string_sub(line,"client1", cname, sizeof(line));
		
		params = str_list_make_shell(NULL, line, " ");
		i = str_list_length(params);

		if (i < 2 || params[0][0] == '#') continue;

		if (!strncmp(params[0],"SMB", 3)) {
			printf("ERROR: You are using a dbench 1 load file\n");
			exit(1);
		}

		if (strncmp(params[i-1], "NT_STATUS_", 10) != 0) {
			printf("Badly formed status at line %d\n", nbench_line_count);
			talloc_free(params);
			continue;
		}

		status = nt_status_string_to_code(params[i-1]);

		DEBUG(9,("run_netbench(%d): %s %s\n", client, params[0], params[1]));

		if (!strcmp(params[0],"NTCreateX")) {
			nb_createx(params[1], ival(params[2]), ival(params[3]), 
				   ival(params[4]), status);
		} else if (!strcmp(params[0],"Close")) {
			nb_close(ival(params[1]), status);
		} else if (!read_only && !strcmp(params[0],"Rename")) {
			nb_rename(params[1], params[2], status);
		} else if (!read_only && !strcmp(params[0],"Unlink")) {
			nb_unlink(params[1], ival(params[2]), status);
		} else if (!read_only && !strcmp(params[0],"Deltree")) {
			nb_deltree(params[1]);
		} else if (!read_only && !strcmp(params[0],"Rmdir")) {
			nb_rmdir(params[1], status);
		} else if (!read_only && !strcmp(params[0],"Mkdir")) {
			nb_mkdir(params[1], status);
		} else if (!strcmp(params[0],"QUERY_PATH_INFORMATION")) {
			nb_qpathinfo(params[1], ival(params[2]), status);
		} else if (!strcmp(params[0],"QUERY_FILE_INFORMATION")) {
			nb_qfileinfo(ival(params[1]), ival(params[2]), status);
		} else if (!strcmp(params[0],"QUERY_FS_INFORMATION")) {
			nb_qfsinfo(ival(params[1]), status);
		} else if (!read_only && !strcmp(params[0],"SET_FILE_INFORMATION")) {
			nb_sfileinfo(ival(params[1]), ival(params[2]), status);
		} else if (!strcmp(params[0],"FIND_FIRST")) {
			nb_findfirst(params[1], ival(params[2]), 
				     ival(params[3]), ival(params[4]), status);
		} else if (!read_only && !strcmp(params[0],"WriteX")) {
			nb_writex(ival(params[1]), 
				  ival(params[2]), ival(params[3]), ival(params[4]),
				  status);
		} else if (!read_only && !strcmp(params[0],"Write")) {
			nb_write(ival(params[1]), 
				 ival(params[2]), ival(params[3]), ival(params[4]),
				 status);
		} else if (!strcmp(params[0],"LockX")) {
			nb_lockx(ival(params[1]), 
				 ival(params[2]), ival(params[3]), status);
		} else if (!strcmp(params[0],"UnlockX")) {
			nb_unlockx(ival(params[1]), 
				 ival(params[2]), ival(params[3]), status);
		} else if (!strcmp(params[0],"ReadX")) {
			nb_readx(ival(params[1]), 
				 ival(params[2]), ival(params[3]), ival(params[4]),
				 status);
		} else if (!strcmp(params[0],"Flush")) {
			nb_flush(ival(params[1]), status);
		} else if (!strcmp(params[0],"Sleep")) {
			nb_sleep(ival(params[1]), status);
		} else {
			printf("[%d] Unknown operation %s\n", nbench_line_count, params[0]);
		}

		talloc_free(params);
		
		if (nb_tick()) goto done;
	}

	rewind(f);
	goto again;

done:
	fclose(f);

	if (!read_only && torture_nprocs == 1) {
		smbcli_deltree(cli->tree, "\\clients");
	}
	if (!torture_close_connection(cli)) {
		correct = False;
	}
	
	return correct;
}


/* run a test that simulates an approximate netbench client load */
BOOL torture_nbench(struct torture_context *torture)
{
	BOOL correct = True;
	int torture_nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
	struct smbcli_state *cli;
	const char *p;

	read_only = lp_parm_bool(-1, "torture", "readonly", False);

	p = torture_setting_string(torture, "timelimit", NULL);
	if (p && *p) {
		timelimit = atoi(p);
	}

	warmup = timelimit / 20;

	loadfile = torture_setting_string(torture, "loadfile", NULL);
	if (!loadfile || !*loadfile) {
		loadfile = "client.txt";
	}

	if (torture_nprocs > 1) {
		if (!torture_open_connection(&cli, 0)) {
			return False;
		}

		if (!read_only && !torture_setup_dir(cli, "\\clients")) {
			return False;
		}
	}

	nbio_shmem(torture_nprocs, timelimit, warmup);

	printf("Running for %d seconds with load '%s' and warmup %d secs\n", 
	       timelimit, loadfile, warmup);

	/* we need to reset SIGCHLD here as the name resolution
	   library may have changed it. We rely on correct signals
	   from childs in the main torture code which reaps
	   children. This is why smbtorture BENCH-NBENCH was sometimes
	   failing */
	signal(SIGCHLD, SIG_DFL);


	signal(SIGALRM, nb_alarm);
	alarm(1);
	torture_create_procs(torture, run_netbench, &correct);
	alarm(0);

	if (!read_only && torture_nprocs > 1) {
		smbcli_deltree(cli->tree, "\\clients");
	}

	printf("\nThroughput %g MB/sec\n", nbio_result());
	return correct;
}

NTSTATUS torture_nbench_init(void)
{
	struct torture_suite *suite = torture_suite_create(
										talloc_autofree_context(),
										"BENCH");

	torture_suite_add_simple_test(suite, "NBENCH", torture_nbench);

	suite->description = talloc_strdup(suite, 
								"Benchmarks");

	torture_register_suite(suite);
	return NT_STATUS_OK;
}