r22876: - try to reconnect once per second, not continously
[ira/wip.git] / source4 / torture / raw / lockbench.c
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 #include "libcli/composite/composite.h"
33 #include "libcli/smb_composite/smb_composite.h"
34
35 #define CHECK_STATUS(status, correct) do { \
36         if (!NT_STATUS_EQUAL(status, correct)) { \
37                 printf("(%s) Incorrect status %s - should be %s\n", \
38                        __location__, nt_errstr(status), nt_errstr(correct)); \
39                 goto failed; \
40         }} while (0)
41
42 #define BASEDIR "\\benchlock"
43 #define FNAME BASEDIR "\\lock.dat"
44
45 static int nprocs;
46 static int lock_failed;
47
48 struct benchlock_state {
49         struct event_context *ev;
50         struct smbcli_tree *tree;
51         TALLOC_CTX *mem_ctx;
52         int client_num;
53         int fnum;
54         int offset;
55         int count;
56         union smb_lock io;
57         struct smb_lock_entry lock[2];
58         struct smbcli_request *req;
59         struct smb_composite_connect reconnect;
60
61         /* these are used for reconnections */
62         int dest_port;
63         const char *dest_host;
64         const char *called_name;
65         const char *service_type;
66 };
67
68 static void lock_completion(struct smbcli_request *);
69
70 /*
71   send the next lock request
72 */
73 static void lock_send(struct benchlock_state *state)
74 {
75         state->io.lockx.in.file.fnum = state->fnum;
76         state->io.lockx.in.ulock_cnt = 1;
77         state->lock[0].pid = state->tree->session->pid;
78         state->lock[1].pid = state->tree->session->pid;
79         state->lock[0].offset = state->offset;
80         state->lock[1].offset = (state->offset+1)%nprocs;
81         state->req = smb_raw_lock_send(state->tree, &state->io);
82         if (state->req == NULL) {
83                 DEBUG(0,("Failed to setup lock\n"));
84                 lock_failed++;
85         }
86         state->req->async.private = state;
87         state->req->async.fn      = lock_completion;
88         state->offset = (state->offset+1)%nprocs;
89 }
90
91 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
92                               struct timeval t, void *private_data);
93
94
95 static void reopen_file(struct event_context *ev, struct timed_event *te, 
96                                       struct timeval t, void *private_data)
97 {
98         struct benchlock_state *state = (struct benchlock_state *)private_data;
99
100         /* reestablish our open file */
101         state->fnum = smbcli_open(state->tree, FNAME, O_RDWR|O_CREAT, DENY_NONE);
102         if (state->fnum == -1) {
103                 printf("Failed to open %s on connection %d\n", FNAME, state->client_num);
104                 exit(1);
105         }
106
107         /* reestablish one lock, preparing for the async lock loop */
108         state->lock[0].offset = state->offset;
109         state->io.lockx.in.ulock_cnt = 0;
110         state->io.lockx.in.file.fnum = state->fnum;
111         state->req = smb_raw_lock_send(state->tree, &state->io);
112         if (state->req == NULL) {
113                 DEBUG(0,("Failed to setup lock\n"));
114                 lock_failed++;
115         }
116         state->req->async.private = state;
117         state->req->async.fn      = lock_completion;
118 }
119
120 /*
121   complete an async reconnect
122  */
123 static void reopen_connection_complete(struct composite_context *ctx)
124 {
125         struct benchlock_state *state = (struct benchlock_state *)ctx->async.private_data;
126         NTSTATUS status;
127         struct smb_composite_connect *io = &state->reconnect;
128
129         status = smb_composite_connect_recv(ctx, state->mem_ctx);
130         if (!NT_STATUS_IS_OK(status)) {
131                 event_add_timed(state->ev, state->mem_ctx, 
132                                 timeval_current_ofs(1,0), 
133                                 reopen_connection, state);
134                 return;
135         }
136
137         talloc_free(state->tree);
138         state->tree = io->out.tree;
139
140         /* do the reopen as a separate event */
141         event_add_timed(state->ev, state->mem_ctx, timeval_zero(), reopen_file, state);
142 }
143
144         
145
146 /*
147   reopen a connection
148  */
149 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
150                               struct timeval t, void *private_data)
151 {
152         struct benchlock_state *state = (struct benchlock_state *)private_data;
153         struct composite_context *ctx;
154         struct smb_composite_connect *io = &state->reconnect;
155         char *host, *share;
156
157         if (!torture_get_conn_index(state->client_num, state->mem_ctx, &host, &share)) {
158                 DEBUG(0,("Can't find host/share for reconnect?!\n"));
159                 exit(1);
160         }
161
162         io->in.dest_host    = state->dest_host;
163         io->in.port         = state->dest_port;
164         io->in.called_name  = state->called_name;
165         io->in.service      = share;
166         io->in.service_type = state->service_type;
167         io->in.credentials  = cmdline_credentials;
168         io->in.fallback_to_anonymous = False;
169         io->in.workgroup    = lp_workgroup();
170
171         DEBUG(0,("reopening connection to //%s/%s\n", host, share));
172
173         /* kill off the remnants of the old connection */
174         talloc_free(state->tree);
175         state->tree = NULL;
176
177         ctx = smb_composite_connect_send(io, state->mem_ctx, state->ev);
178         if (ctx == NULL) {
179                 DEBUG(0,("Failed to setup async reconnect\n"));
180                 exit(1);
181         }
182
183         ctx->async.fn = reopen_connection_complete;
184         ctx->async.private_data = state;
185 }
186
187
188 /*
189   called when a lock completes
190 */
191 static void lock_completion(struct smbcli_request *req)
192 {
193         struct benchlock_state *state = (struct benchlock_state *)req->async.private;
194         NTSTATUS status = smbcli_request_simple_recv(req);
195         state->req = NULL;
196         if (!NT_STATUS_IS_OK(status)) {
197                 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
198                         event_add_timed(state->ev, state->mem_ctx, 
199                                         timeval_current_ofs(1,0), 
200                                         reopen_connection, state);
201                 } else {
202                         DEBUG(0,("Lock failed - %s\n", nt_errstr(status)));
203                         lock_failed++;
204                 }
205         } else {
206                 state->count++;
207                 lock_send(state);
208         }
209 }
210
211 /* 
212    benchmark locking calls
213 */
214 BOOL torture_bench_lock(struct torture_context *torture)
215 {
216         BOOL ret = True;
217         TALLOC_CTX *mem_ctx = talloc_new(torture);
218         int i;
219         int timelimit = torture_setting_int(torture, "timelimit", 10);
220         struct timeval tv;
221         struct event_context *ev = event_context_find(mem_ctx);
222         struct benchlock_state *state;
223         int total = 0, loops=0, minops=0;
224         NTSTATUS status;
225         struct smbcli_state *cli;
226         
227         nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
228
229         state = talloc_zero_array(mem_ctx, struct benchlock_state, nprocs);
230
231         printf("Opening %d connections\n", nprocs);
232         for (i=0;i<nprocs;i++) {
233                 state[i].mem_ctx = talloc_new(state);
234                 state[i].client_num = i;
235                 state[i].ev = ev;
236                 if (!torture_open_connection_ev(&cli, i, ev)) {
237                         return False;
238                 }
239                 talloc_steal(mem_ctx, state);
240                 state[i].tree = cli->tree;
241                 state[i].dest_host = talloc_strdup(state[i].mem_ctx, 
242                                                    cli->tree->session->transport->socket->hostname);
243                 state[i].dest_port = cli->tree->session->transport->socket->port;
244                 state[i].called_name  = talloc_strdup(state[i].mem_ctx,
245                                                       cli->tree->session->transport->called.name);
246                 state[i].service_type = talloc_strdup(state[i].mem_ctx,
247                                                       cli->tree->device);
248         }
249
250         if (!torture_setup_dir(cli, BASEDIR)) {
251                 goto failed;
252         }
253
254         for (i=0;i<nprocs;i++) {
255                 state[i].fnum = smbcli_open(state[i].tree, 
256                                             FNAME, 
257                                             O_RDWR|O_CREAT, DENY_NONE);
258                 if (state[i].fnum == -1) {
259                         printf("Failed to open %s on connection %d\n", FNAME, i);
260                         goto failed;
261                 }
262
263                 state[i].io.lockx.level = RAW_LOCK_LOCKX;
264                 state[i].io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
265                 state[i].io.lockx.in.timeout = 100000;
266                 state[i].io.lockx.in.ulock_cnt = 0;
267                 state[i].io.lockx.in.lock_cnt = 1;
268                 state[i].lock[0].count = 1;
269                 state[i].lock[1].count = 1;
270                 state[i].io.lockx.in.locks = &state[i].lock[0];
271
272                 state[i].offset = i;
273                 state[i].io.lockx.in.file.fnum = state[i].fnum;
274                 state[i].lock[0].offset = state[i].offset;
275                 state[i].lock[0].pid    = state[i].tree->session->pid;
276                 status = smb_raw_lock(state[i].tree, &state[i].io);
277                 CHECK_STATUS(status, NT_STATUS_OK);
278         }
279
280         for (i=0;i<nprocs;i++) {
281                 lock_send(&state[i]);
282         }
283
284         tv = timeval_current(); 
285
286         printf("Running for %d seconds\n", timelimit);
287         while (timeval_elapsed(&tv) < timelimit) {
288                 event_loop_once(ev);
289
290                 if (lock_failed) {
291                         DEBUG(0,("locking failed\n"));
292                         goto failed;
293                 }
294
295                 if (loops++ % 10 != 0) continue;
296
297                 total = 0;
298                 for (i=0;i<nprocs;i++) {
299                         total += state[i].count;
300                 }
301                 if (torture_setting_bool(torture, "progress", true)) {
302                         printf("%.2f ops/second (remaining=%u)\r", 
303                                total/timeval_elapsed(&tv), 
304                                (unsigned)(timelimit - timeval_elapsed(&tv)));
305                         fflush(stdout);
306                 }
307         }
308
309         printf("%.2f ops/second\n", total/timeval_elapsed(&tv));
310         minops = state[0].count;
311         for (i=0;i<nprocs;i++) {
312                 printf("[%d] %u ops\n", i, state[i].count);
313                 if (state[i].count < minops) minops = state[i].count;
314         }
315         if (minops < 0.5*total/nprocs) {
316                 printf("Failed: unbalanced locking\n");
317                 goto failed;
318         }
319
320         for (i=0;i<nprocs;i++) {
321                 talloc_free(state[i].req);
322                 smb_raw_exit(state[i].tree->session);
323         }
324
325         smbcli_deltree(state[0].tree, BASEDIR);
326         talloc_free(mem_ctx);
327         return ret;
328
329 failed:
330         talloc_free(mem_ctx);
331         return False;
332 }