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"
33 #define CHECK_STATUS(status, correct) do { \
34 if (!NT_STATUS_EQUAL(status, correct)) { \
35 printf("(%s) Incorrect status %s - should be %s\n", \
36 __location__, nt_errstr(status), nt_errstr(correct)); \
40 #define BASEDIR "\\benchlock"
41 #define FNAME BASEDIR "\\lock.dat"
44 static int lock_failed;
46 struct benchlock_state {
47 struct smbcli_state *cli;
52 struct smb_lock_entry lock[2];
53 struct smbcli_request *req;
56 static void lock_completion(struct smbcli_request *);
59 send the next lock request
61 static void lock_send(struct benchlock_state *state)
63 state->io.lockx.in.file.fnum = state->fnum;
64 state->io.lockx.in.ulock_cnt = 1;
65 state->lock[0].pid = state->cli->session->pid;
66 state->lock[1].pid = state->cli->session->pid;
67 state->lock[0].offset = state->offset;
68 state->lock[1].offset = (state->offset+1)%nprocs;
69 state->req = smb_raw_lock_send(state->cli->tree, &state->io);
70 if (state->req == NULL) {
71 DEBUG(0,("Failed to setup lock\n"));
74 state->req->async.private = state;
75 state->req->async.fn = lock_completion;
76 state->offset = (state->offset+1)%nprocs;
80 called when a lock completes
82 static void lock_completion(struct smbcli_request *req)
84 struct benchlock_state *state = (struct benchlock_state *)req->async.private;
85 NTSTATUS status = smbcli_request_simple_recv(req);
87 if (!NT_STATUS_IS_OK(status)) {
89 DEBUG(0,("Lock failed - %s\n", nt_errstr(status)));
97 benchmark locking calls
99 BOOL torture_bench_lock(struct torture_context *torture)
102 TALLOC_CTX *mem_ctx = talloc_new(torture);
104 int timelimit = torture_setting_int(torture, "timelimit", 10);
106 struct event_context *ev = event_context_find(mem_ctx);
107 struct benchlock_state *state;
108 int total = 0, loops=0, minops=0;
111 nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
113 state = talloc_zero_array(mem_ctx, struct benchlock_state, nprocs);
115 printf("Opening %d connections\n", nprocs);
116 for (i=0;i<nprocs;i++) {
117 if (!torture_open_connection_ev(&state[i].cli, i, ev)) {
120 talloc_steal(mem_ctx, state);
123 if (!torture_setup_dir(state[0].cli, BASEDIR)) {
127 for (i=0;i<nprocs;i++) {
128 state[i].fnum = smbcli_open(state[i].cli->tree,
130 O_RDWR|O_CREAT, DENY_NONE);
131 if (state[i].fnum == -1) {
132 printf("Failed to open %s on connection %d\n", FNAME, i);
136 state[i].io.lockx.level = RAW_LOCK_LOCKX;
137 state[i].io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
138 state[i].io.lockx.in.timeout = 100000;
139 state[i].io.lockx.in.ulock_cnt = 0;
140 state[i].io.lockx.in.lock_cnt = 1;
141 state[i].lock[0].count = 1;
142 state[i].lock[1].count = 1;
143 state[i].io.lockx.in.locks = &state[i].lock[0];
146 state[i].io.lockx.in.file.fnum = state[i].fnum;
147 state[i].lock[0].offset = state[i].offset;
148 state[i].lock[0].pid = state[i].cli->session->pid;
149 status = smb_raw_lock(state[i].cli->tree, &state[i].io);
150 CHECK_STATUS(status, NT_STATUS_OK);
153 for (i=0;i<nprocs;i++) {
154 lock_send(&state[i]);
157 tv = timeval_current();
159 printf("Running for %d seconds\n", timelimit);
160 while (timeval_elapsed(&tv) < timelimit) {
164 DEBUG(0,("locking failed\n"));
168 if (loops++ % 10 != 0) continue;
171 for (i=0;i<nprocs;i++) {
172 total += state[i].count;
175 if (torture_setting_bool(torture, "progress", true)) {
176 printf("%.2f ops/second\r", total/timeval_elapsed(&tv));
181 printf("%.2f ops/second\n", total/timeval_elapsed(&tv));
182 minops = state[0].count;
183 for (i=0;i<nprocs;i++) {
184 printf("[%d] %u ops\n", i, state[i].count);
185 if (state[i].count < minops) minops = state[i].count;
187 if (minops < 0.5*total/nprocs) {
188 printf("Failed: unbalanced locking\n");
192 for (i=0;i<nprocs;i++) {
193 talloc_free(state[i].req);
194 smb_raw_exit(state[i].cli->session);
197 smbcli_deltree(state[0].cli->tree, BASEDIR);
198 talloc_free(mem_ctx);
202 for (i=0;i<nprocs;i++) {
203 talloc_free(state[i].req);
204 smb_raw_exit(state[i].cli->session);
206 smbcli_deltree(state[0].cli->tree, BASEDIR);
207 talloc_free(mem_ctx);