44b60b8695ec06a6438619e722ac7d56a6942577
[kai/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
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
34 #define BASEDIR "\\benchlock"
35 #define FNAME BASEDIR "\\lock.dat"
36
37 static int nprocs;
38 static int lock_failed;
39 static int num_connected;
40
41 enum lock_stage {LOCK_INITIAL, LOCK_LOCK, LOCK_UNLOCK};
42
43 struct benchlock_state {
44         struct event_context *ev;
45         struct smbcli_tree *tree;
46         TALLOC_CTX *mem_ctx;
47         int client_num;
48         int fnum;
49         enum lock_stage stage;
50         int lock_offset;
51         int unlock_offset;
52         int count;
53         int lastcount;
54         struct smbcli_request *req;
55         struct smb_composite_connect reconnect;
56         struct timed_event *te;
57
58         /* these are used for reconnections */
59         int dest_port;
60         const char *dest_host;
61         const char *called_name;
62         const char *service_type;
63 };
64
65 static void lock_completion(struct smbcli_request *);
66
67 /*
68   send the next lock request
69 */
70 static void lock_send(struct benchlock_state *state)
71 {
72         union smb_lock io;
73         struct smb_lock_entry lock;
74
75         switch (state->stage) {
76         case LOCK_INITIAL:
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;
82                 break;
83         case LOCK_LOCK:
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;
88                 break;
89         case LOCK_UNLOCK:
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);
94                 break;
95         }
96
97         lock.count = 1;
98         lock.pid = state->tree->session->pid;
99
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;
105
106         state->req = smb_raw_lock_send(state->tree, &io);
107         if (state->req == NULL) {
108                 DEBUG(0,("Failed to setup lock\n"));
109                 lock_failed++;
110         }
111         state->req->async.private = state;
112         state->req->async.fn      = lock_completion;
113 }
114
115 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
116                               struct timeval t, void *private_data);
117
118
119 static void reopen_file(struct event_context *ev, struct timed_event *te, 
120                                       struct timeval t, void *private_data)
121 {
122         struct benchlock_state *state = (struct benchlock_state *)private_data;
123
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);
128                 exit(1);
129         }
130
131         num_connected++;
132
133         DEBUG(0,("reconnect to %s finished (%u connected)\n", state->dest_host,
134                  num_connected));
135
136         state->stage = LOCK_INITIAL;
137         lock_send(state);
138 }
139
140 /*
141   complete an async reconnect
142  */
143 static void reopen_connection_complete(struct composite_context *ctx)
144 {
145         struct benchlock_state *state = (struct benchlock_state *)ctx->async.private_data;
146         NTSTATUS status;
147         struct smb_composite_connect *io = &state->reconnect;
148
149         status = smb_composite_connect_recv(ctx, state->mem_ctx);
150         if (!NT_STATUS_IS_OK(status)) {
151                 talloc_free(state->te);
152                 state->te = event_add_timed(state->ev, state->mem_ctx, 
153                                             timeval_current_ofs(1,0), 
154                                             reopen_connection, state);
155                 return;
156         }
157
158         talloc_free(state->tree);
159         state->tree = io->out.tree;
160
161         /* do the reopen as a separate event */
162         event_add_timed(state->ev, state->mem_ctx, timeval_zero(), reopen_file, state);
163 }
164
165         
166
167 /*
168   reopen a connection
169  */
170 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
171                               struct timeval t, void *private_data)
172 {
173         struct benchlock_state *state = (struct benchlock_state *)private_data;
174         struct composite_context *ctx;
175         struct smb_composite_connect *io = &state->reconnect;
176         char *host, *share;
177
178         state->te = NULL;
179
180         if (!torture_get_conn_index(state->client_num, state->mem_ctx, &host, &share)) {
181                 DEBUG(0,("Can't find host/share for reconnect?!\n"));
182                 exit(1);
183         }
184
185         io->in.dest_host    = state->dest_host;
186         io->in.port         = state->dest_port;
187         io->in.called_name  = state->called_name;
188         io->in.service      = share;
189         io->in.service_type = state->service_type;
190         io->in.credentials  = cmdline_credentials;
191         io->in.fallback_to_anonymous = False;
192         io->in.workgroup    = lp_workgroup();
193
194         /* kill off the remnants of the old connection */
195         talloc_free(state->tree);
196         state->tree = NULL;
197
198         ctx = smb_composite_connect_send(io, state->mem_ctx, state->ev);
199         if (ctx == NULL) {
200                 DEBUG(0,("Failed to setup async reconnect\n"));
201                 exit(1);
202         }
203
204         ctx->async.fn = reopen_connection_complete;
205         ctx->async.private_data = state;
206 }
207
208
209 /*
210   called when a lock completes
211 */
212 static void lock_completion(struct smbcli_request *req)
213 {
214         struct benchlock_state *state = (struct benchlock_state *)req->async.private;
215         NTSTATUS status = smbcli_request_simple_recv(req);
216         state->req = NULL;
217         if (!NT_STATUS_IS_OK(status)) {
218                 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
219                     NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
220                         talloc_free(state->tree);
221                         state->tree = NULL;
222                         num_connected--;        
223                         DEBUG(0,("reopening connection to %s\n", state->dest_host));
224                         talloc_free(state->te);
225                         state->te = event_add_timed(state->ev, state->mem_ctx, 
226                                                     timeval_current_ofs(1,0), 
227                                                     reopen_connection, state);
228                 } else {
229                         DEBUG(0,("Lock failed - %s\n", nt_errstr(status)));
230                         lock_failed++;
231                 }
232                 return;
233         }
234
235         switch (state->stage) {
236         case LOCK_INITIAL:
237                 state->stage = LOCK_LOCK;
238                 break;
239         case LOCK_LOCK:
240                 state->stage = LOCK_UNLOCK;
241                 break;
242         case LOCK_UNLOCK:
243                 state->stage = LOCK_LOCK;
244                 break;
245         }
246
247         state->count++;
248         lock_send(state);
249 }
250
251
252 static void echo_completion(struct smbcli_request *req)
253 {
254         struct benchlock_state *state = (struct benchlock_state *)req->async.private;
255         NTSTATUS status = smbcli_request_simple_recv(req);
256         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
257             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
258                 talloc_free(state->tree);
259                 state->tree = NULL;
260                 num_connected--;        
261                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
262                 talloc_free(state->te);
263                 state->te = event_add_timed(state->ev, state->mem_ctx, 
264                                             timeval_current_ofs(1,0), 
265                                             reopen_connection, state);
266         }
267 }
268
269 static void report_rate(struct event_context *ev, struct timed_event *te, 
270                         struct timeval t, void *private_data)
271 {
272         struct benchlock_state *state = talloc_get_type(private_data, 
273                                                         struct benchlock_state);
274         int i;
275         for (i=0;i<nprocs;i++) {
276                 printf("%5u ", (unsigned)(state[i].count - state[i].lastcount));
277                 state[i].lastcount = state[i].count;
278         }
279         printf("\r");
280         fflush(stdout);
281         event_add_timed(ev, state, timeval_current_ofs(1, 0), report_rate, state);
282
283         /* send an echo on each interface to ensure it stays alive - this helps
284            with IP takeover */
285         for (i=0;i<nprocs;i++) {
286                 struct smb_echo p;
287                 struct smbcli_request *req;
288
289                 if (!state[i].tree) {
290                         continue;
291                 }
292
293                 p.in.repeat_count = 1;
294                 p.in.size = 0;
295                 p.in.data = NULL;
296                 req = smb_raw_echo_send(state[i].tree->session->transport, &p);
297                 req->async.private = &state[i];
298                 req->async.fn      = echo_completion;
299         }
300 }
301
302 /* 
303    benchmark locking calls
304 */
305 BOOL torture_bench_lock(struct torture_context *torture)
306 {
307         BOOL ret = True;
308         TALLOC_CTX *mem_ctx = talloc_new(torture);
309         int i;
310         int timelimit = torture_setting_int(torture, "timelimit", 10);
311         struct timeval tv;
312         struct event_context *ev = event_context_find(mem_ctx);
313         struct benchlock_state *state;
314         int total = 0, minops=0;
315         struct smbcli_state *cli;
316         bool progress;
317
318         progress = torture_setting_bool(torture, "progress", true);
319
320         nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
321
322         state = talloc_zero_array(mem_ctx, struct benchlock_state, nprocs);
323
324         printf("Opening %d connections\n", nprocs);
325         for (i=0;i<nprocs;i++) {
326                 state[i].mem_ctx = talloc_new(state);
327                 state[i].client_num = i;
328                 state[i].ev = ev;
329                 if (!torture_open_connection_ev(&cli, i, ev)) {
330                         return False;
331                 }
332                 talloc_steal(mem_ctx, state);
333                 state[i].tree = cli->tree;
334                 state[i].dest_host = talloc_strdup(state[i].mem_ctx, 
335                                                    cli->tree->session->transport->socket->hostname);
336                 state[i].dest_port = cli->tree->session->transport->socket->port;
337                 state[i].called_name  = talloc_strdup(state[i].mem_ctx,
338                                                       cli->tree->session->transport->called.name);
339                 state[i].service_type = talloc_strdup(state[i].mem_ctx,
340                                                       cli->tree->device);
341         }
342
343         num_connected = i;
344
345         if (!torture_setup_dir(cli, BASEDIR)) {
346                 goto failed;
347         }
348
349         for (i=0;i<nprocs;i++) {
350                 state[i].fnum = smbcli_open(state[i].tree, 
351                                             FNAME, 
352                                             O_RDWR|O_CREAT, DENY_NONE);
353                 if (state[i].fnum == -1) {
354                         printf("Failed to open %s on connection %d\n", FNAME, i);
355                         goto failed;
356                 }
357
358                 state[i].stage = LOCK_INITIAL;
359                 lock_send(&state[i]);
360         }
361
362         tv = timeval_current(); 
363
364         if (progress) {
365                 event_add_timed(ev, state, timeval_current_ofs(1, 0), report_rate, state);
366         }
367
368         printf("Running for %d seconds\n", timelimit);
369         while (timeval_elapsed(&tv) < timelimit) {
370                 event_loop_once(ev);
371
372                 if (lock_failed) {
373                         DEBUG(0,("locking failed\n"));
374                         goto failed;
375                 }
376         }
377
378         printf("%.2f ops/second\n", total/timeval_elapsed(&tv));
379         minops = state[0].count;
380         for (i=0;i<nprocs;i++) {
381                 printf("[%d] %u ops\n", i, state[i].count);
382                 if (state[i].count < minops) minops = state[i].count;
383         }
384         if (minops < 0.5*total/nprocs) {
385                 printf("Failed: unbalanced locking\n");
386                 goto failed;
387         }
388
389         for (i=0;i<nprocs;i++) {
390                 talloc_free(state[i].req);
391                 smb_raw_exit(state[i].tree->session);
392         }
393
394         smbcli_deltree(state[0].tree, BASEDIR);
395         talloc_free(mem_ctx);
396         printf("\n");
397         return ret;
398
399 failed:
400         talloc_free(mem_ctx);
401         return false;
402 }