r22579: disable progress printing in the build-farm
[sfrench/samba-autobuild/.git] / source4 / torture / raw / lockbench.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    locking benchmark
5
6    Copyright (C) Andrew Tridgell 2006
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "torture/torture.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "system/time.h"
27 #include "system/filesys.h"
28 #include "libcli/libcli.h"
29 #include "torture/util.h"
30 #include "lib/events/events.h"
31 #include "lib/cmdline/popt_common.h"
32
33 #define CHECK_STATUS(status, correct) do { \
34         if (!NT_STATUS_EQUAL(status, correct)) { \
35                 printf("(%s) Incorrect status %s - should be %s\n", \
36                        __location__, nt_errstr(status), nt_errstr(correct)); \
37                 goto failed; \
38         }} while (0)
39
40 #define BASEDIR "\\benchlock"
41 #define FNAME BASEDIR "\\lock.dat"
42
43 static int nprocs;
44 static int lock_failed;
45
46 struct benchlock_state {
47         struct smbcli_state *cli;
48         int fnum;
49         int offset;
50         int count;
51         union smb_lock io;
52         struct smb_lock_entry lock[2];
53         struct smbcli_request *req;
54 };
55
56 static void lock_completion(struct smbcli_request *);
57
58 /*
59   send the next lock request
60 */
61 static void lock_send(struct benchlock_state *state)
62 {
63         state->io.lockx.in.file.fnum = state->fnum;
64         state->io.lockx.in.ulock_cnt = 1;
65         state->lock[0].pid = state->cli->session->pid;
66         state->lock[1].pid = state->cli->session->pid;
67         state->lock[0].offset = state->offset;
68         state->lock[1].offset = (state->offset+1)%nprocs;
69         state->req = smb_raw_lock_send(state->cli->tree, &state->io);
70         if (state->req == NULL) {
71                 DEBUG(0,("Failed to setup lock\n"));
72                 lock_failed++;
73         }
74         state->req->async.private = state;
75         state->req->async.fn      = lock_completion;
76         state->offset = (state->offset+1)%nprocs;
77 }
78
79 /*
80   called when a lock completes
81 */
82 static void lock_completion(struct smbcli_request *req)
83 {
84         struct benchlock_state *state = (struct benchlock_state *)req->async.private;
85         NTSTATUS status = smbcli_request_simple_recv(req);
86         state->req = NULL;
87         if (!NT_STATUS_IS_OK(status)) {
88                 lock_failed++;
89                 DEBUG(0,("Lock failed - %s\n", nt_errstr(status)));
90         } else {
91                 state->count++;
92                 lock_send(state);
93         }
94 }
95
96 /* 
97    benchmark locking calls
98 */
99 BOOL torture_bench_lock(struct torture_context *torture)
100 {
101         BOOL ret = True;
102         TALLOC_CTX *mem_ctx = talloc_new(torture);
103         int i;
104         int timelimit = torture_setting_int(torture, "timelimit", 10);
105         struct timeval tv;
106         struct event_context *ev = event_context_find(mem_ctx);
107         struct benchlock_state *state;
108         int total = 0, loops=0, minops=0;
109         NTSTATUS status;
110         
111         nprocs = lp_parm_int(-1, "torture", "nprocs", 4);
112
113         state = talloc_zero_array(mem_ctx, struct benchlock_state, nprocs);
114
115         printf("Opening %d connections\n", nprocs);
116         for (i=0;i<nprocs;i++) {
117                 if (!torture_open_connection_ev(&state[i].cli, i, ev)) {
118                         return False;
119                 }
120                 talloc_steal(mem_ctx, state);
121         }
122
123         if (!torture_setup_dir(state[0].cli, BASEDIR)) {
124                 goto failed;
125         }
126
127         for (i=0;i<nprocs;i++) {
128                 state[i].fnum = smbcli_open(state[i].cli->tree, 
129                                             FNAME, 
130                                             O_RDWR|O_CREAT, DENY_NONE);
131                 if (state[i].fnum == -1) {
132                         printf("Failed to open %s on connection %d\n", FNAME, i);
133                         goto failed;
134                 }
135
136                 state[i].io.lockx.level = RAW_LOCK_LOCKX;
137                 state[i].io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
138                 state[i].io.lockx.in.timeout = 100000;
139                 state[i].io.lockx.in.ulock_cnt = 0;
140                 state[i].io.lockx.in.lock_cnt = 1;
141                 state[i].lock[0].count = 1;
142                 state[i].lock[1].count = 1;
143                 state[i].io.lockx.in.locks = &state[i].lock[0];
144
145                 state[i].offset = i;
146                 state[i].io.lockx.in.file.fnum = state[i].fnum;
147                 state[i].lock[0].offset = state[i].offset;
148                 state[i].lock[0].pid    = state[i].cli->session->pid;
149                 status = smb_raw_lock(state[i].cli->tree, &state[i].io);
150                 CHECK_STATUS(status, NT_STATUS_OK);
151         }
152
153         for (i=0;i<nprocs;i++) {
154                 lock_send(&state[i]);
155         }
156
157         tv = timeval_current(); 
158
159         printf("Running for %d seconds\n", timelimit);
160         while (timeval_elapsed(&tv) < timelimit) {
161                 event_loop_once(ev);
162
163                 if (lock_failed) {
164                         DEBUG(0,("locking failed\n"));
165                         goto failed;
166                 }
167
168                 if (loops++ % 10 != 0) continue;
169
170                 total = 0;
171                 for (i=0;i<nprocs;i++) {
172                         total += state[i].count;
173                 }
174
175                 if (torture_setting_bool(torture, "progress", true)) {
176                         printf("%.2f ops/second\r", total/timeval_elapsed(&tv));
177                         fflush(stdout);
178                 }
179         }
180
181         printf("%.2f ops/second\n", total/timeval_elapsed(&tv));
182         minops = state[0].count;
183         for (i=0;i<nprocs;i++) {
184                 printf("[%d] %u ops\n", i, state[i].count);
185                 if (state[i].count < minops) minops = state[i].count;
186         }
187         if (minops < 0.5*total/nprocs) {
188                 printf("Failed: unbalanced locking\n");
189                 goto failed;
190         }
191
192         for (i=0;i<nprocs;i++) {
193                 talloc_free(state[i].req);
194                 smb_raw_exit(state[i].cli->session);
195         }
196
197         smbcli_deltree(state[0].cli->tree, BASEDIR);
198         talloc_free(mem_ctx);
199         return ret;
200
201 failed:
202         for (i=0;i<nprocs;i++) {
203                 talloc_free(state[i].req);
204                 smb_raw_exit(state[i].cli->session);
205         }
206         smbcli_deltree(state[0].cli->tree, BASEDIR);
207         talloc_free(mem_ctx);
208         return False;
209 }