]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tools/speed.c
tdb2: more stats
[ccan] / ccan / tdb2 / tools / speed.c
index 36eef6938aef08e2735857d0276883a68acfd2d6..881d101300ead9a102f9a787aa2d74b083da9d6a 100644 (file)
@@ -43,76 +43,116 @@ static int count_record(struct tdb_context *tdb,
        return 0;
 }
 
-static void dump_and_clear_stats(struct tdb_attribute_stats *stats)
+static void dump_and_clear_stats(struct tdb_context **tdb,
+                                int flags,
+                                union tdb_attribute *attr)
 {
+       union tdb_attribute stats;
+       enum TDB_ERROR ecode;
+
+       stats.base.attr = TDB_ATTRIBUTE_STATS;
+       stats.stats.size = sizeof(stats.stats);
+       ecode = tdb_get_attribute(*tdb, &stats);
+       if (ecode != TDB_SUCCESS)
+               errx(1, "Getting stats: %s", tdb_errorstr(ecode));
+
        printf("allocs = %llu\n",
-              (unsigned long long)stats->allocs);
+              (unsigned long long)stats.stats.allocs);
        printf("  alloc_subhash = %llu\n",
-              (unsigned long long)stats->alloc_subhash);
+              (unsigned long long)stats.stats.alloc_subhash);
        printf("  alloc_chain = %llu\n",
-              (unsigned long long)stats->alloc_chain);
+              (unsigned long long)stats.stats.alloc_chain);
        printf("  alloc_bucket_exact = %llu\n",
-              (unsigned long long)stats->alloc_bucket_exact);
+              (unsigned long long)stats.stats.alloc_bucket_exact);
        printf("  alloc_bucket_max = %llu\n",
-              (unsigned long long)stats->alloc_bucket_max);
+              (unsigned long long)stats.stats.alloc_bucket_max);
        printf("  alloc_leftover = %llu\n",
-              (unsigned long long)stats->alloc_leftover);
+              (unsigned long long)stats.stats.alloc_leftover);
        printf("  alloc_coalesce_tried = %llu\n",
-              (unsigned long long)stats->alloc_coalesce_tried);
+              (unsigned long long)stats.stats.alloc_coalesce_tried);
+       printf("    alloc_coalesce_iterate_clash = %llu\n",
+              (unsigned long long)stats.stats.alloc_coalesce_iterate_clash);
        printf("    alloc_coalesce_lockfail = %llu\n",
-              (unsigned long long)stats->alloc_coalesce_lockfail);
+              (unsigned long long)stats.stats.alloc_coalesce_lockfail);
        printf("    alloc_coalesce_race = %llu\n",
-              (unsigned long long)stats->alloc_coalesce_race);
+              (unsigned long long)stats.stats.alloc_coalesce_race);
        printf("    alloc_coalesce_succeeded = %llu\n",
-              (unsigned long long)stats->alloc_coalesce_succeeded);
-       printf("       alloc_coalesce_num_merged = %llu\n",
-              (unsigned long long)stats->alloc_coalesce_num_merged);
+              (unsigned long long)stats.stats.alloc_coalesce_succeeded);
+       printf("      alloc_coalesce_num_merged = %llu\n",
+              (unsigned long long)stats.stats.alloc_coalesce_num_merged);
        printf("compares = %llu\n",
-              (unsigned long long)stats->compares);
+              (unsigned long long)stats.stats.compares);
        printf("  compare_wrong_bucket = %llu\n",
-              (unsigned long long)stats->compare_wrong_bucket);
+              (unsigned long long)stats.stats.compare_wrong_bucket);
        printf("  compare_wrong_offsetbits = %llu\n",
-              (unsigned long long)stats->compare_wrong_offsetbits);
+              (unsigned long long)stats.stats.compare_wrong_offsetbits);
        printf("  compare_wrong_keylen = %llu\n",
-              (unsigned long long)stats->compare_wrong_keylen);
+              (unsigned long long)stats.stats.compare_wrong_keylen);
        printf("  compare_wrong_rechash = %llu\n",
-              (unsigned long long)stats->compare_wrong_rechash);
+              (unsigned long long)stats.stats.compare_wrong_rechash);
        printf("  compare_wrong_keycmp = %llu\n",
-              (unsigned long long)stats->compare_wrong_keycmp);
+              (unsigned long long)stats.stats.compare_wrong_keycmp);
+       printf("transactions = %llu\n",
+              (unsigned long long)stats.stats.transactions);
+       printf("  transaction_cancel = %llu\n",
+              (unsigned long long)stats.stats.transaction_cancel);
+       printf("  transaction_nest = %llu\n",
+              (unsigned long long)stats.stats.transaction_nest);
+       printf("  transaction_expand_file = %llu\n",
+              (unsigned long long)stats.stats.transaction_expand_file);
+       printf("  transaction_read_direct = %llu\n",
+              (unsigned long long)stats.stats.transaction_read_direct);
+       printf("    transaction_read_direct_fail = %llu\n",
+              (unsigned long long)stats.stats.transaction_read_direct_fail);
+       printf("  transaction_write_direct = %llu\n",
+              (unsigned long long)stats.stats.transaction_write_direct);
+       printf("    transaction_write_direct_fail = %llu\n",
+              (unsigned long long)stats.stats.transaction_write_direct_fail);
        printf("expands = %llu\n",
-              (unsigned long long)stats->expands);
+              (unsigned long long)stats.stats.expands);
        printf("frees = %llu\n",
-              (unsigned long long)stats->frees);
+              (unsigned long long)stats.stats.frees);
        printf("locks = %llu\n",
-              (unsigned long long)stats->locks);
-       printf("   lock_lowlevel = %llu\n",
-              (unsigned long long)stats->lock_lowlevel);
-       printf("   lock_nonblock = %llu\n",
-              (unsigned long long)stats->lock_nonblock);
+              (unsigned long long)stats.stats.locks);
+       printf("  lock_lowlevel = %llu\n",
+              (unsigned long long)stats.stats.lock_lowlevel);
+       printf("  lock_nonblock = %llu\n",
+              (unsigned long long)stats.stats.lock_nonblock);
+       printf("    lock_nonblock_fail = %llu\n",
+              (unsigned long long)stats.stats.lock_nonblock_fail);
 
        /* Now clear. */
