2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2006
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "torture/torture.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "system/time.h"
27 #include "system/filesys.h"
28 #include "libcli/libcli.h"
29 #include "torture/util.h"
30 #include "lib/events/events.h"
31 #include "lib/cmdline/popt_common.h"
32 #include "libcli/composite/composite.h"
33 #include "libcli/smb_composite/smb_composite.h"
35 #define BASEDIR "\\benchlock"
36 #define FNAME BASEDIR "\\lock.dat"
39 static int lock_failed;
40 static int num_connected;
42 enum lock_stage {LOCK_INITIAL, LOCK_LOCK, LOCK_UNLOCK};
44 struct benchlock_state {
45 struct event_context *ev;
46 struct smbcli_tree *tree;
50 enum lock_stage stage;
55 struct smbcli_request *req;
56 struct smb_composite_connect reconnect;
58 /* these are used for reconnections */
60 const char *dest_host;
61 const char *called_name;
62 const char *service_type;
65 static void lock_completion(struct smbcli_request *);
68 send the next lock request
70 static void lock_send(struct benchlock_state *state)
73 struct smb_lock_entry lock;
75 switch (state->stage) {
77 io.lockx.in.ulock_cnt = 0;
78 io.lockx.in.lock_cnt = 1;
79 state->lock_offset = 0;
80 state->unlock_offset = 0;
81 lock.offset = state->lock_offset;
84 io.lockx.in.ulock_cnt = 0;
85 io.lockx.in.lock_cnt = 1;
86 state->lock_offset = (state->lock_offset+1)%(nprocs+1);
87 lock.offset = state->lock_offset;
90 io.lockx.in.ulock_cnt = 1;
91 io.lockx.in.lock_cnt = 0;
92 lock.offset = state->unlock_offset;
93 state->unlock_offset = (state->unlock_offset+1)%(nprocs+1);
98 lock.pid = state->tree->session->pid;
100 io.lockx.level = RAW_LOCK_LOCKX;
101 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
102 io.lockx.in.timeout = 100000;
103 io.lockx.in.locks = &lock;
104 io.lockx.in.file.fnum = state->fnum;
106 state->req = smb_raw_lock_send(state->tree, &io);
107 if (state->req == NULL) {
108 DEBUG(0,("Failed to setup lock\n"));
111 state->req->async.private = state;
112 state->req->async.fn = lock_completion;
115 static void reopen_connection(struct event_context *ev, struct timed_event *te,
116 struct timeval t, void *private_data);
119 static void reopen_file(struct event_context *ev, struct timed_event *te,
120 struct timeval t, void *private_data)
122 struct benchlock_state *state = (struct benchlock_state *)private_data;
124 /* reestablish our open file */
125 state->fnum = smbcli_open(state->tree, FNAME, O_RDWR|O_CREAT, DENY_NONE);
126 if (state->fnum == -1) {
127 printf("Failed to open %s on connection %d\n", FNAME, state->client_num);
133 DEBUG(0,("reconnect to %s finished (%u connected)\n", state->dest_host,
136 state->stage = LOCK_INITIAL;
141 complete an async reconnect
143 static void reopen_connection_complete(struct composite_context *ctx)
145 struct benchlock_state *state = (struct benchlock_state *)ctx->async.private_data;
147 struct smb_composite_connect *io = &state->reconnect;
149 status = smb_composite_connect_recv(ctx, state->mem_ctx);
150 if (!NT_STATUS_IS_OK(status)) {
151 event_add_timed(state->ev, state->mem_ctx,
152 timeval_current_ofs(1,0),
153 reopen_connection, state);
157 talloc_free(state->tree);
158 state->tree = io->out.tree;
160 /* do the reopen as a separate event */
161 event_add_timed(state->ev, state->mem_ctx, timeval_zero(), reopen_file, state);
169 static void reopen_connection(struct event_context *ev, struct timed_event *te,
170 struct timeval t, void *private_data)
172 struct benchlock_state *state = (struct benchlock_state *)private_data;
173 struct composite_context *ctx;
174 struct smb_composite_connect *io = &state->reconnect;
177 if (!torture_get_conn_index(state->client_num, state->mem_ctx, &host, &share)) {
178 DEBUG(0,("Can't find host/share for reconnect?!\n"));
182 io->in.dest_host = state->dest_host;
183 io->in.port = state->dest_port;
184 io->in.called_name = state->called_name;
185 io->in.service = share;
186 io->in.service_type = state->service_type;
187 io->in.credentials = cmdline_credentials;
188 io->in.fallback_to_anonymous = False;
189 io->in.workgroup = lp_workgroup();
191 /* kill off the remnants of the old connection */
192 talloc_free(state->tree);
195 ctx = smb_composite_connect_send(io, state->mem_ctx, state->ev);
197 DEBUG(0,("Failed to setup async reconnect\n"));
201 ctx->async.fn = reopen_connection_complete;
202 ctx->async.private_data = state;
207 called when a lock completes
209 static void lock_completion(struct smbcli_request *req)
211 struct benchlock_state *state = (struct benchlock_state *)req->async.private;
212 NTSTATUS status = smbcli_request_simple_recv(req);
214 if (!NT_STATUS_IS_OK(status)) {
215 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
216 talloc_free(state->tree);
219 DEBUG(0,("reopening connection to %s\n", state->dest_host));
220 event_add_timed(state->ev, state->mem_ctx,
221 timeval_current_ofs(1,0),
222 reopen_connection, state);
224 DEBUG(0,("Lock failed - %s\n", nt_errstr(status)));
230 switch (state->stage) {
232 state->stage = LOCK_LOCK;
235 state->stage = LOCK_UNLOCK;
238 state->stage = LOCK_LOCK;
247 static void report_rate(struct event_context *ev, struct timed_event *te,
248 struct timeval t, void *private_data)
250 struct benchlock_state *state = talloc_get_type(private_data,
251 struct benchlock_state);
253 for (i=0;i<nprocs;i++) {
254 printf("%5u ", (unsigned)(state[i].count - state[i].lastcount));
255 state[i].lastcount = state[i].count;
259 event_add_timed(ev, state, timeval_current_ofs(1, 0), report_rate, state);
261 /* send an echo on each interface to ensure it stays alive - this helps
263 for (i=0;i<nprocs;i++) {
265 p.in.repeat_count = 0;
268 smb_raw_echo_send(state[i].tree->session->transport, &p);
273 benchmark locking calls
275 BOOL torture_bench_lock(struct torture_context *torture)
278 TALLOC_CTX *mem_ctx = talloc_new(torture);
280 int timelimit = torture_setting_int(torture, "timelimit", 10);
282 struct event_context *ev = event_context_find(mem_ctx);
283 struct benchlock_state *state;
284 int total = 0, minops=0;
285 struct smbcli_state *cli;
288 progress = torture_setting_bool(torture, "progress", true);
290 nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
292 state = talloc_zero_array(mem_ctx, struct benchlock_state, nprocs);
294 printf("Opening %d connections\n", nprocs);
295 for (i=0;i<nprocs;i++) {
296 state[i].mem_ctx = talloc_new(state);
297 state[i].client_num = i;
299 if (!torture_open_connection_ev(&cli, i, ev)) {
302 talloc_steal(mem_ctx, state);
303 state[i].tree = cli->tree;
304 state[i].dest_host = talloc_strdup(state[i].mem_ctx,
305 cli->tree->session->transport->socket->hostname);
306 state[i].dest_port = cli->tree->session->transport->socket->port;
307 state[i].called_name = talloc_strdup(state[i].mem_ctx,
308 cli->tree->session->transport->called.name);
309 state[i].service_type = talloc_strdup(state[i].mem_ctx,
315 if (!torture_setup_dir(cli, BASEDIR)) {
319 for (i=0;i<nprocs;i++) {
320 state[i].fnum = smbcli_open(state[i].tree,
322 O_RDWR|O_CREAT, DENY_NONE);
323 if (state[i].fnum == -1) {
324 printf("Failed to open %s on connection %d\n", FNAME, i);
328 state[i].stage = LOCK_INITIAL;
329 lock_send(&state[i]);
332 tv = timeval_current();
335 event_add_timed(ev, state, timeval_current_ofs(1, 0), report_rate, state);
338 printf("Running for %d seconds\n", timelimit);
339 while (timeval_elapsed(&tv) < timelimit) {
343 DEBUG(0,("locking failed\n"));
348 printf("%.2f ops/second\n", total/timeval_elapsed(&tv));
349 minops = state[0].count;
350 for (i=0;i<nprocs;i++) {
351 printf("[%d] %u ops\n", i, state[i].count);
352 if (state[i].count < minops) minops = state[i].count;
354 if (minops < 0.5*total/nprocs) {
355 printf("Failed: unbalanced locking\n");
359 for (i=0;i<nprocs;i++) {
360 talloc_free(state[i].req);
361 smb_raw_exit(state[i].tree->session);
364 smbcli_deltree(state[0].tree, BASEDIR);
365 talloc_free(mem_ctx);
369 talloc_free(mem_ctx);