s4-torture: ran minimal_includes.pl over source4/torture
[Samba/ekacnet.git] / source4 / torture / raw / pingpong.c
blobf620c4854df9d2a03cbb22fbd5e33c0949c86767
1 /*
2 Unix SMB/CIFS implementation.
4 ping pong test
6 Copyright (C) Ronnie Sahlberg 2007
8 Significantly based on and borrowed from lockbench.c by
9 Copyright (C) Andrew Tridgell 2006
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.
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.
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/>.
26 filename is specified by
27 --option=torture:filename=...
29 number of locks is specified by
30 --option=torture:num_locks=...
32 locktimeout is specified in ms by
33 --option=torture:locktimeout=...
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.
39 reading from the file can be enabled with
40 --option=torture:read=true
42 writing to the file can be enabled with
43 --option=torture:write=true
46 #include "includes.h"
47 #include "system/time.h"
48 #include "system/filesys.h"
49 #include "libcli/libcli.h"
50 #include "torture/util.h"
52 static void lock_byte(struct smbcli_state *cli, int fd, int offset, int lock_timeout)
54 union smb_lock io;
55 struct smb_lock_entry lock;
56 NTSTATUS status;
58 try_again:
59 ZERO_STRUCT(lock);
60 io.lockx.in.ulock_cnt = 0;
61 io.lockx.in.lock_cnt = 1;
63 lock.count = 1;
64 lock.offset = offset;
65 lock.pid = cli->tree->session->pid;
66 io.lockx.level = RAW_LOCK_LOCKX;
67 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
68 io.lockx.in.timeout = lock_timeout;
69 io.lockx.in.locks = &lock;
70 io.lockx.in.file.fnum = fd;
72 status = smb_raw_lock(cli->tree, &io);
74 /* If we dont use timeouts and we got file lock conflict
75 just try the lock again.
77 if (lock_timeout==0) {
78 if ( (NT_STATUS_EQUAL(NT_STATUS_FILE_LOCK_CONFLICT, status))
79 ||(NT_STATUS_EQUAL(NT_STATUS_LOCK_NOT_GRANTED, status)) ) {
80 goto try_again;
84 if (!NT_STATUS_IS_OK(status)) {
85 DEBUG(0,("Lock failed\n"));
86 exit(1);
90 static void unlock_byte(struct smbcli_state *cli, int fd, int offset)
92 union smb_lock io;
93 struct smb_lock_entry lock;
94 NTSTATUS status;
96 ZERO_STRUCT(lock);
97 io.lockx.in.ulock_cnt = 1;
98 io.lockx.in.lock_cnt = 0;
100 lock.count = 1;
101 lock.offset = offset;
102 lock.pid = cli->tree->session->pid;
103 io.lockx.level = RAW_LOCK_LOCKX;
104 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
105 io.lockx.in.timeout = 100000;
106 io.lockx.in.locks = &lock;
107 io.lockx.in.file.fnum = fd;
109 status = smb_raw_lock(cli->tree, &io);
111 if (!NT_STATUS_IS_OK(status)) {
112 DEBUG(0,("Unlock failed\n"));
113 exit(1);
117 static void write_byte(struct smbcli_state *cli, int fd, uint8_t c, int offset)
119 union smb_write io;
120 NTSTATUS status;
122 io.generic.level = RAW_WRITE_WRITEX;
123 io.writex.in.file.fnum = fd;
124 io.writex.in.offset = offset;
125 io.writex.in.wmode = 0;
126 io.writex.in.remaining = 0;
127 io.writex.in.count = 1;
128 io.writex.in.data = &c;
130 status = smb_raw_write(cli->tree, &io);
131 if (!NT_STATUS_IS_OK(status)) {
132 printf("write failed\n");
133 exit(1);
137 static void read_byte(struct smbcli_state *cli, int fd, uint8_t *c, int offset)
139 union smb_read io;
140 NTSTATUS status;
142 io.generic.level = RAW_READ_READX;
143 io.readx.in.file.fnum = fd;
144 io.readx.in.mincnt = 1;
145 io.readx.in.maxcnt = 1;
146 io.readx.in.offset = offset;
147 io.readx.in.remaining = 0;
148 io.readx.in.read_for_execute = false;
149 io.readx.out.data = c;
151 status = smb_raw_read(cli->tree, &io);
152 if (!NT_STATUS_IS_OK(status)) {
153 printf("read failed\n");
154 exit(1);
159 static struct timeval tp1, tp2;
161 static void start_timer(void)
163 gettimeofday(&tp1, NULL);
166 static double end_timer(void)
168 gettimeofday(&tp2, NULL);
169 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
170 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
174 ping pong
176 bool torture_ping_pong(struct torture_context *torture)
178 const char *fn;
179 int num_locks;
180 TALLOC_CTX *mem_ctx = talloc_new(torture);
181 static bool do_reads;
182 static bool do_writes;
183 int lock_timeout;
184 int fd;
185 struct smbcli_state *cli;
186 int i;
187 uint8_t incr=0, last_incr=0;
188 uint8_t *val;
189 int count, loops;
191 fn = torture_setting_string(torture, "filename", NULL);
192 if (fn == NULL) {
193 DEBUG(0,("You must specify the filename using --option=torture:filename=...\n"));
194 return false;
197 num_locks = torture_setting_int(torture, "num_locks", -1);
198 if (num_locks == -1) {
199 DEBUG(0,("You must specify num_locks using --option=torture:num_locks=...\n"));
200 return false;
203 do_reads = torture_setting_bool(torture, "read", false);
204 do_writes = torture_setting_bool(torture, "write", false);
205 lock_timeout = torture_setting_int(torture, "lock_timeout", 100000);
207 if (!torture_open_connection(&cli, torture, 0)) {
208 DEBUG(0,("Could not open connection\n"));
209 return false;
212 fd = smbcli_open(cli->tree, fn, O_RDWR|O_CREAT, DENY_NONE);
213 if (fd == -1) {
214 printf("Failed to open %s\n", fn);
215 exit(1);
218 write_byte(cli, fd, 0, num_locks);
219 lock_byte(cli, fd, 0, lock_timeout);
222 start_timer();
223 val = talloc_zero_array(mem_ctx, uint8_t, num_locks);
224 i = 0;
225 count = 0;
226 loops = 0;
227 while (1) {
228 lock_byte(cli, fd, (i+1)%num_locks, lock_timeout);
230 if (do_reads) {
231 uint8_t c;
232 read_byte(cli, fd, &c, i);
233 incr = c-val[i];
234 val[i] = c;
237 if (do_writes) {
238 uint8_t c = val[i] + 1;
239 write_byte(cli, fd, c, i);
242 unlock_byte(cli, fd, i);
244 i = (i+1)%num_locks;
245 count++;
246 if (loops>num_locks && incr!=last_incr) {
247 last_incr = incr;
248 printf("data increment = %u\n", incr);
249 fflush(stdout);
251 if (end_timer() > 1.0) {
252 printf("%8u locks/sec\r",
253 (unsigned)(2*count/end_timer()));
254 fflush(stdout);
255 start_timer();
256 count=0;
258 loops++;
261 talloc_free(mem_ctx);
262 return true;