r26644: Janitorial: Pass resolve_context explicitly to various SMB functions, should...
[gd/samba-autobuild/.git] / source4 / torture / raw / openbench.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    open benchmark
5
6    Copyright (C) Andrew Tridgell 2007
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 #include "libcli/resolve/resolve.h"
34 #include "param/param.h"
35
36 #define BASEDIR "\\benchopen"
37
38 static int nprocs;
39 static int open_failed;
40 static int open_retries;
41 static char **fnames;
42 static int num_connected;
43 static struct timed_event *report_te;
44
45 struct benchopen_state {
46         struct torture_context *tctx;
47         TALLOC_CTX *mem_ctx;
48         struct event_context *ev;
49         struct smbcli_state *cli;
50         struct smbcli_tree *tree;
51         int client_num;
52         int old_fnum;
53         int fnum;
54         int file_num;
55         int count;
56         int lastcount;
57         union smb_open open_parms;
58         union smb_close close_parms;
59         struct smbcli_request *req_open;
60         struct smbcli_request *req_close;
61         struct smb_composite_connect reconnect;
62         struct timed_event *te;
63
64         /* these are used for reconnections */
65         int dest_port;
66         const char *dest_host;
67         const char *called_name;
68         const char *service_type;
69 };
70
71 static void next_open(struct benchopen_state *state);
72 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
73                               struct timeval t, void *private_data);
74
75
76 /*
77   complete an async reconnect
78  */
79 static void reopen_connection_complete(struct composite_context *ctx)
80 {
81         struct benchopen_state *state = (struct benchopen_state *)ctx->async.private_data;
82         NTSTATUS status;
83         struct smb_composite_connect *io = &state->reconnect;
84
85         status = smb_composite_connect_recv(ctx, state->mem_ctx);
86         if (!NT_STATUS_IS_OK(status)) {
87                 talloc_free(state->te);
88                 state->te = event_add_timed(state->ev, state->mem_ctx, 
89                                             timeval_current_ofs(1,0), 
90                                             reopen_connection, state);
91                 return;
92         }
93
94         state->tree = io->out.tree;
95
96         num_connected++;
97
98         DEBUG(0,("reconnect to %s finished (%u connected)\n", state->dest_host,
99                  num_connected));
100
101         state->fnum = -1;
102         state->old_fnum = -1;
103         next_open(state);
104 }
105
106         
107
108 /*
109   reopen a connection
110  */
111 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
112                               struct timeval t, void *private_data)
113 {
114         struct benchopen_state *state = (struct benchopen_state *)private_data;
115         struct composite_context *ctx;
116         struct smb_composite_connect *io = &state->reconnect;
117         char *host, *share;
118
119         state->te = NULL;
120
121         if (!torture_get_conn_index(state->client_num, state->mem_ctx, state->tctx, &host, &share)) {
122                 DEBUG(0,("Can't find host/share for reconnect?!\n"));
123                 exit(1);
124         }
125
126         io->in.dest_host    = state->dest_host;
127         io->in.dest_ports   = state->dest_port;
128         io->in.called_name  = state->called_name;
129         io->in.service      = share;
130         io->in.service_type = state->service_type;
131         io->in.credentials  = cmdline_credentials;
132         io->in.fallback_to_anonymous = false;
133         io->in.workgroup    = lp_workgroup(state->tctx->lp_ctx);
134
135         /* kill off the remnants of the old connection */
136         talloc_free(state->tree);
137         state->tree = NULL;
138         state->fnum = -1;
139
140         ctx = smb_composite_connect_send(io, state->mem_ctx, 
141                                          lp_resolve_context(state->tctx->lp_ctx), 
142                                          state->ev);
143         if (ctx == NULL) {
144                 DEBUG(0,("Failed to setup async reconnect\n"));
145                 exit(1);
146         }
147
148         ctx->async.fn = reopen_connection_complete;
149         ctx->async.private_data = state;
150 }
151
152 static void open_completed(struct smbcli_request *req);
153 static void close_completed(struct smbcli_request *req);
154
155
156 static void next_open(struct benchopen_state *state)
157 {
158         state->count++;
159
160         state->file_num = (state->file_num+1) % (3*nprocs);
161
162         DEBUG(2,("[%d] opening %u\n", state->client_num, state->file_num));
163         state->open_parms.ntcreatex.level = RAW_OPEN_NTCREATEX;
164         state->open_parms.ntcreatex.in.flags = 0;
165         state->open_parms.ntcreatex.in.root_fid = 0;
166         state->open_parms.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
167         state->open_parms.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
168         state->open_parms.ntcreatex.in.alloc_size = 0;
169         state->open_parms.ntcreatex.in.share_access = 0;
170         state->open_parms.ntcreatex.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
171         state->open_parms.ntcreatex.in.create_options = 0;
172         state->open_parms.ntcreatex.in.impersonation = 0;
173         state->open_parms.ntcreatex.in.security_flags = 0;
174         state->open_parms.ntcreatex.in.fname = fnames[state->file_num];
175
176         state->req_open = smb_raw_open_send(state->tree, &state->open_parms);
177         state->req_open->async.fn = open_completed;
178         state->req_open->async.private = state;
179 }
180
181
182 static void next_close(struct benchopen_state *state)
183 {
184         DEBUG(2,("[%d] closing %d\n", state->client_num, state->old_fnum));
185         if (state->old_fnum == -1) {
186                 return;
187         }
188         state->close_parms.close.level = RAW_CLOSE_CLOSE;
189         state->close_parms.close.in.file.fnum = state->old_fnum;
190         state->close_parms.close.in.write_time = 0;
191
192         state->req_close = smb_raw_close_send(state->tree, &state->close_parms);
193         state->req_close->async.fn = close_completed;
194         state->req_close->async.private = state;
195         state->old_fnum = -1;
196 }
197
198 /*
199   called when a open completes
200 */
201 static void open_completed(struct smbcli_request *req)
202 {
203         struct benchopen_state *state = (struct benchopen_state *)req->async.private;
204         TALLOC_CTX *tmp_ctx = talloc_new(state->mem_ctx);
205         NTSTATUS status;
206
207         status = smb_raw_open_recv(req, tmp_ctx, &state->open_parms);
208
209         talloc_free(tmp_ctx);
210
211         state->req_open = NULL;
212
213         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
214             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
215                 talloc_free(state->tree);
216                 talloc_free(state->cli);
217                 state->tree = NULL;
218                 state->cli = NULL;
219                 num_connected--;        
220                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
221                 talloc_free(state->te);
222                 state->te = event_add_timed(state->ev, state->mem_ctx, 
223                                             timeval_current_ofs(1,0), 
224                                             reopen_connection, state);
225                 return;
226         }
227
228         if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
229                 DEBUG(2,("[%d] retrying open\n", state->client_num));
230                 open_retries++;
231                 state->req_open = smb_raw_open_send(state->tree, &state->open_parms);
232                 state->req_open->async.fn = open_completed;
233                 state->req_open->async.private = state;
234                 return;
235         }
236
237         if (!NT_STATUS_IS_OK(status)) {
238                 open_failed++;
239                 DEBUG(0,("open failed - %s\n", nt_errstr(status)));
240                 return;
241         }
242
243         state->old_fnum = state->fnum;
244         state->fnum = state->open_parms.ntcreatex.out.file.fnum;
245
246         DEBUG(2,("[%d] open completed: fnum=%d old_fnum=%d\n", 
247                  state->client_num, state->fnum, state->old_fnum));
248
249         if (state->old_fnum != -1) {
250                 next_close(state);
251         }
252
253         next_open(state);
254 }       
255
256 /*
257   called when a close completes
258 */
259 static void close_completed(struct smbcli_request *req)
260 {
261         struct benchopen_state *state = (struct benchopen_state *)req->async.private;
262         NTSTATUS status = smbcli_request_simple_recv(req);
263
264         state->req_close = NULL;
265
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                 talloc_free(state->cli);
270                 state->tree = NULL;
271                 state->cli = NULL;
272                 num_connected--;        
273                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
274                 talloc_free(state->te);
275                 state->te = event_add_timed(state->ev, state->mem_ctx, 
276                                             timeval_current_ofs(1,0), 
277                                             reopen_connection, state);
278                 return;
279         }
280
281         if (!NT_STATUS_IS_OK(status)) {
282                 open_failed++;
283                 DEBUG(0,("close failed - %s\n", nt_errstr(status)));
284                 return;
285         }
286
287         DEBUG(2,("[%d] close completed: fnum=%d old_fnum=%d\n", 
288                  state->client_num, state->fnum, state->old_fnum));
289 }       
290
291 static void echo_completion(struct smbcli_request *req)
292 {
293         struct benchopen_state *state = (struct benchopen_state *)req->async.private;
294         NTSTATUS status = smbcli_request_simple_recv(req);
295         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
296             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
297                 talloc_free(state->tree);
298                 state->tree = NULL;
299                 num_connected--;        
300                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
301                 talloc_free(state->te);
302                 state->te = event_add_timed(state->ev, state->mem_ctx, 
303                                             timeval_current_ofs(1,0), 
304                                             reopen_connection, state);
305         }
306 }
307
308 static void report_rate(struct event_context *ev, struct timed_event *te, 
309                         struct timeval t, void *private_data)
310 {
311         struct benchopen_state *state = talloc_get_type(private_data, 
312                                                         struct benchopen_state);
313         int i;
314         for (i=0;i<nprocs;i++) {
315                 printf("%5u ", (unsigned)(state[i].count - state[i].lastcount));
316                 state[i].lastcount = state[i].count;
317         }
318         printf("\r");
319         fflush(stdout);
320         report_te = event_add_timed(ev, state, timeval_current_ofs(1, 0), 
321                                     report_rate, state);
322
323         /* send an echo on each interface to ensure it stays alive - this helps
324            with IP takeover */
325         for (i=0;i<nprocs;i++) {
326                 struct smb_echo p;
327                 struct smbcli_request *req;
328
329                 if (!state[i].tree) {
330                         continue;
331                 }
332
333                 p.in.repeat_count = 1;
334                 p.in.size = 0;
335                 p.in.data = NULL;
336                 req = smb_raw_echo_send(state[i].tree->session->transport, &p);
337                 req->async.private = &state[i];
338                 req->async.fn      = echo_completion;
339         }
340 }
341
342 /* 
343    benchmark open calls
344 */
345 bool torture_bench_open(struct torture_context *torture)
346 {
347         bool ret = true;
348         TALLOC_CTX *mem_ctx = talloc_new(torture);
349         int i;
350         int timelimit = torture_setting_int(torture, "timelimit", 10);
351         struct timeval tv;
352         struct event_context *ev = event_context_find(mem_ctx);
353         struct benchopen_state *state;
354         int total = 0, minops=0;
355         bool progress=false;
356
357         progress = torture_setting_bool(torture, "progress", true);
358         
359         nprocs = torture_setting_int(torture, "nprocs", 4);
360
361         state = talloc_zero_array(mem_ctx, struct benchopen_state, nprocs);
362
363         printf("Opening %d connections\n", nprocs);
364         for (i=0;i<nprocs;i++) {
365                 state[i].tctx = torture;
366                 state[i].mem_ctx = talloc_new(state);
367                 state[i].client_num = i;
368                 state[i].ev = ev;
369                 if (!torture_open_connection_ev(&state[i].cli, i, torture, ev)) {
370                         return false;
371                 }
372                 talloc_steal(mem_ctx, state);
373                 state[i].tree = state[i].cli->tree;
374                 state[i].dest_host = talloc_strdup(state[i].mem_ctx, 
375                                                    state[i].cli->tree->session->transport->socket->hostname);
376                 state[i].dest_port = state[i].cli->tree->session->transport->socket->port;
377                 state[i].called_name  = talloc_strdup(state[i].mem_ctx,
378                                                       state[i].cli->tree->session->transport->called.name);
379                 state[i].service_type = talloc_strdup(state[i].mem_ctx,
380                                                       state[i].cli->tree->device);
381         }
382
383         num_connected = i;
384
385         if (!torture_setup_dir(state[0].cli, BASEDIR)) {
386                 goto failed;
387         }
388
389         fnames = talloc_array(mem_ctx, char *, 3*nprocs);
390         for (i=0;i<3*nprocs;i++) {
391                 fnames[i] = talloc_asprintf(fnames, "%s\\file%d.dat", BASEDIR, i);
392         }
393
394         for (i=0;i<nprocs;i++) {
395                 state[i].file_num = i;          
396                 state[i].fnum = smbcli_open(state[i].tree, 
397                                             fnames[state->file_num], 
398                                             O_RDWR|O_CREAT, DENY_ALL);
399                 state[i].old_fnum = -1;
400                 next_open(&state[i]);
401         }
402
403         tv = timeval_current(); 
404
405         if (progress) {
406                 report_te = event_add_timed(ev, state, timeval_current_ofs(1, 0), 
407                                             report_rate, state);
408         }
409
410         printf("Running for %d seconds\n", timelimit);
411         while (timeval_elapsed(&tv) < timelimit) {
412                 event_loop_once(ev);
413
414                 if (open_failed) {
415                         DEBUG(0,("open failed\n"));
416                         goto failed;
417                 }
418         }
419
420         talloc_free(report_te);
421
422         printf("%.2f ops/second (%d retries)\n", 
423                total/timeval_elapsed(&tv), open_retries);
424         minops = state[0].count;
425         for (i=0;i<nprocs;i++) {
426                 printf("[%d] %u ops\n", i, state[i].count);
427                 if (state[i].count < minops) minops = state[i].count;
428         }
429         if (minops < 0.5*total/nprocs) {
430                 printf("Failed: unbalanced open\n");
431                 goto failed;
432         }
433
434         for (i=0;i<nprocs;i++) {
435                 talloc_free(state[i].req_open);
436                 talloc_free(state[i].req_close);
437                 smb_raw_exit(state[i].tree->session);
438         }
439
440         smbcli_deltree(state[0].tree, BASEDIR);
441         talloc_free(mem_ctx);
442         return ret;
443
444 failed:
445         talloc_free(mem_ctx);
446         return false;
447 }