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