Browse Source

tdb: remove tally dependency

We lose the histograms, but this prepares it for upstream merge.
Rusty Russell 15 years ago
parent
commit
d6b00a7164
4 changed files with 78 additions and 99 deletions
  1. 0 1
      ccan/tdb/_info
  2. 76 93
      ccan/tdb/summary.c
  3. 1 4
      ccan/tdb/tdb.h
  4. 1 1
      ccan/tdb/tools/tdbtool.c

+ 0 - 1
ccan/tdb/_info

@@ -76,7 +76,6 @@ int main(int argc, char *argv[])
 
 
 	if (strcmp(argv[1], "depends") == 0) {
 	if (strcmp(argv[1], "depends") == 0) {
 		printf("ccan/compiler\n");
 		printf("ccan/compiler\n");
-		printf("ccan/tally\n");
 		return 0;
 		return 0;
 	}
 	}
 
 

+ 76 - 93
ccan/tdb/summary.c

@@ -16,26 +16,54 @@
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 */
 #include "tdb_private.h"
 #include "tdb_private.h"
-#include <ccan/tally/tally.h>
 
 
 #define SUMMARY_FORMAT \
 #define SUMMARY_FORMAT \
 	"Size of file/data: %u/%zu\n" \
 	"Size of file/data: %u/%zu\n" \
 	"Number of records: %zu\n" \
 	"Number of records: %zu\n" \
-	"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" \
+	"Smallest/average/largest keys: %zu/%zu/%zu\n" \
+	"Smallest/average/largest data: %zu/%zu/%zu\n" \
+	"Smallest/average/largest padding: %zu/%zu/%zu\n" \
 	"Number of dead records: %zu\n" \
 	"Number of dead records: %zu\n" \
-	"Smallest/average/largest dead records: %zu/%zu/%zu\n%s" \
+	"Smallest/average/largest dead records: %zu/%zu/%zu\n" \
 	"Number of free records: %zu\n" \
 	"Number of free records: %zu\n" \
-	"Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
+	"Smallest/average/largest free records: %zu/%zu/%zu\n" \
 	"Number of hash chains: %zu\n" \
 	"Number of hash chains: %zu\n" \
-	"Smallest/average/largest hash chains: %zu/%zu/%zu\n%s" \
+	"Smallest/average/largest hash chains: %zu/%zu/%zu\n" \
 	"Number of uncoalesced records: %zu\n" \
 	"Number of uncoalesced records: %zu\n" \
-	"Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
+	"Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n" \
 	"Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
 	"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
+/* We don't use tally module, to keep upstream happy. */
+struct tally {
+	size_t min, max, total;
+	size_t num;
+};
+
+static void tally_init(struct tally *tally)
+{
+	tally->total = 0;
+	tally->num = 0;
+	tally->min = tally->max = 0;
+}
+
+static void tally_add(struct tally *tally, size_t len)
+{
+	if (tally->num == 0)
+		tally->max = tally->min = len;
+	else if (len > tally->max)
+		tally->max = len;
+	else if (len < tally->min)
+		tally->min = len;
+	tally->num++;
+	tally->total += len;
+}
+
+static size_t tally_mean(const struct tally *tally)
+{
+	if (!tally->num)
+		return 0;
+	return tally->total / tally->num;
+}
 
 
 static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
 static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
 {
 {
@@ -56,18 +84,15 @@ static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
 	return count;
 	return count;
 }
 }
 
 