-       memset(&stats->allocs, 0, (char *)(stats+1) - (char *)&stats->allocs);
+       tdb_close(*tdb);
+       *tdb = tdb_open("/tmp/speed.tdb", flags, O_RDWR, 0, attr);
+}
+
+static void tdb_log(struct tdb_context *tdb, enum tdb_log_level level,
+                   const char *message, void *data)
+{
+       fputs(message, stderr);
+       putc('\n', stderr);
 }
 
 int main(int argc, char *argv[])
 {
        unsigned int i, j, num = 1000, stage = 0, stopat = -1;
        int flags = TDB_DEFAULT;
-       bool transaction = false;
+       bool transaction = false, summary = false;
        TDB_DATA key, data;
        struct tdb_context *tdb;
        struct timeval start, stop;
-       union tdb_attribute seed, stats;
+       union tdb_attribute seed, log;
+       bool do_stats = false;
+       enum TDB_ERROR ecode;
 
        /* Try to keep benchmarks even. */
        seed.base.attr = TDB_ATTRIBUTE_SEED;
        seed.base.next = NULL;
        seed.seed.seed = 0;
 
-       memset(&stats, 0, sizeof(stats));
-       stats.base.attr = TDB_ATTRIBUTE_STATS;
-       stats.base.next = NULL;
-       stats.stats.size = sizeof(stats);
+       log.base.attr = TDB_ATTRIBUTE_LOG;
+       log.base.next = &seed;
+       log.log.fn = tdb_log;
 
        if (argv[1] && strcmp(argv[1], "--internal") == 0) {
                flags = TDB_INTERNAL;
@@ -124,14 +164,24 @@ int main(int argc, char *argv[])
                argc--;
                argv++;
        }
+       if (argv[1] && strcmp(argv[1], "--no-sync") == 0) {
+               flags |= TDB_NOSYNC;
+               argc--;
+               argv++;
+       }
+       if (argv[1] && strcmp(argv[1], "--summary") == 0) {
+               summary = true;
+               argc--;
+               argv++;
+       }
        if (argv[1] && strcmp(argv[1], "--stats") == 0) {
-               seed.base.next = &stats;
+               do_stats = true;
                argc--;
                argv++;
        }
 
        tdb = tdb_open("/tmp/speed.tdb", flags, O_RDWR|O_CREAT|O_TRUNC,
-                      0600, &seed);
+                      0600, &log);
        if (!tdb)
                err(1, "Opening /tmp/speed.tdb");
 
