2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "torture/torture.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
27 #include "libcli/libcli.h"
28 #include "torture/util.h"
29 #include "lib/events/events.h"
30 #include "lib/cmdline/popt_common.h"
31 #include "libcli/composite/composite.h"
32 #include "libcli/smb_composite/smb_composite.h"
33 #include "libcli/resolve/resolve.h"
34 #include "param/param.h"
36 #define BASEDIR "\\benchopen"
39 static int open_failed;
40 static int open_retries;
42 static int num_connected;
43 static struct timed_event *report_te;
45 struct benchopen_state {
46 struct torture_context *tctx;
48 struct event_context *ev;
49 struct smbcli_state *cli;
50 struct smbcli_tree *tree;
57 union smb_open open_parms;
58 union smb_close close_parms;
59 struct smbcli_request *req_open;
60 struct smbcli_request *req_close;
61 struct smb_composite_connect reconnect;
62 struct timed_event *te;
64 /* these are used for reconnections */
65 const char **dest_ports;
66 const char *dest_host;
67 const char *called_name;
68 const char *service_type;
71 static void next_open(struct benchopen_state *state);
72 static void reopen_connection(struct event_context *ev, struct timed_event *te,
73 struct timeval t, void *private_data);
77 complete an async reconnect
79 static void reopen_connection_complete(struct composite_context *ctx)
81 struct benchopen_state *state = (struct benchopen_state *)ctx->async.private_data;
83 struct smb_composite_connect *io = &state->reconnect;
85 status = smb_composite_connect_recv(ctx, state->mem_ctx);
86 if (!NT_STATUS_IS_OK(status)) {
87 talloc_free(state->te);
88 state->te = event_add_timed(state->ev, state->mem_ctx,
89 timeval_current_ofs(1,0),
90 reopen_connection, state);
94 state->tree = io->out.tree;
98 DEBUG(0,("reconnect to %s finished (%u connected)\n", state->dest_host,
102 state->old_fnum = -1;
111 static void reopen_connection(struct event_context *ev, struct timed_event *te,
112 struct timeval t, void *private_data)
114 struct benchopen_state *state = (struct benchopen_state *)private_data;
115 struct composite_context *ctx;
116 struct smb_composite_connect *io = &state->reconnect;
121 if (!torture_get_conn_index(state->client_num, state->mem_ctx, state->tctx, &host, &share)) {
122 DEBUG(0,("Can't find host/share for reconnect?!\n"));
126 io->in.dest_host = state->dest_host;
127 io->in.dest_ports = state->dest_ports;
128 io->in.called_name = state->called_name;
129 io->in.service = share;
130 io->in.service_type = state->service_type;
131 io->in.credentials = cmdline_credentials;
132 io->in.fallback_to_anonymous = false;
133 io->in.workgroup = lp_workgroup(state->tctx->lp_ctx);
134 lp_smbcli_options(state->tctx->lp_ctx, &io->in.options);
136 /* kill off the remnants of the old connection */
137 talloc_free(state->tree);
141 ctx = smb_composite_connect_send(io, state->mem_ctx,
142 lp_resolve_context(state->tctx->lp_ctx),
145 DEBUG(0,("Failed to setup async reconnect\n"));
149 ctx->async.fn = reopen_connection_complete;
150 ctx->async.private_data = state;
153 static void open_completed(struct smbcli_request *req);
154 static void close_completed(struct smbcli_request *req);
157 static void next_open(struct benchopen_state *state)
161 state->file_num = (state->file_num+1) % (3*nprocs);
163 DEBUG(2,("[%d] opening %u\n", state->client_num, state->file_num));
164 state->open_parms.ntcreatex.level = RAW_OPEN_NTCREATEX;
165 state->open_parms.ntcreatex.in.flags = 0;
166 state->open_parms.ntcreatex.in.root_fid = 0;
167 state->open_parms.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
168 state->open_parms.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
169 state->open_parms.ntcreatex.in.alloc_size = 0;
170 state->open_parms.ntcreatex.in.share_access = 0;
171 state->open_parms.ntcreatex.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
172 state->open_parms.ntcreatex.in.create_options = 0;
173 state->open_parms.ntcreatex.in.impersonation = 0;
174 state->open_parms.ntcreatex.in.security_flags = 0;
175 state->open_parms.ntcreatex.in.fname = fnames[state->file_num];
177 state->req_open = smb_raw_open_send(state->tree, &state->open_parms);
178 state->req_open->async.fn = open_completed;
179 state->req_open->async.private = state;
183 static void next_close(struct benchopen_state *state)
185 DEBUG(2,("[%d] closing %d\n", state->client_num, state->old_fnum));
186 if (state->old_fnum == -1) {
189 state->close_parms.close.level = RAW_CLOSE_CLOSE;
190 state->close_parms.close.in.file.fnum = state->old_fnum;
191 state->close_parms.close.in.write_time = 0;
193 state->req_close = smb_raw_close_send(state->tree, &state->close_parms);
194 state->req_close->async.fn = close_completed;
195 state->req_close->async.private = state;
196 state->old_fnum = -1;
200 called when a open completes
202 static void open_completed(struct smbcli_request *req)
204 struct benchopen_state *state = (struct benchopen_state *)req->async.private;
205 TALLOC_CTX *tmp_ctx = talloc_new(state->mem_ctx);
208 status = smb_raw_open_recv(req, tmp_ctx, &state->open_parms);
210 talloc_free(tmp_ctx);
212 state->req_open = NULL;
214 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
215 NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
216 talloc_free(state->tree);
217 talloc_free(state->cli);
221 DEBUG(0,("reopening connection to %s\n", state->dest_host));
222 talloc_free(state->te);
223 state->te = event_add_timed(state->ev, state->mem_ctx,
224 timeval_current_ofs(1,0),
225 reopen_connection, state);
229 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
230 DEBUG(2,("[%d] retrying open\n", state->client_num));
232 state->req_open = smb_raw_open_send(state->tree, &state->open_parms);
233 state->req_open->async.fn = open_completed;
234 state->req_open->async.private = state;
238 if (!NT_STATUS_IS_OK(status)) {
240 DEBUG(0,("open failed - %s\n", nt_errstr(status)));
244 state->old_fnum = state->fnum;
245 state->fnum = state->open_parms.ntcreatex.out.file.fnum;
247 DEBUG(2,("[%d] open completed: fnum=%d old_fnum=%d\n",
248 state->client_num, state->fnum, state->old_fnum));
250 if (state->old_fnum != -1) {
258 called when a close completes
260 static void close_completed(struct smbcli_request *req)
262 struct benchopen_state *state = (struct benchopen_state *)req->async.private;
263 NTSTATUS status = smbcli_request_simple_recv(req);
265 state->req_close = NULL;
267 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
268 NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
269 talloc_free(state->tree);
270 talloc_free(state->cli);
274 DEBUG(0,("reopening connection to %s\n", state->dest_host));
275 talloc_free(state->te);
276 state->te = event_add_timed(state->ev, state->mem_ctx,
277 timeval_current_ofs(1,0),
278 reopen_connection, state);
282 if (!NT_STATUS_IS_OK(status)) {
284 DEBUG(0,("close failed - %s\n", nt_errstr(status)));
288 DEBUG(2,("[%d] close completed: fnum=%d old_fnum=%d\n",
289 state->client_num, state->fnum, state->old_fnum));
292 static void echo_completion(struct smbcli_request *req)
294 struct benchopen_state *state = (struct benchopen_state *)req->async.private;
295 NTSTATUS status = smbcli_request_simple_recv(req);
296 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
297 NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
298 talloc_free(state->tree);
301 DEBUG(0,("reopening connection to %s\n", state->dest_host));
302 talloc_free(state->te);
303 state->te = event_add_timed(state->ev, state->mem_ctx,
304 timeval_current_ofs(1,0),
305 reopen_connection, state);
309 static void report_rate(struct event_context *ev, struct timed_event *te,
310 struct timeval t, void *private_data)
312 struct benchopen_state *state = talloc_get_type(private_data,
313 struct benchopen_state);
315 for (i=0;i<nprocs;i++) {
316 printf("%5u ", (unsigned)(state[i].count - state[i].lastcount));
317 state[i].lastcount = state[i].count;
321 report_te = event_add_timed(ev, state, timeval_current_ofs(1, 0),
324 /* send an echo on each interface to ensure it stays alive - this helps
326 for (i=0;i<nprocs;i++) {
328 struct smbcli_request *req;
330 if (!state[i].tree) {
334 p.in.repeat_count = 1;
337 req = smb_raw_echo_send(state[i].tree->session->transport, &p);
338 req->async.private = &state[i];
339 req->async.fn = echo_completion;
346 bool torture_bench_open(struct torture_context *torture)
349 TALLOC_CTX *mem_ctx = talloc_new(torture);
351 int timelimit = torture_setting_int(torture, "timelimit", 10);
353 struct event_context *ev = event_context_find(mem_ctx);
354 struct benchopen_state *state;
355 int total = 0, minops=0;
358 progress = torture_setting_bool(torture, "progress", true);
360 nprocs = torture_setting_int(torture, "nprocs", 4);
362 state = talloc_zero_array(mem_ctx, struct benchopen_state, nprocs);
364 printf("Opening %d connections\n", nprocs);
365 for (i=0;i<nprocs;i++) {
366 state[i].tctx = torture;
367 state[i].mem_ctx = talloc_new(state);
368 state[i].client_num = i;
370 if (!torture_open_connection_ev(&state[i].cli, i, torture, ev)) {
373 talloc_steal(mem_ctx, state);
374 state[i].tree = state[i].cli->tree;
375 state[i].dest_host = talloc_strdup(state[i].mem_ctx,
376 state[i].cli->tree->session->transport->socket->hostname);
377 state[i].dest_ports = talloc_array(state[i].mem_ctx,
379 state[i].dest_ports[0] = talloc_asprintf(state[i].dest_ports,
380 "%u", state[i].cli->tree->session->transport->socket->port);
381 state[i].dest_ports[1] = NULL;
382 state[i].called_name = talloc_strdup(state[i].mem_ctx,
383 state[i].cli->tree->session->transport->called.name);
384 state[i].service_type = talloc_strdup(state[i].mem_ctx,
385 state[i].cli->tree->device);
390 if (!torture_setup_dir(state[0].cli, BASEDIR)) {
394 fnames = talloc_array(mem_ctx, char *, 3*nprocs);
395 for (i=0;i<3*nprocs;i++) {
396 fnames[i] = talloc_asprintf(fnames, "%s\\file%d.dat", BASEDIR, i);
399 for (i=0;i<nprocs;i++) {
400 state[i].file_num = i;
401 state[i].fnum = smbcli_open(state[i].tree,
402 fnames[state->file_num],
403 O_RDWR|O_CREAT, DENY_ALL);
404 state[i].old_fnum = -1;
405 next_open(&state[i]);
408 tv = timeval_current();
411 report_te = event_add_timed(ev, state, timeval_current_ofs(1, 0),
415 printf("Running for %d seconds\n", timelimit);
416 while (timeval_elapsed(&tv) < timelimit) {
420 DEBUG(0,("open failed\n"));
425 talloc_free(report_te);
427 printf("%.2f ops/second (%d retries)\n",
428 total/timeval_elapsed(&tv), open_retries);
429 minops = state[0].count;
430 for (i=0;i<nprocs;i++) {
431 printf("[%d] %u ops\n", i, state[i].count);
432 if (state[i].count < minops) minops = state[i].count;
434 if (minops < 0.5*total/nprocs) {
435 printf("Failed: unbalanced open\n");
439 for (i=0;i<nprocs;i++) {
440 talloc_free(state[i].req_open);
441 talloc_free(state[i].req_close);
442 smb_raw_exit(state[i].tree->session);
445 smbcli_deltree(state[0].tree, BASEDIR);
446 talloc_free(mem_ctx);
450 talloc_free(mem_ctx);