s3-waf: remove version from libpdb.so for now.
[Samba/vl.git] / source4 / torture / raw / openbench.c
blob9543eea1b10813c70078c2e115e4d97ca7737f16
1 /*
2 Unix SMB/CIFS implementation.
4 open benchmark
6 Copyright (C) Andrew Tridgell 2007
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.
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.
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/>.
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"
38 #define BASEDIR "\\benchopen"
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;
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;
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;
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);
83 complete an async reconnect
85 static void reopen_connection_complete(struct composite_context *ctx)
87 struct benchopen_state *state = (struct benchopen_state *)ctx->async.private_data;
88 NTSTATUS status;
89 struct smb_composite_connect *io = &state->reconnect;
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;
100 state->tree = io->out.tree;
102 num_connected++;
104 DEBUG(0,("[%u] reconnect to %s finished (%u connected)\n",
105 state->client_num, state->dest_host, num_connected));
107 state->open_fnum = -1;
108 state->close_fnum = -1;
109 next_open(state);
115 reopen a connection
117 static void reopen_connection(struct tevent_context *ev, struct tevent_timer *te,
118 struct timeval t, void *private_data)
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;
125 state->te = NULL;
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);
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);
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;
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);
159 ctx->async.fn = reopen_connection_complete;
160 ctx->async.private_data = state;
163 static void open_completed(struct smbcli_request *req);
164 static void close_completed(struct smbcli_request *req);
167 static void next_open(struct benchopen_state *state)
169 state->count++;
171 state->pending_file_num = state->next_file_num;
172 state->next_file_num = (state->next_file_num+1) % (3*nprocs);
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];
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;
194 static void next_close(struct benchopen_state *state)
196 if (state->close_fnum == -1) {
197 return;
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;
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;
211 called when a open completes
213 static void open_completed(struct smbcli_request *req)
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;
219 status = smb_raw_open_recv(req, tmp_ctx, &state->open_parms);
221 talloc_free(tmp_ctx);
223 state->req_open = NULL;
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;
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;
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;
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;
265 DEBUG(2,("[%d] open completed %d (fnum[%d])\n",
266 state->client_num, state->open_file_num, state->open_fnum));
268 if (state->close_fnum != -1) {
269 next_close(state);
272 next_open(state);
276 called when a close completes
278 static void close_completed(struct smbcli_request *req)
280 struct benchopen_state *state = (struct benchopen_state *)req->async.private_data;
281 NTSTATUS status = smbcli_request_simple_recv(req);
283 state->req_close = NULL;
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;
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;
311 DEBUG(2,("[%d] close completed %d (fnum[%d])\n",
312 state->client_num, state->close_file_num,
313 state->close_fnum));
316 static void echo_completion(struct smbcli_request *req)
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);
335 static void report_rate(struct tevent_context *ev, struct tevent_timer *te,
336 struct timeval t, void *private_data)
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;
345 printf("\r");
346 fflush(stdout);
347 report_te = tevent_add_timer(ev, state, timeval_current_ofs(1, 0),
348 report_rate, state);
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;
356 if (!state[i].tree) {
357 continue;
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;
370 benchmark open calls
372 bool torture_bench_open(struct torture_context *torture)
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;
385 progress = torture_setting_bool(torture, "progress", true);
387 nprocs = torture_setting_int(torture, "nprocs", 4);
389 state = talloc_zero_array(mem_ctx, struct benchopen_state, nprocs);
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;
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);
415 num_connected = i;
417 if (!torture_setup_dir(state[0].cli, BASEDIR)) {
418 goto failed;
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);
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]);
434 tv = timeval_current();
436 if (progress) {
437 report_te = tevent_add_timer(torture->ev, state, timeval_current_ofs(1, 0),
438 report_rate, state);
441 printf("Running for %d seconds\n", timelimit);
442 while (timeval_elapsed(&tv) < timelimit) {
443 tevent_loop_once(torture->ev);
445 if (open_failed) {
446 DEBUG(0,("open failed\n"));
447 goto failed;
449 if (close_failed) {
450 DEBUG(0,("open failed\n"));
451 goto failed;
455 talloc_free(report_te);
456 if (progress) {
457 for (i=0;i<nprocs;i++) {
458 printf(" ");
460 printf("\r");
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;
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;
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);
484 smbcli_deltree(state[0].tree, BASEDIR);
485 talloc_free(mem_ctx);
486 return ret;
488 failed:
489 talloc_free(mem_ctx);
490 return false;