@@ -151,77 +201,100 @@ int main(int argc, char *argv[])
                argc--;
        }
 
-       if (transaction && tdb_transaction_start(tdb))
-               errx(1, "starting transaction: %s", tdb_errorstr(tdb));
-
        /* Add 1000 records. */
        printf("Adding %u records: ", num); fflush(stdout);
+       if (transaction && (ecode = tdb_transaction_start(tdb)))
+               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (i = 0; i < num; i++)
-               if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
+               if ((ecode = tdb_store(tdb, key, data, TDB_INSERT)) != 0)
                        errx(1, "Inserting key %u in tdb: %s",
-                            i, tdb_errorstr(tdb));
+                            i, tdb_errorstr(ecode));
        gettimeofday(&stop, NULL);
-       if (transaction && tdb_transaction_commit(tdb))
-               errx(1, "committing transaction: %s", tdb_errorstr(tdb));
+       if (transaction && (ecode = tdb_transaction_commit(tdb)))
+               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
 
-       if (seed.base.next)
-               dump_and_clear_stats(&stats.stats);
+       if (tdb_check(tdb, NULL, NULL))
+               errx(1, "tdb_check failed!");
+       if (summary) {
+               char *sumstr = NULL;
+               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               printf("%s\n", sumstr);
+               free(sumstr);
+       }
+       if (do_stats)
+               dump_and_clear_stats(&tdb, flags, &log);
+
        if (++stage == stopat)
                exit(0);
 
-       if (transaction && tdb_transaction_start(tdb))
-               errx(1, "starting transaction: %s", tdb_errorstr(tdb));
-
        /* Finding 1000 records. */
        printf("Finding %u records: ", num); fflush(stdout);
+       if (transaction && (ecode = tdb_transaction_start(tdb)))
+               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (i = 0; i < num; i++) {
-               int *dptr;
-               dptr = (int *)tdb_fetch(tdb, key).dptr;
-               if (!dptr || *dptr != i)
+               struct tdb_data dbuf;
+               if ((ecode = tdb_fetch(tdb, key, &dbuf)) != TDB_SUCCESS
+                   || *(int *)dbuf.dptr != i) {
                        errx(1, "Fetching key %u in tdb gave %u",
-                            i, dptr ? *dptr : -1);
+                            i, ecode ? ecode : *(int *)dbuf.dptr);
+               }
        }
        gettimeofday(&stop, NULL);
