X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Fsummary.c;h=25be07b0083e7252cfdf7560f9344d83d076159a;hp=b54b56e7a712239a74efa87de8a6bde0cce023dc;hb=0aa58f6e6017138a78ce4e86fb758732719acaca;hpb=96b169e986cda1de9ffbbdc98042e1099515ca34 diff --git a/ccan/tdb2/summary.c b/ccan/tdb2/summary.c index b54b56e7..25be07b0 100644 --- a/ccan/tdb2/summary.c +++ b/ccan/tdb2/summary.c @@ -1,7 +1,7 @@ - /* + /* Trivial Database 2: human-readable summary code Copyright (C) Rusty Russell 2010 - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -37,13 +37,14 @@ static int count_hash(struct tdb_context *tdb, static bool summarize(struct tdb_context *tdb, struct tally *hashes, - struct tally *flists, - struct tally *free, + struct tally *ftables, + struct tally *fr, struct tally *keys, struct tally *data, struct tally *extra, struct tally *uncoal, - struct tally *buckets) + struct tally *buckets, + struct tally *chains) { tdb_off_t off; tdb_len_t len; @@ -68,11 +69,11 @@ static bool summarize(struct tdb_context *tdb, len = sizeof(p->r) + p->r.max_len; } else if (frec_magic(&p->f) == TDB_FREE_MAGIC) { len = frec_len(&p->f); - tally_add(free, len); + tally_add(fr, len); tally_add(buckets, size_to_bucket(len)); len += sizeof(p->u); unc++; - } else if (rec_magic(&p->u) == TDB_MAGIC) { + } else if (rec_magic(&p->u) == TDB_USED_MAGIC) { if (unc) { tally_add(uncoal, unc); unc = 0; @@ -82,23 +83,31 @@ static bool summarize(struct tdb_context *tdb, + rec_data_length(&p->u) + rec_extra_padding(&p->u); - /* FIXME: Use different magic for hashes, flists. */ - if (!rec_key_length(&p->u) && rec_hash(&p->u) < 2) { - if (rec_hash(&p->u) == 0) { - int count = count_hash(tdb, - off + sizeof(p->u), - TDB_SUBLEVEL_HASH_BITS); - if (count == -1) - return false; - tally_add(hashes, count); - } else { - tally_add(flists, - rec_data_length(&p->u)); - } - } else { - tally_add(keys, rec_key_length(&p->u)); - tally_add(data, rec_data_length(&p->u)); - } + tally_add(keys, rec_key_length(&p->u)); + tally_add(data, rec_data_length(&p->u)); + tally_add(extra, rec_extra_padding(&p->u)); + } else if (rec_magic(&p->u) == TDB_HTABLE_MAGIC) { + int count = count_hash(tdb, + off + sizeof(p->u), + TDB_SUBLEVEL_HASH_BITS); + if (count == -1) + return false; + tally_add(hashes, count); + tally_add(extra, rec_extra_padding(&p->u)); + len = sizeof(p->u) + + rec_data_length(&p->u) + + rec_extra_padding(&p->u); + } else if (rec_magic(&p->u) == TDB_FTABLE_MAGIC) { + len = sizeof(p->u) + + rec_data_length(&p->u) + + rec_extra_padding(&p->u); + tally_add(ftables, rec_data_length(&p->u)); + tally_add(extra, rec_extra_padding(&p->u)); + } else if (rec_magic(&p->u) == TDB_CHAIN_MAGIC) { + len = sizeof(p->u) + + rec_data_length(&p->u) + + rec_extra_padding(&p->u); + tally_add(chains, 1); tally_add(extra, rec_extra_padding(&p->u)); } else len = dead_space(tdb, off); @@ -121,6 +130,7 @@ static bool summarize(struct tdb_context *tdb, "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \ "Number of free lists: %zu\n%s" \ "Toplevel hash used: %u of %u\n" \ + "Number of chains: %zu\n" \ "Number of subhashes: %zu\n" \ "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \ "Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n" @@ -138,8 +148,8 @@ static bool summarize(struct tdb_context *tdb, char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) { tdb_len_t len; - struct tally *flists, *hashes, *freet, *keys, *data, *extra, *uncoal, - *buckets; + struct tally *ftables, *hashes, *freet, *keys, *data, *extra, *uncoal, + *buckets, *chains; char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg, *bucketsg; char *ret = NULL; @@ -154,7 +164,7 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) } /* Start stats off empty. */ - flists = tally_new(HISTO_HEIGHT); + ftables = tally_new(HISTO_HEIGHT); hashes = tally_new(HISTO_HEIGHT); freet = tally_new(HISTO_HEIGHT); keys = tally_new(HISTO_HEIGHT); @@ -162,14 +172,16 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) extra = tally_new(HISTO_HEIGHT); uncoal = tally_new(HISTO_HEIGHT); buckets = tally_new(HISTO_HEIGHT); - if (!flists || !hashes || !freet || !keys || !data || !extra - || !uncoal || !buckets) { - tdb->ecode = TDB_ERR_OOM; + chains = tally_new(HISTO_HEIGHT); + if (!ftables || !hashes || !freet || !keys || !data || !extra + || !uncoal || !buckets || !chains) { + tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR, + "tdb_summary: failed to allocate tally structures"); goto unlock; } - if (!summarize(tdb, hashes, flists, freet, keys, data, extra, uncoal, - buckets)) + if (!summarize(tdb, hashes, ftables, freet, keys, data, extra, uncoal, + buckets, chains)) goto unlock; if (flags & TDB_SUMMARY_HISTOGRAMS) { @@ -217,6 +229,7 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) count_hash(tdb, offsetof(struct tdb_header, hashtable), TDB_TOPLEVEL_HASH_BITS), 1 << TDB_TOPLEVEL_HASH_BITS, + tally_num(chains), tally_num(hashes), tally_min(hashes), tally_mean(hashes), tally_max(hashes), hashesg ? hashesg : "", @@ -226,11 +239,12 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) tally_total(freet, NULL) * 100.0 / tdb->map_size, (tally_num(keys) + tally_num(freet) + tally_num(hashes)) * sizeof(struct tdb_used_record) * 100.0 / tdb->map_size, - tally_num(flists) * sizeof(struct tdb_freelist) + tally_num(ftables) * sizeof(struct tdb_freetable) * 100.0 / tdb->map_size, (tally_num(hashes) * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) - + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS)) + + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS) + + sizeof(struct tdb_chain) * tally_num(chains)) * 100.0 / tdb->map_size); unlock: @@ -248,6 +262,8 @@ unlock: free(data); free(extra); free(uncoal); + free(ftables); + free(chains); tdb_allrecord_unlock(tdb, F_RDLCK); tdb_unlock_expand(tdb, F_RDLCK);