s3-includes: only include system/filesys.h when needed.
[metze/samba/wip.git] / source3 / utils / dbwrap_torture.c
1 /*
2    Samba Linux/Unix CIFS implementation
3
4    simple tool to test persistent databases
5
6    Copyright (C) Michael Adam     2009
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "popt_common.h"
25 #include "dbwrap.h"
26
27 #if 0
28 #include "lib/events/events.h"
29 #include "system/filesys.h"
30 #include "popt.h"
31 #include "cmdline.h"
32
33 #include <sys/time.h>
34 #include <time.h>
35 #endif
36
37 #define DEFAULT_DB_NAME "transaction.tdb"
38
39 static int timelimit = 10;
40 static int torture_delay = 0;
41 static int verbose = 0;
42 static int no_trans = 0;
43 static char *db_name = (char *)discard_const(DEFAULT_DB_NAME);
44
45
46 static unsigned int pnn;
47
48 static TDB_DATA old_data;
49
50 static int success = true;
51
52 static void print_counters(void)
53 {
54         int i;
55         uint32_t *old_counters;
56
57         printf("[%4u] Counters: ", getpid());
58         old_counters = (uint32_t *)old_data.dptr;
59         for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
60                 printf("%6u ", old_counters[i]);
61         }
62         printf("\n");
63 }
64
65 static void each_second(struct tevent_context *ev,
66                         struct tevent_timer *te,
67                         struct timeval t,
68                         void *private_data)
69 {
70         struct db_context *db = talloc_get_type(private_data, struct db_context);
71
72         print_counters();
73
74         tevent_add_timer(ev, db, timeval_current_ofs(1, 0), each_second, db);
75 }
76
77 static bool check_counters(struct db_context *db, TDB_DATA data)
78 {
79         int i;
80         uint32_t *counters, *old_counters;
81
82         counters     = (uint32_t *)data.dptr;
83         old_counters = (uint32_t *)old_data.dptr;
84
85         /* check that all the counters are monotonic increasing */
86         for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
87                 if (counters[i] < old_counters[i]) {
88                         printf("[%4u] ERROR: counters has decreased for node %u  From %u to %u\n",
89                                getpid(), i, old_counters[i], counters[i]);
90                         success = false;
91                         return false;
92                 }
93         }
94
95         if (old_data.dsize != data.dsize) {
96                 old_data.dsize = data.dsize;
97                 old_data.dptr = (unsigned char*)talloc_realloc_size(db, old_data.dptr, old_data.dsize);
98         }
99
100         memcpy(old_data.dptr, data.dptr, data.dsize);
101         if (verbose) print_counters();
102
103         return true;
104 }
105
106
107 static void do_sleep(unsigned int sec)
108 {
109         unsigned int i;
110
111         if (sec == 0) {
112                 return;
113         }
114
115         for (i=0; i<sec; i++) {
116                 if (verbose) printf(".");
117                 sleep(1);
118         }
119
120         if (verbose) printf("\n");
121 }
122
123 static void test_store_records(struct db_context *db, struct tevent_context *ev)
124 {
125         TDB_DATA key;
126         uint32_t *counters;
127         TALLOC_CTX *tmp_ctx = talloc_stackframe();
128         struct timeval start;
129
130         key.dptr = (unsigned char *)discard_const("testkey");
131         key.dsize = strlen((const char *)key.dptr)+1;
132
133         start = timeval_current();
134         while ((timelimit == 0) || (timeval_elapsed(&start) < timelimit)) {
135                 struct db_record *rec;
136                 TDB_DATA data;
137                 int ret;
138                 NTSTATUS status;
139
140                 if (!no_trans) {
141                         if (verbose) DEBUG(1, ("starting transaction\n"));
142                         ret = db->transaction_start(db);
143                         if (ret != 0) {
144                                 DEBUG(0, ("Failed to start transaction on node "
145                                           "%d\n", pnn));
146                                 goto fail;
147                         }
148                         if (verbose) DEBUG(1, ("transaction started\n"));
149                         do_sleep(torture_delay);
150                 }
151
152                 if (verbose) DEBUG(1, ("calling fetch_lock\n"));
153                 rec = db->fetch_locked(db, tmp_ctx, key);
154                 if (rec == NULL) {
155                         DEBUG(0, ("Failed to fetch record\n"));
156                         goto fail;
157                 }
158                 if (verbose) DEBUG(1, ("fetched record ok\n"));
159                 do_sleep(torture_delay);
160
161                 data.dsize = MAX(rec->value.dsize, sizeof(uint32_t) * (pnn+1));
162                 data.dptr = (unsigned char *)talloc_zero_size(tmp_ctx,
163                                                               data.dsize);
164                 if (data.dptr == NULL) {
165                         DEBUG(0, ("Failed to allocate data\n"));
166                         goto fail;
167                 }
168                 memcpy(data.dptr, rec->value.dptr,rec->value.dsize);
169
170                 counters = (uint32_t *)data.dptr;
171
172                 /* bump our counter */
173                 counters[pnn]++;
174
175                 if (verbose) DEBUG(1, ("storing data\n"));
176                 status = rec->store(rec, data, TDB_REPLACE);
177                 if (!NT_STATUS_IS_OK(status)) {
178                         DEBUG(0, ("Failed to store record\n"));
179                         if (!no_trans) {
180                                 ret = db->transaction_cancel(db);
181                                 if (ret != 0) {
182                                         DEBUG(0, ("Error cancelling transaction.\n"));
183                                 }
184                         }
185                         goto fail;
186                 }
187                 talloc_free(rec);
188                 if (verbose) DEBUG(1, ("stored data ok\n"));
189                 do_sleep(torture_delay);
190
191                 if (!no_trans) {
192                         if (verbose) DEBUG(1, ("calling transaction_commit\n"));
193                         ret = db->transaction_commit(db);
194                         if (ret != 0) {
195                                 DEBUG(0, ("Failed to commit transaction\n"));
196                                 goto fail;
197                         }
198                         if (verbose) DEBUG(1, ("transaction committed\n"));
199                 }
200
201                 /* store the counters and verify that they are sane */
202                 if (verbose || (pnn == 0)) {
203                         if (!check_counters(db, data)) {
204                                 goto fail;
205                         }
206                 }
207                 talloc_free(data.dptr);
208
209                 do_sleep(torture_delay);
210         }
211
212         goto done;
213
214 fail:
215         success = false;
216
217 done:
218         talloc_free(tmp_ctx);
219         return;
220 }
221
222 /*
223   main program
224 */
225 int main(int argc, const char *argv[])
226 {
227         TALLOC_CTX *mem_ctx;
228         struct tevent_context *ev_ctx;
229         struct messaging_context *msg_ctx;
230         struct db_context *db;
231
232         int unsafe_writes = 0;
233         struct poptOption popt_options[] = {
234                 POPT_AUTOHELP
235                 POPT_COMMON_SAMBA
236                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "INTEGER" },
237                 { "delay", 'D', POPT_ARG_INT, &torture_delay, 0, "delay (in seconds) between operations", "INTEGER" },
238                 { "verbose", 'v', POPT_ARG_NONE,  &verbose, 0, "switch on verbose mode", NULL },
239                 { "db-name", 'N', POPT_ARG_STRING, &db_name, 0, "name of the test db", "NAME" },
240                 { "no-trans", 'n', POPT_ARG_NONE, &no_trans, 0, "use fetch_lock/record store instead of transactions", NULL },
241                 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
242                 POPT_TABLEEND
243         };
244         int opt;
245         const char **extra_argv;
246         int extra_argc = 0;
247         poptContext pc;
248         int tdb_flags;
249
250         int ret = 1;
251
252         mem_ctx = talloc_stackframe();
253
254         if (verbose) {
255                 setbuf(stdout, (char *)NULL); /* don't buffer */
256         } else {
257                 setlinebuf(stdout);
258         }
259
260         load_case_tables();
261
262         setup_logging(argv[0], DEBUG_STDERR);
263         lp_set_cmdline("log level", "0");
264
265         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
266
267         while ((opt = poptGetNextOpt(pc)) != -1) {
268                 switch (opt) {
269                 default:
270                         fprintf(stderr, "Invalid option %s: %s\n",
271                                 poptBadOption(pc, 0), poptStrerror(opt));
272                         goto done;
273                 }
274         }
275
276         /* setup the remaining options for the main program to use */
277         extra_argv = poptGetArgs(pc);
278         if (extra_argv) {
279                 extra_argv++;
280                 while (extra_argv[extra_argc]) extra_argc++;
281         }
282
283         lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
284
285         ev_ctx = tevent_context_init(mem_ctx);
286         if (ev_ctx == NULL) {
287                 d_fprintf(stderr, "ERROR: could not init event context\n");
288                 goto done;
289         }
290
291         msg_ctx = messaging_init(mem_ctx, procid_self(), ev_ctx);
292         if (msg_ctx == NULL) {
293                 d_fprintf(stderr, "ERROR: could not init messaging context\n");
294                 goto done;
295         }
296
297         if (unsafe_writes == 1) {
298                 tdb_flags = TDB_NOSYNC;
299         } else {
300                 tdb_flags = TDB_DEFAULT;
301         }
302
303         if (no_trans) {
304                 tdb_flags |= TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
305         }
306
307         db = db_open(mem_ctx, db_name, 0, tdb_flags,  O_RDWR | O_CREAT, 0644);
308
309         if (db == NULL) {
310                 d_fprintf(stderr, "failed to open db '%s': %s\n", db_name,
311                           strerror(errno));
312                 goto done;
313         }
314
315         if (get_my_vnn() == NONCLUSTER_VNN) {
316                 set_my_vnn(0);
317         }
318         pnn = get_my_vnn();
319
320         printf("Starting test on node %u. running for %u seconds. "
321                "sleep delay: %u seconds.\n", pnn, timelimit, torture_delay);
322
323         if (!verbose && (pnn == 0)) {
324                 tevent_add_timer(ev_ctx, db, timeval_current_ofs(1, 0), each_second, db);
325         }
326
327         test_store_records(db, ev_ctx);
328
329         if (verbose || (pnn == 0)) {
330                 if (success != true) {
331                         printf("The test FAILED\n");
332                         ret = 2;
333                 } else {
334                         printf("SUCCESS!\n");
335                         ret = 0;
336                 }
337         }
338
339 done:
340         talloc_free(mem_ctx);
341         return ret;
342 }