tdb2: add stats to tdb1 backend.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:08 +0000 (15:31 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:08 +0000 (15:31 +0930)
It's actually quite a good fit; we use compare_wrong_bucket for dead
records, which is kind of correct (they should be in the free list).

ccan/tdb2/tdb1_freelist.c
ccan/tdb2/tdb1_io.c
ccan/tdb2/tdb1_tdb.c
ccan/tdb2/tdb1_transaction.c

index 37f19ab28b587d7897d70a0b9074a894a8daf7c2..af0129372173d3b12f048f4960c66cdfb14d844b 100644 (file)
@@ -83,6 +83,7 @@ int tdb1_free(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *re
                goto fail;
        }
 
+       tdb->stats.alloc_coalesce_tried++;
        /* Look left */
        if (offset - sizeof(tdb1_off_t) > TDB1_DATA_START(tdb->tdb1.header.hash_size)) {
                tdb1_off_t left = offset - sizeof(tdb1_off_t);
@@ -131,6 +132,9 @@ int tdb1_free(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *re
                                           "tdb1_free: update_tailer failed at %u", offset);
                                goto fail;
                        }
+                       tdb->stats.alloc_coalesce_succeeded++;
+                       tdb->stats.alloc_coalesce_num_merged++;
+                       tdb->stats.frees++;
                        tdb1_unlock(tdb, -1, F_WRLCK);
                        return 0;
                }
@@ -151,6 +155,7 @@ update:
        }
 
        /* And we're done. */
+       tdb->stats.frees++;
        tdb1_unlock(tdb, -1, F_WRLCK);
        return 0;
 
@@ -188,6 +193,7 @@ static tdb1_off_t tdb1_allocate_ofs(struct tdb_context *tdb,
                if (tdb1_rec_write(tdb, rec_ptr, rec) == -1) {
                        return 0;
                }
+               tdb->stats.allocs++;
                return rec_ptr;
        }
 
@@ -215,6 +221,8 @@ static tdb1_off_t tdb1_allocate_ofs(struct tdb_context *tdb,
                return 0;
        }
 
+       tdb->stats.allocs++;
+       tdb->stats.alloc_leftover++;
        return rec_ptr;
 }
 
index 8219e93046e373f96e6812771ee5813bfe226ce0..ba6deeef2069f7cbeb0f67d69f9446a84d672809 100644 (file)
@@ -307,6 +307,7 @@ static int tdb1_expand_file(struct tdb_context *tdb, tdb1_off_t size, tdb1_off_t
                addition -= written;
                size += written;
        }
+       tdb->stats.expands++;
        return 0;
 }
 
index 98f830cc9a81d0c18f78cfa5e5aae6e4b44ca0f4..9730dceffc2b5b7b8bccbeaef1ca6bacd689c2a4 100644 (file)
@@ -89,11 +89,18 @@ static tdb1_off_t tdb1_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash
                if (tdb1_rec_read(tdb, rec_ptr, r) == -1)
                        return 0;
 
-               if (!TDB1_DEAD(r) && hash==r->full_hash
-                   && key.dsize==r->key_len
-                   && tdb1_parse_data(tdb, key, rec_ptr + sizeof(*r),
-                                     r->key_len, tdb1_key_compare,
-                                     NULL) == 0) {
+               tdb->stats.compares++;
+               if (TDB1_DEAD(r)) {
+                       tdb->stats.compare_wrong_bucket++;
+               } else if (key.dsize != r->key_len) {
+                       tdb->stats.compare_wrong_keylen++;
+               } else if (hash != r->full_hash) {
+                       tdb->stats.compare_wrong_rechash++;
+               } else if (tdb1_parse_data(tdb, key, rec_ptr + sizeof(*r),
+                                          r->key_len, tdb1_key_compare,
+                                          NULL) != 0) {
+                       tdb->stats.compare_wrong_keycmp++;
+               } else {
                        return rec_ptr;
                }
                /* detect tight infinite loop */
index ecd7d2628e4981e42634da4617c6581f831a0d87..126f7684689ba50b7fbd352d75aa8c0679b185e8 100644 (file)
@@ -432,6 +432,7 @@ static int _tdb1_transaction_start(struct tdb_context *tdb)
                        tdb->last_error = TDB_ERR_EINVAL;
                        return -1;
                }
+               tdb->stats.transaction_nest++;
                tdb->tdb1.transaction->nesting++;
                return 0;
        }
@@ -511,6 +512,7 @@ static int _tdb1_transaction_start(struct tdb_context *tdb)
        tdb->tdb1.transaction->io_methods = tdb->tdb1.io;
        tdb->tdb1.io = &transaction1_methods;
 
+       tdb->stats.transactions++;
        return 0;
 
 fail:
@@ -621,6 +623,7 @@ static int _tdb1_transaction_cancel(struct tdb_context *tdb)
 */
 int tdb1_transaction_cancel(struct tdb_context *tdb)
 {
+       tdb->stats.transaction_cancel++;
        return _tdb1_transaction_cancel(tdb);
 }
 
@@ -739,6 +742,7 @@ static int tdb1_recovery_allocate(struct tdb_context *tdb,
                           " failed to create recovery area");
                return -1;
        }
+       tdb->stats.transaction_expand_file++;
 
        /* remap the file (if using mmap) */
        methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
@@ -1000,6 +1004,7 @@ static int _tdb1_transaction_prepare_commit(struct tdb_context *tdb)
                                   " expansion failed");
                        return -1;
                }
+               tdb->stats.transaction_expand_file++;
                tdb->file->map_size = tdb->tdb1.transaction->old_map_size;
                methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
        }