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