Debian Codegraph @

source4/torture/raw/openbench.c

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