summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2010-12-29 12:50:47 +1030
committerRusty Russell <rusty@rustcorp.com.au>2010-12-29 10:12:05 +0100
commitcac57328a6077dc428396402036636095f139569 (patch)
treeada85a847d12a29fe5246226cd08b1b6fda7b6e8 /lib
parent9b31f6ab6cce55824f3e62f59061085abc1240db (diff)
downloadsamba-cac57328a6077dc428396402036636095f139569.tar.gz
samba-cac57328a6077dc428396402036636095f139569.tar.xz
samba-cac57328a6077dc428396402036636095f139569.zip
tdb: tdb_summary() support.
Autobuild-User: Rusty Russell <rusty@rustcorp.com.au> Autobuild-Date: Wed Dec 29 10:12:05 CET 2010 on sn-devel-104
Diffstat (limited to 'lib')
-rw-r--r--lib/tdb/common/check.c4
-rw-r--r--lib/tdb/common/summary.c192
-rw-r--r--lib/tdb/common/tdb_private.h1
-rw-r--r--lib/tdb/include/tdb.h1
-rw-r--r--lib/tdb/libtdb.m42
-rw-r--r--lib/tdb/tools/tdbtool.c12
-rw-r--r--lib/tdb/wscript4
7 files changed, 206 insertions, 10 deletions
diff --git a/lib/tdb/common/check.c b/lib/tdb/common/check.c
index bbb566c6f77..3387fbd098d 100644
--- a/lib/tdb/common/check.c
+++ b/lib/tdb/common/check.c
@@ -308,7 +308,7 @@ static bool tdb_check_free_record(struct tdb_context *tdb,
}
/* Slow, but should be very rare. */
-static size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
+size_t tdb_dead_space(struct tdb_context *tdb, tdb_off_t off)
{
size_t len;
@@ -406,7 +406,7 @@ _PUBLIC_ int tdb_check(struct tdb_context *tdb,
found_recovery = true;
break;
}
- dead = dead_space(tdb, off);
+ dead = tdb_dead_space(tdb, off);
if (dead < sizeof(rec))
goto corrupt;
diff --git a/lib/tdb/common/summary.c b/lib/tdb/common/summary.c
new file mode 100644
index 00000000000..29959f9e5f3
--- /dev/null
+++ b/lib/tdb/common/summary.c
@@ -0,0 +1,192 @@
+ /*
+ Trivial Database: 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
+ version 3 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#include "tdb_private.h"
+
+#define SUMMARY_FORMAT \
+ "Size of file/data: %u/%zu\n" \
+ "Number of records: %zu\n" \
+ "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" \
+ "Smallest/average/largest dead records: %zu/%zu/%zu\n" \
+ "Number of free records: %zu\n" \
+ "Smallest/average/largest free records: %zu/%zu/%zu\n" \
+ "Number of hash chains: %zu\n" \
+ "Smallest/average/largest hash chains: %zu/%zu/%zu\n" \
+ "Number of uncoalesced records: %zu\n" \
+ "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"
+
+/* 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)
+{
+ tdb_off_t rec_ptr;
+ size_t count = 0;
+
+ if (tdb_ofs_read(tdb, TDB_HASH_TOP(i), &rec_ptr) == -1)
+ return 0;
+
+ /* keep looking until we find the right record */
+ while (rec_ptr) {
+ struct tdb_record r;
+ ++count;
+ if (tdb_rec_read(tdb, rec_ptr, &r) == -1)
+ return 0;
+ rec_ptr = r.next;
+ }
+ return count;
+}
+
+_PUBLIC_ char *tdb_summary(struct tdb_context *tdb)
+{
+ tdb_off_t off;
+ struct tally freet, keys, data, dead, extra, hash, uncoal;
+ struct tdb_record rec;
+ char *ret = NULL;
+ bool locked;
+ 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. */
+ if (tdb->read_only || tdb->allrecord_lock.count != 0) {
+ locked = false;
+ } else {
+ if (tdb_lockall_read(tdb) == -1)
+ return NULL;
+ locked = true;
+ }
+
+ 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);
+ off < tdb->map_size - 1;
+ off += sizeof(rec) + rec.rec_len) {
+ if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
+ DOCONV()) == -1)
+ goto unlock;
+ switch (rec.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));
+ if (unc > 1)
+ tally_add(&uncoal, unc - 1);
+ unc = 0;
+ break;
+ case TDB_FREE_MAGIC:
+ 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 = tdb_dead_space(tdb, off) - sizeof(rec);
+ /* Fall through */
+ case TDB_DEAD_MAGIC:
+ tally_add(&dead, rec.rec_len);
+ break;
+ default:
+ TDB_LOG((tdb, TDB_DEBUG_ERROR,
+ "Unexpected record magic 0x%x at offset %d\n",
+ rec.magic, off));
+ goto unlock;
+ }
+ }
+ if (unc > 1)
+ tally_add(&uncoal, unc - 1);
+
+ for (off = 0; off < tdb->header.hash_size; off++)
+ tally_add(&hash, get_hash_length(tdb, off));
+
+ /* 20 is max length of a %zu. */
+ len = strlen(SUMMARY_FORMAT) + 35*20 + 1;
+ ret = malloc(len);
+ if (!ret)
+ goto unlock;
+
+ sprintf(ret, SUMMARY_FORMAT,
+ 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))
+ * 100.0 / tdb->map_size,
+ tdb->header.hash_size * sizeof(tdb_off_t)
+ * 100.0 / tdb->map_size);
+
+unlock:
+ if (locked) {
+ tdb_unlockall_read(tdb);
+ }
+ return ret;
+}
diff --git a/lib/tdb/common/tdb_private.h b/lib/tdb/common/tdb_private.h
index 0c621636fa2..0186fb9530a 100644
--- a/lib/tdb/common/tdb_private.h
+++ b/lib/tdb/common/tdb_private.h
@@ -274,3 +274,4 @@ int tdb_transaction_recover(struct tdb_context *tdb);
void tdb_header_hash(struct tdb_context *tdb,
uint32_t *magic1_hash, uint32_t *magic2_hash);
unsigned int tdb_old_hash(TDB_DATA *key);
+size_t tdb_dead_space(struct tdb_context *tdb, tdb_off_t off);
diff --git a/lib/tdb/include/tdb.h b/lib/tdb/include/tdb.h
index 115c6fada60..0ee5e1b5bd0 100644
--- a/lib/tdb/include/tdb.h
+++ b/lib/tdb/include/tdb.h
@@ -168,6 +168,7 @@ void tdb_dump_all(struct tdb_context *tdb);
int tdb_printfreelist(struct tdb_context *tdb);
int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
int tdb_freelist_size(struct tdb_context *tdb);
+char *tdb_summary(struct tdb_context *tdb);
extern TDB_DATA tdb_null;
diff --git a/lib/tdb/libtdb.m4 b/lib/tdb/libtdb.m4
index e650bc684e0..b5164fc5b20 100644
--- a/lib/tdb/libtdb.m4
+++ b/lib/tdb/libtdb.m4
@@ -13,7 +13,7 @@ if test x"$tdbdir" = "x"; then
AC_MSG_ERROR([cannot find tdb source in $tdbpaths])
fi
TDB_OBJ="common/tdb.o common/dump.o common/transaction.o common/error.o common/traverse.o"
-TDB_OBJ="$TDB_OBJ common/freelist.o common/freelistcheck.o common/io.o common/lock.o common/open.o common/check.o common/hash.o"
+TDB_OBJ="$TDB_OBJ common/freelist.o common/freelistcheck.o common/io.o common/lock.o common/open.o common/check.o common/hash.o common/summary.o"
AC_SUBST(TDB_OBJ)
AC_SUBST(LIBREPLACEOBJ)
diff --git a/lib/tdb/tools/tdbtool.c b/lib/tdb/tools/tdbtool.c
index 2ba7efc8ab2..3511dc1e565 100644
--- a/lib/tdb/tools/tdbtool.c
+++ b/lib/tdb/tools/tdbtool.c
@@ -409,12 +409,14 @@ static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *
static void info_tdb(void)
{
- int count;
- total_bytes = 0;
- if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
+ char *summary = tdb_summary(tdb);
+
+ if (!summary) {
printf("Error = %s\n", tdb_errorstr(tdb));
- else
- printf("%d records totalling %d bytes\n", count, total_bytes);
+ } else {
+ printf("%s", summary);
+ free(summary);
+ }
}
static void speed_tdb(const char *tlimit)
diff --git a/lib/tdb/wscript b/lib/tdb/wscript
index 8146a986081..d20c93893cf 100644
--- a/lib/tdb/wscript
+++ b/lib/tdb/wscript
@@ -1,7 +1,7 @@
#!/usr/bin/env python
APPNAME = 'tdb'
-VERSION = '1.2.8'
+VERSION = '1.2.9'
blddir = 'bin'
@@ -60,7 +60,7 @@ def build(bld):
COMMON_SRC = bld.SUBDIR('common',
'''check.c error.c tdb.c traverse.c
freelistcheck.c lock.c dump.c freelist.c
- io.c open.c transaction.c hash.c''')
+ io.c open.c transaction.c hash.c summary.c''')
if bld.env.standalone_tdb:
bld.env.PKGCONFIGDIR = '${LIBDIR}/pkgconfig'