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