r20647: add cluster code
[samba.git] / source4 / torture / nbench / nbio.c
1 /*
2   TODO: add splitting of writes for servers with signing
3 */
4
5
6 /* 
7    Unix SMB/CIFS implementation.
8    SMB torture tester
9    Copyright (C) Andrew Tridgell 1997-1998
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27 #include "system/time.h"
28 #include "system/filesys.h"
29 #include "lib/util/dlinklist.h"
30 #include "libcli/libcli.h"
31 #include "libcli/raw/libcliraw.h"
32 #include "torture/torture.h"
33 #include "libcli/libcli.h"
34 #include "torture/util.h"
35
36 extern int nbench_line_count;
37 static int nbio_id = -1;
38 static int nprocs;
39 static BOOL bypass_io;
40 static struct timeval tv_start, tv_end;
41 static int warmup, timelimit;
42 static int in_cleanup;
43
44 struct ftable {
45         struct ftable *next, *prev;
46         int fd;     /* the fd that we got back from the server */
47         int handle; /* the handle in the load file */
48 };
49
50 static struct ftable *ftable;
51
52 static struct {
53         double bytes, warmup_bytes;
54         int line;
55         int done;
56         double max_latency;
57         struct timeval starttime;
58 } *children;
59
60 void nbio_time_reset(void)
61 {
62         children[nbio_id].starttime = timeval_current();        
63 }
64
65 void nbio_time_delay(double targett)
66 {
67         double elapsed = timeval_elapsed(&children[nbio_id].starttime);
68         if (targett > elapsed) {
69                 msleep(1000*(targett - elapsed));
70         } else if (elapsed - targett > children[nbio_id].max_latency) {
71                 children[nbio_id].max_latency = elapsed - targett;
72         }
73 }
74
75 double nbio_result(void)
76 {
77         int i;
78         double total = 0;
79         for (i=0;i<nprocs;i++) {
80                 total += children[i].bytes - children[i].warmup_bytes;
81         }
82         return 1.0e-6 * total / timeval_elapsed2(&tv_start, &tv_end);
83 }
84
85 double nbio_latency(void)
86 {
87         int i;
88         double max_latency = 0;
89         for (i=0;i<nprocs;i++) {
90                 if (children[i].max_latency > max_latency) {
91                         max_latency = children[i].max_latency;
92                         children[i].max_latency = 0;
93                 }
94         }
95         return max_latency;
96 }
97
98 BOOL nb_tick(void)
99 {
100         return children[nbio_id].done;
101 }
102
103
104 void nb_alarm(int sig)
105 {
106         int i;
107         int lines=0;
108         double t;
109         int in_warmup = 0;
110
111         if (nbio_id != -1) return;
112
113         for (i=0;i<nprocs;i++) {
114                 if (children[i].bytes == 0) {
115                         in_warmup = 1;
116                 }
117                 lines += children[i].line;
118         }
119
120         t = timeval_elapsed(&tv_start);
121
122         if (!in_warmup && warmup>0 && t > warmup) {
123                 tv_start = timeval_current();
124                 warmup = 0;
125                 for (i=0;i<nprocs;i++) {
126                         children[i].warmup_bytes = children[i].bytes;
127                 }
128                 goto next;
129         }
130         if (t < warmup) {
131                 in_warmup = 1;
132         } else if (!in_warmup && !in_cleanup && t > timelimit) {
133                 for (i=0;i<nprocs;i++) {
134                         children[i].done = 1;
135                 }
136                 tv_end = timeval_current();
137                 in_cleanup = 1;
138         }
139         if (t < 1) {
140                 goto next;
141         }
142         if (!in_cleanup) {
143                 tv_end = timeval_current();
144         }
145
146         if (in_warmup) {
147                 printf("%4d  %8d  %.2f MB/sec  warmup %.0f sec   \n", 
148                        nprocs, lines/nprocs, 
149                        nbio_result(), t);
150         } else if (in_cleanup) {
151                 printf("%4d  %8d  %.2f MB/sec  cleanup %.0f sec   \n", 
152                        nprocs, lines/nprocs, 
153                        nbio_result(), t);
154         } else {
155                 printf("%4d  %8d  %.2f MB/sec  execute %.0f sec  latency %.2f msec \n", 
156                        nprocs, lines/nprocs, 
157                        nbio_result(), t, nbio_latency() * 1.0e3);
158         }
159
160         fflush(stdout);
161 next:
162         signal(SIGALRM, nb_alarm);
163         alarm(1);       
164 }
165
166 void nbio_shmem(int n, int t_timelimit, int t_warmup)
167 {
168         nprocs = n;
169         children = shm_setup(sizeof(*children) * nprocs);
170         if (!children) {
171                 printf("Failed to setup shared memory!\n");
172                 exit(1);
173         }
174         memset(children, 0, sizeof(*children) * nprocs);
175         timelimit = t_timelimit;
176         warmup = t_warmup;
177         in_cleanup = 0;
178         tv_start = timeval_current();
179 }
180
181 static struct ftable *find_ftable(int handle)
182 {
183         struct ftable *f;
184
185         for (f=ftable;f;f=f->next) {
186                 if (f->handle == handle) return f;
187         }
188         return NULL;
189 }
190
191 static int find_handle(int handle)
192 {
193         struct ftable *f;
194
195         children[nbio_id].line = nbench_line_count;
196
197         f = find_ftable(handle);
198         if (f) {
199                 return f->fd;
200         }
201         printf("(%d) ERROR: handle %d was not found\n", 
202                nbench_line_count, handle);
203         exit(1);
204
205         return -1;              /* Not reached */
206 }
207
208
209
210 static struct smbcli_state *c;
211
212 /*
213   a handler function for oplock break requests
214 */
215 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, 
216                            uint16_t fnum, uint8_t level, void *private)
217 {
218         struct smbcli_tree *tree = private;
219         return smbcli_oplock_ack(tree, fnum, OPLOCK_BREAK_TO_NONE);
220 }
221
222 void nb_setup(struct smbcli_state *cli, int id)
223 {
224         nbio_id = id;
225         c = cli;
226         if (bypass_io)
227                 printf("skipping I/O\n");
228
229         if (cli) {
230                 smbcli_oplock_handler(cli->transport, oplock_handler, cli->tree);
231         }
232 }
233
234
235 static void check_status(const char *op, NTSTATUS status, NTSTATUS ret)
236 {
237         if (!NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(ret)) {
238                 printf("[%d] Error: %s should have failed with %s\n", 
239                        nbench_line_count, op, nt_errstr(status));
240                 exit(1);
241         }
242
243         if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ret)) {
244                 printf("[%d] Error: %s should have succeeded - %s\n", 
245                        nbench_line_count, op, nt_errstr(ret));
246                 exit(1);
247         }
248
249         if (!NT_STATUS_EQUAL(status, ret)) {
250                 printf("[%d] Warning: got status %s but expected %s\n",
251                        nbench_line_count, nt_errstr(ret), nt_errstr(status));
252         }
253 }
254
255
256 void nb_unlink(const char *fname, int attr, NTSTATUS status)
257 {
258         union smb_unlink io;
259         NTSTATUS ret;
260
261         io.unlink.in.pattern = fname;
262
263         io.unlink.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
264         if (strchr(fname, '*') == 0) {
265                 io.unlink.in.attrib |= FILE_ATTRIBUTE_DIRECTORY;
266         }
267
268         ret = smb_raw_unlink(c->tree, &io);
269
270         check_status("Unlink", status, ret);
271 }
272
273
274 void nb_createx(const char *fname, 
275                 uint_t create_options, uint_t create_disposition, int handle,
276                 NTSTATUS status)
277 {
278         union smb_open io;      
279         uint32_t desired_access;
280         NTSTATUS ret;
281         TALLOC_CTX *mem_ctx;
282         uint_t flags = 0;
283         struct ftable *f;
284
285         mem_ctx = talloc_init("raw_open");
286
287         if (create_options & NTCREATEX_OPTIONS_DIRECTORY) {
288                 desired_access = SEC_FILE_READ_DATA;
289         } else {
290                 desired_access = 
291                         SEC_FILE_READ_DATA | 
292                         SEC_FILE_WRITE_DATA |
293                         SEC_FILE_READ_ATTRIBUTE |
294                         SEC_FILE_WRITE_ATTRIBUTE;
295                 flags = NTCREATEX_FLAGS_EXTENDED |
296                         NTCREATEX_FLAGS_REQUEST_OPLOCK | 
297                         NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK;
298         }
299
300         io.ntcreatex.level = RAW_OPEN_NTCREATEX;
301         io.ntcreatex.in.flags = flags;
302         io.ntcreatex.in.root_fid = 0;
303         io.ntcreatex.in.access_mask = desired_access;
304         io.ntcreatex.in.file_attr = 0;
305         io.ntcreatex.in.alloc_size = 0;
306         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE;
307         io.ntcreatex.in.open_disposition = create_disposition;
308         io.ntcreatex.in.create_options = create_options;
309         io.ntcreatex.in.impersonation = 0;
310         io.ntcreatex.in.security_flags = 0;
311         io.ntcreatex.in.fname = fname;
312
313         ret = smb_raw_open(c->tree, mem_ctx, &io);
314
315         talloc_free(mem_ctx);
316
317         check_status("NTCreateX", status, ret);
318
319         if (!NT_STATUS_IS_OK(ret)) return;
320
321         f = malloc_p(struct ftable);
322         f->handle = handle;
323         f->fd = io.ntcreatex.out.file.fnum;
324
325         DLIST_ADD_END(ftable, f, struct ftable *);
326 }
327
328 void nb_writex(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
329 {
330         union smb_write io;
331         int i;
332         NTSTATUS ret;
333         uint8_t *buf;
334
335         i = find_handle(handle);
336
337         if (bypass_io) return;
338
339         buf = malloc(size);
340         memset(buf, 0xab, size);
341
342         io.writex.level = RAW_WRITE_WRITEX;
343         io.writex.in.file.fnum = i;
344         io.writex.in.wmode = 0;
345         io.writex.in.remaining = 0;
346         io.writex.in.offset = offset;
347         io.writex.in.count = size;
348         io.writex.in.data = buf;
349
350         ret = smb_raw_write(c->tree, &io);
351
352         free(buf);
353
354         check_status("WriteX", status, ret);
355
356         if (NT_STATUS_IS_OK(ret) && io.writex.out.nwritten != ret_size) {
357                 printf("[%d] Warning: WriteX got count %d expected %d\n", 
358                        nbench_line_count,
359                        io.writex.out.nwritten, ret_size);
360         }       
361
362         children[nbio_id].bytes += ret_size;
363 }
364
365 void nb_write(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
366 {
367         union smb_write io;
368         int i;
369         NTSTATUS ret;
370         uint8_t *buf;
371
372         i = find_handle(handle);
373
374         if (bypass_io) return;
375
376         buf = malloc(size);
377
378         memset(buf, 0x12, size);
379
380         io.write.level = RAW_WRITE_WRITE;
381         io.write.in.file.fnum = i;
382         io.write.in.remaining = 0;
383         io.write.in.offset = offset;
384         io.write.in.count = size;
385         io.write.in.data = buf;
386
387         ret = smb_raw_write(c->tree, &io);
388
389         free(buf);
390
391         check_status("Write", status, ret);
392
393         if (NT_STATUS_IS_OK(ret) && io.write.out.nwritten != ret_size) {
394                 printf("[%d] Warning: Write got count %d expected %d\n", 
395                        nbench_line_count,
396                        io.write.out.nwritten, ret_size);
397         }       
398
399         children[nbio_id].bytes += ret_size;
400 }
401
402
403 void nb_lockx(int handle, off_t offset, int size, NTSTATUS status)
404 {
405         union smb_lock io;
406         int i;
407         NTSTATUS ret;
408         struct smb_lock_entry lck;
409
410         i = find_handle(handle);
411
412         lck.pid = getpid();
413         lck.offset = offset;
414         lck.count = size;
415
416         io.lockx.level = RAW_LOCK_LOCKX;
417         io.lockx.in.file.fnum = i;
418         io.lockx.in.mode = 0;
419         io.lockx.in.timeout = 0;
420         io.lockx.in.ulock_cnt = 0;
421         io.lockx.in.lock_cnt = 1;
422         io.lockx.in.locks = &lck;
423
424         ret = smb_raw_lock(c->tree, &io);
425
426         check_status("LockX", status, ret);
427 }
428
429 void nb_unlockx(int handle, uint_t offset, int size, NTSTATUS status)
430 {
431         union smb_lock io;
432         int i;
433         NTSTATUS ret;
434         struct smb_lock_entry lck;
435
436         i = find_handle(handle);
437
438         lck.pid = getpid();
439         lck.offset = offset;
440         lck.count = size;
441
442         io.lockx.level = RAW_LOCK_LOCKX;
443         io.lockx.in.file.fnum = i;
444         io.lockx.in.mode = 0;
445         io.lockx.in.timeout = 0;
446         io.lockx.in.ulock_cnt = 1;
447         io.lockx.in.lock_cnt = 0;
448         io.lockx.in.locks = &lck;
449
450         ret = smb_raw_lock(c->tree, &io);
451
452         check_status("UnlockX", status, ret);
453 }
454
455 void nb_readx(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
456 {
457         union smb_read io;
458         int i;
459         NTSTATUS ret;
460         uint8_t *buf;
461
462         i = find_handle(handle);
463
464         if (bypass_io) return;
465
466         buf = malloc(size);
467
468         io.readx.level = RAW_READ_READX;
469         io.readx.in.file.fnum = i;
470         io.readx.in.offset    = offset;
471         io.readx.in.mincnt    = size;
472         io.readx.in.maxcnt    = size;
473         io.readx.in.remaining = 0;
474         io.readx.in.read_for_execute = False;
475         io.readx.out.data     = buf;
476         
477         ret = smb_raw_read(c->tree, &io);
478
479         free(buf);
480
481         check_status("ReadX", status, ret);
482
483         if (NT_STATUS_IS_OK(ret) && io.readx.out.nread != ret_size) {
484                 printf("[%d] ERROR: ReadX got count %d expected %d\n", 
485                        nbench_line_count,
486                        io.readx.out.nread, ret_size);
487                 exit(1);
488         }       
489
490         children[nbio_id].bytes += ret_size;
491 }
492
493 void nb_close(int handle, NTSTATUS status)
494 {
495         NTSTATUS ret;
496         union smb_close io;
497         int i;
498
499         i = find_handle(handle);
500
501         io.close.level = RAW_CLOSE_CLOSE;
502         io.close.in.file.fnum = i;
503         io.close.in.write_time = 0;
504
505         ret = smb_raw_close(c->tree, &io);
506
507         check_status("Close", status, ret);
508
509         if (NT_STATUS_IS_OK(ret)) {
510                 struct ftable *f = find_ftable(handle);
511                 DLIST_REMOVE(ftable, f);
512                 free(f);
513         }
514 }
515
516 void nb_rmdir(const char *dname, NTSTATUS status)
517 {
518         NTSTATUS ret;
519         struct smb_rmdir io;
520
521         io.in.path = dname;
522
523         ret = smb_raw_rmdir(c->tree, &io);
524
525         check_status("Rmdir", status, ret);
526 }
527
528 void nb_mkdir(const char *dname, NTSTATUS status)
529 {
530         union smb_mkdir io;
531
532         io.mkdir.level = RAW_MKDIR_MKDIR;
533         io.mkdir.in.path = dname;
534
535         /* NOTE! no error checking. Used for base fileset creation */
536         smb_raw_mkdir(c->tree, &io);
537 }
538
539 void nb_rename(const char *old, const char *new, NTSTATUS status)
540 {
541         NTSTATUS ret;
542         union smb_rename io;
543
544         io.generic.level = RAW_RENAME_RENAME;
545         io.rename.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
546         io.rename.in.pattern1 = old;
547         io.rename.in.pattern2 = new;
548
549         ret = smb_raw_rename(c->tree, &io);
550
551         check_status("Rename", status, ret);
552 }
553
554
555 void nb_qpathinfo(const char *fname, int level, NTSTATUS status)
556 {
557         union smb_fileinfo io;
558         TALLOC_CTX *mem_ctx;
559         NTSTATUS ret;
560
561         mem_ctx = talloc_init("nb_qpathinfo");
562
563         io.generic.level = level;
564         io.generic.in.file.path = fname;
565
566         ret = smb_raw_pathinfo(c->tree, mem_ctx, &io);
567
568         talloc_free(mem_ctx);
569
570         check_status("Pathinfo", status, ret);
571 }
572
573
574 void nb_qfileinfo(int fnum, int level, NTSTATUS status)
575 {
576         union smb_fileinfo io;
577         TALLOC_CTX *mem_ctx;
578         NTSTATUS ret;
579         int i;
580
581         i = find_handle(fnum);
582
583         mem_ctx = talloc_init("nb_qfileinfo");
584
585         io.generic.level = level;
586         io.generic.in.file.fnum = i;
587
588         ret = smb_raw_fileinfo(c->tree, mem_ctx, &io);
589
590         talloc_free(mem_ctx);
591
592         check_status("Fileinfo", status, ret);
593 }
594
595 void nb_sfileinfo(int fnum, int level, NTSTATUS status)
596 {
597         union smb_setfileinfo io;
598         NTSTATUS ret;
599         int i;
600
601         if (level != RAW_SFILEINFO_BASIC_INFORMATION) {
602                 printf("[%d] Warning: setfileinfo level %d not handled\n", nbench_line_count, level);
603                 return;
604         }
605
606         ZERO_STRUCT(io);
607
608         i = find_handle(fnum);
609
610         io.generic.level = level;
611         io.generic.in.file.fnum = i;
612         unix_to_nt_time(&io.basic_info.in.create_time, time(NULL));
613         unix_to_nt_time(&io.basic_info.in.access_time, 0);
614         unix_to_nt_time(&io.basic_info.in.write_time, 0);
615         unix_to_nt_time(&io.basic_info.in.change_time, 0);
616         io.basic_info.in.attrib = 0;
617
618         ret = smb_raw_setfileinfo(c->tree, &io);
619
620         check_status("Setfileinfo", status, ret);
621 }
622
623 void nb_qfsinfo(int level, NTSTATUS status)
624 {
625         union smb_fsinfo io;
626         TALLOC_CTX *mem_ctx;
627         NTSTATUS ret;
628
629         mem_ctx = talloc_init("smbcli_dskattr");
630
631         io.generic.level = level;
632         ret = smb_raw_fsinfo(c->tree, mem_ctx, &io);
633
634         talloc_free(mem_ctx);
635         
636         check_status("Fsinfo", status, ret);    
637 }
638
639 /* callback function used for trans2 search */
640 static BOOL findfirst_callback(void *private, union smb_search_data *file)
641 {
642         return True;
643 }
644
645 void nb_findfirst(const char *mask, int level, int maxcnt, int count, NTSTATUS status)
646 {
647         union smb_search_first io;
648         TALLOC_CTX *mem_ctx;
649         NTSTATUS ret;
650
651         mem_ctx = talloc_init("smbcli_dskattr");
652
653         io.t2ffirst.level = RAW_SEARCH_TRANS2;
654         io.t2ffirst.data_level = level;
655         io.t2ffirst.in.max_count = maxcnt;
656         io.t2ffirst.in.search_attrib = FILE_ATTRIBUTE_DIRECTORY;
657         io.t2ffirst.in.pattern = mask;
658         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
659         io.t2ffirst.in.storage_type = 0;
660                         
661         ret = smb_raw_search_first(c->tree, mem_ctx, &io, NULL, findfirst_callback);
662
663         talloc_free(mem_ctx);
664
665         check_status("Search", status, ret);
666
667         if (NT_STATUS_IS_OK(ret) && io.t2ffirst.out.count != count) {
668                 printf("[%d] Warning: got count %d expected %d\n", 
669                        nbench_line_count,
670                        io.t2ffirst.out.count, count);
671         }
672 }
673
674 void nb_flush(int fnum, NTSTATUS status)
675 {
676         union smb_flush io;
677         NTSTATUS ret;
678         int i;
679         i = find_handle(fnum);
680
681         io.flush.level          = RAW_FLUSH_FLUSH;
682         io.flush.in.file.fnum   = i;
683
684         ret = smb_raw_flush(c->tree, &io);
685
686         check_status("Flush", status, ret);
687 }
688
689 void nb_sleep(int usec, NTSTATUS status)
690 {
691         usleep(usec);
692 }
693
694 void nb_deltree(const char *dname)
695 {
696         int total_deleted;
697
698         smb_raw_exit(c->session);
699
700         while (ftable) {
701                 struct ftable *f = ftable;
702                 DLIST_REMOVE(ftable, f);
703                 free(f);
704         }
705
706         total_deleted = smbcli_deltree(c->tree, dname);
707
708         if (total_deleted == -1) {
709                 printf("Failed to cleanup tree %s - exiting\n", dname);
710                 exit(1);
711         }
712
713         smbcli_rmdir(c->tree, dname);
714 }
715