add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / utils / locktest2.c
blobac16055c0f178a8786f69b0b34242763d0cd13b4
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 byte range lock tester - with local filesystem support
5 Copyright (C) Andrew Tridgell 1999
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define NO_SYSLOG
24 #include "includes.h"
26 static fstring password;
27 static fstring username;
28 static int got_pass;
29 static int numops = 1000;
30 static BOOL showall;
31 static BOOL analyze;
32 static BOOL hide_unlock_fails;
33 static BOOL use_oplocks;
35 #define FILENAME "\\locktest.dat"
36 #define LOCKRANGE 100
37 #define LOCKBASE 0
40 #define LOCKBASE (0x40000000 - 50)
43 #define READ_PCT 50
44 #define LOCK_PCT 25
45 #define UNLOCK_PCT 65
46 #define RANGE_MULTIPLE 1
48 #define NSERVERS 2
49 #define NCONNECTIONS 2
50 #define NUMFSTYPES 2
51 #define NFILES 2
52 #define LOCK_TIMEOUT 0
54 #define FSTYPE_SMB 0
55 #define FSTYPE_NFS 1
57 struct record {
58 char r1, r2;
59 char conn, f, fstype;
60 unsigned start, len;
61 char needed;
64 static struct record *recorded;
66 static int try_open(struct cli_state *c, char *nfs, int fstype, char *fname, int flags)
68 pstring path;
70 switch (fstype) {
71 case FSTYPE_SMB:
72 return cli_open(c, fname, flags, DENY_NONE);
74 case FSTYPE_NFS:
75 slprintf(path, sizeof(path), "%s%s", nfs, fname);
76 pstring_sub(path,"\\", "/");
77 return open(path, flags, 0666);
80 return -1;
83 static BOOL try_close(struct cli_state *c, int fstype, int fd)
85 switch (fstype) {
86 case FSTYPE_SMB:
87 return cli_close(c, fd);
89 case FSTYPE_NFS:
90 return close(fd) == 0;
93 return False;
96 static BOOL try_lock(struct cli_state *c, int fstype,
97 int fd, unsigned start, unsigned len,
98 int op)
100 struct flock lock;
102 switch (fstype) {
103 case FSTYPE_SMB:
104 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
106 case FSTYPE_NFS:
107 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
108 lock.l_whence = SEEK_SET;
109 lock.l_start = start;
110 lock.l_len = len;
111 lock.l_pid = getpid();
112 return fcntl(fd,F_SETLK,&lock) == 0;
115 return False;
118 static BOOL try_unlock(struct cli_state *c, int fstype,
119 int fd, unsigned start, unsigned len)
121 struct flock lock;
123 switch (fstype) {
124 case FSTYPE_SMB:
125 return cli_unlock(c, fd, start, len);
127 case FSTYPE_NFS:
128 lock.l_type = F_UNLCK;
129 lock.l_whence = SEEK_SET;
130 lock.l_start = start;
131 lock.l_len = len;
132 lock.l_pid = getpid();
133 return fcntl(fd,F_SETLK,&lock) == 0;
136 return False;
139 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
140 enum brl_type lock_type,
141 br_off start, br_off size)
143 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
144 (int)pid, (int)dev, (int)ino,
145 lock_type==READ_LOCK?"R":"W",
146 (double)start, (double)start+size-1,(double)size);
150 /*****************************************************
151 return a connection to a server
152 *******************************************************/
153 struct cli_state *connect_one(char *share)
155 struct cli_state *c;
156 struct nmb_name called, calling;
157 char *server_n;
158 fstring server;
159 struct in_addr ip;
160 fstring myname;
161 static int count;
163 fstrcpy(server,share+2);
164 share = strchr(server,'\\');
165 if (!share) return NULL;
166 *share = 0;
167 share++;
169 server_n = server;
171 zero_ip(&ip);
173 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
175 make_nmb_name(&calling, myname, 0x0);
176 make_nmb_name(&called , server, 0x20);
178 again:
179 zero_ip(&ip);
181 /* have to open a new connection */
182 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
183 !cli_connect(c, server_n, &ip)) {
184 DEBUG(0,("Connection to %s failed\n", server_n));
185 return NULL;
188 if (!cli_session_request(c, &calling, &called)) {
189 DEBUG(0,("session request to %s failed\n", called.name));
190 cli_shutdown(c);
191 if (strcmp(called.name, "*SMBSERVER")) {
192 make_nmb_name(&called , "*SMBSERVER", 0x20);
193 goto again;
195 return NULL;
198 DEBUG(4,(" session request ok\n"));
200 if (!cli_negprot(c)) {
201 DEBUG(0,("protocol negotiation failed\n"));
202 cli_shutdown(c);
203 return NULL;
206 if (!got_pass) {
207 char *pass = getpass("Password: ");
208 if (pass) {
209 pstrcpy(password, pass);
213 if (!cli_session_setup(c, username,
214 password, strlen(password),
215 password, strlen(password),
216 lp_workgroup())) {
217 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
218 return NULL;
222 * These next two lines are needed to emulate
223 * old client behaviour for people who have
224 * scripts based on client output.
225 * QUESTION ? Do we want to have a 'client compatibility
226 * mode to turn these on/off ? JRA.
229 if (*c->server_domain || *c->server_os || *c->server_type)
230 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
231 c->server_domain,c->server_os,c->server_type));
233 DEBUG(4,(" session setup ok\n"));
235 if (!cli_send_tconX(c, share, "?????",
236 password, strlen(password)+1)) {
237 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
238 cli_shutdown(c);
239 return NULL;
242 DEBUG(4,(" tconx ok\n"));
244 c->use_oplocks = use_oplocks;
246 return c;
250 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
251 char *nfs[NSERVERS],
252 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
253 char *share1, char *share2)
255 int server, conn, f, fstype;
256 char *share[2];
257 share[0] = share1;
258 share[1] = share2;
260 fstype = FSTYPE_SMB;
262 for (server=0;server<NSERVERS;server++)
263 for (conn=0;conn<NCONNECTIONS;conn++) {
264 if (cli[server][conn]) {
265 for (f=0;f<NFILES;f++) {
266 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
268 cli_ulogoff(cli[server][conn]);
269 cli_shutdown(cli[server][conn]);
270 SAFE_FREE(cli[server][conn]);
271 cli[server][conn] = NULL;
273 cli[server][conn] = connect_one(share[server]);
274 if (!cli[server][conn]) {
275 DEBUG(0,("Failed to connect to %s\n", share[server]));
276 exit(1);
283 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
284 char *nfs[NSERVERS],
285 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
286 struct record *rec)
288 unsigned conn = rec->conn;
289 unsigned f = rec->f;
290 unsigned fstype = rec->fstype;
291 unsigned start = rec->start;
292 unsigned len = rec->len;
293 unsigned r1 = rec->r1;
294 unsigned r2 = rec->r2;
295 unsigned op;
296 int server;
297 BOOL ret[NSERVERS];
299 if (r1 < READ_PCT) {
300 op = READ_LOCK;
301 } else {
302 op = WRITE_LOCK;
305 if (r2 < LOCK_PCT) {
306 /* set a lock */
307 for (server=0;server<NSERVERS;server++) {
308 ret[server] = try_lock(cli[server][conn], fstype,
309 fnum[server][fstype][conn][f],
310 start, len, op);
312 if (showall || ret[0] != ret[1]) {
313 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
314 conn, fstype, f,
315 start, start+len-1, len,
316 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
317 ret[0], ret[1]);
319 if (showall) brl_forall(print_brl);
320 if (ret[0] != ret[1]) return False;
321 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
322 /* unset a lock */
323 for (server=0;server<NSERVERS;server++) {
324 ret[server] = try_unlock(cli[server][conn], fstype,
325 fnum[server][fstype][conn][f],
326 start, len);
328 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
329 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
330 conn, fstype, f,
331 start, start+len-1, len,
332 ret[0], ret[1]);
334 if (showall) brl_forall(print_brl);
335 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
336 } else {
337 /* reopen the file */
338 for (server=0;server<NSERVERS;server++) {
339 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
340 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
341 O_RDWR|O_CREAT);
342 if (fnum[server][fstype][conn][f] == -1) {
343 printf("failed to reopen on share1\n");
344 return False;
347 if (showall) {
348 printf("reopen conn=%u fstype=%u f=%u\n",
349 conn, fstype, f);
350 brl_forall(print_brl);
353 return True;
356 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
357 char *nfs[NSERVERS],
358 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
360 int server, conn, f, fstype;
362 for (server=0;server<NSERVERS;server++)
363 for (fstype=0;fstype<NUMFSTYPES;fstype++)
364 for (conn=0;conn<NCONNECTIONS;conn++)
365 for (f=0;f<NFILES;f++) {
366 if (fnum[server][fstype][conn][f] != -1) {
367 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
368 fnum[server][fstype][conn][f] = -1;
371 for (server=0;server<NSERVERS;server++) {
372 cli_unlink(cli[server][0], FILENAME);
376 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
377 char *nfs[NSERVERS],
378 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
380 int server, fstype, conn, f;
382 for (server=0;server<NSERVERS;server++)
383 for (fstype=0;fstype<NUMFSTYPES;fstype++)
384 for (conn=0;conn<NCONNECTIONS;conn++)
385 for (f=0;f<NFILES;f++) {
386 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
387 O_RDWR|O_CREAT);
388 if (fnum[server][fstype][conn][f] == -1) {
389 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
390 server, fstype, conn, f);
391 exit(1);
397 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
398 char *nfs[NSERVERS],
399 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
400 int n)
402 int i;
403 printf("testing %u ...\n", n);
404 for (i=0; i<n; i++) {
405 if (i && i % 100 == 0) {
406 printf("%u\n", i);
409 if (recorded[i].needed &&
410 !test_one(cli, nfs, fnum, &recorded[i])) return i;
412 return n;
416 /* each server has two connections open to it. Each connection has two file
417 descriptors open on the file - 8 file descriptors in total
419 we then do random locking ops in tamdem on the 4 fnums from each
420 server and ensure that the results match
422 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
424 struct cli_state *cli[NSERVERS][NCONNECTIONS];
425 char *nfs[NSERVERS];
426 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
427 int n, i, n1;
429 nfs[0] = nfspath1;
430 nfs[1] = nfspath2;
432 ZERO_STRUCT(fnum);
433 ZERO_STRUCT(cli);
435 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
437 for (n=0; n<numops; n++) {
438 recorded[n].conn = random() % NCONNECTIONS;
439 recorded[n].fstype = random() % NUMFSTYPES;
440 recorded[n].f = random() % NFILES;
441 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
442 recorded[n].len = 1 +
443 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
444 recorded[n].start *= RANGE_MULTIPLE;
445 recorded[n].len *= RANGE_MULTIPLE;
446 recorded[n].r1 = random() % 100;
447 recorded[n].r2 = random() % 100;
448 recorded[n].needed = True;
451 reconnect(cli, nfs, fnum, share1, share2);
452 open_files(cli, nfs, fnum);
453 n = retest(cli, nfs, fnum, numops);
455 if (n == numops || !analyze) return;
456 n++;
458 while (1) {
459 n1 = n;
461 close_files(cli, nfs, fnum);
462 reconnect(cli, nfs, fnum, share1, share2);
463 open_files(cli, nfs, fnum);
465 for (i=0;i<n-1;i++) {
466 int m;
467 recorded[i].needed = False;
469 close_files(cli, nfs, fnum);
470 open_files(cli, nfs, fnum);
472 m = retest(cli, nfs, fnum, n);
473 if (m == n) {
474 recorded[i].needed = True;
475 } else {
476 if (i < m) {
477 memmove(&recorded[i], &recorded[i+1],
478 (m-i)*sizeof(recorded[0]));
480 n = m;
481 i--;
485 if (n1 == n) break;
488 close_files(cli, nfs, fnum);
489 reconnect(cli, nfs, fnum, share1, share2);
490 open_files(cli, nfs, fnum);
491 showall = True;
492 n1 = retest(cli, nfs, fnum, n);
493 if (n1 != n-1) {
494 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
496 close_files(cli, nfs, fnum);
498 for (i=0;i<n;i++) {
499 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
500 recorded[i].r1,
501 recorded[i].r2,
502 recorded[i].conn,
503 recorded[i].fstype,
504 recorded[i].f,
505 recorded[i].start,
506 recorded[i].len,
507 recorded[i].needed);
513 static void usage(void)
515 printf(
516 "Usage:\n\
517 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
518 options:\n\
519 -U user%%pass\n\
520 -s seed\n\
521 -o numops\n\
522 -u hide unlock fails\n\
523 -a (show all ops)\n\
524 -O use oplocks\n\
528 /****************************************************************************
529 main program
530 ****************************************************************************/
531 int main(int argc,char *argv[])
533 char *share1, *share2, *nfspath1, *nfspath2;
534 extern char *optarg;
535 extern int optind;
536 extern FILE *dbf;
537 int opt;
538 char *p;
539 int seed;
540 static pstring servicesf = CONFIGFILE;
542 setlinebuf(stdout);
544 dbf = stderr;
546 if (argc < 5 || argv[1][0] == '-') {
547 usage();
548 exit(1);
551 share1 = argv[1];
552 share2 = argv[2];
553 nfspath1 = argv[3];
554 nfspath2 = argv[4];
556 all_string_sub(share1,"/","\\",0);
557 all_string_sub(share2,"/","\\",0);
559 setup_logging(argv[0],True);
561 argc -= 4;
562 argv += 4;
564 TimeInit();
565 charset_initialise();
566 codepage_initialise(lp_client_code_page());
568 lp_load(servicesf,True,False,False);
569 load_interfaces();
571 if (getenv("USER")) {
572 pstrcpy(username,getenv("USER"));
575 seed = time(NULL);
577 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
578 switch (opt) {
579 case 'U':
580 pstrcpy(username,optarg);
581 p = strchr(username,'%');
582 if (p) {
583 *p = 0;
584 pstrcpy(password, p+1);
585 got_pass = 1;
587 break;
588 case 's':
589 seed = atoi(optarg);
590 break;
591 case 'u':
592 hide_unlock_fails = True;
593 break;
594 case 'o':
595 numops = atoi(optarg);
596 break;
597 case 'O':
598 use_oplocks = True;
599 break;
600 case 'a':
601 showall = True;
602 break;
603 case 'A':
604 analyze = True;
605 break;
606 case 'h':
607 usage();
608 exit(1);
609 default:
610 printf("Unknown option %c (%d)\n", (char)opt, opt);
611 exit(1);
615 argc -= optind;
616 argv += optind;
618 DEBUG(0,("seed=%u\n", seed));
619 srandom(seed);
621 locking_init(1);
622 test_locks(share1, share2, nfspath1, nfspath2);
624 return(0);