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