r24872: Use torture API a bit more
[kai/samba.git] / source4 / torture / raw / pingpong.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    ping pong test
5
6    Copyright (C) Ronnie Sahlberg 2007
7
8    Significantly based on and borrowed from lockbench.c by
9    Copyright (C) Andrew Tridgell 2006
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 /*
26    filename is specified by
27          --option=torture:filename=...
28
29    number of locks is specified by
30          --option=torture:num_locks=...
31
32    locktimeout is specified in ms by
33          --option=torture:locktimeout=...
34
35        default is 100 seconds
36        if set to 0 pingpong will instead loop trying the lock operation
37        over and over until it completes.
38
39    reading from the file can be enabled with
40          --option=torture:read=True
41
42    writing to the file can be enabled with
43          --option=torture:write=True
44
45 */
46 #include "includes.h"
47 #include "torture/torture.h"
48 #include "libcli/raw/libcliraw.h"
49 #include "system/time.h"
50 #include "system/filesys.h"
51 #include "libcli/libcli.h"
52 #include "torture/util.h"
53
54
55
56
57 void lock_byte(struct smbcli_state *cli, int fd, int offset, int lock_timeout)
58 {
59         union smb_lock io;
60         struct smb_lock_entry lock;
61         NTSTATUS status;
62
63 try_again:
64         ZERO_STRUCT(lock);
65         io.lockx.in.ulock_cnt = 0;
66         io.lockx.in.lock_cnt = 1;
67
68         lock.count = 1;
69         lock.offset = offset;
70         lock.pid = cli->tree->session->pid;
71         io.lockx.level = RAW_LOCK_LOCKX;
72         io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
73         io.lockx.in.timeout = lock_timeout;
74         io.lockx.in.locks = &lock;
75         io.lockx.in.file.fnum = fd;
76
77         status = smb_raw_lock(cli->tree, &io);
78
79         /* If we dont use timeouts and we got file lock conflict
80            just try the lock again.
81         */
82         if (lock_timeout==0) {
83                 if ( (NT_STATUS_EQUAL(NT_STATUS_FILE_LOCK_CONFLICT, status))
84                    ||(NT_STATUS_EQUAL(NT_STATUS_LOCK_NOT_GRANTED, status)) ) {
85                         goto try_again;
86                 }
87         }
88
89         if (!NT_STATUS_IS_OK(status)) {
90                 DEBUG(0,("Lock failed\n"));
91                 exit(1);
92         }
93 }
94
95 void unlock_byte(struct smbcli_state *cli, int fd, int offset)
96 {
97         union smb_lock io;
98         struct smb_lock_entry lock;
99         NTSTATUS status;
100
101         ZERO_STRUCT(lock);
102         io.lockx.in.ulock_cnt = 1;
103         io.lockx.in.lock_cnt = 0;
104
105         lock.count = 1;
106         lock.offset = offset;
107         lock.pid = cli->tree->session->pid;
108         io.lockx.level = RAW_LOCK_LOCKX;
109         io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
110         io.lockx.in.timeout = 100000;
111         io.lockx.in.locks = &lock;
112         io.lockx.in.file.fnum = fd;
113
114         status = smb_raw_lock(cli->tree, &io);
115
116         if (!NT_STATUS_IS_OK(status)) {
117                 DEBUG(0,("Unlock failed\n"));
118                 exit(1);
119         }
120 }
121
122 void write_byte(struct smbcli_state *cli, int fd, uint8_t c, int offset)
123 {
124         union smb_write io;
125         NTSTATUS status;
126
127         io.generic.level = RAW_WRITE_WRITEX;
128         io.writex.in.file.fnum = fd;
129         io.writex.in.offset = offset;
130         io.writex.in.wmode = 0;
131         io.writex.in.remaining = 0;
132         io.writex.in.count = 1;
133         io.writex.in.data = &c;
134
135         status = smb_raw_write(cli->tree, &io);
136         if (!NT_STATUS_IS_OK(status)) {
137                 printf("write failed\n");
138                 exit(1);
139         }
140 }       
141
142 void read_byte(struct smbcli_state *cli, int fd, uint8_t *c, int offset)
143 {
144         union smb_read io;
145         NTSTATUS status;
146
147         io.generic.level = RAW_READ_READX;
148         io.readx.in.file.fnum = fd;
149         io.readx.in.mincnt = 1;
150         io.readx.in.maxcnt = 1;
151         io.readx.in.offset = offset;
152         io.readx.in.remaining = 0;
153         io.readx.in.read_for_execute = False;
154         io.readx.out.data = c;
155
156         status = smb_raw_read(cli->tree, &io);
157         if (!NT_STATUS_IS_OK(status)) {
158                 printf("read failed\n");
159                 exit(1);
160         }
161 }       
162
163
164 static struct timeval tp1, tp2;
165
166 static void start_timer(void)
167 {
168         gettimeofday(&tp1, NULL);
169 }
170
171 static double end_timer(void)
172 {
173         gettimeofday(&tp2, NULL);
174         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
175                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
176 }
177
178 /* 
179    ping pong
180 */
181 BOOL torture_ping_pong(struct torture_context *torture)
182 {
183         const char *fn;
184         int num_locks;
185         TALLOC_CTX *mem_ctx = talloc_new(torture);
186         static BOOL do_reads;
187         static BOOL do_writes;
188         int lock_timeout;
189         int fd;
190         struct smbcli_state *cli;
191         int i;
192         uint8_t incr=0, last_incr=0;
193         uint8_t *val;
194         int count, loops;
195
196         fn = lp_parm_string(-1, "torture", "filename");
197         if (fn == NULL) {
198                 DEBUG(0,("You must specify the filename using --option=torture:filename=...\n"));
199                 return false;
200         }
201
202         num_locks = lp_parm_int(-1, "torture", "num_locks", -1);
203         if (num_locks == -1) {
204                 DEBUG(0,("You must specify num_locks using --option=torture:num_locks=...\n"));
205                 return false;
206         }
207
208         do_reads     = lp_parm_bool(-1, "torture", "read", False);
209         do_writes    = lp_parm_bool(-1, "torture", "write", False);
210         lock_timeout =  lp_parm_int(-1, "torture", "lock_timeout", 100000);
211
212         if (!torture_open_connection(&cli, 0)) {
213                 DEBUG(0,("Could not open connection\n"));
214                 return false;
215         }
216
217         fd = smbcli_open(cli->tree, fn, O_RDWR|O_CREAT, DENY_NONE);
218         if (fd == -1) {
219                 printf("Failed to open %s\n", fn);
220                 exit(1);
221         }
222
223         write_byte(cli, fd, 0, num_locks);
224         lock_byte(cli, fd, 0, lock_timeout);
225
226
227         start_timer();
228         val = talloc_zero_array(mem_ctx, uint8_t, num_locks);
229         i = 0;
230         count = 0;
231         loops = 0;
232         while (1) {
233                 lock_byte(cli, fd, (i+1)%num_locks, lock_timeout);
234
235                 if (do_reads) {
236                         uint8_t c;
237                         read_byte(cli, fd, &c, i);
238                         incr   = c-val[i];
239                         val[i] = c;                     
240                 }
241
242                 if (do_writes) {
243                         uint8_t c = val[i] + 1;
244                         write_byte(cli, fd, c, i);
245                 }
246
247                 unlock_byte(cli, fd, i);
248
249                 i = (i+1)%num_locks;
250                 count++;
251                 if (loops>num_locks && incr!=last_incr) {
252                         last_incr = incr;
253                         printf("data increment = %u\n", incr);
254                         fflush(stdout);
255                 }
256                 if (end_timer() > 1.0) {
257                         printf("%8u locks/sec\r", 
258                                (unsigned)(2*count/end_timer()));
259                         fflush(stdout);
260                         start_timer();
261                         count=0;
262                 }
263                 loops++;
264         }
265
266         talloc_free(mem_ctx);
267         return true;
268 }
269