r23792: convert Samba4 to GPLv3
[garming/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 = talloc_get_type(req->async.private, 
289                                                         struct benchopen_state);
290         NTSTATUS status = smbcli_request_simple_recv(req);
291         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
292             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
293                 talloc_free(state->tree);
294                 state->tree = NULL;
295                 num_connected--;        
296                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
297                 talloc_free(state->te);
298                 state->te = event_add_timed(state->ev, state->mem_ctx, 
299                                             timeval_current_ofs(1,0), 
300                                             reopen_connection, state);
301         }
302 }
303
304 static void report_rate(struct event_context *ev, struct timed_event *te, 
305                         struct timeval t, void *private_data)
306 {
307         struct benchopen_state *state = talloc_get_type(private_data, 
308                                                         struct benchopen_state);
309         int i;
310         for (i=0;i<nprocs;i++) {
311                 printf("%5u ", (unsigned)(state[i].count - state[i].lastcount));
312                 state[i].lastcount = state[i].count;
313         }
314         printf("\r");
315         fflush(stdout);
316         report_te = event_add_timed(ev, state, timeval_current_ofs(1, 0), 
317                                     report_rate, state);
318
319         /* send an echo on each interface to ensure it stays alive - this helps
320            with IP takeover */
321         for (i=0;i<nprocs;i++) {
322                 struct smb_echo p;
323                 struct smbcli_request *req;
324
325                 if (!state[i].tree) {
326                         continue;
327                 }
328
329                 p.in.repeat_count = 1;
330                 p.in.size = 0;
331                 p.in.data = NULL;
332                 req = smb_raw_echo_send(state[i].tree->session->transport, &p);
333                 req->async.private = state;
334                 req->async.fn      = echo_completion;
335         }
336 }
337
338 /* 
339    benchmark open calls
340 */
341 BOOL torture_bench_open(struct torture_context *torture)
342 {
343         BOOL ret = True;
344         TALLOC_CTX *mem_ctx = talloc_new(torture);
345         int i;
346         int timelimit = torture_setting_int(torture, "timelimit", 10);
347         struct timeval tv;
348         struct event_context *ev = event_context_find(mem_ctx);
349         struct benchopen_state *state;
350         int total = 0, minops=0;
351         bool progress=False;
352
353         progress = torture_setting_bool(torture, "progress", true);
354         
355         nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
356
357         state = talloc_zero_array(mem_ctx, struct benchopen_state, nprocs);
358
359         printf("Opening %d connections\n", nprocs);
360         for (i=0;i<nprocs;i++) {
361                 state[i].mem_ctx = talloc_new(state);
362                 state[i].client_num = i;
363                 state[i].ev = ev;
364                 if (!torture_open_connection_ev(&state[i].cli, i, ev)) {
365                         return False;
366                 }
367                 talloc_steal(mem_ctx, state);
368                 state[i].tree = state[i].cli->tree;
369                 state[i].dest_host = talloc_strdup(state[i].mem_ctx, 
370                                                    state[i].cli->tree->session->transport->socket->hostname);
371                 state[i].dest_port = state[i].cli->tree->session->transport->socket->port;
372                 state[i].called_name  = talloc_strdup(state[i].mem_ctx,
373                                                       state[i].cli->tree->session->transport->called.name);
374                 state[i].service_type = talloc_strdup(state[i].mem_ctx,
375                                                       state[i].cli->tree->device);
376         }
377
378         num_connected = i;
379
380         if (!torture_setup_dir(state[0].cli, BASEDIR)) {
381                 goto failed;
382         }
383
384         fnames = talloc_array(mem_ctx, char *, 3*nprocs);
385         for (i=0;i<3*nprocs;i++) {
386                 fnames[i] = talloc_asprintf(fnames, "%s\\file%d.dat", BASEDIR, i);
387         }
388
389         for (i=0;i<nprocs;i++) {
390                 state[i].file_num = i;          
391                 state[i].fnum = smbcli_open(state[i].tree, 
392                                             fnames[state->file_num], 
393                                             O_RDWR|O_CREAT, DENY_ALL);
394                 state[i].old_fnum = -1;
395                 next_open(&state[i]);
396         }
397
398         tv = timeval_current(); 
399
400         if (progress) {
401                 report_te = event_add_timed(ev, state, timeval_current_ofs(1, 0), 
402                                             report_rate, state);
403         }
404
405         printf("Running for %d seconds\n", timelimit);
406         while (timeval_elapsed(&tv) < timelimit) {
407                 event_loop_once(ev);
408
409                 if (open_failed) {
410                         DEBUG(0,("open failed\n"));
411                         goto failed;
412                 }
413         }
414
415         talloc_free(report_te);
416
417         printf("%.2f ops/second (%d retries)\n", 
418                total/timeval_elapsed(&tv), open_retries);
419         minops = state[0].count;
420         for (i=0;i<nprocs;i++) {
421                 printf("[%d] %u ops\n", i, state[i].count);
422                 if (state[i].count < minops) minops = state[i].count;
423         }
424         if (minops < 0.5*total/nprocs) {
425                 printf("Failed: unbalanced open\n");
426                 goto failed;
427         }
428
429         for (i=0;i<nprocs;i++) {
430                 talloc_free(state[i].req_open);
431                 talloc_free(state[i].req_close);
432                 smb_raw_exit(state[i].tree->session);
433         }
434
435         smbcli_deltree(state[0].tree, BASEDIR);
436         talloc_free(mem_ctx);
437         return ret;
438
439 failed:
440         talloc_free(mem_ctx);
441         return False;
442 }