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