r25554: Convert last instances of BOOL, True and False to the standard types.
[sfrench/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         TALLOC_CTX *mem_ctx;
46         struct event_context *ev;
47         struct smbcli_state *cli;
48         struct smbcli_tree *tree;
49         int client_num;
50         int old_fnum;
51         int fnum;
52         int file_num;
53         int count;
54         int lastcount;
55         union smb_open open_parms;
56         union smb_close close_parms;
57         struct smbcli_request *req_open;
58         struct smbcli_request *req_close;
59         struct smb_composite_connect reconnect;
60         struct timed_event *te;
61
62         /* these are used for reconnections */
63         int dest_port;
64         const char *dest_host;
65         const char *called_name;
66         const char *service_type;
67 };
68
69 static void next_open(struct benchopen_state *state);
70 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
71                               struct timeval t, void *private_data);
72
73
74 /*
75   complete an async reconnect
76  */
77 static void reopen_connection_complete(struct composite_context *ctx)
78 {
79         struct benchopen_state *state = (struct benchopen_state *)ctx->async.private_data;
80         NTSTATUS status;
81         struct smb_composite_connect *io = &state->reconnect;
82
83         status = smb_composite_connect_recv(ctx, state->mem_ctx);
84         if (!NT_STATUS_IS_OK(status)) {
85                 talloc_free(state->te);
86                 state->te = event_add_timed(state->ev, state->mem_ctx, 
87                                             timeval_current_ofs(1,0), 
88                                             reopen_connection, state);
89                 return;
90         }
91
92         state->tree = io->out.tree;
93
94         num_connected++;
95
96         DEBUG(0,("reconnect to %s finished (%u connected)\n", state->dest_host,
97                  num_connected));
98
99         state->fnum = -1;
100         state->old_fnum = -1;
101         next_open(state);
102 }
103
104         
105
106 /*
107   reopen a connection
108  */
109 static void reopen_connection(struct event_context *ev, struct timed_event *te, 
110                               struct timeval t, void *private_data)
111 {
112         struct benchopen_state *state = (struct benchopen_state *)private_data;
113         struct composite_context *ctx;
114         struct smb_composite_connect *io = &state->reconnect;
115         char *host, *share;
116
117         state->te = NULL;
118
119         if (!torture_get_conn_index(state->client_num, state->mem_ctx, &host, &share)) {
120                 DEBUG(0,("Can't find host/share for reconnect?!\n"));
121                 exit(1);
122         }
123
124         io->in.dest_host    = state->dest_host;
125         io->in.port         = state->dest_port;
126         io->in.called_name  = state->called_name;
127         io->in.service      = share;
128         io->in.service_type = state->service_type;
129         io->in.credentials  = cmdline_credentials;
130         io->in.fallback_to_anonymous = false;
131         io->in.workgroup    = lp_workgroup(global_loadparm);
132
133         /* kill off the remnants of the old connection */
134         talloc_free(state->tree);
135         state->tree = NULL;
136         state->fnum = -1;
137
138         ctx = smb_composite_connect_send(io, state->mem_ctx, state->ev);
139         if (ctx == NULL) {
140                 DEBUG(0,("Failed to setup async reconnect\n"));
141                 exit(1);
142         }
143
144         ctx->async.fn = reopen_connection_complete;
145         ctx->async.private_data = state;
146 }
147
148 static void open_completed(struct smbcli_request *req);
149 static void close_completed(struct smbcli_request *req);
150
151
152 static void next_open(struct benchopen_state *state)
153 {
154         state->count++;
155
156         state->file_num = (state->file_num+1) % (3*nprocs);
157
158         DEBUG(2,("[%d] opening %u\n", state->client_num, state->file_num));
159         state->open_parms.ntcreatex.level = RAW_OPEN_NTCREATEX;
160         state->open_parms.ntcreatex.in.flags = 0;
161         state->open_parms.ntcreatex.in.root_fid = 0;
162         state->open_parms.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
163         state->open_parms.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
164         state->open_parms.ntcreatex.in.alloc_size = 0;
165         state->open_parms.ntcreatex.in.share_access = 0;
166         state->open_parms.ntcreatex.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
167         state->open_parms.ntcreatex.in.create_options = 0;
168         state->open_parms.ntcreatex.in.impersonation = 0;
169         state->open_parms.ntcreatex.in.security_flags = 0;
170         state->open_parms.ntcreatex.in.fname = fnames[state->file_num];
171
172         state->req_open = smb_raw_open_send(state->tree, &state->open_parms);
173         state->req_open->async.fn = open_completed;
174         state->req_open->async.private = state;
175 }
176
177
178 static void next_close(struct benchopen_state *state)
179 {
180         DEBUG(2,("[%d] closing %d\n", state->client_num, state->old_fnum));
181         if (state->old_fnum == -1) {
182                 return;
183         }
184         state->close_parms.close.level = RAW_CLOSE_CLOSE;
185         state->close_parms.close.in.file.fnum = state->old_fnum;
186         state->close_parms.close.in.write_time = 0;
187
188         state->req_close = smb_raw_close_send(state->tree, &state->close_parms);
189         state->req_close->async.fn = close_completed;
190         state->req_close->async.private = state;
191         state->old_fnum = -1;
192 }
193
194 /*
195   called when a open completes
196 */
197 static void open_completed(struct smbcli_request *req)
198 {
199         struct benchopen_state *state = (struct benchopen_state *)req->async.private;
200         TALLOC_CTX *tmp_ctx = talloc_new(state->mem_ctx);
201         NTSTATUS status;
202
203         status = smb_raw_open_recv(req, tmp_ctx, &state->open_parms);
204
205         talloc_free(tmp_ctx);
206
207         state->req_open = NULL;
208
209         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
210             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
211                 talloc_free(state->tree);
212                 talloc_free(state->cli);
213                 state->tree = NULL;
214                 state->cli = NULL;
215                 num_connected--;        
216                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
217                 talloc_free(state->te);
218                 state->te = event_add_timed(state->ev, state->mem_ctx, 
219                                             timeval_current_ofs(1,0), 
220                                             reopen_connection, state);
221                 return;
222         }
223
224         if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
225                 DEBUG(2,("[%d] retrying open\n", state->client_num));
226                 open_retries++;
227                 state->req_open = smb_raw_open_send(state->tree, &state->open_parms);
228                 state->req_open->async.fn = open_completed;
229                 state->req_open->async.private = state;
230                 return;
231         }
232
233         if (!NT_STATUS_IS_OK(status)) {
234                 open_failed++;
235                 DEBUG(0,("open failed - %s\n", nt_errstr(status)));
236                 return;
237         }
238
239         state->old_fnum = state->fnum;
240         state->fnum = state->open_parms.ntcreatex.out.file.fnum;
241
242         DEBUG(2,("[%d] open completed: fnum=%d old_fnum=%d\n", 
243                  state->client_num, state->fnum, state->old_fnum));
244
245         if (state->old_fnum != -1) {
246                 next_close(state);
247         }
248
249         next_open(state);
250 }       
251
252 /*
253   called when a close completes
254 */
255 static void close_completed(struct smbcli_request *req)
256 {
257         struct benchopen_state *state = (struct benchopen_state *)req->async.private;
258         NTSTATUS status = smbcli_request_simple_recv(req);
259
260         state->req_close = NULL;
261
262         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
263             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
264                 talloc_free(state->tree);
265                 talloc_free(state->cli);
266                 state->tree = NULL;
267                 state->cli = NULL;
268                 num_connected--;        
269                 DEBUG(0,("reopening connection to %s\n", state->dest_host));
270                 talloc_free(state->te);
271                 state->te = event_add_timed(state->ev, state->mem_ctx, 
272                                             timeval_current_ofs(1,0), 
273                                             reopen_connection, state);
274                 return;
275         }
276
277         if (!NT_STATUS_IS_OK(status)) {
278                 open_failed++;
279                 DEBUG(0,("close failed - %s\n", nt_errstr(status)));
280                 return;
281         }
282
283         DEBUG(2,("[%d] close completed: fnum=%d old_fnum=%d\n", 
284                  state->client_num, state->fnum, state->old_fnum));
285 }       
286
287 static void echo_completion(struct smbcli_request *req)
288 {
289         struct benchopen_state *state = (struct benchopen_state *)req->async.private;
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[i];
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 = torture_setting_int(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 }