added latency reporting
[ira/wip.git] / source / torture / raw / offline.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2008
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21   test offline files
22  */
23
24 #include "includes.h"
25 #include "torture/torture.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "system/time.h"
28 #include "system/filesys.h"
29 #include "libcli/libcli.h"
30 #include "torture/util.h"
31 #include "lib/events/events.h"
32 #include "lib/cmdline/popt_common.h"
33 #include "libcli/composite/composite.h"
34 #include "libcli/smb_composite/smb_composite.h"
35 #include "libcli/resolve/resolve.h"
36 #include "param/param.h"
37
38 #define BASEDIR "\\testoffline"
39
40 static int nconnections;
41 static int numstates;
42 static int num_connected;
43 static int test_failed;
44 extern int torture_numops;
45 static bool test_finished;
46
47 enum offline_op {OP_LOADFILE, OP_SAVEFILE, OP_SETOFFLINE, OP_GETOFFLINE, OP_ENDOFLIST};
48
49 static double latencies[OP_ENDOFLIST];
50 static double worst_latencies[OP_ENDOFLIST];
51
52 #define FILE_SIZE 8192
53
54
55 struct offline_state {
56         struct torture_context *tctx;
57         struct event_context *ev;
58         struct smbcli_tree *tree;
59         TALLOC_CTX *mem_ctx;
60         int client;
61         int fnum;
62         uint32_t count;
63         uint32_t lastcount;
64         uint32_t fnumber;
65         uint32_t offline_count;
66         uint32_t online_count;
67         char *fname;
68         struct smb_composite_loadfile *loadfile;
69         struct smb_composite_savefile *savefile;
70         struct smbcli_request *req;
71         enum offline_op op;
72         struct timeval tv_start;
73 };
74
75 static void test_offline(struct offline_state *state);
76
77
78 static char *filename(TALLOC_CTX *ctx, int i)
79 {
80         char *s = talloc_asprintf(ctx, BASEDIR "\\file%u.dat", i);
81         return s;
82 }
83
84
85 /*
86   called when a loadfile completes
87  */
88 static void loadfile_callback(struct composite_context *ctx) 
89 {
90         struct offline_state *state = ctx->async.private_data;
91         NTSTATUS status;
92         int i;
93
94         status = smb_composite_loadfile_recv(ctx, state->mem_ctx);
95         if (!NT_STATUS_IS_OK(status)) {
96                 printf("Failed to read file '%s' - %s\n", 
97                        state->loadfile->in.fname, nt_errstr(status));
98                 test_failed++;
99         }
100
101         /* check the data is correct */
102         if (state->loadfile->out.size != FILE_SIZE) {
103                 printf("Wrong file size %u - expected %u\n", 
104                        state->loadfile->out.size, FILE_SIZE);
105                 test_failed++;
106                 return;
107         }
108
109         for (i=0;i<FILE_SIZE;i++) {
110                 if (state->loadfile->out.data[i] != state->fnumber % 256) {
111                         printf("Bad data in file %u\n", state->fnumber);
112                         test_failed++;
113                         return;
114                 }
115         }
116         
117         talloc_steal(state->loadfile, state->loadfile->out.data);
118
119         state->count++;
120         talloc_free(state->loadfile);
121         state->loadfile = NULL;
122
123         if (!test_finished) {
124                 test_offline(state);
125         }
126 }
127
128
129 /*
130   called when a savefile completes
131  */
132 static void savefile_callback(struct composite_context *ctx) 
133 {
134         struct offline_state *state = ctx->async.private_data;
135         NTSTATUS status;
136
137         status = smb_composite_savefile_recv(ctx);
138         if (!NT_STATUS_IS_OK(status)) {
139                 printf("Failed to save file '%s' - %s\n", 
140                        state->savefile->in.fname, nt_errstr(status));
141                 test_failed++;
142         }
143
144         state->count++;
145         talloc_free(state->savefile);
146         state->savefile = NULL;
147
148         if (!test_finished) {
149                 test_offline(state);
150         }
151 }
152
153
154 /*
155   called when a setoffline completes
156  */
157 static void setoffline_callback(struct smbcli_request *req) 
158 {
159         struct offline_state *state = req->async.private;
160         NTSTATUS status;
161
162         status = smbcli_request_simple_recv(req);
163         if (!NT_STATUS_IS_OK(status)) {
164                 printf("Failed to set offline file '%s' - %s\n", 
165                        state->fname, nt_errstr(status));
166                 test_failed++;
167         }
168
169         state->req = NULL;
170         state->count++;
171
172         if (!test_finished) {
173                 test_offline(state);
174         }
175 }
176
177
178 /*
179   called when a getoffline completes
180  */
181 static void getoffline_callback(struct smbcli_request *req) 
182 {
183         struct offline_state *state = req->async.private;
184         NTSTATUS status;
185         union smb_fileinfo io;
186
187         io.getattr.level = RAW_FILEINFO_GETATTR;
188         
189         status = smb_raw_pathinfo_recv(req, state->mem_ctx, &io);
190         if (!NT_STATUS_IS_OK(status)) {
191                 printf("Failed to get offline file '%s' - %s\n", 
192                        state->fname, nt_errstr(status));
193                 test_failed++;
194         }
195
196         if (io.getattr.out.attrib & FILE_ATTRIBUTE_OFFLINE) {
197                 state->offline_count++;
198         } else {
199                 state->online_count++;
200         }
201
202         state->req = NULL;
203         state->count++;
204
205         if (!test_finished) {
206                 test_offline(state);
207         }
208 }
209
210
211 /*
212   send the next offline file fetch request
213 */
214 static void test_offline(struct offline_state *state)
215 {
216         struct composite_context *ctx;
217         double lat;
218
219         lat = timeval_elapsed(&state->tv_start);
220         if (latencies[state->op] < lat) {
221                 latencies[state->op] = lat;
222         }
223
224         state->op = (enum offline_op) (random() % OP_ENDOFLIST);
225         
226         state->fnumber = random() % torture_numops;
227         talloc_free(state->fname);
228         state->fname = filename(state->mem_ctx, state->fnumber);
229
230         state->tv_start = timeval_current();
231
232         switch (state->op) {
233         case OP_LOADFILE:
234                 state->loadfile = talloc_zero(state->mem_ctx, struct smb_composite_loadfile);
235                 state->loadfile->in.fname = state->fname;
236         
237                 ctx = smb_composite_loadfile_send(state->tree, state->loadfile);
238                 if (ctx == NULL) {
239                         printf("Failed to setup loadfile for %s\n", state->fname);
240                         test_failed = true;
241                 }
242
243                 talloc_steal(state->loadfile, ctx);
244
245                 ctx->async.fn = loadfile_callback;
246                 ctx->async.private_data = state;
247                 break;
248
249         case OP_SAVEFILE:
250                 state->savefile = talloc_zero(state->mem_ctx, struct smb_composite_savefile);
251
252                 state->savefile->in.fname = state->fname;
253                 state->savefile->in.data  = talloc_size(state->savefile, FILE_SIZE);
254                 state->savefile->in.size  = FILE_SIZE;
255                 memset(state->savefile->in.data, state->fnumber, FILE_SIZE);
256         
257                 ctx = smb_composite_savefile_send(state->tree, state->savefile);
258                 if (ctx == NULL) {
259                         printf("Failed to setup savefile for %s\n", state->fname);
260                         test_failed = true;
261                 }
262
263                 talloc_steal(state->savefile, ctx);
264
265                 ctx->async.fn = savefile_callback;
266                 ctx->async.private_data = state;
267                 break;
268
269         case OP_SETOFFLINE: {
270                 union smb_setfileinfo io;
271                 ZERO_STRUCT(io);
272                 io.setattr.level = RAW_SFILEINFO_SETATTR;
273                 io.setattr.in.attrib = FILE_ATTRIBUTE_OFFLINE;
274                 io.setattr.in.file.path = state->fname;
275                 /* make the file 1 hour old, to get past mininum age restrictions 
276                    for HSM systems */
277                 io.setattr.in.write_time = time(NULL) - 60*60;
278
279                 state->req = smb_raw_setpathinfo_send(state->tree, &io);
280                 if (state->req == NULL) {
281                         printf("Failed to setup setoffline for %s\n", state->fname);
282                         test_failed = true;
283                 }
284                 
285                 state->req->async.fn = setoffline_callback;
286                 state->req->async.private = state;
287                 break;
288         }
289
290         case OP_GETOFFLINE: {
291                 union smb_fileinfo io;
292                 ZERO_STRUCT(io);
293                 io.getattr.level = RAW_FILEINFO_GETATTR;
294                 io.getattr.in.file.path = state->fname;
295
296                 state->req = smb_raw_pathinfo_send(state->tree, &io);
297                 if (state->req == NULL) {
298                         printf("Failed to setup getoffline for %s\n", state->fname);
299                         test_failed = true;
300                 }
301                 
302                 state->req->async.fn = getoffline_callback;
303                 state->req->async.private = state;
304                 break;
305         }
306
307         default:
308                 printf("bad operation??\n");
309                 break;
310         }
311 }
312
313
314
315
316 static void echo_completion(struct smbcli_request *req)
317 {
318         struct offline_state *state = (struct offline_state *)req->async.private;
319         NTSTATUS status = smbcli_request_simple_recv(req);
320         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) ||
321             NT_STATUS_EQUAL(status, NT_STATUS_LOCAL_DISCONNECT)) {
322                 talloc_free(state->tree);
323                 state->tree = NULL;
324                 num_connected--;        
325                 DEBUG(0,("lost connection\n"));
326                 test_failed++;
327         }
328 }
329
330 static void report_rate(struct event_context *ev, struct timed_event *te, 
331                         struct timeval t, void *private_data)
332 {
333         struct offline_state *state = talloc_get_type(private_data, 
334                                                         struct offline_state);
335         int i;
336         uint32_t total=0, total_offline=0, total_online=0;
337         for (i=0;i<numstates;i++) {
338                 total += state[i].count - state[i].lastcount;
339                 if (timeval_elapsed(&state[i].tv_start) > latencies[state[i].op]) {
340                         latencies[state[i].op] = timeval_elapsed(&state[i].tv_start);
341                 }
342                 state[i].lastcount = state[i].count;            
343                 total_online += state[i].online_count;
344                 total_offline += state[i].offline_count;
345         }
346         printf("ops/s=%4u  offline=%5u online=%4u  set_lat=%.1f get_lat=%.1f save_lat=%.1f load_lat=%.1f\r",
347                total, total_offline, total_online,
348                latencies[OP_SETOFFLINE],
349                latencies[OP_GETOFFLINE],
350                latencies[OP_SAVEFILE],
351                latencies[OP_LOADFILE]);
352         fflush(stdout);
353         event_add_timed(ev, state, timeval_current_ofs(1, 0), report_rate, state);
354
355         for (i=0;i<OP_ENDOFLIST;i++) {
356                 if (latencies[i] > worst_latencies[i]) {
357                         worst_latencies[i] = latencies[i];
358                 }
359                 latencies[i] = 0;
360         }
361
362         /* send an echo on each interface to ensure it stays alive - this helps
363            with IP takeover */
364         for (i=0;i<numstates;i++) {
365                 struct smb_echo p;
366                 struct smbcli_request *req;
367
368                 if (!state[i].tree) {
369                         continue;
370                 }
371
372                 p.in.repeat_count = 1;
373                 p.in.size = 0;
374                 p.in.data = NULL;
375                 req = smb_raw_echo_send(state[i].tree->session->transport, &p);
376                 req->async.private = &state[i];
377                 req->async.fn      = echo_completion;
378         }
379 }
380
381 /* 
382    test offline file handling
383 */
384 bool torture_test_offline(struct torture_context *torture)
385 {
386         bool ret = true;
387         TALLOC_CTX *mem_ctx = talloc_new(torture);
388         int i;
389         int timelimit = torture_setting_int(torture, "timelimit", 10);
390         struct timeval tv;
391         struct event_context *ev = event_context_find(mem_ctx);
392         struct offline_state *state;
393         struct smbcli_state *cli;
394         bool progress;
395         progress = torture_setting_bool(torture, "progress", true);
396
397         nconnections = torture_setting_int(torture, "nprocs", 4);
398         numstates = nconnections * 5;
399
400         state = talloc_zero_array(mem_ctx, struct offline_state, numstates);
401
402         printf("Opening %d connections with %d simultaneous operations and %u files\n", nconnections, numstates, torture_numops);
403         for (i=0;i<nconnections;i++) {
404                 state[i].tctx = torture;
405                 state[i].mem_ctx = talloc_new(state);
406                 state[i].ev = ev;
407                 if (!torture_open_connection_ev(&cli, i, torture, ev)) {
408                         return false;
409                 }
410                 state[i].tree = cli->tree;
411                 state[i].client = i;
412                 /* allow more time for offline files */
413                 state[i].tree->session->transport->options.request_timeout = 200;
414         }
415
416         /* the others are repeats on the earlier connections */
417         for (i=nconnections;i<numstates;i++) {
418                 state[i].tctx = torture;
419                 state[i].mem_ctx = talloc_new(state);
420                 state[i].ev = ev;
421                 state[i].tree = state[i % nconnections].tree;
422                 state[i].client = i;
423         }
424
425         num_connected = i;
426
427         if (!torture_setup_dir(cli, BASEDIR)) {
428                 goto failed;
429         }
430
431         /* pre-create files */
432         printf("Pre-creating %u files ....\n", torture_numops);
433         for (i=0;i<torture_numops;i++) {
434                 int fnum;
435                 char *fname = filename(mem_ctx, i);
436                 char buf[FILE_SIZE];
437                 NTSTATUS status;
438
439                 memset(buf, i % 256, sizeof(buf));
440
441                 fnum = smbcli_open(state[0].tree, fname, O_RDWR|O_CREAT, DENY_NONE);
442                 if (fnum == -1) {
443                         printf("Failed to open %s on connection %d\n", fname, i);
444                         goto failed;
445                 }
446
447                 if (smbcli_write(state[0].tree, fnum, 0, buf, 0, sizeof(buf)) != sizeof(buf)) {
448                         printf("Failed to write file of size %u\n", FILE_SIZE);
449                         goto failed;
450                 }
451
452                 status = smbcli_close(state[0].tree, fnum);
453                 if (!NT_STATUS_IS_OK(status)) {
454                         printf("Close failed - %s\n", nt_errstr(status));
455                         goto failed;
456                 }
457
458                 talloc_free(fname);
459         }
460
461         /* start the async ops */
462         for (i=0;i<numstates;i++) {
463                 state[i].tv_start = timeval_current();
464                 test_offline(&state[i]);
465         }
466
467         tv = timeval_current(); 
468
469         if (progress) {
470                 event_add_timed(ev, state, timeval_current_ofs(1, 0), report_rate, state);
471         }
472
473         printf("Running for %d seconds\n", timelimit);
474         while (timeval_elapsed(&tv) < timelimit) {
475                 event_loop_once(ev);
476
477                 if (test_failed) {
478                         DEBUG(0,("test failed\n"));
479                         goto failed;
480                 }
481         }
482
483         printf("\nWaiting for completion\n");
484         test_finished = true;
485         for (i=0;i<numstates;i++) {
486                 while (state[i].loadfile || 
487                        state[i].savefile ||
488                        state[i].req) {
489                         event_loop_once(ev);
490                 }
491         }       
492
493         printf("worst latencies: set_lat=%.1f get_lat=%.1f save_lat=%.1f load_lat=%.1f\n",
494                worst_latencies[OP_SETOFFLINE],
495                worst_latencies[OP_GETOFFLINE],
496                worst_latencies[OP_SAVEFILE],
497                worst_latencies[OP_LOADFILE]);
498
499         smbcli_deltree(state[0].tree, BASEDIR);
500         talloc_free(mem_ctx);
501         printf("\n");
502         return ret;
503
504 failed:
505         talloc_free(mem_ctx);
506         return false;
507 }