-char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
+char *tdb_summary(struct tdb_context *tdb)
 {
 {
 	tdb_off_t off;
 	tdb_off_t off;
-	struct tally *freet, *keys, *data, *dead, *extra, *hash, *uncoal;
-	char *freeg, *keysg, *datag, *deadg, *extrag, *hashg, *uncoalg;
+	struct tally freet, keys, data, dead, extra, hash, uncoal;
 	struct tdb_record rec;
 	struct tdb_record rec;
 	char *ret = NULL;
 	char *ret = NULL;
 	bool locked;
 	bool locked;
 	size_t len, unc = 0;
 	size_t len, unc = 0;
 
 
-	freeg = keysg = datag = deadg = extrag = hashg = uncoalg = NULL;
-
 	/* Read-only databases use no locking at all: it's best-effort.
 	/* Read-only databases use no locking at all: it's best-effort.
 	 * We may have a write lock already, so skip that case too. */
 	 * We may have a write lock already, so skip that case too. */
 	if (tdb->read_only || tdb->allrecord_lock.count != 0) {
 	if (tdb->read_only || tdb->allrecord_lock.count != 0) {
@@ -78,17 +103,13 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
 		locked = true;
 		locked = true;
 	}
 	}
 
 
-	freet = 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);
-	uncoal = tally_new(HISTO_HEIGHT);
-	if (!freet || !keys || !data || !dead || !extra || !hash || !uncoal) {
-		tdb->ecode = TDB_ERR_OOM;
-		goto unlock;
-	}
+	tally_init(&freet);
+	tally_init(&keys);
+	tally_init(&data);
+	tally_init(&dead);
+	tally_init(&extra);
+	tally_init(&hash);
+	tally_init(&uncoal);
 
 
 	for (off = TDB_DATA_START(tdb->header.hash_size);
 	for (off = TDB_DATA_START(tdb->header.hash_size);
 	     off < tdb->map_size - 1;
 	     off < tdb->map_size - 1;
@@ -98,16 +119,16 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
 			goto unlock;
 			goto unlock;
 		switch (rec.magic) {
 		switch (rec.magic) {
 		case TDB_MAGIC:
 		case TDB_MAGIC:
-			tally_add(keys, rec.key_len);
-			tally_add(data, rec.data_len);
-			tally_add(extra, rec.rec_len - (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));
 			if (unc > 1)
 			if (unc > 1)
-				tally_add(uncoal, unc - 1);
+				tally_add(&uncoal, unc - 1);
 			unc = 0;
 			unc = 0;
 			break;
 			break;
 		case TDB_FREE_MAGIC:
 		case TDB_FREE_MAGIC:
-			tally_add(freet, rec.rec_len);
+			tally_add(&freet, rec.rec_len);
 			unc++;
 			unc++;
 			break;
 			break;
 		/* If we crash after ftruncate, we can get zeroes or fill. */
 		/* If we crash after ftruncate, we can get zeroes or fill. */
@@ -117,7 +138,7 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
 			rec.rec_len = tdb_dead_space(tdb, off) - sizeof(rec);
 			rec.rec_len = tdb_dead_space(tdb, off) - sizeof(rec);
 			/* Fall through */
 			/* Fall through */
 		case TDB_DEAD_MAGIC:
 		case TDB_DEAD_MAGIC:
-			tally_add(dead, rec.rec_len);
+			tally_add(&dead, rec.rec_len);
 			break;
 			break;
 		default:
 		default:
 			TDB_LOG((tdb, TDB_DEBUG_ERROR,
 			TDB_LOG((tdb, TDB_DEBUG_ERROR,
@@ -127,81 +148,43 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
 		}
 		}
 	}
 	}
 	if (unc > 1)
 	if (unc > 1)
-		tally_add(uncoal, unc - 1);
+		tally_add(&uncoal, unc - 1);
 
 
 	for (off = 0; off < tdb->header.hash_size; off++)
 	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);
-		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);
-	}
+		tally_add(&hash, get_hash_length(tdb, off));
 
 
 	/* 20 is max length of a %zu. */
 	/* 20 is max length of a %zu. */
-	len = strlen(SUMMARY_FORMAT) + 29*20 + 1
-		+ (freeg ? strlen(freeg) : 0)
-		+ (keysg ? strlen(keysg) : 0)
-		+ (datag ? strlen(datag) : 0)
-		+ (deadg ? strlen(deadg) : 0)
-		+ (extrag ? strlen(extrag) : 0)
-		+ (hashg ? strlen(hashg) : 0)
-		+ (uncoalg ? strlen(uncoalg) : 0);
+	len = strlen(SUMMARY_FORMAT) + 35*20 + 1;
 	ret = malloc(len);
 	ret = malloc(len);
 	if (!ret)
 	if (!ret)
 		goto unlock;
 		goto unlock;
 
 
 	sprintf(ret, SUMMARY_FORMAT,
 	sprintf(ret, SUMMARY_FORMAT,
-		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),
-		tally_min(dead), tally_mean(dead), tally_max(dead),
-		deadg ? deadg : "",
-		tally_num(freet),
-		tally_min(freet), tally_mean(freet), tally_max(freet),
-		freeg ? freeg : "",
-		tally_num(hash),
-		tally_min(hash), tally_mean(hash), tally_max(hash),
-		hashg ? hashg : "",
-		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))
+		tdb->map_size, keys.total+data.total,
+		keys.num,
+		keys.min, tally_mean(&keys), keys.max,
+		data.min, tally_mean(&data), data.max,
+		extra.min, tally_mean(&extra), extra.max,
+		dead.num,
+		dead.min, tally_mean(&dead), dead.max,
+		freet.num,
+		freet.min, tally_mean(&freet), freet.max,
+		hash.num,
+		hash.min, tally_mean(&hash), hash.max,
+		uncoal.total,
+		uncoal.min, tally_mean(&uncoal), uncoal.max,
+		keys.total * 100.0 / tdb->map_size,
+		data.total * 100.0 / tdb->map_size,
+		extra.total * 100.0 / tdb->map_size,
+		freet.total * 100.0 / tdb->map_size,
+		dead.total * 100.0 / tdb->map_size,
+		(keys.num + freet.num + dead.num)
 		* (sizeof(struct tdb_record) + sizeof(uint32_t))
 		* (sizeof(struct tdb_record) + sizeof(uint32_t))
 		* 100.0 / tdb->map_size,
 		* 100.0 / tdb->map_size,
 		tdb->header.hash_size * sizeof(tdb_off_t)
 		tdb->header.hash_size * sizeof(tdb_off_t)
 		* 100.0 / tdb->map_size);
 		* 100.0 / tdb->map_size);
 
 
 unlock:
 unlock:
