rewrote smbtorture to use the new dbench 2 format and methods
[kai/samba.git] / source / torture / nbio.c
1 #define NBDEBUG 0
2
3 /* 
4    Unix SMB/CIFS implementation.
5    SMB torture tester
6    Copyright (C) Andrew Tridgell 1997-1998
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 #define NO_SYSLOG
24
25 #include "includes.h"
26
27 #define MAX_FILES 1000
28
29 static char buf[70000];
30 extern int line_count;
31 extern int nbio_id;
32 static int nprocs;
33
34 static struct {
35         int fd;
36         int handle;
37 } ftable[MAX_FILES];
38
39 static struct {
40         double bytes_in, bytes_out;
41         int line;
42 } *children;
43
44 double nbio_total(void)
45 {
46         int i;
47         double total = 0;
48         for (i=0;i<nprocs;i++) {
49                 total += children[i].bytes_out + children[i].bytes_in;
50         }
51         return total;
52 }
53
54 void nb_alarm(void)
55 {
56         int i;
57         int lines=0;
58         if (nbio_id != -1) return;
59
60         for (i=0;i<nprocs;i++) {
61                 lines += children[i].line;
62         }
63
64         printf("%8d  %.2f MB/sec\r", lines/nprocs, 1.0e-6 * nbio_total() / end_timer());
65
66         signal(SIGALRM, nb_alarm);
67         alarm(1);       
68 }
69
70 void nbio_shmem(int n)
71 {
72         nprocs = n;
73         children = shm_setup(sizeof(*children) * nprocs);
74         if (!children) {
75                 printf("Failed to setup shared memory!\n");
76                 exit(1);
77         }
78 }
79
80 static int find_handle(int handle)
81 {
82         int i;
83         children[nbio_id].line = line_count;
84         for (i=0;i<MAX_FILES;i++) {
85                 if (ftable[i].handle == handle) return i;
86         }
87         printf("ERROR: handle %d was not found\n", 
88                line_count, handle);
89         exit(1);
90 }
91
92
93 static struct cli_state *c;
94
95 static void sigsegv(int sig)
96 {
97         char line[200];
98         printf("segv at line %d\n", line_count);
99         slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d", 
100                 (int)getpid(), (int)getpid());
101         system(line);
102         exit(1);
103 }
104
105 void nb_setup(struct cli_state *cli)
106 {
107         signal(SIGSEGV, sigsegv);
108         c = cli;
109         start_timer();
110 }
111
112
113 void nb_unlink(char *fname)
114 {
115         if (!cli_unlink(c, fname)) {
116 #if NBDEBUG
117                 printf("(%d) unlink %s failed (%s)\n", 
118                        line_count, fname, cli_errstr(c));
119 #endif
120         }
121 }
122
123
124 void nb_createx(char *fname, 
125                 unsigned create_options, unsigned create_disposition, int handle)
126 {
127         int fd, i;
128         static int count;
129
130         fd = cli_nt_create_full(c, fname, 
131                                 FILE_READ_DATA | FILE_WRITE_DATA,
132                                 0,
133                                 FILE_SHARE_READ|FILE_SHARE_WRITE, 
134                                 create_disposition, 
135                                 create_options);
136         if (fd == -1 && handle != -1) {
137                 printf("ERROR: cli_nt_create_full failed for %s - %s\n",
138                        fname, cli_errstr(c));
139                 exit(1);
140         }
141         if (fd != -1 && handle == -1) {
142                 printf("ERROR: cli_nt_create_full succeeded for %s\n", fname);
143                 exit(1);
144         }
145         if (fd == -1) return;
146
147         for (i=0;i<MAX_FILES;i++) {
148                 if (ftable[i].handle == 0) break;
149         }
150         if (i == MAX_FILES) {
151                 printf("(%d) file table full for %s\n", line_count, 
152                        fname);
153                 exit(1);
154         }
155         ftable[i].handle = handle;
156         ftable[i].fd = fd;
157 }
158
159 void nb_writex(int handle, int offset, int size, int ret_size)
160 {
161         int i;
162
163         if (buf[0] == 0) memset(buf, 1, sizeof(buf));
164
165         i = find_handle(handle);
166         if (cli_write(c, ftable[i].fd, 0, buf, offset, size) != ret_size) {
167                 printf("(%d) ERROR: write failed on handle %d, fd %d \
168 errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
169                 exit(1);
170         }
171
172         children[nbio_id].bytes_out += ret_size;
173 }
174
175 void nb_readx(int handle, int offset, int size, int ret_size)
176 {
177         int i, ret;
178
179         i = find_handle(handle);
180         if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != ret_size) {
181                 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
182                         line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
183                 exit(1);
184         }
185         children[nbio_id].bytes_in += ret_size;
186 }
187
188 void nb_close(int handle)
189 {
190         int i;
191         i = find_handle(handle);
192         if (!cli_close(c, ftable[i].fd)) {
193                 printf("(%d) close failed on handle %d\n", line_count, handle);
194                 exit(1);
195         }
196         ftable[i].handle = 0;
197 }
198
199 void nb_rmdir(char *fname)
200 {
201         if (!cli_rmdir(c, fname)) {
202                 printf("ERROR: rmdir %s failed (%s)\n", 
203                        fname, cli_errstr(c));
204                 exit(1);
205         }
206 }
207
208 void nb_rename(char *old, char *new)
209 {
210         if (!cli_rename(c, old, new)) {
211                 printf("ERROR: rename %s %s failed (%s)\n", 
212                        old, new, cli_errstr(c));
213                 exit(1);
214         }
215 }
216
217
218 void nb_qpathinfo(char *fname)
219 {
220         cli_qpathinfo(c, fname, NULL, NULL, NULL, NULL, NULL);
221 }
222
223 void nb_qfileinfo(int fnum)
224 {
225         int i;
226         i = find_handle(fnum);
227         cli_qfileinfo(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
228 }
229
230 void nb_qfsinfo(int level)
231 {
232         int bsize, total, avail;
233         /* this is not the right call - we need cli_qfsinfo() */
234         cli_dskattr(c, &bsize, &total, &avail);
235 }
236
237 static void find_fn(file_info *finfo, const char *name, void *state)
238 {
239         /* noop */
240 }
241
242 void nb_findfirst(char *mask)
243 {
244         cli_list(c, mask, 0, find_fn, NULL);
245 }
246
247 void nb_flush(int fnum)
248 {
249         int i;
250         i = find_handle(fnum);
251         /* hmmm, we don't have cli_flush() yet */
252 }
253
254 static int total_deleted;
255
256 static void delete_fn(file_info *finfo, const char *name, void *state)
257 {
258         char *s, *n;
259         if (finfo->name[0] == '.') return;
260
261         n = strdup(name);
262         n[strlen(n)-1] = 0;
263         asprintf(&s, "%s%s", n, finfo->name);
264         if (finfo->mode & aDIR) {
265                 nb_deltree(s);
266         } else {
267                 total_deleted++;
268                 nb_unlink(s);
269         }
270         free(s);
271         free(n);
272 }
273
274 void nb_deltree(char *dname)
275 {
276         char *mask;
277         asprintf(&mask, "%s\\*", dname);
278
279         total_deleted = 0;
280
281         cli_list(c, mask, aDIR, delete_fn, NULL);
282         free(mask);
283         nb_rmdir(dname);
284
285         if (total_deleted) printf("WARNING: Cleaned up %d files\n", total_deleted);
286 }