From: Rusty Russell Date: Sat, 25 Sep 2010 07:54:50 +0000 (+0930) Subject: tdb: separate key and data size summary in tdb_summary(). X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=43d881a232a4999e0d23db8e4335604dd5f3474a tdb: separate key and data size summary in tdb_summary(). --- diff --git a/ccan/tdb/summary.c b/ccan/tdb/summary.c index a9dc4f80..b8f7f976 100644 --- a/ccan/tdb/summary.c +++ b/ccan/tdb/summary.c @@ -19,9 +19,10 @@ #include #define SUMMARY_FORMAT \ - "Size of file: %zu\n" \ + "Size of file/data: %zu/%zu\n" \ "Number of records: %zu\n" \ - "Smallest/average/largest records: %zu/%zu/%zu\n%s" \ + "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \ + "Smallest/average/largest data: %zu/%zu/%zu\n%s" \ "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \ "Number of dead records: %zu\n" \ "Smallest/average/largest dead records: %zu/%zu/%zu\n%s" \ @@ -29,7 +30,9 @@ "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \ "Number of hash chains: %zu\n" \ "Smallest/average/largest hash chains: %zu/%zu/%zu\n%s" \ - "Total data = %zu (%.0f%%)\n" + "Number of uncoalesced records: %zu\n" \ + "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \ + "Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n" #define HISTO_WIDTH 70 #define HISTO_HEIGHT 20 @@ -71,12 +74,12 @@ static size_t get_hash_length(struct tdb_context *tdb, unsigned int i) char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) { tdb_off_t off; - struct tally *freet, *used, *dead, *extra, *hash; - char *freeg, *usedg, *deadg, *extrag, *hashg; + struct tally *freet, *keys, *data, *dead, *extra, *hash, *uncoal; + char *freeg, *keysg, *datag, *deadg, *extrag, *hashg, *uncoalg; struct tdb_record rec; char *ret = NULL; bool locked; - size_t len; + size_t len, unc = 0; /* Read-only databases use no locking at all: it's best-effort. * We may have a write lock already, so skip that case too. */ @@ -89,11 +92,13 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) } freet = tally_new(HISTO_HEIGHT); - used = tally_new(HISTO_HEIGHT); + keys = tally_new(HISTO_HEIGHT); + data = tally_new(HISTO_HEIGHT); dead = tally_new(HISTO_HEIGHT); extra = tally_new(HISTO_HEIGHT); hash = tally_new(HISTO_HEIGHT); - if (!freet || !used || !dead || !extra || !hash) { + uncoal = tally_new(HISTO_HEIGHT); + if (!freet || !keys || !data || !dead || !extra || !hash || !uncoal) { tdb->ecode = TDB_ERR_OOM; goto unlock; } @@ -106,20 +111,23 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) goto unlock; switch (rec.magic) { case TDB_MAGIC: - tally_add(used, rec.key_len + rec.data_len); + tally_add(keys, rec.key_len); + tally_add(data, rec.data_len); tally_add(extra, rec.rec_len - (rec.key_len + rec.data_len)); break; case TDB_FREE_MAGIC: - tally_add(freet, sizeof(rec) + rec.rec_len); + tally_add(freet, rec.rec_len); + unc++; break; /* If we crash after ftruncate, we can get zeroes or fill. */ case TDB_RECOVERY_INVALID_MAGIC: case 0x42424242: + unc++; rec.rec_len = dead_space(tdb, off) - sizeof(rec); /* Fall through */ case TDB_DEAD_MAGIC: - tally_add(dead, sizeof(rec) + rec.rec_len); + tally_add(dead, rec.rec_len); break; default: TDB_LOG((tdb, TDB_DEBUG_ERROR, @@ -127,37 +135,51 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) rec.magic, off)); goto unlock; } + + if (unc && + (rec.magic == TDB_MAGIC || rec.magic == TDB_DEAD_MAGIC)) { + tally_add(uncoal, unc); + unc = 0; + } } + if (unc) + tally_add(uncoal, unc); for (off = 0; off < tdb->header.hash_size; off++) tally_add(hash, get_hash_length(tdb, off)); if (flags & TDB_SUMMARY_HISTOGRAMS) { freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT); - usedg = tally_histogram(used, HISTO_WIDTH, HISTO_HEIGHT); + keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT); + datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT); deadg = tally_histogram(dead, HISTO_WIDTH, HISTO_HEIGHT); extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT); hashg = tally_histogram(hash, HISTO_WIDTH, HISTO_HEIGHT); + uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT); } else { - freeg = usedg = deadg = extrag = hashg = NULL; + freeg = keysg = datag = deadg = extrag = hashg = NULL; } /* 20 is max length of a %zu. */ - len = strlen(SUMMARY_FORMAT) + 22*20 + 1 + len = strlen(SUMMARY_FORMAT) + 29*20 + 1 + (freeg ? strlen(freeg) : 0) - + (usedg ? strlen(usedg) : 0) + + (keysg ? strlen(keysg) : 0) + + (datag ? strlen(datag) : 0) + (deadg ? strlen(deadg) : 0) + (extrag ? strlen(extrag) : 0) - + (hashg ? strlen(hashg) : 0); + + (hashg ? strlen(hashg) : 0) + + (uncoalg ? strlen(uncoalg) : 0); ret = malloc(len); if (!ret) goto unlock; sprintf(ret, SUMMARY_FORMAT, - tdb->map_size, - tally_num(used), - tally_min(used), tally_mean(used), tally_max(used), - usedg ? usedg : "", + tdb->map_size, tally_total(keys, NULL)+tally_total(data, NULL), + tally_num(keys), + tally_min(keys), tally_mean(keys), tally_max(keys), + keysg ? keysg : "", + tally_min(data), tally_mean(data), tally_max(data), + datag ? datag : "", tally_min(extra), tally_mean(extra), tally_max(extra), extrag ? extrag : "", tally_num(dead), @@ -169,20 +191,35 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) tally_num(hash), tally_min(hash), tally_mean(hash), tally_max(hash), hashg ? hashg : "", - tally_total(used, NULL), - tally_total(used, NULL) * 100.0 / tdb->map_size); + tally_total(uncoal, NULL), + tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal), + uncoalg ? uncoalg : "", + tally_total(keys, NULL) * 100.0 / tdb->map_size, + tally_total(data, NULL) * 100.0 / tdb->map_size, + tally_total(extra, NULL) * 100.0 / tdb->map_size, + tally_total(freet, NULL) * 100.0 / tdb->map_size, + tally_total(dead, NULL) * 100.0 / tdb->map_size, + (tally_num(keys) + tally_num(freet) + tally_num(dead)) + * (sizeof(struct tdb_record) + sizeof(uint32_t)) + * 100.0 / tdb->map_size, + tdb->header.hash_size * sizeof(tdb_off_t) + * 100.0 / tdb->map_size); unlock: free(freeg); - free(usedg); + free(keysg); + free(datag); free(deadg); free(extrag); free(hashg); + free(uncoalg); free(freet); - free(used); + free(keys); + free(data); free(dead); free(extra); free(hash); + free(uncoal); if (locked) { tdb_unlockall_read(tdb); }