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