1 /*
2    Unix SMB/CIFS implementation.
3 
4    locking benchmark
5 
6    Copyright (C) Andrew Tridgell 2006
7 
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.
12 
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.
17 
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.
21 */
22 
23 #include "includes.h"
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 
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)); \
37 		goto failed; \
38 	}} while (0)
39 
40 #define BASEDIR "\\benchlock"
41 #define FNAME BASEDIR "\\lock.dat"
42 
43 static int nprocs;
44 static int lock_failed;
45 
46 struct benchlock_state {
47 	struct smbcli_state *cli;
48 	int fnum;
49 	int offset;
50 	int count;
51 	union smb_lock io;
52 	struct smb_lock_entry lock[2];
53 	struct smbcli_request *req;
54 };
55 
56 static void lock_completion(struct smbcli_request *);
57 
58 /*
59   send the next lock request
60 */
lock_send(struct benchlock_state * state)61 static void lock_send(struct benchlock_state *state)
62 {
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"));
72 		lock_failed++;
73 	}
74 	state->req->async.private = state;
75 	state->req->async.fn      = lock_completion;
76 	state->offset = (state->offset+1)%nprocs;
77 }
78 
79 /*
80   called when a lock completes
81 */
lock_completion(struct smbcli_request * req)82 static void lock_completion(struct smbcli_request *req)
83 {
84 	struct benchlock_state *state = (struct benchlock_state *)req->async.private;
85 	NTSTATUS status = smbcli_request_simple_recv(req);
86 	if (!NT_STATUS_IS_OK(status)) {
87 		lock_failed++;
88 		DEBUG(0,("Lock failed - %s\n", nt_errstr(status)));
89 	} else {
90 		state->count++;
91 		lock_send(state);
92 	}
93 }
94 
95 /*
96    benchmark locking calls
97 */
torture_bench_lock(struct torture_context * torture)98 BOOL torture_bench_lock(struct torture_context *torture)
99 {
100 	BOOL ret = True;
101 	TALLOC_CTX *mem_ctx = talloc_new(torture);
102 	int i;
103 	int timelimit = torture_setting_int(torture, "timelimit", 10);
104 	struct timeval tv;
105 	struct event_context *ev = event_context_find(mem_ctx);
106 	struct benchlock_state *state;
107 	int total = 0, loops=0;
108 	NTSTATUS status;
109 
110 	nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
111 
112 	state = talloc_zero_array(mem_ctx, struct benchlock_state, nprocs);
113 
114 	printf("Opening %d connections\n", nprocs);
115 	for (i=0;i<nprocs;i++) {
116 		if (!torture_open_connection_ev(&state[i].cli, i, ev)) {
117 			return False;
118 		}
119 		talloc_steal(mem_ctx, state);
120 	}
121 
122 	if (!torture_setup_dir(state[0].cli, BASEDIR)) {
123 		goto failed;
124 	}
125 
126 	for (i=0;i<nprocs;i++) {
127 		state[i].fnum = smbcli_open(state[i].cli->tree,
128 					    FNAME,
129 					    O_RDWR|O_CREAT, DENY_NONE);
130 		if (state[i].fnum == -1) {
131 			printf("Failed to open %s on connection %d\n", FNAME, i);
132 			goto failed;
133 		}
134 
135 		state[i].io.lockx.level = RAW_LOCK_LOCKX;
136 		state[i].io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
137 		state[i].io.lockx.in.timeout = 100000;
138 		state[i].io.lockx.in.ulock_cnt = 0;
139 		state[i].io.lockx.in.lock_cnt = 1;
140 		state[i].lock[0].count = 1;
141 		state[i].lock[1].count = 1;
142 		state[i].io.lockx.in.locks = &state[i].lock[0];
143 
144 		state[i].offset = i;
145 		state[i].io.lockx.in.file.fnum = state[i].fnum;
146 		state[i].lock[0].offset = state[i].offset;
147 		state[i].lock[0].pid    = state[i].cli->session->pid;
148 		status = smb_raw_lock(state[i].cli->tree, &state[i].io);
149 		CHECK_STATUS(status, NT_STATUS_OK);
150 	}
151 
152 	for (i=0;i<nprocs;i++) {
153 		lock_send(&state[i]);
154 	}
155 
156 	tv = timeval_current();
157 
158 	printf("Running for %d seconds\n", timelimit);
159 	while (timeval_elapsed(&tv) < timelimit) {
160 		event_loop_once(ev);
161 
162 		if (lock_failed) {
163 			DEBUG(0,("locking failed\n"));
164 			goto failed;
165 		}
166 
167 		if (loops++ % 10 != 0) continue;
168 
169 		total = 0;
170 		for (i=0;i<nprocs;i++) {
171 			total += state[i].count;
172 		}
173 		printf("%.2f ops/second\r", total/timeval_elapsed(&tv));
174 		fflush(stdout);
175 	}
176 
177 	printf("%.2f ops/second\n", total/timeval_elapsed(&tv));
178 
179 	for (i=0;i<nprocs;i++) {
180 		talloc_free(state[i].req);
181 		smb_raw_exit(state[i].cli->session);
182 	}
183 
184 	smbcli_deltree(state[0].cli->tree, BASEDIR);
185 	talloc_free(mem_ctx);
186 	return ret;
187 
188 failed:
189 	for (i=0;i<nprocs;i++) {
190 		talloc_free(state[i].req);
191 		smb_raw_exit(state[i].cli->session);
192 	}
193 	smbcli_deltree(state[0].cli->tree, BASEDIR);
194 	talloc_free(mem_ctx);
195 	return False;
196 }
197