-       if (transaction && tdb_transaction_commit(tdb))
-               errx(1, "committing transaction: %s", tdb_errorstr(tdb));
+       if (transaction && (ecode = tdb_transaction_commit(tdb)))
+               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (seed.base.next)
-               dump_and_clear_stats(&stats.stats);
+       if (tdb_check(tdb, NULL, NULL))
+               errx(1, "tdb_check failed!");
+       if (summary) {
+               char *sumstr = NULL;
+               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               printf("%s\n", sumstr);
+               free(sumstr);
+       }
+       if (do_stats)
+               dump_and_clear_stats(&tdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
-       if (transaction && tdb_transaction_start(tdb))
-               errx(1, "starting transaction: %s", tdb_errorstr(tdb));
-
        /* Missing 1000 records. */
        printf("Missing %u records: ", num); fflush(stdout);
+       if (transaction && (ecode = tdb_transaction_start(tdb)))
+               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (i = num; i < num*2; i++) {
-               int *dptr;
-               dptr = (int *)tdb_fetch(tdb, key).dptr;
-               if (dptr)
-                       errx(1, "Fetching key %u in tdb gave %u", i, *dptr);
+               struct tdb_data dbuf;
+               ecode = tdb_fetch(tdb, key, &dbuf);
+               if (ecode != TDB_ERR_NOEXIST)
+                       errx(1, "Fetching key %u in tdb gave %s",
+                            i, tdb_errorstr(ecode));
        }
        gettimeofday(&stop, NULL);
-       if (transaction && tdb_transaction_commit(tdb))
-               errx(1, "committing transaction: %s", tdb_errorstr(tdb));
+       if (transaction && (ecode = tdb_transaction_commit(tdb)))
+               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (seed.base.next)
-               dump_and_clear_stats(&stats.stats);
+       if (tdb_check(tdb, NULL, NULL))
+               errx(1, "tdb_check failed!");
+       if (summary) {
+               char *sumstr = NULL;
+               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               printf("%s\n", sumstr);
+               free(sumstr);
+       }
+       if (do_stats)
+               dump_and_clear_stats(&tdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
-       if (transaction && tdb_transaction_start(tdb))
-               errx(1, "starting transaction: %s", tdb_errorstr(tdb));
-
        /* Traverse 1000 records. */
        printf("Traversing %u records: ", num); fflush(stdout);
+       if (transaction && (ecode = tdb_transaction_start(tdb)))
+               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
        i = 0;
        gettimeofday(&start, NULL);
        if (tdb_traverse(tdb, count_record, &i) != num)
@@ -229,101 +302,137 @@ int main(int argc, char *argv[])
        if (i != (num - 1) * (num / 2))
                errx(1, "Traverse tallied to %u", i);
        gettimeofday(&stop, NULL);
-       if (transaction && tdb_transaction_commit(tdb))
-               errx(1, "committing transaction: %s", tdb_errorstr(tdb));
+       if (transaction && (ecode = tdb_transaction_commit(tdb)))
+               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (seed.base.next)
-               dump_and_clear_stats(&stats.stats);
+       if (tdb_check(tdb, NULL, NULL))
+               errx(1, "tdb_check failed!");
+       if (summary) {
+               char *sumstr = NULL;
+               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               printf("%s\n", sumstr);
+               free(sumstr);
+       }
+       if (do_stats)
+               dump_and_clear_stats(&tdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
-       if (transaction && tdb_transaction_start(tdb))
-               errx(1, "starting transaction: %s", tdb_errorstr(tdb));
-
        /* Delete 1000 records (not in order). */
        printf("Deleting %u records: ", num); fflush(stdout);
+       if (transaction && (ecode = tdb_transaction_start(tdb)))
+               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (j = 0; j < num; j++) {
                i = (j + 100003) % num;
-               if (tdb_delete(tdb, key) != 0)
+               if ((ecode = tdb_delete(tdb, key)) != TDB_SUCCESS)
                        errx(1, "Deleting key %u in tdb: %s",
-                            i, tdb_errorstr(tdb));
+                            i, tdb_errorstr(ecode));
        }
        gettimeofday(&stop, NULL);
-       if (transaction && tdb_transaction_commit(tdb))
-               errx(1, "committing transaction: %s", tdb_errorstr(tdb));
+       if (transaction && (ecode = tdb_transaction_commit(tdb)))
+               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (seed.base.next)
-               dump_and_clear_stats(&stats.stats);
+       if (tdb_check(tdb, NULL, NULL))
+               errx(1, "tdb_check failed!");
+       if (summary) {
+               char *sumstr = NULL;
+               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               printf("%s\n", sumstr);
+               free(sumstr);
+       }
+       if (do_stats)
+               dump_and_clear_stats(&tdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
-       if (transaction && tdb_transaction_start(tdb))
-               errx(1, "starting transaction: %s", tdb_errorstr(tdb));
-
        /* Re-add 1000 records (not in order). */
        printf("Re-adding %u records: ", num); fflush(stdout);
+       if (transaction && (ecode = tdb_transaction_start(tdb)))
+               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (j = 0; j < num; j++) {
                i = (j + 100003) % num;
-               if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
+               if ((ecode = tdb_store(tdb, key, data, TDB_INSERT)) != 0)
                        errx(1, "Inserting key %u in tdb: %s",
-                            i, tdb_errorstr(tdb));
+                            i, tdb_errorstr(ecode));
        }
        gettimeofday(&stop, NULL);
-       if (transaction && tdb_transaction_commit(tdb))
-               errx(1, "committing transaction: %s", tdb_errorstr(tdb));
+       if (transaction && (ecode = tdb_transaction_commit(tdb)))
+               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (seed.base.next)
-               dump_and_clear_stats(&stats.stats);
+       if (tdb_check(tdb, NULL, NULL))
+               errx(1, "tdb_check failed!");
+       if (summary) {
+               char *sumstr = NULL;
+               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               printf("%s\n", sumstr);
+               free(sumstr);
+       }
+       if (do_stats)
+               dump_and_clear_stats(&tdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
-       if (transaction && tdb_transaction_start(tdb))
-               errx(1, "starting transaction: %s", tdb_errorstr(tdb));
-
        /* Append 1000 records. */
+       if (transaction && (ecode = tdb_transaction_start(tdb)))
+               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
        printf("Appending %u records: ", num); fflush(stdout);
        gettimeofday(&start, NULL);
        for (i = 0; i < num; i++)
-               if (tdb_append(tdb, key, data) != 0)
+               if ((ecode = tdb_append(tdb, key, data)) != TDB_SUCCESS)
                        errx(1, "Appending key %u in tdb: %s",
-                            i, tdb_errorstr(tdb));
+                            i, tdb_errorstr(ecode));
        gettimeofday(&stop, NULL);
-       if (transaction && tdb_transaction_commit(tdb))
-               errx(1, "committing transaction: %s", tdb_errorstr(tdb));
+       if (transaction && (ecode = tdb_transaction_commit(tdb)))
+               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
+       if (tdb_check(tdb, NULL, NULL))
+               errx(1, "tdb_check failed!");
+       if (summary) {
+               char *sumstr = NULL;
+               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               printf("%s\n", sumstr);
+               free(sumstr);
+       }
        if (++stage == stopat)
                exit(0);
 
-       if (transaction && tdb_transaction_start(tdb))
-               errx(1, "starting transaction: %s", tdb_errorstr(tdb));
-
        /* Churn 1000 records: not in order! */
+       if (transaction && (ecode = tdb_transaction_start(tdb)))
+               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
        printf("Churning %u records: ", num); fflush(stdout);
        gettimeofday(&start, NULL);
        for (j = 0; j < num; j++) {
                i = (j + 1000019) % num;
-               if (tdb_delete(tdb, key) != 0)
+               if ((ecode = tdb_delete(tdb, key)) != TDB_SUCCESS)
                        errx(1, "Deleting key %u in tdb: %s",
-                            i, tdb_errorstr(tdb));
+                            i, tdb_errorstr(ecode));
                i += num;
-               if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
+               if ((ecode = tdb_store(tdb, key, data, TDB_INSERT)) != 0)
                        errx(1, "Inserting key %u in tdb: %s",
-                            i, tdb_errorstr(tdb));
+                            i, tdb_errorstr(ecode));
        }
        gettimeofday(&stop, NULL);
-       if (transaction && tdb_transaction_commit(tdb))
-               errx(1, "committing transaction: %s", tdb_errorstr(tdb));
+       if (transaction && (ecode = tdb_transaction_commit(tdb)))
+               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
 
-       if (seed.base.next)
-               dump_and_clear_stats(&stats.stats);
+       if (tdb_check(tdb, NULL, NULL))
+               errx(1, "tdb_check failed!");
+       if (summary) {
+               char *sumstr = NULL;
+               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               printf("%s\n", sumstr);
+               free(sumstr);
+       }
+       if (do_stats)
+               dump_and_clear_stats(&tdb, flags, &log);
        if (++stage == stopat)
                exit(0);