-	free(freeg);
-	free(keysg);
-	free(datag);
-	free(deadg);
-	free(extrag);
-	free(hashg);
-	free(uncoalg);
-	free(freet);
-	free(keys);
-	free(data);
-	free(dead);
-	free(extra);
-	free(hash);
-	free(uncoal);
 	if (locked) {
 	if (locked) {
 		tdb_unlockall_read(tdb);
 		tdb_unlockall_read(tdb);
 	}
 	}

+ 1 - 4
ccan/tdb/tdb.h

@@ -66,9 +66,6 @@ enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
 		TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
 		TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
 		TDB_ERR_NESTING};
 		TDB_ERR_NESTING};
 
 
-/* flags for tdb_summary. Logical or to combine. */
-enum tdb_summary_flags { TDB_SUMMARY_HISTOGRAMS = 1 };
-
 /* debugging uses one of the following levels */
 /* debugging uses one of the following levels */
 enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR, 
 enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR, 
 		      TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
 		      TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
@@ -167,7 +164,7 @@ void tdb_dump_all(struct tdb_context *tdb);
 int tdb_printfreelist(struct tdb_context *tdb);
 int tdb_printfreelist(struct tdb_context *tdb);
 int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
 int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
 int tdb_freelist_size(struct tdb_context *tdb);
 int tdb_freelist_size(struct tdb_context *tdb);
-char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags);
+char *tdb_summary(struct tdb_context *tdb);
 
 
 extern TDB_DATA tdb_null;
 extern TDB_DATA tdb_null;
 
 

+ 1 - 1
ccan/tdb/tools/tdbtool.c

@@ -412,7 +412,7 @@ static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *
 
 
 static void info_tdb(void)
 static void info_tdb(void)
 {
 {
-	char *summary = tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS);
+	char *summary = tdb_summary(tdb);
 
 
 	if (!summary) {
 	if (!summary) {
 		printf("Error = %s\n", tdb_errorstr(tdb));
 		printf("Error = %s\n", tdb_errorstr(tdb));