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