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