2 Samba Linux/Unix CIFS implementation
4 simple tool to test persistent databases
6 Copyright (C) Michael Adam 2009
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.
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.
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/>.
25 #include "lib/events/events.h"
26 #include "system/filesys.h"
34 extern bool AllowDebugChange;
36 static struct timeval tp1,tp2;
38 static void start_timer(void)
40 gettimeofday(&tp1,NULL);
43 static double end_timer(void)
45 gettimeofday(&tp2,NULL);
46 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
47 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
50 #define DEFAULT_DB_NAME "transaction.tdb"
52 static int timelimit = 10;
54 static int verbose = 0;
55 static int no_trans = 0;
56 static char *db_name = (char *)discard_const(DEFAULT_DB_NAME);
59 static unsigned int pnn;
61 static TDB_DATA old_data;
63 static int success = true;
65 static void print_counters(void)
68 uint32_t *old_counters;
70 printf("[%4u] Counters: ", getpid());
71 old_counters = (uint32_t *)old_data.dptr;
72 for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
73 printf("%6u ", old_counters[i]);
78 static void each_second(struct tevent_context *ev,
79 struct tevent_timer *te,
83 struct db_context *db = talloc_get_type(private_data, struct db_context);
87 tevent_add_timer(ev, db, timeval_current_ofs(1, 0), each_second, db);
90 static bool check_counters(struct db_context *db, TDB_DATA data)
93 uint32_t *counters, *old_counters;
95 counters = (uint32_t *)data.dptr;
96 old_counters = (uint32_t *)old_data.dptr;
98 /* check that all the counters are monotonic increasing */
99 for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
100 if (counters[i] < old_counters[i]) {
101 printf("[%4u] ERROR: counters has decreased for node %u From %u to %u\n",
102 getpid(), i, old_counters[i], counters[i]);
108 if (old_data.dsize != data.dsize) {
109 old_data.dsize = data.dsize;
110 old_data.dptr = (unsigned char*)talloc_realloc_size(db, old_data.dptr, old_data.dsize);
113 memcpy(old_data.dptr, data.dptr, data.dsize);
114 if (verbose) print_counters();
120 static void do_sleep(unsigned int sec)
128 for (i=0; i<sec; i++) {
129 if (verbose) printf(".");
133 if (verbose) printf("\n");
136 static void test_store_records(struct db_context *db, struct tevent_context *ev)
140 TALLOC_CTX *tmp_ctx = talloc_stackframe();
142 key.dptr = (unsigned char *)discard_const("testkey");
143 key.dsize = strlen((const char *)key.dptr)+1;
146 while ((timelimit == 0) || (end_timer() < timelimit)) {
147 struct db_record *rec;
153 if (verbose) DEBUG(1, ("starting transaction\n"));
154 ret = db->transaction_start(db);
156 DEBUG(0, ("Failed to start transaction on node "
160 if (verbose) DEBUG(1, ("transaction started\n"));
164 if (verbose) DEBUG(1, ("calling fetch_lock\n"));
165 rec = db->fetch_locked(db, tmp_ctx, key);
167 DEBUG(0, ("Failed to fetch record\n"));
170 if (verbose) DEBUG(1, ("fetched record ok\n"));
173 data.dsize = MAX(rec->value.dsize, sizeof(uint32_t) * (pnn+1));
174 data.dptr = (unsigned char *)talloc_zero_size(tmp_ctx,
176 if (data.dptr == NULL) {
177 DEBUG(0, ("Failed to allocate data\n"));
180 memcpy(data.dptr, rec->value.dptr,rec->value.dsize);
182 counters = (uint32_t *)data.dptr;
184 /* bump our counter */
187 if (verbose) DEBUG(1, ("storing data\n"));
188 status = rec->store(rec, data, TDB_REPLACE);
189 if (!NT_STATUS_IS_OK(status)) {
190 DEBUG(0, ("Failed to store record\n"));
192 ret = db->transaction_cancel(db);
194 DEBUG(0, ("Error cancelling transaction.\n"));
200 if (verbose) DEBUG(1, ("stored data ok\n"));
204 if (verbose) DEBUG(1, ("calling transaction_commit\n"));
205 ret = db->transaction_commit(db);
207 DEBUG(0, ("Failed to commit transaction\n"));
210 if (verbose) DEBUG(1, ("transaction committed\n"));
213 /* store the counters and verify that they are sane */
214 if (verbose || (pnn == 0)) {
215 if (!check_counters(db, data)) {
219 talloc_free(data.dptr);
230 talloc_free(tmp_ctx);
237 int main(int argc, const char *argv[])
240 struct tevent_context *ev_ctx;
241 struct messaging_context *msg_ctx;
242 struct db_context *db;
244 int unsafe_writes = 0;
245 struct poptOption popt_options[] = {
248 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "INTEGER" },
249 { "delay", 'D', POPT_ARG_INT, &delay, 0, "delay (in seconds) between operations", "INTEGER" },
250 { "verbose", 'v', POPT_ARG_NONE, &verbose, 0, "switch on verbose mode", NULL },
251 { "db-name", 'N', POPT_ARG_STRING, &db_name, 0, "name of the test db", "NAME" },
252 { "no-trans", 'n', POPT_ARG_NONE, &no_trans, 0, "use fetch_lock/record store instead of transactions", NULL },
253 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
257 const char **extra_argv;
264 mem_ctx = talloc_stackframe();
267 setbuf(stdout, (char *)NULL); /* don't buffer */
272 DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
274 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
276 while ((opt = poptGetNextOpt(pc)) != -1) {
279 fprintf(stderr, "Invalid option %s: %s\n",
280 poptBadOption(pc, 0), poptStrerror(opt));
285 /* setup the remaining options for the main program to use */
286 extra_argv = poptGetArgs(pc);
289 while (extra_argv[extra_argc]) extra_argc++;
294 AllowDebugChange = false;
295 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
297 ev_ctx = tevent_context_init(mem_ctx);
298 if (ev_ctx == NULL) {
299 d_fprintf(stderr, "ERROR: could not init event context\n");
303 msg_ctx = messaging_init(mem_ctx, server_id_self(), ev_ctx);
304 if (msg_ctx == NULL) {
305 d_fprintf(stderr, "ERROR: could not init messaging context\n");
309 if (unsafe_writes == 1) {
310 tdb_flags = TDB_NOSYNC;
312 tdb_flags = TDB_DEFAULT;
315 db = db_open(mem_ctx, db_name, 0, tdb_flags, O_RDWR | O_CREAT, 0644);
318 d_fprintf(stderr, "failed to open db '%s': %s\n", db_name,
323 if (get_my_vnn() == NONCLUSTER_VNN) {
328 printf("Starting test on node %u. running for %u seconds. "
329 "sleep delay: %u seconds.\n", pnn, timelimit, delay);
331 if (!verbose && (pnn == 0)) {
332 tevent_add_timer(ev_ctx, db, timeval_current_ofs(1, 0), each_second, db);
335 test_store_records(db, ev_ctx);
337 if (verbose || (pnn == 0)) {
338 if (success != true) {
339 printf("The test FAILED\n");
342 printf("SUCCESS!\n");
348 talloc_free(mem_ctx);